2
0
mirror of https://github.com/tenrok/axios.git synced 2026-05-30 15:24:11 +03:00

fix(fetch): use current global fetch instead of cached one when env fetch is not specified to keep MSW support; (#7030)

This commit is contained in:
Dmitriy Mozgovoy
2025-09-13 23:24:16 +03:00
committed by GitHub
parent c26d00f451
commit cf78825e12
2 changed files with 40 additions and 11 deletions
+13 -11
View File
@@ -12,9 +12,9 @@ const DEFAULT_CHUNK_SIZE = 64 * 1024;
const {isFunction} = utils;
const globalFetchAPI = (({fetch, Request, Response}) => ({
fetch, Request, Response
}))(utils.global);
const globalFetchAPI = (({Request, Response}) => ({
Request, Response
}))(utils.global);
const {
ReadableStream, TextEncoder
@@ -30,8 +30,12 @@ const test = (fn, ...args) => {
}
const factory = (env) => {
const {fetch, Request, Response} = Object.assign({}, globalFetchAPI, env);
const isFetchSupported = isFunction(fetch);
env = utils.merge.call({
skipUndefined: true
}, globalFetchAPI, env);
const {fetch: envFetch, Request, Response} = env;
const isFetchSupported = envFetch ? isFunction(envFetch) : typeof fetch === 'function';
const isRequestSupported = isFunction(Request);
const isResponseSupported = isFunction(Response);
@@ -134,6 +138,8 @@ const factory = (env) => {
fetchOptions
} = resolveConfig(config);
let _fetch = envFetch || fetch;
responseType = responseType ? (responseType + '').toLowerCase() : 'text';
let composedSignal = composeSignals([signal, cancelToken && cancelToken.toAbortSignal()], timeout);
@@ -193,7 +199,7 @@ const factory = (env) => {
request = isRequestSupported && new Request(url, resolvedOptions);
let response = await (isRequestSupported ? fetch(request, fetchOptions) : fetch(url, resolvedOptions));
let response = await (isRequestSupported ? _fetch(request, fetchOptions) : _fetch(url, resolvedOptions));
const isStreamResponse = supportsResponseStream && (responseType === 'stream' || responseType === 'response');
@@ -256,12 +262,8 @@ const factory = (env) => {
const seedCache = new Map();
export const getFetch = (config) => {
let env = utils.merge.call({
skipUndefined: true
}, globalFetchAPI, config ? config.env : null);
let env = config ? config.env : {};
const {fetch, Request, Response} = env;
const seeds = [
Request, Response, fetch
];
+27
View File
@@ -489,5 +489,32 @@ describe('supports fetch with nodejs', function () {
assert.strictEqual(data, 'OK');
});
it('should use current global fetch when env fetch is not specified', async() => {
const globalFetch = fetch;
fetch = async () => {
return {
headers: {
foo: '1'
},
text: async () => 'global'
}
};
try {
server = await startHTTPServer((req, res) => res.end('OK'));
const {data} = await fetchAxios.get('/', {
env: {
fetch: undefined
}
});
assert.strictEqual(data, 'global');
} finally {
fetch = globalFetch;
}
});
});
});