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

Add jsonContentType to fetchBaseQuery options #2403

Merged
merged 2 commits into from
Jun 11, 2022
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
12 changes: 9 additions & 3 deletions packages/toolkit/src/query/fetchBaseQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ export type FetchBaseQueryArgs = {
* ```
*/
isJsonContentType?: (headers: Headers) => boolean
/**
* Defaults to `application/json`;
*/
jsonContentType?: string
} & RequestInit

export type FetchBaseQueryMeta = { request: Request; response?: Response }
Expand Down Expand Up @@ -171,17 +175,19 @@ export type FetchBaseQueryMeta = { request: Request; response?: Response }
*
* @param {(params: Record<string, unknown>) => string} paramsSerializer
* An optional function that can be used to stringify querystring parameters.
*
*
* @param {(headers: Headers) => boolean} isJsonContentType
* An optional predicate function to determine if `JSON.stringify()` should be called on the `body` arg of `FetchArgs`

*
* @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.
*/
export function fetchBaseQuery({
baseUrl,
prepareHeaders = (x) => x,
fetchFn = defaultFetchFn,
paramsSerializer,
isJsonContentType = defaultIsJsonContentType,
jsonContentType = 'application/json',
...baseFetchOptions
}: FetchBaseQueryArgs = {}): BaseQueryFn<
string | FetchArgs,
Expand Down Expand Up @@ -229,7 +235,7 @@ export function fetchBaseQuery({
typeof body.toJSON === 'function')

if (!config.headers.has('content-type') && isJsonifiable(body)) {
config.headers.set('content-type', 'application/json')
config.headers.set('content-type', jsonContentType)
}

if (body && isJsonContentType(config.headers)) {
Expand Down
29 changes: 26 additions & 3 deletions packages/toolkit/src/query/tests/fetchBaseQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,27 @@ describe('fetchBaseQuery', () => {
expect(request.headers['content-type']).toBe('text/html')
expect(request.body).toEqual(data.join(','))
})

it('supports a custom jsonContentType', async () => {
const baseQuery = fetchBaseQuery({
baseUrl,
fetchFn: fetchFn as any,
jsonContentType: 'application/vnd.api+json',
})

let request: any
;({ data: request } = await baseQuery(
{
url: '/echo',
body: {},
method: 'POST',
},
commonBaseQueryApi,
{}
))

expect(request.headers['content-type']).toBe('application/vnd.api+json')
})
})

describe('arg.params', () => {
Expand Down Expand Up @@ -408,9 +429,11 @@ describe('fetchBaseQuery', () => {
baseUrl,
fetchFn: fetchFn as any,
isJsonContentType: (headers) =>
['application/vnd.api+json', 'application/json', 'application/vnd.hal+json'].includes(
headers.get('content-type') ?? ''
),
[
'application/vnd.api+json',
'application/json',
'application/vnd.hal+json',
].includes(headers.get('content-type') ?? ''),
})

let request: any
Expand Down