From 54be457a470efe45b9684da8ed68e31ea14a57a1 Mon Sep 17 00:00:00 2001 From: sam Date: Mon, 2 Dec 2024 16:31:48 +0100 Subject: [PATCH] chore(frontend): add docs to RequestArgs --- Foxnouns.Frontend/src/lib/api/index.ts | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/Foxnouns.Frontend/src/lib/api/index.ts b/Foxnouns.Frontend/src/lib/api/index.ts index 0c8293d..f7a517d 100644 --- a/Foxnouns.Frontend/src/lib/api/index.ts +++ b/Foxnouns.Frontend/src/lib/api/index.ts @@ -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,