import type { ApiResult, HealthResponse, SessionDetailResponse } from './types'; export interface ApiClient { health(): Promise>; getSession(code: string): Promise>; } export function createApiClient(baseUrl = '', fetchImpl: typeof fetch = fetch): ApiClient { async function request(path: string): Promise> { let response: Response; try { response = await fetchImpl(`${baseUrl}${path}`, { method: 'GET', headers: { Accept: 'application/json' } }); } catch { return { ok: false, status: 0, error: { kind: 'network', status: 0, message: 'Network error while contacting API' } }; } let payload: unknown; try { payload = await response.json(); } catch { return { ok: false, status: response.status, error: { kind: 'parse', status: response.status, message: 'Invalid JSON response from API' } }; } if (!response.ok) { return { ok: false, status: response.status, error: { kind: 'http', status: response.status, message: `HTTP ${response.status}`, payload } }; } return { ok: true, status: response.status, data: payload as T }; } return { health: () => request('/healthz'), getSession: (code: string) => request(`/lobby/sessions/${encodeURIComponent(code.trim().toUpperCase())}`) }; }