1
1
import { joinUrls } from './utils' ;
2
2
import { isPlainObject } from '@reduxjs/toolkit' ;
3
3
import { BaseQueryFn } from './apiTypes' ;
4
+ import { Override } from './tsHelpers' ;
4
5
5
6
export type ResponseHandler = 'json' | 'text' | ( ( response : Response ) => Promise < any > ) ;
6
7
7
- export interface FetchArgs extends RequestInit {
8
+ type CustomRequestInit = Override <
9
+ RequestInit ,
10
+ { headers ?: Headers | string [ ] [ ] | Record < string , string | undefined > | undefined }
11
+ > ;
12
+
13
+ export interface FetchArgs extends CustomRequestInit {
8
14
url : string ;
9
15
params ?: Record < string , any > ;
10
16
body ?: any ;
@@ -36,6 +42,36 @@ export interface FetchBaseQueryError {
36
42
data : unknown ;
37
43
}
38
44
45
+ function cleanUndefinedHeaders ( headers : any ) {
46
+ if ( ! isPlainObject ( headers ) ) {
47
+ return headers ;
48
+ }
49
+ const copy : Record < string , any > = { ...headers } ;
50
+ for ( const [ k , v ] of Object . entries ( copy ) ) {
51
+ if ( typeof v === 'undefined' ) delete copy [ k ] ;
52
+ }
53
+ return copy ;
54
+ }
55
+
56
+ /**
57
+ * This is a very small wrapper around fetch that aims to simplify requests.
58
+ *
59
+ * @param {string } baseUrl
60
+ * The base URL for an API service.
61
+ * Typically in the format of http://example.com/
62
+ *
63
+ * @param {(headers: Headers, api: { getState: () => unknown }) => Headers } prepareHeaders
64
+ * An optional function that can be used to inject headers on requests.
65
+ * Provides a Headers object, as well as the `getState` function from the
66
+ * redux store. Can be useful for authentication.
67
+ *
68
+ * @link https://developer.mozilla.org/en-US/docs/Web/API/Headers
69
+ *
70
+ * @param {(input: RequestInfo, init?: RequestInit | undefined) => Promise<Response> } fetchFn
71
+ * Accepts a custom `fetch` function if you do not want to use the default on the window.
72
+ * Useful in SSR environments if you need to use a library such as `isomorphic-fetch` or `cross-fetch`
73
+ *
74
+ */
39
75
export function fetchBaseQuery ( {
40
76
baseUrl,
41
77
prepareHeaders = ( x ) => x ,
@@ -44,10 +80,6 @@ export function fetchBaseQuery({
44
80
} : {
45
81
baseUrl ?: string ;
46
82
prepareHeaders ?: ( headers : Headers , api : { getState : ( ) => unknown } ) => Headers ;
47
- /**
48
- * Accepts a custom `fetch` function if you do not want to use the default on the window.
49
- * Useful in SSR environments if you need to pass isomorphic-fetch or cross-fetch
50
- */
51
83
fetchFn ?: ( input : RequestInfo , init ?: RequestInit | undefined ) => Promise < Response > ;
52
84
} & RequestInit = { } ) : BaseQueryFn < string | FetchArgs , unknown , FetchBaseQueryError , { } > {
53
85
return async ( arg , { signal, getState } ) => {
@@ -69,7 +101,7 @@ export function fetchBaseQuery({
69
101
...rest ,
70
102
} ;
71
103
72
- config . headers = prepareHeaders ( new Headers ( headers ) , { getState } ) ;
104
+ config . headers = prepareHeaders ( new Headers ( cleanUndefinedHeaders ( headers ) ) , { getState } ) ;
73
105
74
106
if ( ! config . headers . has ( 'content-type' ) ) {
75
107
config . headers . set ( 'content-type' , 'application/json' ) ;
0 commit comments