Skip to content

Commit 3e77381

Browse files
phryneasmarkerikson
authored andcommitted
fetchBaseQuery: expose extraOptions to prepareHeaders
1 parent 7b50a61 commit 3e77381

File tree

2 files changed

+54
-6
lines changed

2 files changed

+54
-6
lines changed

packages/toolkit/src/query/fetchBaseQuery.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,14 @@ function stripUndefined(obj: any) {
105105
return copy
106106
}
107107

108-
export type FetchBaseQueryArgs = {
108+
export type FetchBaseQueryArgs<ExtraOptions = {}> = {
109109
baseUrl?: string
110110
prepareHeaders?: (
111111
headers: Headers,
112112
api: Pick<
113113
BaseQueryApi,
114114
'getState' | 'extra' | 'endpoint' | 'type' | 'forced'
115-
> & { arg: string | FetchArgs },
115+
> & { arg: string | FetchArgs; extraOptions: ExtraOptions },
116116
) => MaybePromise<Headers | void>
117117
fetchFn?: (
118118
input: RequestInfo,
@@ -188,7 +188,7 @@ export type FetchBaseQueryMeta = { request: Request; response?: Response }
188188
* @param {number} timeout
189189
* A number in milliseconds that represents the maximum time a request can take before timing out.
190190
*/
191-
export function fetchBaseQuery({
191+
export function fetchBaseQuery<ExtraOptions>({
192192
baseUrl,
193193
prepareHeaders = (x) => x,
194194
fetchFn = defaultFetchFn,
@@ -200,19 +200,19 @@ export function fetchBaseQuery({
200200
responseHandler: globalResponseHandler,
201201
validateStatus: globalValidateStatus,
202202
...baseFetchOptions
203-
}: FetchBaseQueryArgs = {}): BaseQueryFn<
203+
}: FetchBaseQueryArgs<ExtraOptions> = {}): BaseQueryFn<
204204
string | FetchArgs,
205205
unknown,
206206
FetchBaseQueryError,
207-
{},
207+
ExtraOptions,
208208
FetchBaseQueryMeta
209209
> {
210210
if (typeof fetch === 'undefined' && fetchFn === defaultFetchFn) {
211211
console.warn(
212212
'Warning: `fetch` is not available. Please supply a custom `fetchFn` property to use `fetchBaseQuery` on SSR environments.',
213213
)
214214
}
215-
return async (arg, api) => {
215+
return async (arg, api, extraOptions) => {
216216
const { getState, extra, endpoint, forced, type } = api
217217
let meta: FetchBaseQueryMeta | undefined
218218
let {
@@ -248,6 +248,7 @@ export function fetchBaseQuery({
248248
endpoint,
249249
forced,
250250
type,
251+
extraOptions,
251252
})) || headers
252253

253254
// Only set the content-type to json if appropriate. Will not be true for FormData, ArrayBuffer, Blob, etc.

packages/toolkit/src/query/tests/fetchBaseQuery.test.tsx

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { createSlice } from '@reduxjs/toolkit'
22
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query'
33
import { headersToObject } from 'headers-polyfill'
44
import { HttpResponse, delay, http } from 'msw'
5+
// @ts-ignore
56
import nodeFetch from 'node-fetch'
67
import queryString from 'query-string'
78
import { vi } from 'vitest'
@@ -852,6 +853,52 @@ describe('fetchBaseQuery', () => {
852853
expect(_forced).toBe(true)
853854
expect(_extra).toBe(fakeAuth0Client)
854855
})
856+
857+
test('can be instantiated with a `ExtraOptions` generic and `extraOptions` will be available in `prepareHeaders', async () => {
858+
const prepare = vitest.fn()
859+
const baseQuery = fetchBaseQuery<{ foo?: string; bar?: number }>({
860+
prepareHeaders(headers, api) {
861+
expectTypeOf(api.extraOptions).toEqualTypeOf<{ foo?: string; bar?: number }>()
862+
prepare.call(undefined, arguments)
863+
},
864+
})
865+
baseQuery('', commonBaseQueryApi, { foo: 'baz', bar: 5 })
866+
expect(prepare).toHaveBeenCalledWith(
867+
expect.anything(),
868+
expect.objectContaining({ extraOptions: { foo: 'baz', bar: 5 } })
869+
)
870+
871+
// ensure types
872+
createApi({
873+
baseQuery,
874+
endpoints(build) {
875+
return {
876+
testQuery: build.query({
877+
query: () => ({ url: '/echo', headers: {} }),
878+
extraOptions: {
879+
foo: 'asd',
880+
bar: 1,
881+
// @ts-expect-error
882+
baz: 5,
883+
},
884+
}),
885+
testMutation: build.mutation({
886+
query: () => ({
887+
url: '/echo',
888+
method: 'POST',
889+
credentials: 'omit',
890+
}),
891+
extraOptions: {
892+
foo: 'qwe',
893+
bar: 15,
894+
// @ts-expect-error
895+
baz: 5,
896+
},
897+
}),
898+
}
899+
},
900+
})
901+
})
855902
})
856903

857904
test('can pass `headers` into `fetchBaseQuery`', async () => {

0 commit comments

Comments
 (0)