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

13 lines
435 B
TypeScript
Raw Normal View History

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