| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- import type { CreateAxiosDefaults } from 'axios';
- import type { IAxiosRetryConfig } from 'axios-retry';
- import { stringify } from 'qs';
- import { isHttpSuccess } from './shared';
- import type { RequestOption } from './type';
- export function createDefaultOptions<
- ResponseData,
- ApiData = ResponseData,
- State extends Record<string, unknown> = Record<string, unknown>
- >(options?: Partial<RequestOption<ResponseData, ApiData, State>>) {
- const opts: RequestOption<ResponseData, ApiData, State> = {
- defaultState: {} as State,
- transform: async response => response.data as unknown as ApiData,
- transformBackendResponse: async response => response.data as unknown as ApiData,
- onRequest: async config => config,
- isBackendSuccess: _response => true,
- onBackendFail: async () => {},
- onError: async () => {}
- };
- if (options?.transform) {
- opts.transform = options.transform;
- } else {
- opts.transform = options?.transformBackendResponse || opts.transform;
- }
- Object.assign(opts, options);
- return opts;
- }
- export function createRetryOptions(config?: Partial<CreateAxiosDefaults>) {
- const retryConfig: IAxiosRetryConfig = {
- retries: 0
- };
- Object.assign(retryConfig, config);
- return retryConfig;
- }
- export function createAxiosConfig(config?: Partial<CreateAxiosDefaults>) {
- const TEN_SECONDS = 10 * 10000;
- const axiosConfig: CreateAxiosDefaults = {
- timeout: TEN_SECONDS,
- headers: {
- 'Content-Type': 'application/json'
- },
- validateStatus: isHttpSuccess,
- paramsSerializer: params => {
- return stringify(params);
- }
- };
- Object.assign(axiosConfig, config);
- return axiosConfig;
- }
|