import { auth } from '$lib/auth.svelte'; export type ApiResult = { ok: true; data: T } | { ok: false; error: string }; export async function apiFetch(method: string, path: string, body?: unknown): Promise> { try { const headers: Record = { 'Content-Type': 'application/json' }; if (auth.token) headers['Authorization'] = `Bearer ${auth.token}`; const res = await fetch(path, { method, headers, body: body ? JSON.stringify(body) : undefined }); if (res.status === 204) return { ok: true, data: undefined as T }; const data = await res.json(); if (!res.ok) return { ok: false, error: data?.error?.message ?? 'Something went wrong' }; return { ok: true, data: data as T }; } catch { return { ok: false, error: 'Unable to connect to the server' }; } }