Skip to content

Commit

Permalink
Merge pull request #4291 from reduxjs/pr/fetchBaseQuery-extraOptions
Browse files Browse the repository at this point in the history
fetchBaseQuery: expose extraOptions to prepareHeaders
  • Loading branch information
markerikson authored Oct 14, 2024
2 parents 7b50a61 + 896e4df commit fa0906e
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
6 changes: 4 additions & 2 deletions packages/toolkit/src/query/fetchBaseQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export type FetchBaseQueryArgs = {
api: Pick<
BaseQueryApi,
'getState' | 'extra' | 'endpoint' | 'type' | 'forced'
> & { arg: string | FetchArgs },
> & { arg: string | FetchArgs; extraOptions: unknown },
) => MaybePromise<Headers | void>
fetchFn?: (
input: RequestInfo,
Expand Down Expand Up @@ -188,6 +188,7 @@ export type FetchBaseQueryMeta = { request: Request; response?: Response }
* @param {number} timeout
* A number in milliseconds that represents the maximum time a request can take before timing out.
*/

export function fetchBaseQuery({
baseUrl,
prepareHeaders = (x) => x,
Expand All @@ -212,7 +213,7 @@ export function fetchBaseQuery({
'Warning: `fetch` is not available. Please supply a custom `fetchFn` property to use `fetchBaseQuery` on SSR environments.',
)
}
return async (arg, api) => {
return async (arg, api, extraOptions) => {
const { getState, extra, endpoint, forced, type } = api
let meta: FetchBaseQueryMeta | undefined
let {
Expand Down Expand Up @@ -248,6 +249,7 @@ export function fetchBaseQuery({
endpoint,
forced,
type,
extraOptions,
})) || headers

// Only set the content-type to json if appropriate. Will not be true for FormData, ArrayBuffer, Blob, etc.
Expand Down
46 changes: 46 additions & 0 deletions packages/toolkit/src/query/tests/fetchBaseQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { createSlice } from '@reduxjs/toolkit'
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query'
import { headersToObject } from 'headers-polyfill'
import { HttpResponse, delay, http } from 'msw'
// @ts-ignore
import nodeFetch from 'node-fetch'
import queryString from 'query-string'
import { vi } from 'vitest'
Expand Down Expand Up @@ -852,6 +853,51 @@ describe('fetchBaseQuery', () => {
expect(_forced).toBe(true)
expect(_extra).toBe(fakeAuth0Client)
})

test('can be instantiated with a `ExtraOptions` generic and `extraOptions` will be available in `prepareHeaders', async () => {
const prepare = vitest.fn()
const baseQuery = fetchBaseQuery({
prepareHeaders(headers, api) {
expectTypeOf(api.extraOptions).toEqualTypeOf<unknown>()
prepare.apply(undefined, arguments as unknown as any[])
},
})
baseQuery('http://example.com', commonBaseQueryApi, {
foo: 'baz',
bar: 5,
})
expect(prepare).toHaveBeenCalledWith(
expect.anything(),
expect.objectContaining({ extraOptions: { foo: 'baz', bar: 5 } }),
)

// ensure types
createApi({
baseQuery,
endpoints(build) {
return {
testQuery: build.query({
query: () => ({ url: '/echo', headers: {} }),
extraOptions: {
foo: 'asd',
bar: 1,
},
}),
testMutation: build.mutation({
query: () => ({
url: '/echo',
method: 'POST',
credentials: 'omit',
}),
extraOptions: {
foo: 'qwe',
bar: 15,
},
}),
}
},
})
})
})

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

0 comments on commit fa0906e

Please sign in to comment.