Type some Netlify functions more strongly

So no more `any`, and use some typing from some libraries
This commit is contained in:
Taevas 2025-02-17 13:26:10 +01:00
parent 96911a8d95
commit 41d33ab964
10 changed files with 158 additions and 144 deletions

View file

@ -1,12 +1,24 @@
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(async response => {
if (!response.ok) {
console.error(response.status, response.statusText);
throw new Error("Request failed :(");
}
return response.json() as Promise<T>;
export async function api<T>(url: string, restful_token?: string, post?: boolean, body?: BodyInit): Promise<T> {
let fetched: Promise<Response>;
if (post) {
fetched = fetch(url, {
method: "POST",
headers: {
"Authorization": `Bearer ${restful_token}`,
"Content-Type": "application/json",
},
body,
});
} else {
fetched = (restful_token ? fetch(url, {headers: {"Authorization": `Bearer ${restful_token}`}}) : fetch(url));
}
return fetched.then(async response => {
if (!response.ok) {
console.error(response.status, response.statusText);
throw new Error("Request failed :(");
}
return response.json() as Promise<T>;
});
}