'use strict'; declare class HttpError extends Error { status?: number; responseText?: string; } declare interface Http { setCsrfToken(token: string): void; get(url: string): Promise; post(url: string, body: Record): Promise; blob(url: string): Promise; text(url: string): Promise; } declare interface HttpResponse { data: any; ok: boolean; status: number; } declare interface HttpBlobResponse { blob: Blob; ok: boolean; status: number; } declare interface HttpTextResponse { text: string; ok: boolean; status: number; } /** * Static HTTP client wrapping fetch with CSRF support. * * Usage: * const resp = await Http.get('backend.php?op=list'); * const data = resp.data; * * await Http.post('backend.php', { op: 'storelastread', id: 42 }); * * const { blob } = await Http.blob('backend.php?op=download&id=42'); */ class Http { private static _csrfToken = ''; /** * Set the CSRF token for subsequent requests. * @param token - The CSRF token string. */ static setCsrfToken(token: string): void { Http._csrfToken = token; } /** * Append query parameters to a URL string. * @private */ private static _appendParams(url: string, params: Record | null | undefined): string { if (!params || Object.keys(params).length === 0) return url; const separator = url.includes('?') ? '&' : '?'; const query = new URLSearchParams(params as Record).toString(); return `${url}${separator}${query}`; } /** * Throw a structured error for failed HTTP responses. * @private */ private static _throwHttpError(resp: Response): never { const err = new Error(`HTTP ${resp.status}: ${resp.statusText}`) as HttpError; (err as any).status = resp.status; (err as any).responseText = ''; // Read error body asynchronously — don't block the throw resp.text().then((t: string) => { (err as any).responseText = t; }).catch(() => { /* ignore */ }); throw err; } /** * Perform a GET request. Returns parsed JSON. * @param params - Optional query parameters. */ static async get(url: string, params: Record | null = null): Promise { const urlWithParams = Http._appendParams(url, params); const resp = await fetch(urlWithParams, { method: 'GET', credentials: 'same-origin', }); if (!resp.ok) { Http._throwHttpError(resp); } const data = await resp.json(); return { data, ok: true, status: resp.status }; } /** * Perform a POST request with form-encoded body. Returns parsed JSON. * @param body - Form-encoded body parameters. */ static async post(url: string, body: Record): Promise { const params = new URLSearchParams(body); if (Http._csrfToken) { params.append('csrf_token', Http._csrfToken); } const resp = await fetch(url, { method: 'POST', credentials: 'same-origin', body: params, // passing URLSearchParams here gives us automatic application/x-www-form-urlencoded content type }); if (!resp.ok) { Http._throwHttpError(resp); } const data = await resp.json(); return { data, ok: true, status: resp.status }; } /** * Perform a GET request and return the body as a Blob. * @param params - Optional query parameters. */ static async blob(url: string, params: Record | null = null): Promise { const urlWithParams = Http._appendParams(url, params); const resp = await fetch(urlWithParams, { method: 'GET', credentials: 'same-origin', }); if (!resp.ok) { Http._throwHttpError(resp); } const blob = await resp.blob(); return { blob, ok: true, status: resp.status }; } /** * Perform a GET request and return the body as plain text. * @param params - Optional query parameters. */ static async text(url: string, params: Record | null = null): Promise { const urlWithParams = Http._appendParams(url, params); const resp = await fetch(urlWithParams, { method: 'GET', credentials: 'same-origin', }); if (!resp.ok) { Http._throwHttpError(resp); } const text = await resp.text(); return { text, ok: true, status: resp.status }; } }