Skip to content

Commit fddfc6d

Browse files
committed
fix: 增加对不支持的 HTTP 方法的错误处理
1 parent df65501 commit fddfc6d

File tree

2 files changed

+91
-2
lines changed

2 files changed

+91
-2
lines changed

packages/effects/request/src/request-client/modules/downloader.test.ts

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ describe('fileDownloader', () => {
3030
expect(result).toBeInstanceOf(Blob);
3131
expect(result).toEqual(mockBlob);
3232
expect(mockAxiosInstance.get).toHaveBeenCalledWith(url, {
33+
method: 'GET',
3334
responseType: 'blob',
3435
responseReturn: 'body',
3536
});
@@ -51,6 +52,7 @@ describe('fileDownloader', () => {
5152
expect(result).toEqual(mockBlob);
5253
expect(mockAxiosInstance.get).toHaveBeenCalledWith(url, {
5354
...customConfig,
55+
method: 'GET',
5456
responseType: 'blob',
5557
responseReturn: 'body',
5658
});
@@ -84,3 +86,72 @@ describe('fileDownloader', () => {
8486
);
8587
});
8688
});
89+
90+
describe('fileDownloader use other method', () => {
91+
let fileDownloader: FileDownloader;
92+
93+
it('should call request using get', async () => {
94+
const url = 'https://example.com/file';
95+
const mockBlob = new Blob(['file content'], { type: 'text/plain' });
96+
const mockResponse: Blob = mockBlob;
97+
98+
const mockAxiosInstance = {
99+
request: vi.fn(),
100+
} as any;
101+
102+
fileDownloader = new FileDownloader(mockAxiosInstance);
103+
104+
mockAxiosInstance.request.mockResolvedValueOnce(mockResponse);
105+
106+
const result = await fileDownloader.download(url);
107+
108+
expect(result).toBeInstanceOf(Blob);
109+
expect(result).toEqual(mockBlob);
110+
expect(mockAxiosInstance.request).toHaveBeenCalledWith(url, {
111+
method: 'GET',
112+
responseType: 'blob',
113+
responseReturn: 'body',
114+
});
115+
});
116+
117+
it('should call post', async () => {
118+
const url = 'https://example.com/file';
119+
120+
const mockAxiosInstance = {
121+
post: vi.fn(),
122+
} as any;
123+
124+
fileDownloader = new FileDownloader(mockAxiosInstance);
125+
126+
const customConfig: AxiosRequestConfig = {
127+
method: 'POST',
128+
data: { name: 'aa' },
129+
};
130+
131+
await fileDownloader.download(url, customConfig);
132+
133+
expect(mockAxiosInstance.post).toHaveBeenCalledWith(
134+
url,
135+
{ name: 'aa' },
136+
{
137+
method: 'POST',
138+
responseType: 'blob',
139+
responseReturn: 'body',
140+
},
141+
);
142+
});
143+
144+
it('should handle errors gracefully', async () => {
145+
const url = 'https://example.com/file';
146+
const mockAxiosInstance = {
147+
post: vi.fn(),
148+
} as any;
149+
150+
fileDownloader = new FileDownloader(mockAxiosInstance);
151+
await expect(() =>
152+
fileDownloader.download(url, { method: 'postt' }),
153+
).rejects.toThrow(
154+
'RequestClient does not support method "POSTT". Please ensure the method is properly implemented in your RequestClient instance.',
155+
);
156+
});
157+
});

packages/effects/request/src/request-client/modules/downloader.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,27 @@ class FileDownloader {
3333
responseType: 'blob',
3434
};
3535

36-
const response = await this.client.request<T>(url, finalConfig);
36+
// Prefer a generic request if available; otherwise, dispatch to method-specific calls.
37+
const method = (finalConfig.method || 'GET').toUpperCase();
38+
const clientAny = this.client as any;
3739

38-
return response;
40+
if (typeof clientAny.request === 'function') {
41+
return await clientAny.request(url, finalConfig);
42+
}
43+
const lower = method.toLowerCase();
44+
45+
if (typeof clientAny[lower] === 'function') {
46+
if (['POST', 'PUT'].includes(method)) {
47+
const { data, ...rest } = finalConfig as Record<string, any>;
48+
return await clientAny[lower](url, data, rest);
49+
}
50+
51+
return await clientAny[lower](url, finalConfig);
52+
}
53+
54+
throw new Error(
55+
`RequestClient does not support method "${method}". Please ensure the method is properly implemented in your RequestClient instance.`,
56+
);
3957
}
4058
}
4159

0 commit comments

Comments
 (0)