Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fetchBaseQuery: expose extraOptions to prepareHeaders #4291

Merged
merged 5 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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