2020-09-28 20:19:10 +08:00
|
|
|
import type { AxiosRequestConfig, AxiosInstance, AxiosResponse } from 'axios';
|
|
|
|
|
|
|
|
|
|
import axios from 'axios';
|
|
|
|
|
import { AxiosCanceler } from './axiosCancel';
|
|
|
|
|
import { isFunction } from '/@/utils/is';
|
|
|
|
|
import { cloneDeep } from 'lodash-es';
|
|
|
|
|
|
2020-11-12 00:06:12 +08:00
|
|
|
import type { RequestOptions, CreateAxiosOptions, Result, UploadFileParams } from './types';
|
2020-10-13 01:40:18 +08:00
|
|
|
import { errorResult } from './const';
|
2020-11-12 00:06:12 +08:00
|
|
|
import { ContentTypeEnum } from '/@/enums/httpEnum';
|
2021-02-24 20:45:15 +08:00
|
|
|
import qs from 'qs';
|
|
|
|
|
import { RequestEnum } from '../../../enums/httpEnum';
|
2020-09-28 20:19:10 +08:00
|
|
|
|
|
|
|
|
export * from './axiosTransform';
|
|
|
|
|
|
|
|
|
|
/**
|
2021-02-13 09:16:36 +08:00
|
|
|
* @description: axios module
|
2020-09-28 20:19:10 +08:00
|
|
|
*/
|
|
|
|
|
export class VAxios {
|
|
|
|
|
private axiosInstance: AxiosInstance;
|
2020-11-12 22:20:15 +08:00
|
|
|
private readonly options: CreateAxiosOptions;
|
2020-09-28 20:19:10 +08:00
|
|
|
|
|
|
|
|
constructor(options: CreateAxiosOptions) {
|
|
|
|
|
this.options = options;
|
|
|
|
|
this.axiosInstance = axios.create(options);
|
|
|
|
|
this.setupInterceptors();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-02-13 09:16:36 +08:00
|
|
|
* @description: Create axios instance
|
2020-09-28 20:19:10 +08:00
|
|
|
*/
|
|
|
|
|
private createAxios(config: CreateAxiosOptions): void {
|
|
|
|
|
this.axiosInstance = axios.create(config);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private getTransform() {
|
|
|
|
|
const { transform } = this.options;
|
|
|
|
|
return transform;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getAxios(): AxiosInstance {
|
|
|
|
|
return this.axiosInstance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-02-13 09:16:36 +08:00
|
|
|
* @description: Reconfigure axios
|
2020-09-28 20:19:10 +08:00
|
|
|
*/
|
|
|
|
|
configAxios(config: CreateAxiosOptions) {
|
|
|
|
|
if (!this.axiosInstance) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
this.createAxios(config);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-02-13 09:16:36 +08:00
|
|
|
* @description: Set general header
|
2020-09-28 20:19:10 +08:00
|
|
|
*/
|
|
|
|
|
setHeader(headers: any): void {
|
|
|
|
|
if (!this.axiosInstance) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
Object.assign(this.axiosInstance.defaults.headers, headers);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-02-13 09:16:36 +08:00
|
|
|
* @description: Interceptor configuration
|
2020-09-28 20:19:10 +08:00
|
|
|
*/
|
|
|
|
|
private setupInterceptors() {
|
|
|
|
|
const transform = this.getTransform();
|
|
|
|
|
if (!transform) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const {
|
|
|
|
|
requestInterceptors,
|
|
|
|
|
requestInterceptorsCatch,
|
|
|
|
|
responseInterceptors,
|
|
|
|
|
responseInterceptorsCatch,
|
|
|
|
|
} = transform;
|
|
|
|
|
|
|
|
|
|
const axiosCanceler = new AxiosCanceler();
|
|
|
|
|
|
2021-02-13 09:16:36 +08:00
|
|
|
// Request interceptor configuration processing
|
2020-09-28 20:19:10 +08:00
|
|
|
this.axiosInstance.interceptors.request.use((config: AxiosRequestConfig) => {
|
2020-12-09 22:11:34 +08:00
|
|
|
// If cancel repeat request is turned on, then cancel repeat request is prohibited
|
2021-02-10 00:53:47 +08:00
|
|
|
const {
|
|
|
|
|
headers: { ignoreCancelToken = false },
|
|
|
|
|
} = config;
|
2020-09-28 20:19:10 +08:00
|
|
|
!ignoreCancelToken && axiosCanceler.addPending(config);
|
|
|
|
|
if (requestInterceptors && isFunction(requestInterceptors)) {
|
|
|
|
|
config = requestInterceptors(config);
|
|
|
|
|
}
|
|
|
|
|
return config;
|
|
|
|
|
}, undefined);
|
|
|
|
|
|
2021-02-13 09:16:36 +08:00
|
|
|
// Request interceptor error capture
|
2020-09-28 20:19:10 +08:00
|
|
|
requestInterceptorsCatch &&
|
|
|
|
|
isFunction(requestInterceptorsCatch) &&
|
|
|
|
|
this.axiosInstance.interceptors.request.use(undefined, requestInterceptorsCatch);
|
|
|
|
|
|
2021-02-13 09:16:36 +08:00
|
|
|
// Response result interceptor processing
|
2020-09-28 20:19:10 +08:00
|
|
|
this.axiosInstance.interceptors.response.use((res: AxiosResponse<any>) => {
|
|
|
|
|
res && axiosCanceler.removePending(res.config);
|
|
|
|
|
if (responseInterceptors && isFunction(responseInterceptors)) {
|
|
|
|
|
res = responseInterceptors(res);
|
|
|
|
|
}
|
|
|
|
|
return res;
|
|
|
|
|
}, undefined);
|
|
|
|
|
|
2021-02-13 09:16:36 +08:00
|
|
|
// Response result interceptor error capture
|
2020-09-28 20:19:10 +08:00
|
|
|
responseInterceptorsCatch &&
|
|
|
|
|
isFunction(responseInterceptorsCatch) &&
|
|
|
|
|
this.axiosInstance.interceptors.response.use(undefined, responseInterceptorsCatch);
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-12 00:06:12 +08:00
|
|
|
/**
|
2021-02-13 09:16:36 +08:00
|
|
|
* @description: File Upload
|
2020-11-12 00:06:12 +08:00
|
|
|
*/
|
|
|
|
|
uploadFile<T = any>(config: AxiosRequestConfig, params: UploadFileParams) {
|
|
|
|
|
const formData = new window.FormData();
|
|
|
|
|
|
|
|
|
|
if (params.data) {
|
|
|
|
|
Object.keys(params.data).forEach((key) => {
|
|
|
|
|
if (!params.data) return;
|
|
|
|
|
const value = params.data[key];
|
|
|
|
|
if (Array.isArray(value)) {
|
|
|
|
|
value.forEach((item) => {
|
|
|
|
|
formData.append(`${key}[]`, item);
|
|
|
|
|
});
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
formData.append(key, params.data[key]);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
formData.append(params.name || 'file', params.file, params.filename);
|
|
|
|
|
|
|
|
|
|
return this.axiosInstance.request<T>({
|
|
|
|
|
...config,
|
|
|
|
|
method: 'POST',
|
|
|
|
|
data: formData,
|
|
|
|
|
headers: {
|
|
|
|
|
'Content-type': ContentTypeEnum.FORM_DATA,
|
|
|
|
|
ignoreCancelToken: true,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
2020-09-28 20:19:10 +08:00
|
|
|
|
2021-02-24 20:45:15 +08:00
|
|
|
// support form-data
|
|
|
|
|
supportFormData(config: AxiosRequestConfig) {
|
|
|
|
|
const headers = this.options?.headers;
|
|
|
|
|
const contentType = headers?.['Content-Type'] || headers?.['content-type'];
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
contentType !== ContentTypeEnum.FORM_URLENCODED ||
|
|
|
|
|
!Reflect.has(config, 'data') ||
|
|
|
|
|
config.method?.toUpperCase() === RequestEnum.GET
|
|
|
|
|
) {
|
|
|
|
|
return config;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
...config,
|
|
|
|
|
data: qs.stringify(config.data),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-28 20:19:10 +08:00
|
|
|
request<T = any>(config: AxiosRequestConfig, options?: RequestOptions): Promise<T> {
|
|
|
|
|
let conf: AxiosRequestConfig = cloneDeep(config);
|
|
|
|
|
const transform = this.getTransform();
|
|
|
|
|
|
|
|
|
|
const { requestOptions } = this.options;
|
|
|
|
|
|
|
|
|
|
const opt: RequestOptions = Object.assign({}, requestOptions, options);
|
|
|
|
|
|
|
|
|
|
const { beforeRequestHook, requestCatch, transformRequestData } = transform || {};
|
|
|
|
|
if (beforeRequestHook && isFunction(beforeRequestHook)) {
|
|
|
|
|
conf = beforeRequestHook(conf, opt);
|
|
|
|
|
}
|
2021-02-24 20:45:15 +08:00
|
|
|
|
|
|
|
|
conf = this.supportFormData(conf);
|
2020-09-28 20:19:10 +08:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
this.axiosInstance
|
|
|
|
|
.request<any, AxiosResponse<Result>>(conf)
|
|
|
|
|
.then((res: AxiosResponse<Result>) => {
|
|
|
|
|
if (transformRequestData && isFunction(transformRequestData)) {
|
|
|
|
|
const ret = transformRequestData(res, opt);
|
2020-10-13 01:40:18 +08:00
|
|
|
ret !== errorResult ? resolve(ret) : reject(new Error('request error!'));
|
2020-09-28 20:19:10 +08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
resolve((res as unknown) as Promise<T>);
|
|
|
|
|
})
|
|
|
|
|
.catch((e: Error) => {
|
|
|
|
|
if (requestCatch && isFunction(requestCatch)) {
|
|
|
|
|
reject(requestCatch(e));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
reject(e);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|