2023-11-29 03:13:55 -08:00
|
|
|
async function getAccessToken() {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function authenticatedFetch<T>(...args: Parameters<typeof fetch>): Promise<T> {
|
|
|
|
const accessToken = await getAccessToken();
|
|
|
|
|
|
|
|
const response = await fetch(args[0], {
|
|
|
|
...args[1],
|
|
|
|
mode: 'cors',
|
|
|
|
cache: 'no-cache',
|
|
|
|
headers: {
|
2024-01-09 03:11:39 -08:00
|
|
|
'Content-Type': 'application/json',
|
2023-11-29 03:13:55 -08:00
|
|
|
...(accessToken ? { authorization: `Bearer ${accessToken}` } : {}),
|
|
|
|
...args[1]?.headers,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2024-01-18 01:28:01 -08:00
|
|
|
return (await response.json()) as T;
|
2023-11-29 03:13:55 -08:00
|
|
|
}
|
|
|
|
|
2024-01-09 03:11:39 -08:00
|
|
|
export async function get<T>(url: string, query: object = {}, options: RequestInit = {}) {
|
2023-11-29 03:13:55 -08:00
|
|
|
let resolvedUrl = url;
|
|
|
|
if (Object.keys(query).length > 0) {
|
2024-01-09 03:11:39 -08:00
|
|
|
resolvedUrl = `${resolvedUrl}?${new URLSearchParams(
|
|
|
|
query as Record<string, string>,
|
|
|
|
).toString()}`;
|
2023-11-29 03:13:55 -08:00
|
|
|
}
|
|
|
|
|
2024-01-18 01:28:01 -08:00
|
|
|
return await authenticatedFetch<T>(resolvedUrl, { ...options, method: 'GET' });
|
2023-11-29 03:13:55 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function post<T>(url: string, body: object = {}, options: RequestInit = {}) {
|
2024-01-18 01:28:01 -08:00
|
|
|
return await authenticatedFetch<T>(url, {
|
2023-11-29 03:13:55 -08:00
|
|
|
...options,
|
|
|
|
method: 'POST',
|
|
|
|
body: JSON.stringify(body),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function put<T>(url: string, body: object = {}, options: RequestInit = {}) {
|
2024-01-18 01:28:01 -08:00
|
|
|
return await authenticatedFetch<T>(url, {
|
2023-11-29 03:13:55 -08:00
|
|
|
...options,
|
|
|
|
method: 'PUT',
|
|
|
|
body: JSON.stringify(body),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function patch<T>(url: string, body: object = {}, options: RequestInit = {}) {
|
2024-01-18 01:28:01 -08:00
|
|
|
return await authenticatedFetch<T>(url, {
|
2023-11-29 03:13:55 -08:00
|
|
|
...options,
|
|
|
|
method: 'PATCH',
|
|
|
|
body: JSON.stringify(body),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function del<T>(url: string, body: object = {}, options: RequestInit = {}) {
|
2024-01-18 01:28:01 -08:00
|
|
|
return await authenticatedFetch<T>(url, {
|
2023-11-29 03:13:55 -08:00
|
|
|
...options,
|
|
|
|
method: 'DELETE',
|
|
|
|
body: JSON.stringify(body),
|
|
|
|
});
|
|
|
|
}
|