SPB Git forge

spb/spinza

Public
8commits 1branches 0releases
1.6 MBsize
maindefault branch
16 days agolast push
TypeScript 97.6% SQL 1.4% JavaScript 0.5%
915 B · 27 lines typescript
Raw Blame History
1import "server-only";2import { cookies, headers } from "next/headers";34const apiUrl = (process.env.API_URL ?? "http://127.0.0.1:8231").replace(/\/$/, "");56/** Server-side fetch to the API, forwarding the player's session cookie. Returns null on 401/404. */7export async function apiServer<T>(path: string, init: RequestInit = {}): Promise<T | null> {8  const c = await cookies();9  const h = await headers();10  const cookie = c11    .getAll()12    .map((x) => `${x.name}=${x.value}`)13    .join("; ");14  try {15    const res = await fetch(`${apiUrl}${path}`, {16      ...init,17      cache: "no-store",18      headers: { ...(init.headers ?? {}), cookie, "x-forwarded-for": h.get("x-forwarded-for") ?? "", "user-agent": h.get("user-agent") ?? "" },19    });20    if (res.status === 401 || res.status === 404) return null;21    if (!res.ok) return null;22    return (await res.json()) as T;23  } catch {24    return null;25  }26}27