2023-11-01 21:15:29 +01:00
|
|
|
export async function api<T>(url: string, restful_token?: string): Promise<T> {
|
2024-04-27 18:45:18 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
2023-11-01 21:15:29 +01:00
|
|
|
return (restful_token ? fetch(url, {headers: {"Authorization": `Bearer ${restful_token}`}}) : fetch(url))
|
2024-04-27 18:45:18 +02:00
|
|
|
.then(async response => {
|
2023-05-07 02:13:48 +02:00
|
|
|
if (!response.ok) {
|
2024-04-27 18:45:18 +02:00
|
|
|
console.error(response.status, response.statusText);
|
|
|
|
throw new Error("Request failed :(");
|
2023-05-07 02:13:48 +02:00
|
|
|
}
|
2024-04-27 18:45:18 +02:00
|
|
|
|
|
|
|
return response.json() as Promise<T>;
|
|
|
|
});
|
2023-05-07 02:13:48 +02:00
|
|
|
}
|