12345678910111213141516171819202122232425262728293031323334353637383940 |
- import type { RequestClient } from '../request-client';
- import type { RequestClientConfig } from '../types';
- class FileUploader {
- private client: RequestClient;
- constructor(client: RequestClient) {
- this.client = client;
- }
- public async upload<T = any>(
- url: string,
- data: Record<string, any> & { file: Blob | File },
- config?: RequestClientConfig,
- ): Promise<T> {
- const formData = new FormData();
- Object.entries(data).forEach(([key, value]) => {
- if (Array.isArray(value)) {
- value.forEach((item, index) => {
- formData.append(`${key}[${index}]`, item);
- });
- } else {
- formData.append(key, value);
- }
- });
- const finalConfig: RequestClientConfig = {
- ...config,
- headers: {
- 'Content-Type': 'multipart/form-data',
- ...config?.headers,
- },
- };
- return this.client.post(url, formData, finalConfig);
- }
- }
- export { FileUploader };
|