From 85abc34f7177bfb24857f623dd2539d99362145a Mon Sep 17 00:00:00 2001 From: Lenz Weber-Tronic Date: Sat, 15 Oct 2022 18:28:35 +0200 Subject: [PATCH] `fetchBaseQuery`: allow `headers` option --- packages/toolkit/src/query/fetchBaseQuery.ts | 2 +- .../src/query/tests/fetchBaseQuery.test.tsx | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/packages/toolkit/src/query/fetchBaseQuery.ts b/packages/toolkit/src/query/fetchBaseQuery.ts index 1127706392..336b094f11 100644 --- a/packages/toolkit/src/query/fetchBaseQuery.ts +++ b/packages/toolkit/src/query/fetchBaseQuery.ts @@ -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, diff --git a/packages/toolkit/src/query/tests/fetchBaseQuery.test.tsx b/packages/toolkit/src/query/tests/fetchBaseQuery.test.tsx index e6d7540f80..d55a35e50e 100644 --- a/packages/toolkit/src/query/tests/fetchBaseQuery.test.tsx +++ b/packages/toolkit/src/query/tests/fetchBaseQuery.test.tsx @@ -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(