import type { AreaRow, MapData, MetricsData, OptionsResponse, PredictRequest, PredictResponse, } from "./types"; // Same-origin in production; Vite proxies /api to the backend in dev. const BASE = "/api"; async function getJSON(path: string): Promise { const res = await fetch(`${BASE}${path}`); if (!res.ok) { const detail = await res.text().catch(() => res.statusText); throw new Error(`${res.status}: ${detail}`); } return res.json() as Promise; } export const api = { options: () => getJSON("/options"), areas: () => getJSON("/areas"), map: () => getJSON("/map"), metrics: () => getJSON("/metrics"), predict: async (body: PredictRequest): Promise => { const res = await fetch(`${BASE}/predict`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(body), }); if (!res.ok) { let detail = res.statusText; try { const data = await res.json(); detail = data.detail ?? detail; } catch { /* keep statusText */ } throw new Error(detail); } return res.json() as Promise; }, figureUrl: (name: string) => `${BASE}/figures/${name}`, };