import "server-only"; import { cookies, headers } from "next/headers"; const apiUrl = (process.env.API_URL ?? "http://127.0.0.1:8231").replace(/\/$/, ""); /** Server-side fetch to the API, forwarding the player's session cookie. Returns null on 401/404. */ export async function apiServer(path: string, init: RequestInit = {}): Promise { const c = await cookies(); const h = await headers(); const cookie = c .getAll() .map((x) => `${x.name}=${x.value}`) .join("; "); try { const res = await fetch(`${apiUrl}${path}`, { ...init, cache: "no-store", headers: { ...(init.headers ?? {}), cookie, "x-forwarded-for": h.get("x-forwarded-for") ?? "", "user-agent": h.get("user-agent") ?? "" }, }); if (res.status === 401 || res.status === 404) return null; if (!res.ok) return null; return (await res.json()) as T; } catch { return null; } }