// Thin fetch wrapper for the podsteadr API (session cookie auth). export class ApiError extends Error { constructor(public status: number, message: string) { super(message); } } async function request(method: string, path: string, body?: unknown, headers?: Record): Promise { const res = await fetch(path, { method, credentials: 'same-origin', headers: { ...(body !== undefined ? { 'content-type': 'application/json' } : {}), ...headers, }, body: body !== undefined ? JSON.stringify(body) : undefined, }); if (!res.ok) { let message = res.statusText; try { message = ((await res.json()) as { error?: string }).error ?? message; } catch { /* keep statusText */ } throw new ApiError(res.status, message); } return (await res.json()) as T; } export const api = { get: (path: string) => request('GET', path), post: (path: string, body?: unknown, headers?: Record) => request('POST', path, body, headers), put: (path: string, body?: unknown) => request('PUT', path, body), delete: (path: string) => request('DELETE', path), };