SPB Git
598 B · 10 lines typescript
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
Files · d0878ed8ec6f375df09d3e415bf35d941ff62d79
Menu

Theme