const API_BASE = '/api'; export class ApiError extends Error { constructor( public status: number, public body: string, ) { super(`API ${status}: ${body}`); } } export async function api(path: string, init?: RequestInit): Promise { const res = await fetch(`${API_BASE}${path}`, { credentials: 'include', ...init, headers: { 'content-type': 'application/json', ...(init?.headers ?? {}), }, }); if (!res.ok) throw new ApiError(res.status, await res.text()); return (await res.json()) as T; }