import { clearToken, getToken } from '@/lib/auth' export class ApiError extends Error { status: number code: string constructor(status: number, code: string, message: string) { super(message) this.status = status this.code = code } } async function request( method: string, path: string, body?: unknown, ): Promise { const headers: Record = { Accept: 'application/json', } const token = getToken() if (token) headers.Authorization = `Bearer ${token}` if (body !== undefined) headers['Content-Type'] = 'application/json' const res = await fetch(path, { method, headers, credentials: 'include', body: body === undefined ? undefined : JSON.stringify(body), }) if (res.status === 204) return undefined as T const text = await res.text() let data: unknown = null if (text) { try { data = JSON.parse(text) } catch { data = { error: { code: 'PARSE_ERROR', message: text } } } } if (!res.ok) { if (res.status === 401) clearToken() const err = data as { error?: { code?: string; message?: string } } | null throw new ApiError( res.status, err?.error?.code ?? 'ERROR', err?.error?.message ?? res.statusText, ) } return data as T } export const api = { get: (path: string) => request('GET', path), post: (path: string, body?: unknown) => request('POST', path, body), patch: (path: string, body?: unknown) => request('PATCH', path, body), put: (path: string, body?: unknown) => request('PUT', path, body), delete: (path: string) => request('DELETE', path), }