Skip to content

Commit d38ff98

Browse files
authored
Merge pull request #4638 from kyletsang/prepareheaders-args
Pass query args to prepareHeaders function
2 parents c855b8b + 3a22c67 commit d38ff98

File tree

3 files changed

+19
-11
lines changed

3 files changed

+19
-11
lines changed

docs/rtk-query/api/fetchBaseQuery.mdx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ type FetchBaseQueryArgs = {
6464
api: Pick<
6565
BaseQueryApi,
6666
'getState' | 'extra' | 'endpoint' | 'type' | 'forced'
67-
>,
67+
> & { arg: string | FetchArgs },
6868
) => MaybePromise<Headers | void>
6969
fetchFn?: (
7070
input: RequestInfo,
@@ -105,7 +105,7 @@ Typically a string like `https://api.your-really-great-app.com/v1/`. If you don'
105105

106106
_(optional)_
107107

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.
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 `arg`, `extra`, `endpoint`, `type`, and `forced` to unlock more granular conditional behaviors.
109109

110110
You can mutate the `headers` argument directly, and returning it is optional.
111111

@@ -114,6 +114,7 @@ type prepareHeaders = (
114114
headers: Headers,
115115
api: {
116116
getState: () => unknown
117+
arg: string | FetchArgs
117118
extra: unknown
118119
endpoint: string
119120
type: 'query' | 'mutation'
@@ -303,8 +304,8 @@ The default response handler is `"json"`, which is equivalent to the following f
303304

304305
```ts title="Default responseHandler"
305306
const defaultResponseHandler = async (res: Response) => {
306-
const text = await res.text();
307-
return text.length ? JSON.parse(text) : null;
307+
const text = await res.text()
308+
return text.length ? JSON.parse(text) : null
308309
}
309310
```
310311

packages/toolkit/src/query/fetchBaseQuery.ts

Lines changed: 10 additions & 5 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-
>,
115+
> & { arg: string | FetchArgs },
116116
) => MaybePromise<Headers | void>
117117
fetchFn?: (
118118
input: RequestInfo,
@@ -164,9 +164,9 @@ export type FetchBaseQueryMeta = { request: Request; response?: Response }
164164
* The base URL for an API service.
165165
* Typically in the format of https://example.com/
166166
*
167-
* @param {(headers: Headers, api: { getState: () => unknown; extra: unknown; endpoint: string; type: 'query' | 'mutation'; forced: boolean; }) => Headers} prepareHeaders
167+
* @param {(headers: Headers, api: { getState: () => unknown; arg: string | FetchArgs; extra: unknown; endpoint: string; type: 'query' | 'mutation'; forced: boolean; }) => Headers} prepareHeaders
168168
* 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).
169+
* Provides a Headers object, most of the `BaseQueryApi` (`dispatch` is not available), and the arg passed into the query function.
170170
* Useful for setting authentication or headers that need to be set conditionally.
171171
*
172172
* @link https://developer.mozilla.org/en-US/docs/Web/API/Headers
@@ -225,7 +225,8 @@ export function fetchBaseQuery({
225225
...rest
226226
} = typeof arg == 'string' ? { url: arg } : arg
227227

228-
let abortController: AbortController | undefined, signal = api.signal
228+
let abortController: AbortController | undefined,
229+
signal = api.signal
229230
if (timeout) {
230231
abortController = new AbortController()
231232
api.signal.addEventListener('abort', abortController.abort)
@@ -242,6 +243,7 @@ export function fetchBaseQuery({
242243
config.headers =
243244
(await prepareHeaders(headers, {
244245
getState,
246+
arg,
245247
extra,
246248
endpoint,
247249
forced,
@@ -297,7 +299,10 @@ export function fetchBaseQuery({
297299
}
298300
} finally {
299301
if (timeoutId) clearTimeout(timeoutId)
300-
abortController?.signal.removeEventListener('abort', abortController.abort)
302+
abortController?.signal.removeEventListener(
303+
'abort',
304+
abortController.abort,
305+
)
301306
}
302307
const responseClone = response.clone()
303308

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -799,16 +799,17 @@ describe('fetchBaseQuery', () => {
799799
})
800800

801801
test('prepareHeaders provides extra api information for getState, extra, endpoint, type and forced', async () => {
802-
let _getState, _extra, _endpoint, _type, _forced
802+
let _getState, _arg: any, _extra, _endpoint, _type, _forced
803803

804804
const baseQuery = fetchBaseQuery({
805805
baseUrl,
806806
fetchFn: fetchFn as any,
807807
prepareHeaders: (
808808
headers,
809-
{ getState, extra, endpoint, type, forced },
809+
{ getState, arg, extra, endpoint, type, forced },
810810
) => {
811811
_getState = getState
812+
_arg = arg
812813
_endpoint = endpoint
813814
_type = type
814815
_forced = forced
@@ -845,6 +846,7 @@ describe('fetchBaseQuery', () => {
845846
await doRequest()
846847

847848
expect(_getState).toBeDefined()
849+
expect(_arg!.url).toBe('/echo')
848850
expect(_endpoint).toBe('someEndpointName')
849851
expect(_type).toBe('query')
850852
expect(_forced).toBe(true)

0 commit comments

Comments
 (0)