Skip to content

Commit

Permalink
feat: export getAdapter function (#5324)
Browse files Browse the repository at this point in the history
  • Loading branch information
夜葬 authored Aug 26, 2023
1 parent 9a414bb commit ca73eb8
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions index.d.cts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
3 changes: 3 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const {
AxiosHeaders,
HttpStatusCode,
formToJSON,
getAdapter,
mergeConfig
} = axios;

Expand All @@ -37,5 +38,6 @@ export {
AxiosHeaders,
HttpStatusCode,
formToJSON,
getAdapter,
mergeConfig
}
3 changes: 3 additions & 0 deletions lib/axios.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand Down Expand Up @@ -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;
Expand Down
28 changes: 28 additions & 0 deletions test/module/typings/esm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import axios, {
ParamsSerializerOptions,
toFormData,
formToJSON,
getAdapter,
all,
isCancel,
isAxiosError,
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions test/specs/api.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down
1 change: 1 addition & 0 deletions test/specs/instance.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ describe('instance', function () {
'getUri',
'isAxiosError',
'mergeConfig',
'getAdapter',
'VERSION',
'default',
'toFormData',
Expand Down

0 comments on commit ca73eb8

Please sign in to comment.