From 03ff056e518c3e880ecb084a527d7c7dbe598875 Mon Sep 17 00:00:00 2001 From: Johny Ho Date: Mon, 2 Dec 2024 05:46:19 -0500 Subject: [PATCH] Pass options down to fetch This takes any additional options and passes it down to fetch when calling visit or remote. --- superglue/lib/action_creators/requests.ts | 18 ++++-------------- superglue/lib/index.tsx | 1 + superglue/lib/types/index.ts | 2 ++ superglue/lib/types/requests.ts | 4 ++-- superglue/lib/utils/request.ts | 10 ++++++++-- 5 files changed, 17 insertions(+), 18 deletions(-) diff --git a/superglue/lib/action_creators/requests.ts b/superglue/lib/action_creators/requests.ts index fc0de8f9..bf04d565 100644 --- a/superglue/lib/action_creators/requests.ts +++ b/superglue/lib/action_creators/requests.ts @@ -80,23 +80,17 @@ sure you want to proceed, use force: true. export const remote: RemoteCreator = ( path, { - method = 'GET', - headers, - body, pageKey: targetPageKey, force = false, beforeSave = (prevPage: Page, receivedPage: PageResponse) => receivedPage, + ...rest } = {} ) => { path = withoutBusters(path) targetPageKey = targetPageKey && urlToPageKey(targetPageKey) return (dispatch, getState) => { - const fetchArgs = argsForFetch(getState, path, { - method, - headers, - body, - }) + const fetchArgs = argsForFetch(getState, path, rest) const currentPageKey = getState().superglue.currentPageKey dispatch(beforeRemote({ currentPageKey, fetchArgs })) @@ -143,12 +137,10 @@ let lastVisitController = { export const visit: VisitCreator = ( path, { - method = 'GET', - headers, - body, placeholderKey, beforeSave = (prevPage: Page, receivedPage: PageResponse) => receivedPage, revisit = false, + ...rest } = {} ) => { path = withoutBusters(path) @@ -176,9 +168,7 @@ export const visit: VisitCreator = ( const controller = new AbortController() const { signal } = controller const fetchArgs = argsForFetch(getState, path, { - headers, - body, - method, + ...rest, signal, }) diff --git a/superglue/lib/index.tsx b/superglue/lib/index.tsx index 4371bd0e..65b2e542 100644 --- a/superglue/lib/index.tsx +++ b/superglue/lib/index.tsx @@ -197,6 +197,7 @@ const Application = forwardRef(function Application( visit, remote, ujsAttributePrefix: 'data-sg', + store }) useEffect(() => { diff --git a/superglue/lib/types/index.ts b/superglue/lib/types/index.ts index 1c34d428..108b4092 100644 --- a/superglue/lib/types/index.ts +++ b/superglue/lib/types/index.ts @@ -335,10 +335,12 @@ export type UJSHandlers = ({ ujsAttributePrefix, visit, remote, + store, }: { ujsAttributePrefix: string visit: Visit remote: Remote + store: SuperglueStore }) => Handlers /** diff --git a/superglue/lib/types/requests.ts b/superglue/lib/types/requests.ts index 68776e93..0464c9b1 100644 --- a/superglue/lib/types/requests.ts +++ b/superglue/lib/types/requests.ts @@ -27,7 +27,7 @@ export interface Visit { /** * Options for Visit */ -export interface VisitProps extends BaseProps { +export interface VisitProps extends Omit { /** * When present, Superglue will use the page state located at that pageKey and * optimistally navigates to it as the next page's state while the requests @@ -62,7 +62,7 @@ export interface Remote { /** * Basic options shared betwen {@link VisitProps} and {@link RemoteProps} */ -interface BaseProps { +interface BaseProps extends RequestInit { /** The HTTP method */ method?: string /** The HTTP body */ diff --git a/superglue/lib/utils/request.ts b/superglue/lib/utils/request.ts index 629847d8..eb2ae283 100644 --- a/superglue/lib/utils/request.ts +++ b/superglue/lib/utils/request.ts @@ -63,7 +63,13 @@ export function handleServerErrors(args: ParsedResponse): ParsedResponse { export function argsForFetch( getState: () => RootState, pathQuery: string, - { method = 'GET', headers = {}, body = '', signal }: BasicRequestInit = {} + { + method = 'GET', + headers = {}, + body = '', + signal, + ...rest + }: BasicRequestInit = {} ): [string, BasicRequestInit] { method = method.toUpperCase() const currentState = getState().superglue @@ -130,7 +136,7 @@ export function argsForFetch( delete options.body } - return [fetchPath.toString(), options] + return [fetchPath.toString(), { ...options, ...rest }] } export function extractJSON(rsp: Response): PromiseLike {