|
|
@ -8,6 +8,8 @@ import { cloneDeep } from 'lodash-es'; |
|
|
import type { RequestOptions, CreateAxiosOptions, Result, UploadFileParams } from './types'; |
|
|
import type { RequestOptions, CreateAxiosOptions, Result, UploadFileParams } from './types'; |
|
|
import { errorResult } from './const'; |
|
|
import { errorResult } from './const'; |
|
|
import { ContentTypeEnum } from '/@/enums/httpEnum'; |
|
|
import { ContentTypeEnum } from '/@/enums/httpEnum'; |
|
|
|
|
|
import qs from 'qs'; |
|
|
|
|
|
import { RequestEnum } from '../../../enums/httpEnum'; |
|
|
|
|
|
|
|
|
export * from './axiosTransform'; |
|
|
export * from './axiosTransform'; |
|
|
|
|
|
|
|
|
@ -144,6 +146,25 @@ export class VAxios { |
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 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), |
|
|
|
|
|
}; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
request<T = any>(config: AxiosRequestConfig, options?: RequestOptions): Promise<T> { |
|
|
request<T = any>(config: AxiosRequestConfig, options?: RequestOptions): Promise<T> { |
|
|
let conf: AxiosRequestConfig = cloneDeep(config); |
|
|
let conf: AxiosRequestConfig = cloneDeep(config); |
|
|
const transform = this.getTransform(); |
|
|
const transform = this.getTransform(); |
|
|
@ -156,6 +177,8 @@ export class VAxios { |
|
|
if (beforeRequestHook && isFunction(beforeRequestHook)) { |
|
|
if (beforeRequestHook && isFunction(beforeRequestHook)) { |
|
|
conf = beforeRequestHook(conf, opt); |
|
|
conf = beforeRequestHook(conf, opt); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
conf = this.supportFormData(conf); |
|
|
return new Promise((resolve, reject) => { |
|
|
return new Promise((resolve, reject) => { |
|
|
this.axiosInstance |
|
|
this.axiosInstance |
|
|
.request<any, AxiosResponse<Result>>(conf) |
|
|
.request<any, AxiosResponse<Result>>(conf) |
|
|
|