mirror of
https://github.com/tenrok/axios.git
synced 2026-06-17 19:21:29 +03:00
fix(fetch): defer global access in fetch adapter (#7260)
This commit is contained in:
@@ -18,13 +18,6 @@ const DEFAULT_CHUNK_SIZE = 64 * 1024;
|
||||
|
||||
const { isFunction } = utils;
|
||||
|
||||
const globalFetchAPI = (({ Request, Response }) => ({
|
||||
Request,
|
||||
Response,
|
||||
}))(utils.global);
|
||||
|
||||
const { ReadableStream, TextEncoder } = utils.global;
|
||||
|
||||
const test = (fn, ...args) => {
|
||||
try {
|
||||
return !!fn(...args);
|
||||
@@ -34,11 +27,17 @@ const test = (fn, ...args) => {
|
||||
};
|
||||
|
||||
const factory = (env) => {
|
||||
const globalObject = utils.global ?? globalThis;
|
||||
const { ReadableStream, TextEncoder } = globalObject;
|
||||
|
||||
env = utils.merge.call(
|
||||
{
|
||||
skipUndefined: true,
|
||||
},
|
||||
globalFetchAPI,
|
||||
{
|
||||
Request: globalObject.Request,
|
||||
Response: globalObject.Response,
|
||||
},
|
||||
env
|
||||
);
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ import {
|
||||
makeEchoStream,
|
||||
} from '../../setup/server.js';
|
||||
import axios from '../../../index.js';
|
||||
import utils from '../../../lib/utils.js';
|
||||
import { getFetch } from '../../../lib/adapters/fetch.js';
|
||||
import stream from 'stream';
|
||||
import { AbortController } from 'abortcontroller-polyfill/dist/cjs-ponyfill.js';
|
||||
@@ -814,6 +815,24 @@ describe.runIf(typeof fetch === 'function')('supports fetch with nodejs', () =>
|
||||
});
|
||||
|
||||
describe('env config', () => {
|
||||
it('should fallback to globalThis when utils.global is temporarily undefined', () => {
|
||||
const originalGlobal = utils.global;
|
||||
|
||||
try {
|
||||
utils.global = undefined;
|
||||
|
||||
assert.doesNotThrow(() =>
|
||||
getFetch({
|
||||
env: {
|
||||
fetch() {},
|
||||
},
|
||||
})
|
||||
);
|
||||
} finally {
|
||||
utils.global = originalGlobal;
|
||||
}
|
||||
});
|
||||
|
||||
it('should respect env fetch API configuration', async () => {
|
||||
const { data, headers } = await fetchAxios.get('/', {
|
||||
env: {
|
||||
|
||||
Reference in New Issue
Block a user