2022-05-11 02:23:45 +02:00
|
|
|
import axios from "axios";
|
|
|
|
import type { APIError } from "./types";
|
|
|
|
|
2022-05-12 16:41:32 +02:00
|
|
|
export default async function fetchAPI<T>(
|
|
|
|
path: string,
|
|
|
|
method = "GET",
|
|
|
|
body = null
|
|
|
|
) {
|
|
|
|
const resp = await fetch(`/api/v1${path}`, {
|
|
|
|
method,
|
2022-05-11 02:23:45 +02:00
|
|
|
headers: {
|
|
|
|
Authorization: localStorage.getItem("pronouns-token"),
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
2022-05-12 16:41:32 +02:00
|
|
|
body: body ? JSON.stringify(body) : null,
|
2022-05-11 02:23:45 +02:00
|
|
|
});
|
|
|
|
|
2022-05-12 16:41:32 +02:00
|
|
|
const data = await resp.json();
|
|
|
|
if (resp.status !== 200) throw data as APIError;
|
|
|
|
return data as T;
|
2022-05-11 02:23:45 +02:00
|
|
|
}
|