export function makeApiUrl(path: string, protocol: 'http' | 'ws' = 'http') { return new URL(`${protocol}://${window.location.hostname}${path}`); } export type ServerResponse = { ok: boolean; statusCode: number; statusText: string; additionalErrorText?: string; successResult?: T; } export type Scores = { readonly kills: number; readonly deaths: number; readonly wallsDestroyed: number; readonly shotsFired: number; readonly overallScore: number; }; export type Player = { readonly name: string; readonly scores: Scores; }; export async function fetchTyped({url, method}: { url: URL; method: string; }): Promise> { const response = await fetch(url, {method}); const result: ServerResponse = { ok: response.ok, statusCode: response.status, statusText: response.statusText }; if (response.ok) result.successResult = await response.json(); else result.additionalErrorText = await response.text(); return result; } export function postPlayer(name: string) { const url = makeApiUrl('/player'); url.searchParams.set('name', name); return fetchTyped({url, method: 'POST'}); } export async function getMaps() { const url = makeApiUrl('/map'); return await fetchTyped({url, method: 'GET'}); } export function postMaps(map: string) { const url = makeApiUrl('/map'); url.searchParams.set('name', map); return fetchTyped({url, method: 'POST'}); }