diff --git a/web/packages/effects/request/src/request-client/request-client.ts b/web/packages/effects/request/src/request-client/request-client.ts index dfe8419..32c2f50 100644 --- a/web/packages/effects/request/src/request-client/request-client.ts +++ b/web/packages/effects/request/src/request-client/request-client.ts @@ -6,10 +6,11 @@ import { bindMethods, isString, merge } from '@vben/utils'; import axios from 'axios'; -import { FileDownloader } from './modules/downloader'; import { InterceptorManager } from './modules/interceptor'; -import { SSE } from './modules/sse'; -import { FileUploader } from './modules/uploader'; + +import type { FileDownloader } from './modules/downloader'; +import type { SSE } from './modules/sse'; +import type { FileUploader } from './modules/uploader'; function stringifyQuery( params: Record, @@ -74,18 +75,13 @@ class RequestClient { public addRequestInterceptor: InterceptorManager['addRequestInterceptor']; public addResponseInterceptor: InterceptorManager['addResponseInterceptor']; - public download: FileDownloader['download']; - public readonly instance: AxiosInstance; // 是否正在刷新token public isRefreshing = false; - public postSSE: SSE['postSSE']; // 刷新token队列 public refreshTokenQueue: ((token: string) => void)[] = []; // SSE 401 时用于刷新 token 的回调 public refreshToken?: () => Promise; - public requestSSE: SSE['requestSSE']; - public upload: FileUploader['upload']; /** * 构造函数,用于创建Axios实例 @@ -117,16 +113,6 @@ class RequestClient { this.addResponseInterceptor = interceptorManager.addResponseInterceptor.bind(interceptorManager); - // 实例化文件上传器 - const fileUploader = new FileUploader(this); - this.upload = fileUploader.upload.bind(fileUploader); - // 实例化文件下载器 - const fileDownloader = new FileDownloader(this); - this.download = fileDownloader.download.bind(fileDownloader); - // 实例化SSE模块 - const sse = new SSE(this); - this.postSSE = sse.postSSE.bind(sse); - this.requestSSE = sse.requestSSE.bind(sse); } /** @@ -153,6 +139,14 @@ class RequestClient { return this.instance.defaults.baseURL; } + public async download( + url: string, + config?: Parameters[1], + ): Promise { + const { FileDownloader } = await import('./modules/downloader'); + return new FileDownloader(this).download(url, config); + } + /** * POST请求方法 */ @@ -186,6 +180,15 @@ class RequestClient { return this.request(url, { ...config, data, method: 'PATCH' }); } + public async postSSE( + url: string, + data?: any, + requestOptions?: Parameters[2], + ) { + const { SSE } = await import('./modules/sse'); + return new SSE(this).postSSE(url, data, requestOptions); + } + /** * 通用的请求方法 */ @@ -206,6 +209,24 @@ class RequestClient { throw error.response ? error.response.data : error; } } + + public async requestSSE( + url: string, + data?: any, + requestOptions?: Parameters[2], + ) { + const { SSE } = await import('./modules/sse'); + return new SSE(this).requestSSE(url, data, requestOptions); + } + + public async upload( + url: string, + data: Parameters[1], + config?: RequestClientConfig, + ): Promise { + const { FileUploader } = await import('./modules/uploader'); + return new FileUploader(this).upload(url, data, config); + } } export { RequestClient };