import { HttpError } from "./errors"; async function readErrorBody(response: Response): Promise { return await response.text().catch(() => ""); } /** * Fetches a URL and parses a successful response body as JSON. * Throws `HttpError` with the status and response body on non-OK responses; otherwise returns the parsed body as `T`. * Leaves URL, headers, and abort handling unchanged so callers own provider-specific normalization. */ export async function fetchJson(url: string, init?: RequestInit): Promise { const response = await fetch(url, init); if (!response.ok) { throw new HttpError(response.status, await readErrorBody(response)); } return await response.json() as T; } /** * Fetches a URL and reads a successful response body as text. * Throws `HttpError` with the status and response body on non-OK responses; otherwise returns the raw text body. * Performs no response cleanup or binary conversion, so callers must choose this only for true text payloads. */ export async function fetchText(url: string, init?: RequestInit): Promise { const response = await fetch(url, init); if (!response.ok) { throw new HttpError(response.status, await readErrorBody(response)); } return await response.text(); }