2021-08-16 23:14:40 +08:00
|
|
|
import type { AxiosRequestConfig, AxiosInstance, AxiosResponse, AxiosError } from 'axios';
|
2021-10-12 18:30:32 +08:00
|
|
|
import type { RequestOptions, Result, UploadFileParams } from '/#/axios';
|
2021-03-09 23:01:32 +08:00
|
|
|
import type { CreateAxiosOptions } from './axiosTransform';
|
2020-09-28 20:19:10 +08:00
|
|
|
import axios from 'axios';
|
2021-03-09 23:01:32 +08:00
|
|
|
import qs from 'qs';
|
2020-09-28 20:19:10 +08:00
|
|
|
import { AxiosCanceler } from './axiosCancel';
|
|
|
|
|
import { isFunction } from '/@/utils/is';
|
2021-10-05 22:53:21 +08:00
|
|
|
import { cloneDeep } from 'lodash-es';
|
2020-11-12 00:06:12 +08:00
|
|
|
import { ContentTypeEnum } from '/@/enums/httpEnum';
|
2021-06-17 23:25:31 +08:00
|
|
|
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 {
|
2021-03-01 22:54:21 +08:00
|
|
|
headers: { ignoreCancelToken },
|
2021-02-10 00:53:47 +08:00
|
|
|
} = config;
|
2021-03-01 22:54:21 +08:00
|
|
|
|
|
|
|
|
const ignoreCancel =
|
|
|
|
|
ignoreCancelToken !== undefined
|
|
|
|
|
? ignoreCancelToken
|
|
|
|
|
: this.options.requestOptions?.ignoreCancelToken;
|
|
|
|
|
|
|
|
|
|
!ignoreCancel && axiosCanceler.addPending(config);
|
2020-09-28 20:19:10 +08:00
|
|
|
if (requestInterceptors && isFunction(requestInterceptors)) {
|
2021-06-17 23:25:31 +08:00
|
|
|
config = requestInterceptors(config, this.options);
|
2020-09-28 20:19:10 +08:00
|
|
|
}
|
|
|
|
|
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();
|
2021-10-05 22:53:21 +08:00
|
|
|
const customFilename = params.name || 'file';
|
|
|
|
|
|
|
|
|
|
if (params.filename) {
|
|
|
|
|
formData.append(customFilename, params.file, params.filename);
|
|
|
|
|
} else {
|
|
|
|
|
formData.append(customFilename, params.file);
|
|
|
|
|
}
|
2020-11-12 00:06:12 +08:00
|
|
|
|
|
|
|
|
if (params.data) {
|
|
|
|
|
Object.keys(params.data).forEach((key) => {
|
2021-10-05 22:53:21 +08:00
|
|
|
const value = params.data![key];
|
2020-11-12 00:06:12 +08:00
|
|
|
if (Array.isArray(value)) {
|
|
|
|
|
value.forEach((item) => {
|
|
|
|
|
formData.append(`${key}[]`, item);
|
|
|
|
|
});
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-05 22:53:21 +08:00
|
|
|
formData.append(key, params.data![key]);
|
2020-11-12 00:06:12 +08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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) {
|
2021-05-05 21:19:12 +08:00
|
|
|
const headers = config.headers || this.options.headers;
|
2021-02-24 20:45:15 +08:00
|
|
|
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,
|
2021-04-17 22:30:53 +08:00
|
|
|
data: qs.stringify(config.data, { arrayFormat: 'brackets' }),
|
2021-02-24 20:45:15 +08:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-26 22:45:36 +08:00
|
|
|
get<T = any>(config: AxiosRequestConfig, options?: RequestOptions): Promise<T> {
|
|
|
|
|
return this.request({ ...config, method: 'GET' }, options);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
post<T = any>(config: AxiosRequestConfig, options?: RequestOptions): Promise<T> {
|
|
|
|
|
return this.request({ ...config, method: 'POST' }, options);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
put<T = any>(config: AxiosRequestConfig, options?: RequestOptions): Promise<T> {
|
|
|
|
|
return this.request({ ...config, method: 'PUT' }, options);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
delete<T = any>(config: AxiosRequestConfig, options?: RequestOptions): Promise<T> {
|
|
|
|
|
return this.request({ ...config, method: 'DELETE' }, options);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-28 20:19:10 +08:00
|
|
|
request<T = any>(config: AxiosRequestConfig, options?: RequestOptions): Promise<T> {
|
2021-06-16 22:34:32 +08:00
|
|
|
let conf: CreateAxiosOptions = cloneDeep(config);
|
2020-09-28 20:19:10 +08:00
|
|
|
const transform = this.getTransform();
|
|
|
|
|
|
|
|
|
|
const { requestOptions } = this.options;
|
|
|
|
|
|
|
|
|
|
const opt: RequestOptions = Object.assign({}, requestOptions, options);
|
|
|
|
|
|
2021-02-26 22:45:36 +08:00
|
|
|
const { beforeRequestHook, requestCatchHook, transformRequestHook } = transform || {};
|
2020-09-28 20:19:10 +08:00
|
|
|
if (beforeRequestHook && isFunction(beforeRequestHook)) {
|
|
|
|
|
conf = beforeRequestHook(conf, opt);
|
|
|
|
|
}
|
2021-06-16 22:34:32 +08:00
|
|
|
conf.requestOptions = opt;
|
2021-02-24 20:45:15 +08:00
|
|
|
|
|
|
|
|
conf = this.supportFormData(conf);
|
2021-02-26 23:30:22 +08:00
|
|
|
|
2020-09-28 20:19:10 +08:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
this.axiosInstance
|
|
|
|
|
.request<any, AxiosResponse<Result>>(conf)
|
|
|
|
|
.then((res: AxiosResponse<Result>) => {
|
2021-02-26 22:45:36 +08:00
|
|
|
if (transformRequestHook && isFunction(transformRequestHook)) {
|
2021-05-28 00:07:28 +08:00
|
|
|
try {
|
|
|
|
|
const ret = transformRequestHook(res, opt);
|
|
|
|
|
resolve(ret);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
reject(err || new Error('request error!'));
|
|
|
|
|
}
|
2020-09-28 20:19:10 +08:00
|
|
|
return;
|
|
|
|
|
}
|
2021-05-28 00:07:28 +08:00
|
|
|
resolve(res as unknown as Promise<T>);
|
2020-09-28 20:19:10 +08:00
|
|
|
})
|
2021-08-16 23:14:40 +08:00
|
|
|
.catch((e: Error | AxiosError) => {
|
2021-02-26 22:45:36 +08:00
|
|
|
if (requestCatchHook && isFunction(requestCatchHook)) {
|
2021-06-16 22:34:32 +08:00
|
|
|
reject(requestCatchHook(e, opt));
|
2020-09-28 20:19:10 +08:00
|
|
|
return;
|
|
|
|
|
}
|
2021-08-16 23:14:40 +08:00
|
|
|
if (axios.isAxiosError(e)) {
|
|
|
|
|
// rewrite error message from axios in here
|
|
|
|
|
}
|
2020-09-28 20:19:10 +08:00
|
|
|
reject(e);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|