type QueryParams = Record; export type APICallResult = Response & { json: () => Promise }; export async function apiCall( method: "GET", api: string, params?: QueryParams ): Promise>; export async function apiCall( method: "POST" | "PATCH", api: string, params?: QueryParams, body?: object ): Promise>; export async function apiCall( method: "DELETE", api: string, params?: QueryParams, body?: object ): Promise>; export async function apiCall( method: "GET" | "POST" | "PATCH" | "DELETE", api: string, params?: QueryParams, body?: object ): Promise> { let url = window.origin + "/api/" + api; if (params) { const urlsearchparams = new URLSearchParams( Object.fromEntries( Object.entries(params).map(([key, value]): [string, string] => { if (typeof value !== "string") { return [key, value.toString()]; } else { return [key, value]; } }) ) ); url += "?" + urlsearchparams.toString(); } const response = await fetch(url, { headers: { "Content-Type": "application/json; charset=UTF-8" }, credentials: "include", method, body: body !== undefined ? JSON.stringify(body) : undefined }); return response; }