AkurAI Build
Menu

Public repository / 0216cf0f6008aae88a648a82e9b3a462a6688302

AkurAI-Mail / src/lib/api.ts

Latest change 22202d1bde69be4ace8a299dffd91f8ff24378df - Recover deployed webmail frontend (frontend parity with EC2 a6afffc) by Ólafur Búi Ólafsson

/** Sends the browser to the IdP login, returning here afterwards. */
export function loginUrl(returnTo: string) {
  return `/auth/login?return_to=${encodeURIComponent(returnTo)}`;
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any -- JSON payloads are untyped
export async function api(path: string, opts?: RequestInit): Promise<any> {
  const res = await fetch(path, { credentials: "same-origin", ...opts });
  if (res.status === 401) {
    window.location.href = loginUrl(window.location.pathname);
    // The page is navigating away; never settle so callers do not render a half-loaded state.
    return new Promise(() => {});
  }
  return res.json();
}

export async function apiPost(path: string, body: unknown) {
  return api(path, {
    method: "POST",
    headers: { "content-type": "application/json" },
    body: JSON.stringify(body),
  });
}