Type some Netlify functions more strongly
So no more `any`, and use some typing from some libraries
This commit is contained in:
parent
96911a8d95
commit
41d33ab964
10 changed files with 158 additions and 144 deletions
|
@ -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>;
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue