Menu
Public repository / 747387228d3bb5d93d7967570dec82a1ef519f2e
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),
});
}