import { auth } from "./firebase"; const RAW_API_BASE_URL = String(import.meta.env.VITE_API_BASE_URL || "").trim(); const API_BASE_URL = RAW_API_BASE_URL ? RAW_API_BASE_URL.replace(/\/+$/, "") : ""; async function getAuthHeader() { const user = auth.currentUser; if (!user) { throw new Error("Please sign in first."); } const token = await user.getIdToken(); return { Authorization: `Bearer ${token}` }; } export async function apiGet(path: string): Promise { const headers = await getAuthHeader(); const response = await fetch(`${API_BASE_URL}${path}`, { method: "GET", headers, cache: "no-store", }); if (!response.ok) { const body = await response.json().catch(() => ({})); throw new Error(body.detail || `Request failed (${response.status})`); } return response.json() as Promise; } export async function apiPostForm( path: string, formData: FormData, ): Promise { const headers = await getAuthHeader(); const response = await fetch(`${API_BASE_URL}${path}`, { method: "POST", headers, body: formData, }); if (!response.ok) { const body = await response.json().catch(() => ({})); throw new Error(body.detail || `Request failed (${response.status})`); } return response.json() as Promise; } export async function apiPatchForm( path: string, formData: FormData, ): Promise { const headers = await getAuthHeader(); const response = await fetch(`${API_BASE_URL}${path}`, { method: "PATCH", headers, body: formData, }); if (!response.ok) { const body = await response.json().catch(() => ({})); throw new Error(body.detail || `Request failed (${response.status})`); } return response.json() as Promise; } export async function apiPost(path: string, payload: any): Promise { const headers = await getAuthHeader(); const response = await fetch(`${API_BASE_URL}${path}`, { method: "POST", headers: { ...headers, "Content-Type": "application/json", }, body: JSON.stringify(payload), }); if (!response.ok) { const body = await response.json().catch(() => ({})); throw new Error(body.detail || `Request failed (${response.status})`); } return response.json() as Promise; } export async function apiDelete(path: string): Promise { const headers = await getAuthHeader(); const response = await fetch(`${API_BASE_URL}${path}`, { method: "DELETE", headers, }); if (!response.ok) { const body = await response.json().catch(() => ({})); throw new Error(body.detail || `Request failed (${response.status})`); } }