add frontend skeleton
This commit is contained in:
parent
0c52ebb7bc
commit
a80f89d038
23 changed files with 2110 additions and 3 deletions
1
frontend/src/lib/index.ts
Normal file
1
frontend/src/lib/index.ts
Normal file
|
@ -0,0 +1 @@
|
|||
// place files you want to import through the `$lib` alias in this folder.
|
63
frontend/src/lib/request.ts
Normal file
63
frontend/src/lib/request.ts
Normal file
|
@ -0,0 +1,63 @@
|
|||
export type FetchOptions = {
|
||||
fetchFn?: typeof fetch;
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
data?: any;
|
||||
version?: number;
|
||||
extraHeaders?: Record<string, string>;
|
||||
};
|
||||
|
||||
/**
|
||||
* Fetch a path from the API and parse the response.
|
||||
* To make sure the request is authenticated in load functions,
|
||||
* pass `fetch` from the request object into opts.
|
||||
*
|
||||
* @param method The HTTP method, i.e. GET, POST, PATCH
|
||||
* @param path The path to request, minus the leading `/api/v2`
|
||||
* @param opts Extra options for this request
|
||||
* @returns T
|
||||
* @throws APIError
|
||||
*/
|
||||
export default async function request<T>(
|
||||
method: string,
|
||||
path: string,
|
||||
opts: FetchOptions = {},
|
||||
): Promise<T> {
|
||||
const { data, version, extraHeaders } = opts;
|
||||
const fetchFn = opts.fetchFn ?? fetch;
|
||||
|
||||
const resp = await fetchFn(`/api/v${version ?? 2}${path}`, {
|
||||
method,
|
||||
body: data ? JSON.stringify(data) : undefined,
|
||||
headers: { ...extraHeaders, "Content-Type": "application/json" },
|
||||
});
|
||||
|
||||
if (resp.status < 200 || resp.status >= 400) throw await resp.json();
|
||||
return (await resp.json()) as T;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch a path from the API and discard the response.
|
||||
* To make sure the request is authenticated in load functions,
|
||||
* pass `fetch` from the request object into opts.
|
||||
*
|
||||
* @param method The HTTP method, i.e. GET, POST, PATCH
|
||||
* @param path The path to request, minus the leading `/api/v2`
|
||||
* @param opts Extra options for this request
|
||||
* @throws APIError
|
||||
*/
|
||||
export async function fastRequest(
|
||||
method: string,
|
||||
path: string,
|
||||
opts: FetchOptions = {},
|
||||
): Promise<void> {
|
||||
const { data, version, extraHeaders } = opts;
|
||||
const fetchFn = opts.fetchFn ?? fetch;
|
||||
|
||||
const resp = await fetchFn(`/api/v2${version ?? 2}${path}`, {
|
||||
method,
|
||||
body: data ? JSON.stringify(data) : undefined,
|
||||
headers: { ...extraHeaders, "Content-Type": "application/json" },
|
||||
});
|
||||
|
||||
if (resp.status < 200 || resp.status >= 400) throw await resp.json();
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue