Skip to content

Commit cf78825

Browse files
fix(fetch): use current global fetch instead of cached one when env fetch is not specified to keep MSW support; (#7030)
1 parent c26d00f commit cf78825

File tree

2 files changed

+40
-11
lines changed

2 files changed

+40
-11
lines changed

lib/adapters/fetch.js

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ const DEFAULT_CHUNK_SIZE = 64 * 1024;
1212

1313
const {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

1919
const {
2020
ReadableStream, TextEncoder
@@ -30,8 +30,12 @@ const test = (fn, ...args) => {
3030
}
3131

3232
const 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) => {
256262
const seedCache = new Map();
257263

258264
export 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
];

test/unit/adapters/fetch.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,5 +489,32 @@ describe('supports fetch with nodejs', function () {
489489

490490
assert.strictEqual(data, 'OK');
491491
});
492+
493+
it('should use current global fetch when env fetch is not specified', async() => {
494+
const globalFetch = fetch;
495+
496+
fetch = async () => {
497+
return {
498+
headers: {
499+
foo: '1'
500+
},
501+
text: async () => 'global'
502+
}
503+
};
504+
505+
try {
506+
server = await startHTTPServer((req, res) => res.end('OK'));
507+
508+
const {data} = await fetchAxios.get('/', {
509+
env: {
510+
fetch: undefined
511+
}
512+
});
513+
514+
assert.strictEqual(data, 'global');
515+
} finally {
516+
fetch = globalFetch;
517+
}
518+
});
492519
});
493520
});

0 commit comments

Comments
 (0)