/** 后端 API 客户端(开发期指向本地 FastAPI,地址见 .env.local)。 */ const BASE = process.env.NEXT_PUBLIC_API_BASE ?? "http://127.0.0.1:8000/api"; export async function apiGet(path: string): Promise { const resp = await fetch(`${BASE}${path}`); if (!resp.ok) throw new Error(`GET ${path} → ${resp.status}: ${await resp.text()}`); return (await resp.json()) as T; } export async function apiPost(path: string, body: unknown): Promise { const resp = await fetch(`${BASE}${path}`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(body), }); if (!resp.ok) { const text = await resp.text(); throw new Error(`POST ${path} → ${resp.status}: ${text.slice(0, 300)}`); } return (await resp.json()) as T; } export interface ApiError { status: number; message: string; }