Skip to content

Commit c959ff2

Browse files
feat(fetch): add fetch, Request, Response env config variables for the adapter; (#7003)
* feat(fetch): add fetch, Request, Response env config variables for the adapter; * feat(fetch): fixed design issue for environments without fetch API globals;
1 parent a9f47af commit c959ff2

File tree

7 files changed

+323
-175
lines changed

7 files changed

+323
-175
lines changed

index.d.cts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,9 @@ declare namespace axios {
423423
insecureHTTPParser?: boolean;
424424
env?: {
425425
FormData?: new (...args: any[]) => object;
426+
fetch?: (input: URL | Request | string, init?: RequestInit) => Promise<Response>;
427+
Request?: new (input: (RequestInfo | URL), init?: RequestInit) => Request;
428+
Response?: new (body?: (BodyInit | null), init?: ResponseInit) => Response;
426429
};
427430
formSerializer?: FormSerializerOptions;
428431
family?: AddressFamily;

index.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,9 @@ export interface AxiosRequestConfig<D = any> {
355355
insecureHTTPParser?: boolean;
356356
env?: {
357357
FormData?: new (...args: any[]) => object;
358+
fetch?: (input: URL | Request | string, init?: RequestInit) => Promise<Response>;
359+
Request?: new (input: (RequestInfo | URL), init?: RequestInit) => Request;
360+
Response?: new (body?: (BodyInit | null), init?: ResponseInit) => Response;
358361
};
359362
formSerializer?: FormSerializerOptions;
360363
family?: AddressFamily;

lib/adapters/adapters.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import utils from '../utils.js';
22
import httpAdapter from './http.js';
33
import xhrAdapter from './xhr.js';
4-
import fetchAdapter from './fetch.js';
4+
import * as fetchAdapter from './fetch.js';
55
import AxiosError from "../core/AxiosError.js";
66

77
const knownAdapters = {
88
http: httpAdapter,
99
xhr: xhrAdapter,
10-
fetch: fetchAdapter
10+
fetch: {
11+
get: fetchAdapter.getFetch,
12+
}
1113
}
1214

1315
utils.forEach(knownAdapters, (fn, value) => {
@@ -26,7 +28,7 @@ const renderReason = (reason) => `- ${reason}`;
2628
const isResolvedHandle = (adapter) => utils.isFunction(adapter) || adapter === null || adapter === false;
2729

2830
export default {
29-
getAdapter: (adapters) => {
31+
getAdapter: (adapters, config) => {
3032
adapters = utils.isArray(adapters) ? adapters : [adapters];
3133

3234
const {length} = adapters;
@@ -49,7 +51,7 @@ export default {
4951
}
5052
}
5153

52-
if (adapter) {
54+
if (adapter && (utils.isFunction(adapter) || (adapter = adapter.get(config)))) {
5355
break;
5456
}
5557

0 commit comments

Comments
 (0)