export class ApiError extends Error { constructor( message: string, public status: number ) { super(message); } } async function req(method: string, path: string, body?: unknown): Promise { const init: RequestInit = { method, credentials: 'same-origin', headers: {} }; if (body instanceof FormData) init.body = body; else if (body !== undefined) { init.body = JSON.stringify(body); (init.headers as Record)['content-type'] = 'application/json'; } const r = await fetch(path, init); const j = await r.json().catch(() => ({})); if (!r.ok) throw new ApiError(j?.error ?? `Ошибка ${r.status}`, r.status); return j as T; } export const api = { get: (p: string) => req('GET', p), post: (p: string, b?: unknown) => req('POST', p, b ?? {}), patch: (p: string, b?: unknown) => req('PATCH', p, b ?? {}), put: (p: string, b?: unknown) => req('PUT', p, b ?? {}), del: (p: string) => req('DELETE', p) };