spb/market-atlas
Public
TypeScript 96.7%
SQL 1.6%
CSS 0.8%
JavaScript 0.5%
1"use client";23import type { Envelope } from "./types";45/** Browser-side API calls — always same-origin `/v1` (rewritten in dev, served by the edge in production). */6export async function clientApi<T>(path: string, init?: RequestInit & { adminToken?: string }): Promise<T> {7 const headers: Record<string, string> = { accept: "application/json", ...((init?.headers as Record<string, string>) ?? {}) };8 if (init?.adminToken) headers["x-ma-admin-token"] = init.adminToken;9 if (init?.body && !headers["content-type"]) headers["content-type"] = "application/json";10 const res = await fetch(path, { ...init, headers });11 const body = (await res.json().catch(() => null)) as (Envelope<T> & { error?: { code: string; message: string } }) | null;12 if (!res.ok) throw new Error(body?.error?.message ?? `HTTP ${res.status}`);13 return (body as Envelope<T>).data;14}15