import { HttpError } from "./http-error"; async function http(path: string, config: RequestInit): Promise { const request = new Request(path, config); const response: Response = await fetch(request); if (!response.ok) { const errJson = await response.json(); const err = HttpError.fromRequest(request, { ...response, statusText: errJson.message || response.statusText, }); throw err; } // may error if there is no body, return empty array return await response.json(); } export async function get(path: string, config?: RequestInit): Promise { const init = { method: "GET", ...config }; return await http(path, init); } export async function post(path: string, body: T, config?: RequestInit): Promise { const init = { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(body), ...config, }; return await http(path, init); } export async function put(path: string, body: T, config?: RequestInit): Promise { const init = { method: "PUT", headers: { "Content-Type": "application/json" }, body: JSON.stringify(body), ...config, }; return await http(path, init); } export async function patch(path: string, body: T, config?: RequestInit): Promise { const init = { method: "PATCH", headers: { "Content-Type": "application/json" }, body: JSON.stringify(body), ...config, }; return await http(path, init); } export async function remove(path: string, body: T, config?: RequestInit): Promise { const init = { method: "DELETE", headers: { "Content-Type": "application/json" }, body: JSON.stringify(body), ...config, }; return await http(path, init); }