export class ApiError extends Error { constructor(message, status, code) { super(message); this.status = status; this.code = code; } } export async function api(path, { method = 'GET', body, token, playerToken } = {}) { let res; try { res = await fetch(`/api${path}`, { method, headers: { ...(body ? { 'content-type': 'application/json' } : {}), ...(token ? { authorization: `Bearer ${token}` } : {}), // Zweiter Zugang: Spieler-Token des Organisators, wenn er selbst mitspielt ...(playerToken ? { 'x-player-token': playerToken } : {}), }, body: body ? JSON.stringify(body) : undefined, }); } catch { throw new ApiError('Keine Verbindung zum Server. Prüfe deine Internetverbindung.', 0, 'network'); } const data = await res.json().catch(() => ({})); if (!res.ok) throw new ApiError(data.error ?? `Fehler ${res.status}`, res.status, data.code); return data; }