taevas.xyz/netlify/functions/shared/api.ts

13 lines
418 B
TypeScript
Raw Normal View History

2023-11-01 21:15:29 +01:00
export async function api<T>(url: string, restful_token?: string): Promise<T> {
2025-02-16 17:05:02 +01:00
2023-11-01 21:15:29 +01:00
return (restful_token ? fetch(url, {headers: {"Authorization": `Bearer ${restful_token}`}}) : fetch(url))
.then(async response => {
if (!response.ok) {
console.error(response.status, response.statusText);
throw new Error("Request failed :(");
}
return response.json() as Promise<T>;
});
}