Skip to content

Commit a83ea31

Browse files
committed
Pass query args to prepareHeaders function
1 parent 719dac5 commit a83ea31

File tree

3 files changed

+51
-6
lines changed

3 files changed

+51
-6
lines changed

docs/rtk-query/api/fetchBaseQuery.mdx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ type FetchBaseQueryArgs = {
6565
BaseQueryApi,
6666
'getState' | 'extra' | 'endpoint' | 'type' | 'forced'
6767
>,
68+
args: string | FetchArgs
6869
) => MaybePromise<Headers | void>
6970
fetchFn?: (
7071
input: RequestInfo,
@@ -105,7 +106,7 @@ Typically a string like `https://api.your-really-great-app.com/v1/`. If you don'
105106

106107
_(optional)_
107108

108-
Allows you to inject headers on every request. You can specify headers at the endpoint level, but you'll typically want to set common headers like `authorization` here. As a convenience mechanism, the second argument allows you to use `getState` to access your redux store in the event you store information you'll need there such as an auth token. Additionally, it provides access to `extra`, `endpoint`, `type`, and `forced` to unlock more granular conditional behaviors.
109+
Allows you to inject headers on every request. You can specify headers at the endpoint level, but you'll typically want to set common headers like `authorization` here. As a convenience mechanism, the second argument allows you to use `getState` to access your redux store in the event you store information you'll need there such as an auth token. Additionally, it provides access to `extra`, `endpoint`, `type`, and `forced` to unlock more granular conditional behaviors. Arguments passed to the query function is passed as the third argument in case you need those params in the context of the `prepareHeaders` function.
109110

110111
You can mutate the `headers` argument directly, and returning it is optional.
111112

@@ -119,6 +120,7 @@ type prepareHeaders = (
119120
type: 'query' | 'mutation'
120121
forced: boolean | undefined
121122
},
123+
args: string | FetchArgs
122124
) => Headers | void
123125
```
124126

packages/toolkit/src/query/fetchBaseQuery.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ export type FetchBaseQueryArgs = {
113113
BaseQueryApi,
114114
'getState' | 'extra' | 'endpoint' | 'type' | 'forced'
115115
>,
116+
args: string | FetchArgs
116117
) => MaybePromise<Headers | void>
117118
fetchFn?: (
118119
input: RequestInfo,
@@ -164,9 +165,9 @@ export type FetchBaseQueryMeta = { request: Request; response?: Response }
164165
* The base URL for an API service.
165166
* Typically in the format of https://example.com/
166167
*
167-
* @param {(headers: Headers, api: { getState: () => unknown; extra: unknown; endpoint: string; type: 'query' | 'mutation'; forced: boolean; }) => Headers} prepareHeaders
168+
* @param {(headers: Headers, api: { getState: () => unknown; extra: unknown; endpoint: string; type: 'query' | 'mutation'; forced: boolean; }, args: string | FetchArgs) => Headers} prepareHeaders
168169
* An optional function that can be used to inject headers on requests.
169-
* Provides a Headers object, as well as most of the `BaseQueryApi` (`dispatch` is not available).
170+
* Provides a Headers object, most of the `BaseQueryApi` (`dispatch` is not available), and the args passed into the query function.
170171
* Useful for setting authentication or headers that need to be set conditionally.
171172
*
172173
* @link https://developer.mozilla.org/en-US/docs/Web/API/Headers
@@ -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 (args, api) => {
216217
const { signal, getState, extra, endpoint, forced, type } = api
217218
let meta: FetchBaseQueryMeta | undefined
218219
let {
@@ -223,7 +224,7 @@ export function fetchBaseQuery({
223224
validateStatus = globalValidateStatus ?? defaultValidateStatus,
224225
timeout = defaultTimeout,
225226
...rest
226-
} = typeof arg == 'string' ? { url: arg } : arg
227+
} = typeof args == 'string' ? { url: args } : args
227228
let config: RequestInit = {
228229
...baseFetchOptions,
229230
signal,
@@ -238,7 +239,7 @@ export function fetchBaseQuery({
238239
endpoint,
239240
forced,
240241
type,
241-
})) || headers
242+
}, args)) || headers
242243

243244
// Only set the content-type to json if appropriate. Will not be true for FormData, ArrayBuffer, Blob, etc.
244245
const isJsonifiable = (body: any) =>

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -850,6 +850,48 @@ describe('fetchBaseQuery', () => {
850850
expect(_forced).toBe(true)
851851
expect(_extra).toBe(fakeAuth0Client)
852852
})
853+
854+
test('prepareHeaders provides args', async () => {
855+
let _args
856+
857+
const baseQuery = fetchBaseQuery({
858+
baseUrl,
859+
fetchFn: fetchFn as any,
860+
prepareHeaders: (headers, _api, args) => {
861+
_args = args
862+
863+
return headers
864+
},
865+
})
866+
867+
const fakeAuth0Client = {
868+
getTokenSilently: async () => 'fakeToken',
869+
}
870+
871+
const doRequest = async () => {
872+
const abortController = new AbortController()
873+
return baseQuery(
874+
{ url: '/echo' },
875+
{
876+
signal: abortController.signal,
877+
abort: (reason) =>
878+
// @ts-ignore
879+
abortController.abort(reason),
880+
dispatch: storeRef.store.dispatch,
881+
getState: storeRef.store.getState,
882+
extra: fakeAuth0Client,
883+
type: 'query',
884+
forced: true,
885+
endpoint: 'someEndpointName',
886+
},
887+
{},
888+
)
889+
}
890+
891+
await doRequest()
892+
893+
expect(_args!.url).toBe('/echo')
894+
})
853895
})
854896

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

0 commit comments

Comments
 (0)