m
type Request = <T>(method: string, url: string, data?: unknown) => Promise<T>;
const request: Request = async (method, url, data) => {
const h = new Headers();
h.append('Content-Type', 'application/json');
const options: RequestInit = {
method: method.toUpperCase(),
headers: h,
};
if (data) options.body = JSON.stringify(data);
const res = await fetch(url, options);
if (res.ok) {
return res.json();
}
throw await res.json();
};