This commit is contained in:
Sam 2023-03-09 17:08:43 +01:00
commit 90205a1243
Signed by: sam
GPG key ID: B4EF20DDE721CAA1
29 changed files with 3216 additions and 0 deletions

19
src/lib/api/fetch.ts Normal file
View file

@ -0,0 +1,19 @@
import type { APIError } from "./entities";
export async function fetchAPI<T>(
path: string,
{ method, body, token }: { method?: string; body?: any; token?: string },
) {
const resp = await fetch(`${process.env.ORIGIN}/api/v1${path}`, {
method,
headers: {
...(token ? { Authorization: token } : {}),
"Content-Type": "application/json",
},
body: body ? JSON.stringify(body) : null,
});
const data = await resp.json();
if (resp.status < 200 || resp.status >= 300) throw data as APIError;
return data as T;
}