chore(frontend): add docs to RequestArgs
This commit is contained in:
parent
b47ed7b699
commit
54be457a47
1 changed files with 23 additions and 3 deletions
|
@ -6,11 +6,31 @@ import log from "$lib/log";
|
||||||
|
|
||||||
export type Method = "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
|
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 = {
|
export type RequestArgs = {
|
||||||
|
/**
|
||||||
|
* The token for this request. Where possible, `cookies` should be passed instead.
|
||||||
|
* Will override `cookies` if both are passed.
|
||||||
|
*/
|
||||||
token?: string;
|
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;
|
isInternal?: boolean;
|
||||||
|
/**
|
||||||
|
* The body for this request, which will be serialized to JSON. Should be a plain JS object.
|
||||||
|
*/
|
||||||
body?: any;
|
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;
|
fetch?: typeof fetch;
|
||||||
|
/**
|
||||||
|
* The cookies object to try to get the token from. Can only be passed in loader and action functions.
|
||||||
|
*/
|
||||||
cookies?: Cookies;
|
cookies?: Cookies;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -19,7 +39,7 @@ export type RequestArgs = {
|
||||||
* @param method The HTTP method for this request
|
* @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 path The path for this request, without the /api/v2 prefix, starting with a slash.
|
||||||
* @param args Optional arguments to the request function.
|
* @param args Optional arguments to the request function.
|
||||||
* @returns A Promise object.
|
* @returns A Response object.
|
||||||
*/
|
*/
|
||||||
export async function baseRequest(
|
export async function baseRequest(
|
||||||
method: Method,
|
method: Method,
|
||||||
|
@ -29,7 +49,7 @@ export async function baseRequest(
|
||||||
const token = args.token ?? args.cookies?.get(TOKEN_COOKIE_NAME);
|
const token = args.token ?? args.cookies?.get(TOKEN_COOKIE_NAME);
|
||||||
|
|
||||||
const fetchFn = args.fetch ?? fetch;
|
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);
|
log.debug("Sending request to %s %s", method, url);
|
||||||
|
|
||||||
|
@ -38,7 +58,7 @@ export async function baseRequest(
|
||||||
...(token ? { Authorization: token } : {}),
|
...(token ? { Authorization: token } : {}),
|
||||||
};
|
};
|
||||||
|
|
||||||
return await fetchFn(url, {
|
return await fetchFn(PUBLIC_API_BASE + url, {
|
||||||
method,
|
method,
|
||||||
headers,
|
headers,
|
||||||
body: args.body ? JSON.stringify(args.body) : undefined,
|
body: args.body ? JSON.stringify(args.body) : undefined,
|
||||||
|
|
Loading…
Reference in a new issue