admin-vben5/packages/effects/request/src/request-client/modules/uploader.ts

35 lines
788 B
TypeScript
Raw Normal View History

2024-06-02 20:50:51 +08:00
import type { RequestClient } from '../request-client';
import type { RequestClientConfig } from '../types';
2024-06-02 20:50:51 +08:00
class FileUploader {
private client: RequestClient;
constructor(client: RequestClient) {
this.client = client;
}
public async upload<T = any>(
2024-06-02 20:50:51 +08:00
url: string,
data: Record<string, any> & { file: Blob | File },
config?: RequestClientConfig,
): Promise<T> {
2024-06-02 20:50:51 +08:00
const formData = new FormData();
Object.entries(data).forEach(([key, value]) => {
formData.append(key, value);
});
2024-06-02 20:50:51 +08:00
const finalConfig: RequestClientConfig = {
2024-06-02 20:50:51 +08:00
...config,
headers: {
'Content-Type': 'multipart/form-data',
...config?.headers,
},
};
return this.client.post(url, formData, finalConfig);
}
}
export { FileUploader };