Skip to content

Commit

Permalink
perf: the request client upload function supports more parameters (#4439
Browse files Browse the repository at this point in the history
)
  • Loading branch information
anncwb authored Sep 19, 2024
1 parent 161820d commit 4765158
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe('fileUploader', () => {
mockAxiosInstance.post as unknown as ReturnType<typeof vi.fn>
).mockResolvedValueOnce(mockResponse);

const result = await fileUploader.upload(url, file);
const result = await fileUploader.upload(url, { file });
expect(result).toEqual(mockResponse);
expect(mockAxiosInstance.post).toHaveBeenCalledWith(
url,
Expand Down Expand Up @@ -66,7 +66,7 @@ describe('fileUploader', () => {
headers: { 'Custom-Header': 'value' },
};

const result = await fileUploader.upload(url, file, customConfig);
const result = await fileUploader.upload(url, { file }, customConfig);
expect(result).toEqual(mockResponse);
expect(mockAxiosInstance.post).toHaveBeenCalledWith(
url,
Expand All @@ -87,7 +87,7 @@ describe('fileUploader', () => {
mockAxiosInstance.post as unknown as ReturnType<typeof vi.fn>
).mockRejectedValueOnce(new Error('Network Error'));

await expect(fileUploader.upload(url, file)).rejects.toThrow(
await expect(fileUploader.upload(url, { file })).rejects.toThrow(
'Network Error',
);
});
Expand All @@ -99,7 +99,7 @@ describe('fileUploader', () => {
mockAxiosInstance.post as unknown as ReturnType<typeof vi.fn>
).mockRejectedValueOnce(new Error('Request failed with status code 404'));

await expect(fileUploader.upload(url, file)).rejects.toThrow(
await expect(fileUploader.upload(url, { file })).rejects.toThrow(
'Request failed with status code 404',
);
});
Expand All @@ -111,7 +111,7 @@ describe('fileUploader', () => {
mockAxiosInstance.post as unknown as ReturnType<typeof vi.fn>
).mockRejectedValueOnce(new Error('Request failed with status code 404'));

await expect(fileUploader.upload(url, file)).rejects.toThrow(
await expect(fileUploader.upload(url, { file })).rejects.toThrow(
'Request failed with status code 404',
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ class FileUploader {

public async upload(
url: string,
file: Blob | File,
data: { file: Blob | File } & Record<string, any>,
config?: AxiosRequestConfig,
): Promise<AxiosResponse> {
const formData = new FormData();
formData.append('file', file);

Object.entries(data).forEach(([key, value]) => {
formData.append(key, value);
});

const finalConfig: AxiosRequestConfig = {
...config,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ describe('requestClient', () => {
: [400, { error: 'Bad Request' }];
});

const response = await requestClient.upload('/test/upload', fileData);
const response = await requestClient.upload('/test/upload', {
file: fileData,
});
expect(response.data).toEqual({ data: 'file uploaded' });
});

Expand Down

0 comments on commit 4765158

Please sign in to comment.