import { supabase } from "./supabase"; const API_URL = process.env.EXPO_PUBLIC_API_URL ?? "http://localhost:3001"; async function authHeaders(): Promise> { const { data } = await supabase.auth.getSession(); const token = data.session?.access_token; return { "Content-Type": "application/json", ...(token ? { Authorization: `Bearer ${token}` } : {}), }; } async function request(path: string, options: RequestInit = {}): Promise { const headers = await authHeaders(); const res = await fetch(`${API_URL}${path}`, { ...options, headers }); if (!res.ok) { const body = await res.json().catch(() => ({ message: res.statusText })); throw new Error(body.message ?? `Request failed: ${res.status}`); } if (res.status === 204) return undefined as T; return res.json() as Promise; } export const api = { get: (path: string) => request(path), post: (path: string, body?: unknown) => request(path, { method: "POST", body: body ? JSON.stringify(body) : undefined }), put: (path: string, body?: unknown) => request(path, { method: "PUT", body: body ? JSON.stringify(body) : undefined }), patch: (path: string, body: unknown) => request(path, { method: "PATCH", body: JSON.stringify(body) }), delete: (path: string) => request(path, { method: "DELETE" }), };