spb/tools-spboucher-ai
Public
1"use client";2/** Client helper for admin APIs: adds the CSRF token (from <meta name="csrf-token">) and surfaces errors. */3export async function api<T = any>(path: string, init: RequestInit = {}): Promise<T> {4 const csrf = document.querySelector<HTMLMetaElement>('meta[name="csrf-token"]')?.content ?? "";5 const r = await fetch(path, { ...init, headers: { "content-type": "application/json", "x-csrf-token": csrf, ...(init.headers ?? {}) }, credentials: "same-origin" });6 const j = await r.json().catch(() => ({}));7 if (!r.ok) throw new Error(j.error ?? `${r.status}`);8 return j as T;9}10