Skip to content

Commit

Permalink
Pass options down to fetch
Browse files Browse the repository at this point in the history
This takes any additional options and passes it down to fetch when calling
visit or remote.
  • Loading branch information
jho406 committed Dec 2, 2024
1 parent d6a1424 commit 03ff056
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 18 deletions.
18 changes: 4 additions & 14 deletions superglue/lib/action_creators/requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }))
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
})

Expand Down
1 change: 1 addition & 0 deletions superglue/lib/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ const Application = forwardRef(function Application(
visit,
remote,
ujsAttributePrefix: 'data-sg',
store
})

useEffect(() => {
Expand Down
2 changes: 2 additions & 0 deletions superglue/lib/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,10 +335,12 @@ export type UJSHandlers = ({
ujsAttributePrefix,
visit,
remote,
store,
}: {
ujsAttributePrefix: string
visit: Visit
remote: Remote
store: SuperglueStore
}) => Handlers

/**
Expand Down
4 changes: 2 additions & 2 deletions superglue/lib/types/requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export interface Visit {
/**
* Options for Visit
*/
export interface VisitProps extends BaseProps {
export interface VisitProps extends Omit<BaseProps, 'signal'> {
/**
* 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
Expand Down Expand Up @@ -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 */
Expand Down
10 changes: 8 additions & 2 deletions superglue/lib/utils/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<ParsedResponse> {
Expand Down

0 comments on commit 03ff056

Please sign in to comment.