From df655015b1228a1d312fbc1a7fdd73293622eda3 Mon Sep 17 00:00:00 2001 From: lighua <877312980@qq.com> Date: Fri, 8 Aug 2025 15:31:31 +0800 Subject: [PATCH 1/2] =?UTF-8?q?fix:=20=E4=BC=98=E5=8C=96=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E4=B8=8B=E8=BD=BD=E5=99=A8=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../effects/request/src/request-client/modules/downloader.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/effects/request/src/request-client/modules/downloader.ts b/packages/effects/request/src/request-client/modules/downloader.ts index 77e72c6c593..f5bd101a453 100644 --- a/packages/effects/request/src/request-client/modules/downloader.ts +++ b/packages/effects/request/src/request-client/modules/downloader.ts @@ -28,11 +28,12 @@ class FileDownloader { ): Promise { const finalConfig: DownloadRequestConfig = { responseReturn: 'body', + method: 'GET', ...config, responseType: 'blob', }; - const response = await this.client.get(url, finalConfig); + const response = await this.client.request(url, finalConfig); return response; } From fddfc6d4940aa0a7332b5ff3a33a3fcabf5092eb Mon Sep 17 00:00:00 2001 From: lighua <877312980@qq.com> Date: Thu, 21 Aug 2025 11:13:02 +0800 Subject: [PATCH 2/2] =?UTF-8?q?fix:=20=E5=A2=9E=E5=8A=A0=E5=AF=B9=E4=B8=8D?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E7=9A=84=20HTTP=20=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E7=9A=84=E9=94=99=E8=AF=AF=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../request-client/modules/downloader.test.ts | 71 +++++++++++++++++++ .../src/request-client/modules/downloader.ts | 22 +++++- 2 files changed, 91 insertions(+), 2 deletions(-) diff --git a/packages/effects/request/src/request-client/modules/downloader.test.ts b/packages/effects/request/src/request-client/modules/downloader.test.ts index d44dcbb7bd4..27e8a30cbea 100644 --- a/packages/effects/request/src/request-client/modules/downloader.test.ts +++ b/packages/effects/request/src/request-client/modules/downloader.test.ts @@ -30,6 +30,7 @@ describe('fileDownloader', () => { expect(result).toBeInstanceOf(Blob); expect(result).toEqual(mockBlob); expect(mockAxiosInstance.get).toHaveBeenCalledWith(url, { + method: 'GET', responseType: 'blob', responseReturn: 'body', }); @@ -51,6 +52,7 @@ describe('fileDownloader', () => { expect(result).toEqual(mockBlob); expect(mockAxiosInstance.get).toHaveBeenCalledWith(url, { ...customConfig, + method: 'GET', responseType: 'blob', responseReturn: 'body', }); @@ -84,3 +86,72 @@ describe('fileDownloader', () => { ); }); }); + +describe('fileDownloader use other method', () => { + let fileDownloader: FileDownloader; + + it('should call request using get', async () => { + const url = 'https://example.com/file'; + const mockBlob = new Blob(['file content'], { type: 'text/plain' }); + const mockResponse: Blob = mockBlob; + + const mockAxiosInstance = { + request: vi.fn(), + } as any; + + fileDownloader = new FileDownloader(mockAxiosInstance); + + mockAxiosInstance.request.mockResolvedValueOnce(mockResponse); + + const result = await fileDownloader.download(url); + + expect(result).toBeInstanceOf(Blob); + expect(result).toEqual(mockBlob); + expect(mockAxiosInstance.request).toHaveBeenCalledWith(url, { + method: 'GET', + responseType: 'blob', + responseReturn: 'body', + }); + }); + + it('should call post', async () => { + const url = 'https://example.com/file'; + + const mockAxiosInstance = { + post: vi.fn(), + } as any; + + fileDownloader = new FileDownloader(mockAxiosInstance); + + const customConfig: AxiosRequestConfig = { + method: 'POST', + data: { name: 'aa' }, + }; + + await fileDownloader.download(url, customConfig); + + expect(mockAxiosInstance.post).toHaveBeenCalledWith( + url, + { name: 'aa' }, + { + method: 'POST', + responseType: 'blob', + responseReturn: 'body', + }, + ); + }); + + it('should handle errors gracefully', async () => { + const url = 'https://example.com/file'; + const mockAxiosInstance = { + post: vi.fn(), + } as any; + + fileDownloader = new FileDownloader(mockAxiosInstance); + await expect(() => + fileDownloader.download(url, { method: 'postt' }), + ).rejects.toThrow( + 'RequestClient does not support method "POSTT". Please ensure the method is properly implemented in your RequestClient instance.', + ); + }); +}); diff --git a/packages/effects/request/src/request-client/modules/downloader.ts b/packages/effects/request/src/request-client/modules/downloader.ts index f5bd101a453..6c3000589a1 100644 --- a/packages/effects/request/src/request-client/modules/downloader.ts +++ b/packages/effects/request/src/request-client/modules/downloader.ts @@ -33,9 +33,27 @@ class FileDownloader { responseType: 'blob', }; - const response = await this.client.request(url, finalConfig); + // Prefer a generic request if available; otherwise, dispatch to method-specific calls. + const method = (finalConfig.method || 'GET').toUpperCase(); + const clientAny = this.client as any; - return response; + if (typeof clientAny.request === 'function') { + return await clientAny.request(url, finalConfig); + } + const lower = method.toLowerCase(); + + if (typeof clientAny[lower] === 'function') { + if (['POST', 'PUT'].includes(method)) { + const { data, ...rest } = finalConfig as Record; + return await clientAny[lower](url, data, rest); + } + + return await clientAny[lower](url, finalConfig); + } + + throw new Error( + `RequestClient does not support method "${method}". Please ensure the method is properly implemented in your RequestClient instance.`, + ); } }