import { HttpError } from "./errors"; async function readErrorBody(response: Response): Promise { return await response.text().catch(() => ""); } 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; } 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(); }