Skip to content

Commit b0f59fc

Browse files
authored
Add jsonContentType to fetchBaseQuery options (#2403)
* Add `jsonContentType` to `fetchBaseQuery` options * Add a test
1 parent cdc55e7 commit b0f59fc

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

packages/toolkit/src/query/fetchBaseQuery.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@ export type FetchBaseQueryArgs = {
132132
* ```
133133
*/
134134
isJsonContentType?: (headers: Headers) => boolean
135+
/**
136+
* Defaults to `application/json`;
137+
*/
138+
jsonContentType?: string
135139
} & RequestInit
136140

137141
export type FetchBaseQueryMeta = { request: Request; response?: Response }
@@ -171,17 +175,19 @@ export type FetchBaseQueryMeta = { request: Request; response?: Response }
171175
*
172176
* @param {(params: Record<string, unknown>) => string} paramsSerializer
173177
* An optional function that can be used to stringify querystring parameters.
174-
*
178+
*
175179
* @param {(headers: Headers) => boolean} isJsonContentType
176180
* An optional predicate function to determine if `JSON.stringify()` should be called on the `body` arg of `FetchArgs`
177-
181+
*
182+
* @param {string} jsonContentType Defaults to `application/json`. Used when automatically setting the content-type header for a request with a jsonifiable body that does not have an explicit content-type header.
178183
*/
179184
export function fetchBaseQuery({
180185
baseUrl,
181186
prepareHeaders = (x) => x,
182187
fetchFn = defaultFetchFn,
183188
paramsSerializer,
184189
isJsonContentType = defaultIsJsonContentType,
190+
jsonContentType = 'application/json',
185191
...baseFetchOptions
186192
}: FetchBaseQueryArgs = {}): BaseQueryFn<
187193
string | FetchArgs,
@@ -229,7 +235,7 @@ export function fetchBaseQuery({
229235
typeof body.toJSON === 'function')
230236

231237
if (!config.headers.has('content-type') && isJsonifiable(body)) {
232-
config.headers.set('content-type', 'application/json')
238+
config.headers.set('content-type', jsonContentType)
233239
}
234240

235241
if (body && isJsonContentType(config.headers)) {

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

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,27 @@ describe('fetchBaseQuery', () => {
292292
expect(request.headers['content-type']).toBe('text/html')
293293
expect(request.body).toEqual(data.join(','))
294294
})
295+
296+
it('supports a custom jsonContentType', async () => {
297+
const baseQuery = fetchBaseQuery({
298+
baseUrl,
299+
fetchFn: fetchFn as any,
300+
jsonContentType: 'application/vnd.api+json',
301+
})
302+
303+
let request: any
304+
;({ data: request } = await baseQuery(
305+
{
306+
url: '/echo',
307+
body: {},
308+
method: 'POST',
309+
},
310+
commonBaseQueryApi,
311+
{}
312+
))
313+
314+
expect(request.headers['content-type']).toBe('application/vnd.api+json')
315+
})
295316
})
296317

297318
describe('arg.params', () => {
@@ -408,9 +429,11 @@ describe('fetchBaseQuery', () => {
408429
baseUrl,
409430
fetchFn: fetchFn as any,
410431
isJsonContentType: (headers) =>
411-
['application/vnd.api+json', 'application/json', 'application/vnd.hal+json'].includes(
412-
headers.get('content-type') ?? ''
413-
),
432+
[
433+
'application/vnd.api+json',
434+
'application/json',
435+
'application/vnd.hal+json',
436+
].includes(headers.get('content-type') ?? ''),
414437
})
415438

416439
let request: any

0 commit comments

Comments
 (0)