Skip to content

Commit fa0906e

Browse files
authored
Merge pull request #4291 from reduxjs/pr/fetchBaseQuery-extraOptions
fetchBaseQuery: expose extraOptions to prepareHeaders
2 parents 7b50a61 + 896e4df commit fa0906e

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

packages/toolkit/src/query/fetchBaseQuery.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ export type FetchBaseQueryArgs = {
112112
api: Pick<
113113
BaseQueryApi,
114114
'getState' | 'extra' | 'endpoint' | 'type' | 'forced'
115-
> & { arg: string | FetchArgs },
115+
> & { arg: string | FetchArgs; extraOptions: unknown },
116116
) => MaybePromise<Headers | void>
117117
fetchFn?: (
118118
input: RequestInfo,
@@ -188,6 +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+
191192
export function fetchBaseQuery({
192193
baseUrl,
193194
prepareHeaders = (x) => x,
@@ -212,7 +213,7 @@ export function fetchBaseQuery({
212213
'Warning: `fetch` is not available. Please supply a custom `fetchFn` property to use `fetchBaseQuery` on SSR environments.',
213214
)
214215
}
215-
return async (arg, api) => {
216+
return async (arg, api, extraOptions) => {
216217
const { getState, extra, endpoint, forced, type } = api
217218
let meta: FetchBaseQueryMeta | undefined
218219
let {
@@ -248,6 +249,7 @@ export function fetchBaseQuery({
248249
endpoint,
249250
forced,
250251
type,
252+
extraOptions,
251253
})) || headers
252254

253255
// 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: 46 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,51 @@ 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({
860+
prepareHeaders(headers, api) {
861+
expectTypeOf(api.extraOptions).toEqualTypeOf<unknown>()
862+
prepare.apply(undefined, arguments as unknown as any[])
863+
},
864+
})
865+
baseQuery('http://example.com', commonBaseQueryApi, {
866+
foo: 'baz',
867+
bar: 5,
868+
})
869+
expect(prepare).toHaveBeenCalledWith(
870+
expect.anything(),
871+
expect.objectContaining({ extraOptions: { foo: 'baz', bar: 5 } }),
872+
)
873+
874+
// ensure types
875+
createApi({
876+
baseQuery,
877+
endpoints(build) {
878+
return {
879+
testQuery: build.query({
880+
query: () => ({ url: '/echo', headers: {} }),
881+
extraOptions: {
882+
foo: 'asd',
883+
bar: 1,
884+
},
885+
}),
886+
testMutation: build.mutation({
887+
query: () => ({
888+
url: '/echo',
889+
method: 'POST',
890+
credentials: 'omit',
891+
}),
892+
extraOptions: {
893+
foo: 'qwe',
894+
bar: 15,
895+
},
896+
}),
897+
}
898+
},
899+
})
900+
})
855901
})
856902

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

0 commit comments

Comments
 (0)