@@ -37,8 +37,8 @@ const defaultFetchFn: typeof fetch = (...args) => fetch(...args)
37
37
const defaultValidateStatus = ( response : Response ) =>
38
38
response . status >= 200 && response . status <= 299
39
39
40
- const isJsonContentType = ( headers : Headers ) =>
41
- headers . get ( 'content-type' ) ?. trim ( ) ?. startsWith ( 'application/json ')
40
+ const defaultIsJsonContentType = ( headers : Headers ) =>
41
+ /*applicat*/ / i o n \/ ( v n d \. a p i \+ ) ? j s o n / . test ( headers . get ( 'content-type' ) || ' ')
42
42
43
43
const handleResponse = async (
44
44
response : Response ,
@@ -123,6 +123,15 @@ export type FetchBaseQueryArgs = {
123
123
init ?: RequestInit | undefined
124
124
) => Promise < Response >
125
125
paramsSerializer ?: ( params : Record < string , any > ) => string
126
+ /**
127
+ * By default, we only check for 'application/json' and 'application/vnd.api+json' as the content-types for json. If you need to support another format, you can pass
128
+ * in a predicate function for your given api to get the same automatic stringifying behavior
129
+ * @example
130
+ * ```ts
131
+ * const isJsonContentType = (headers: Headers) => ["application/vnd.api+json", "application/json", "application/vnd.hal+json"].includes(headers.get("content-type")?.trim());
132
+ * ```
133
+ */
134
+ isJsonContentType ?: ( headers : Headers ) => boolean
126
135
} & RequestInit
127
136
128
137
export type FetchBaseQueryMeta = { request : Request ; response ?: Response }
@@ -162,12 +171,17 @@ export type FetchBaseQueryMeta = { request: Request; response?: Response }
162
171
*
163
172
* @param {(params: Record<string, unknown>) => string } paramsSerializer
164
173
* An optional function that can be used to stringify querystring parameters.
174
+ *
175
+ * @param {(headers: Headers) => boolean } isJsonContentType
176
+ * An optional predicate function to determine if `JSON.stringify()` should be called on the `body` arg of `FetchArgs`
177
+
165
178
*/
166
179
export function fetchBaseQuery ( {
167
180
baseUrl,
168
181
prepareHeaders = ( x ) => x ,
169
182
fetchFn = defaultFetchFn ,
170
183
paramsSerializer,
184
+ isJsonContentType = defaultIsJsonContentType ,
171
185
...baseFetchOptions
172
186
} : FetchBaseQueryArgs = { } ) : BaseQueryFn <
173
187
string | FetchArgs ,
0 commit comments