Skip to content

Commit

Permalink
fetchBaseQuery: allow headers option
Browse files Browse the repository at this point in the history
  • Loading branch information
phryneas committed Oct 15, 2022
1 parent b9aefe0 commit 85abc34
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/toolkit/src/query/fetchBaseQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ export function fetchBaseQuery({
let {
url,
method = 'GET' as const,
headers = new Headers({}),
headers = new Headers(baseFetchOptions.headers),
body = undefined,
params = undefined,
responseHandler = 'json' as const,
Expand Down
18 changes: 18 additions & 0 deletions packages/toolkit/src/query/tests/fetchBaseQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,24 @@ describe('fetchBaseQuery', () => {
})
})

test('can pass `headers` into `fetchBaseQuery`', async () => {
let request: any

const token = 'accessToken'

const _baseQuery = fetchBaseQuery({
baseUrl,
headers: { authorization: `Bearer ${token}` },
})

const doRequest = async () =>
_baseQuery({ url: '/echo' }, commonBaseQueryApi, {})

;({ data: request } = await doRequest())

expect(request.headers['authorization']).toBe(`Bearer ${token}`)
})

test('lets a header be undefined', async () => {
let request: any
;({ data: request } = await baseQuery(
Expand Down

0 comments on commit 85abc34

Please sign in to comment.