chore(frontend): add docs to RequestArgs

This commit is contained in:
sam 2024-12-02 16:31:48 +01:00
parent b47ed7b699
commit 54be457a47
Signed by: sam
GPG key ID: B4EF20DDE721CAA1

View file

@ -6,11 +6,31 @@ import log from "$lib/log";
export type Method = "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
/**
* Optional arguments for a request. `load` and `action` functions should always pass `fetch` and `cookies`.
*/
export type RequestArgs = {
/**
* The token for this request. Where possible, `cookies` should be passed instead.
* Will override `cookies` if both are passed.
*/
token?: string;
/**
* Whether this request is to an internal endpoint.
* Internal requests bypass the rate limiter and are prefixed with /api/internal/ rather than /api/v2/.
*/
isInternal?: boolean;
/**
* The body for this request, which will be serialized to JSON. Should be a plain JS object.
*/
body?: any;
/**
* The fetch function to use. Should be passed in loader and action functions, but can be safely ignored for client-side requests.
*/
fetch?: typeof fetch;
/**
* The cookies object to try to get the token from. Can only be passed in loader and action functions.
*/
cookies?: Cookies;
};
@ -19,7 +39,7 @@ export type RequestArgs = {
* @param method The HTTP method for this request
* @param path The path for this request, without the /api/v2 prefix, starting with a slash.
* @param args Optional arguments to the request function.
* @returns A Promise object.
* @returns A Response object.
*/
export async function baseRequest(
method: Method,
@ -29,7 +49,7 @@ export async function baseRequest(
const token = args.token ?? args.cookies?.get(TOKEN_COOKIE_NAME);
const fetchFn = args.fetch ?? fetch;
const url = `${PUBLIC_API_BASE}/${args.isInternal ? "internal" : "v2"}${path}`;
const url = `/${args.isInternal ? "internal" : "v2"}${path}`;
log.debug("Sending request to %s %s", method, url);
@ -38,7 +58,7 @@ export async function baseRequest(
...(token ? { Authorization: token } : {}),
};
return await fetchFn(url, {
return await fetchFn(PUBLIC_API_BASE + url, {
method,
headers,
body: args.body ? JSON.stringify(args.body) : undefined,