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

feat: export getAdapter function (#5324)

This commit is contained in:
夜葬
2023-08-27 01:42:25 +08:00
committed by GitHub
parent 9a414bb6c8
commit ca73eb878d
7 changed files with 42 additions and 0 deletions
+1
View File
@@ -523,6 +523,7 @@ declare namespace axios {
isAxiosError<T = any, D = any>(payload: any): payload is AxiosError<T, D>;
toFormData(sourceObj: object, targetFormData?: GenericFormData, options?: FormSerializerOptions): GenericFormData;
formToJSON(form: GenericFormData|GenericHTMLFormElement): object;
getAdapter(adapters: AxiosAdapterConfig | AxiosAdapterConfig[] | undefined): AxiosAdapter;
AxiosHeaders: typeof AxiosHeaders;
}
}
Vendored
+3
View File
@@ -512,6 +512,8 @@ export interface GenericHTMLFormElement {
submit(): void;
}
export function getAdapter(adapters: AxiosAdapterConfig | AxiosAdapterConfig[] | undefined): AxiosAdapter;
export function toFormData(sourceObj: object, targetFormData?: GenericFormData, options?: FormSerializerOptions): GenericFormData;
export function formToJSON(form: GenericFormData|GenericHTMLFormElement): object;
@@ -538,6 +540,7 @@ export interface AxiosStatic extends AxiosInstance {
isAxiosError: typeof isAxiosError;
toFormData: typeof toFormData;
formToJSON: typeof formToJSON;
getAdapter: typeof getAdapter;
CanceledError: typeof CanceledError;
AxiosHeaders: typeof AxiosHeaders;
}
+2
View File
@@ -18,6 +18,7 @@ const {
AxiosHeaders,
HttpStatusCode,
formToJSON,
getAdapter,
mergeConfig
} = axios;
@@ -37,5 +38,6 @@ export {
AxiosHeaders,
HttpStatusCode,
formToJSON,
getAdapter,
mergeConfig
}
+3
View File
@@ -15,6 +15,7 @@ import AxiosError from './core/AxiosError.js';
import spread from './helpers/spread.js';
import isAxiosError from './helpers/isAxiosError.js';
import AxiosHeaders from "./core/AxiosHeaders.js";
import adapters from './adapters/adapters.js';
import HttpStatusCode from './helpers/HttpStatusCode.js';
/**
@@ -78,6 +79,8 @@ axios.AxiosHeaders = AxiosHeaders;
axios.formToJSON = thing => formDataToJSON(utils.isHTMLForm(thing) ? new FormData(thing) : thing);
axios.getAdapter = adapters.getAdapter;
axios.HttpStatusCode = HttpStatusCode;
axios.default = axios;
+28
View File
@@ -16,6 +16,7 @@ import axios, {
ParamsSerializerOptions,
toFormData,
formToJSON,
getAdapter,
all,
isCancel,
isAxiosError,
@@ -570,6 +571,33 @@ axios.get('/user', {
adapter: ['xhr', 'http']
});
{
// getAdapter
getAdapter(axios.create().defaults.adapter);
getAdapter(undefined);
getAdapter([]);
getAdapter(['xhr']);
getAdapter([adapter]);
getAdapter(['xhr', 'http']);
getAdapter([adapter, 'xhr']);
getAdapter([adapter, adapter]);
getAdapter('xhr');
getAdapter(adapter);
const _: AxiosAdapter = getAdapter('xhr');
const __: AxiosAdapter = getAdapter(['xhr']);
// @ts-expect-error
getAdapter();
// @ts-expect-error
getAdapter(123);
// @ts-expect-error
getAdapter([123]);
// @ts-expect-error
getAdapter('xhr', 'http');
}
// AxiosHeaders
// iterator
+4
View File
@@ -54,6 +54,10 @@ describe('static api', function () {
it('should have mergeConfig properties', function () {
expect(typeof axios.mergeConfig).toEqual('function');
});
it('should have getAdapter properties', function () {
expect(typeof axios.getAdapter).toEqual('function');
});
});
describe('instance api', function () {
+1
View File
@@ -24,6 +24,7 @@ describe('instance', function () {
'getUri',
'isAxiosError',
'mergeConfig',
'getAdapter',
'VERSION',
'default',
'toFormData',