export interface ApiOptions { init?: RequestInit; request?: Request; } export async function apiFetch(path: string, options?: ApiOptions): Promise { const { init } = options ?? {}; const response = await fetch(`/api/v1${path}`, { ...init, credentials: "include", headers: init?.headers, }); if (response.status === 401) { window.location.href = "/login"; throw new Error("Unauthorized"); } return response; } export async function apiJson(path: string, options?: ApiOptions): Promise { const response = await apiFetch(path, options); if (!response.ok) { throw new Response(null, { status: response.status, statusText: response.statusText }); } return response.json() as Promise; } export async function getSetupStatus(): Promise<{ configured: boolean }> { const response = await fetch("/api/v1/setup/status", { credentials: "include" }); if (!response.ok) { throw new Response(null, { status: response.status, statusText: response.statusText }); } return response.json(); } export async function getAuthMe(): Promise<{ user: { login: string; name: string; email: string; avatarUrl: string; userUrl: string; }; provider: string; demoMode: boolean; features: { session_sandboxes: boolean; retros: boolean }; }> { const response = await fetch("/api/v1/auth/me", { credentials: "include" }); if (response.status === 401) { throw new Response(null, { status: 401, statusText: "Unauthorized" }); } if (!response.ok) { throw new Response(null, { status: response.status, statusText: response.statusText }); } return response.json(); }