fix: 增加对不支持的 HTTP 方法的错误处理
This commit is contained in:
parent
df655015b1
commit
fddfc6d494
@ -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.',
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@ -33,9 +33,27 @@ class FileDownloader {
|
||||
responseType: 'blob',
|
||||
};
|
||||
|
||||
const response = await this.client.request<T>(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<string, any>;
|
||||
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.`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user