"use client"; import type { Envelope } from "./types"; /** Browser-side API calls — always same-origin `/v1` (rewritten in dev, served by the edge in production). */ export async function clientApi(path: string, init?: RequestInit & { adminToken?: string }): Promise { const headers: Record = { accept: "application/json", ...((init?.headers as Record) ?? {}) }; if (init?.adminToken) headers["x-ma-admin-token"] = init.adminToken; if (init?.body && !headers["content-type"]) headers["content-type"] = "application/json"; const res = await fetch(path, { ...init, headers }); const body = (await res.json().catch(() => null)) as (Envelope & { error?: { code: string; message: string } }) | null; if (!res.ok) throw new Error(body?.error?.message ?? `HTTP ${res.status}`); return (body as Envelope).data; }