| | |
| | | import type { AxiosRequestConfig, AxiosInstance, AxiosResponse } from 'axios'; |
| | | import type { RequestOptions, Result, UploadFileParams } from './types'; |
| | | import type { AxiosRequestConfig, AxiosInstance, AxiosResponse, AxiosError } from 'axios'; |
| | | import type { RequestOptions, Result, UploadFileParams } from '/#/axios'; |
| | | import type { CreateAxiosOptions } from './axiosTransform'; |
| | | |
| | | import axios from 'axios'; |
| | | import qs from 'qs'; |
| | | import { AxiosCanceler } from './axiosCancel'; |
| | | import { isFunction } from '/@/utils/is'; |
| | | import { cloneDeep } from 'lodash-es'; |
| | | |
| | | //import { errorResult } from './const'; |
| | | import { ContentTypeEnum } from '/@/enums/httpEnum'; |
| | | import { RequestEnum } from '../../../enums/httpEnum'; |
| | | import { RequestEnum } from '/@/enums/httpEnum'; |
| | | |
| | | export * from './axiosTransform'; |
| | | |
| | |
| | | // Request interceptor configuration processing |
| | | this.axiosInstance.interceptors.request.use((config: AxiosRequestConfig) => { |
| | | // If cancel repeat request is turned on, then cancel repeat request is prohibited |
| | | const { |
| | | headers: { ignoreCancelToken }, |
| | | } = config; |
| | | |
| | | // @ts-ignore |
| | | const { ignoreCancelToken } = config.requestOptions; |
| | | const ignoreCancel = |
| | | ignoreCancelToken !== undefined |
| | | ? ignoreCancelToken |
| | |
| | | |
| | | !ignoreCancel && axiosCanceler.addPending(config); |
| | | if (requestInterceptors && isFunction(requestInterceptors)) { |
| | | config = requestInterceptors(config); |
| | | config = requestInterceptors(config, this.options); |
| | | } |
| | | return config; |
| | | }, undefined); |
| | |
| | | // Response result interceptor error capture |
| | | responseInterceptorsCatch && |
| | | isFunction(responseInterceptorsCatch) && |
| | | this.axiosInstance.interceptors.response.use(undefined, responseInterceptorsCatch); |
| | | this.axiosInstance.interceptors.response.use(undefined, (error) => { |
| | | // @ts-ignore |
| | | responseInterceptorsCatch(this.axiosInstance, error); |
| | | }); |
| | | } |
| | | |
| | | /** |
| | |
| | | */ |
| | | uploadFile<T = any>(config: AxiosRequestConfig, params: UploadFileParams) { |
| | | const formData = new window.FormData(); |
| | | const customFilename = params.name || 'file'; |
| | | |
| | | if (params.filename) { |
| | | formData.append(customFilename, params.file, params.filename); |
| | | } else { |
| | | formData.append(customFilename, params.file); |
| | | } |
| | | |
| | | if (params.data) { |
| | | Object.keys(params.data).forEach((key) => { |
| | | if (!params.data) return; |
| | | const value = params.data[key]; |
| | | 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(key, params.data![key]); |
| | | }); |
| | | } |
| | | |
| | | formData.append(params.name || 'file', params.file, params.filename); |
| | | |
| | | return this.axiosInstance.request<T>({ |
| | | ...config, |
| | |
| | | data: formData, |
| | | headers: { |
| | | 'Content-type': ContentTypeEnum.FORM_DATA, |
| | | // @ts-ignore |
| | | ignoreCancelToken: true, |
| | | }, |
| | | }); |
| | |
| | | } |
| | | |
| | | request<T = any>(config: AxiosRequestConfig, options?: RequestOptions): Promise<T> { |
| | | let conf: AxiosRequestConfig = cloneDeep(config); |
| | | let conf: CreateAxiosOptions = cloneDeep(config); |
| | | const transform = this.getTransform(); |
| | | |
| | | const { requestOptions } = this.options; |
| | |
| | | if (beforeRequestHook && isFunction(beforeRequestHook)) { |
| | | conf = beforeRequestHook(conf, opt); |
| | | } |
| | | conf.requestOptions = opt; |
| | | |
| | | conf = this.supportFormData(conf); |
| | | |
| | |
| | | } |
| | | resolve(res as unknown as Promise<T>); |
| | | }) |
| | | .catch((e: Error) => { |
| | | .catch((e: Error | AxiosError) => { |
| | | if (requestCatchHook && isFunction(requestCatchHook)) { |
| | | reject(requestCatchHook(e)); |
| | | reject(requestCatchHook(e, opt)); |
| | | return; |
| | | } |
| | | if (axios.isAxiosError(e)) { |
| | | // rewrite error message from axios in here |
| | | } |
| | | reject(e); |
| | | }); |
| | | }); |