// Server components call the API directly (container network); the browser uses same-origin /api. const base = typeof window === 'undefined' ? (process.env.API_INTERNAL_URL ?? 'http://127.0.0.1:3001') : ''; export class ApiError extends Error { constructor( readonly status: number, path: string, ) { super(`API ${status} for ${path}`); } } export async function apiGet(path: string): Promise { let res: Response; try { res = await fetch(`${base}${path}`, { cache: 'no-store' }); } catch { throw new ApiError(503, path); } if (!res.ok) throw new ApiError(res.status, path); return (await res.json()) as T; } /** Like apiGet, but resolves to null for 404 (unknown player, board …). */ export async function apiFind(path: string): Promise { try { return await apiGet(path); } catch (e) { if (e instanceof ApiError && e.status === 404) return null; throw e; } } export const avatarUrl = (uuid: string) => `/api/avatar/${uuid}`;