Files
iot-device-management-frontend/packages/effects/request/src/request-client/modules/downloader.ts

42 lines
1.1 KiB
TypeScript
Raw Normal View History

2024-06-02 20:50:51 +08:00
import type { RequestClient } from '../request-client';
import type { RequestClientConfig } from '../types';
type DownloadRequestConfig = {
/**
*
* raw: 原始的AxiosResponseheadersstatus等
* body: 只返回响应数据的BODY部分(Blob)
*/
responseReturn?: 'body' | 'raw';
} & Omit<RequestClientConfig, 'responseReturn'>;
2024-06-02 20:50:51 +08:00
class FileDownloader {
private client: RequestClient;
constructor(client: RequestClient) {
this.client = client;
}
/**
*
* @param url
* @param config
* @returns config.responseReturn为'body'Blob()RequestResponse<Blob>
*/
public async download<T = Blob>(
2024-06-02 20:50:51 +08:00
url: string,
config?: DownloadRequestConfig,
): Promise<T> {
const finalConfig: DownloadRequestConfig = {
responseReturn: 'body',
2024-06-02 20:50:51 +08:00
...config,
responseType: 'blob',
};
const response = await this.client.get<T>(url, finalConfig);
2024-06-02 20:50:51 +08:00
return response;
}
}
export { FileDownloader };