Skip to content

Commit

Permalink
feat: export getAdapter function
Browse files Browse the repository at this point in the history
  • Loading branch information
geekact committed Jan 11, 2023
1 parent d83316d commit dc0639c
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 1 deletion.
1 change: 1 addition & 0 deletions index.d.cts
Expand Up @@ -496,6 +496,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
Expand Up @@ -485,6 +485,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 @@ -511,6 +513,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
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
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
29 changes: 28 additions & 1 deletion test/module/typings/esm/index.ts
Expand Up @@ -19,7 +19,8 @@ import axios, {
all,
isCancel,
isAxiosError,
spread
spread,
getAdapter
} from 'axios';

const config: RawAxiosRequestConfig = {
Expand Down Expand Up @@ -569,3 +570,29 @@ axios.get('/user', {
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');
}
4 changes: 4 additions & 0 deletions test/specs/api.spec.js
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
Expand Up @@ -24,6 +24,7 @@ describe('instance', function () {
'getUri',
'isAxiosError',
'mergeConfig',
'getAdapter',
'VERSION',
'default',
'toFormData',
Expand Down

0 comments on commit dc0639c

Please sign in to comment.