@@ -12,9 +12,9 @@ const DEFAULT_CHUNK_SIZE = 64 * 1024;
1212
1313const { isFunction} = utils ;
1414
15- const globalFetchAPI = ( ( { fetch , Request, Response} ) => ( {
16- fetch , Request, Response
17- } ) ) ( utils . global ) ;
15+ const globalFetchAPI = ( ( { Request, Response} ) => ( {
16+ Request, Response
17+ } ) ) ( utils . global ) ;
1818
1919const {
2020 ReadableStream, TextEncoder
@@ -30,8 +30,12 @@ const test = (fn, ...args) => {
3030}
3131
3232const factory = ( env ) => {
33- const { fetch, Request, Response} = Object . assign ( { } , globalFetchAPI , env ) ;
34- const isFetchSupported = isFunction ( fetch ) ;
33+ env = utils . merge . call ( {
34+ skipUndefined : true
35+ } , globalFetchAPI , env ) ;
36+
37+ const { fetch : envFetch , Request, Response} = env ;
38+ const isFetchSupported = envFetch ? isFunction ( envFetch ) : typeof fetch === 'function' ;
3539 const isRequestSupported = isFunction ( Request ) ;
3640 const isResponseSupported = isFunction ( Response ) ;
3741
@@ -134,6 +138,8 @@ const factory = (env) => {
134138 fetchOptions
135139 } = resolveConfig ( config ) ;
136140
141+ let _fetch = envFetch || fetch ;
142+
137143 responseType = responseType ? ( responseType + '' ) . toLowerCase ( ) : 'text' ;
138144
139145 let composedSignal = composeSignals ( [ signal , cancelToken && cancelToken . toAbortSignal ( ) ] , timeout ) ;
@@ -193,7 +199,7 @@ const factory = (env) => {
193199
194200 request = isRequestSupported && new Request ( url , resolvedOptions ) ;
195201
196- let response = await ( isRequestSupported ? fetch ( request , fetchOptions ) : fetch ( url , resolvedOptions ) ) ;
202+ let response = await ( isRequestSupported ? _fetch ( request , fetchOptions ) : _fetch ( url , resolvedOptions ) ) ;
197203
198204 const isStreamResponse = supportsResponseStream && ( responseType === 'stream' || responseType === 'response' ) ;
199205
@@ -256,12 +262,8 @@ const factory = (env) => {
256262const seedCache = new Map ( ) ;
257263
258264export const getFetch = ( config ) => {
259- let env = utils . merge . call ( {
260- skipUndefined : true
261- } , globalFetchAPI , config ? config . env : null ) ;
262-
265+ let env = config ? config . env : { } ;
263266 const { fetch, Request, Response} = env ;
264-
265267 const seeds = [
266268 Request , Response , fetch
267269 ] ;
0 commit comments