export class ApiError extends Error {}
export async function api(method, path, body) {
const init = { method };
if (body !== undefined) {
init.headers = { 'content-type': 'application/json' };
init.body = JSON.stringify(body);
}
const res = await fetch(path, init);
let data = null;
try {
data = await res.json();
} catch {
}
if (!res.ok) {
throw new ApiError((data && data.error) || `Request failed (${res.status})`);
}
return data;
}