AkurAI Build
Menu

Public repository / 41b5d1110a34166099a7737c821dcaf20a45fb28

AkurAI-Mail / src/lib/api.ts

Latest change a7e96191cab7cc0297487b9e56f4e19a4c057957 - refactor: switch to adapter-static for Rust API backend by Ólafur Búi Ólafsson

export async function api(path: string, opts?: RequestInit) {
  const res = await fetch(path, { credentials: "same-origin", ...opts });
  if (res.status === 401) {
    const current = window.location.pathname;
    if (current.startsWith("/webmail")) {
      window.location.href = `/webmail/login?next=${encodeURIComponent(current)}`;
    } else {
      window.location.href = `/login?next=${encodeURIComponent(current)}`;
    }
    throw new Error("authentication required");
  }
  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),
  });
}