import { RequestProxyType, CustomConfigType, RequestType } from './index.type'; import Request from './request'; import { AxiosRequestConfig, AxiosResponse } from 'axios'; import { AuthStorageProxyType } from '../types'; class RequestProxy implements RequestProxyType { private axios: RequestType; private authStorage: AuthStorageProxyType; private defaultCustomConfig: CustomConfigType = { isNeedLoading: true, isNeedToken: true, isNeedShowError: true, }; // 记录并行的请求次数 private requestCount = 0; // Loading 控制 // eslint-disable-next-line @typescript-eslint/member-ordering loadingConfig: { timeId?: number; service?: any; } = {}; constructor(config: AxiosRequestConfig, authStorage: AuthStorageProxyType) { config.baseURL = this.transformUrl(config.baseURL, true); this.axios = new Request(config); this.authStorage = authStorage; } /** * 发起请求 * @param config 配置项 * @param customConfig 自定义配置 */ private async transfromRquest( config: AxiosRequestConfig, customConfig: CustomConfigType = {}, ): Promise { customConfig = { ...this.defaultCustomConfig, ...customConfig }; this.transformUrl(config.url); // this.handleLoading(customConfig, true) this.addToken(config, customConfig); // eslint-disable-next-line no-plusplus customConfig.isNeedLoading && this.requestCount++; try { const result = await this.axios.request(config); return result; } catch (error) { this.handleError(customConfig, error); return Promise.reject(error); } finally { // eslint-disable-next-line no-plusplus customConfig.isNeedLoading && this.requestCount--; // this.handleLoading(customConfig, false) } } /** * Loading 的开启关闭 * @param customConfig 自定义配置项 * @param isOpen 是否开启 */ // private handleLoading(customConfig: CustomConfigType, isOpen: boolean) { // if (!customConfig.isNeedLoading) return // // 不重复开启 Loading // if (this.requestCount !== 0) return // if (isOpen) { // this.loadingConfig.timeId = setTimeout(() => { // this.loadingConfig.service = message.loading({ // content: '拼命加载中...' // }) // }, 300) // return // } // clearInterval(this.loadingConfig.timeId) // this.loadingConfig.service && this.loadingConfig.service.destroy() // } /** * token 处理 * @param config 配置项 * @param customConfig 自定义配置项 */ private addToken(config: AxiosRequestConfig, customConfig: CustomConfigType) { if (customConfig.isNeedToken) { const token = this.authStorage.getToken(); if (token) { config.headers = { Authorization: `Bearer ${token}`, }; } } else { config.headers = {}; } } /** * 错误处理 * @param customConfig 配置项 * @param error 错误信息对象 */ private handleError(customConfig: CustomConfigType, error: any) { if (error.message === '取消请求') return; customConfig.isNeedShowError && console.error({ content: `错误提示:${error.message || '太火爆了,请稍后再试!'}`, }); } /** * 处理路径 * @param url 路径 * @param isBaseURL 是否是根路径 */ private transformUrl(url = '', isBaseURL = false) { if (!url) return url; if (isBaseURL) { if (!/\/$/.test(url)) { return `${url}/`; } return url; } if (/^\//.test(url)) { return `${url.substr(1)}`; } return url; } // eslint-disable-next-line @typescript-eslint/member-ordering public async get(url: string, config?: AxiosRequestConfig, customConfig?: CustomConfigType): Promise { return this.transfromRquest( { url, method: 'GET', ...config, }, customConfig, ); } // eslint-disable-next-line @typescript-eslint/member-ordering public async post( url: string, config?: AxiosRequestConfig, customConfig: CustomConfigType = this.defaultCustomConfig, ): Promise { return this.transfromRquest( { url, method: 'POST', ...config, }, customConfig, ); } // eslint-disable-next-line @typescript-eslint/member-ordering public async put( url: string, config?: AxiosRequestConfig, customConfig: CustomConfigType = this.defaultCustomConfig, ): Promise { return this.transfromRquest( { url, method: 'PUT', ...config, }, customConfig, ); } // eslint-disable-next-line @typescript-eslint/member-ordering public async patch( url: string, config?: AxiosRequestConfig, customConfig: CustomConfigType = this.defaultCustomConfig, ): Promise { return this.transfromRquest( { url, method: 'PATCH', ...config, }, customConfig, ); } // eslint-disable-next-line @typescript-eslint/member-ordering public async delete( url: string, config?: AxiosRequestConfig, customConfig: CustomConfigType = this.defaultCustomConfig, ): Promise { return this.transfromRquest( { url, method: 'DELETE', ...config, }, customConfig, ); } } export default RequestProxy;