fix: request download and upload not support responseReturn (#5456)

* fix: request download and upload not support `responseReturn`

* docs: update

* fix: type of request client upload result
This commit is contained in:
Netfan
2025-01-22 00:59:10 +08:00
committed by GitHub
parent 195ceec9b4
commit e225159cce
7 changed files with 93 additions and 28 deletions

View File

@@ -1,7 +1,14 @@
import type { AxiosRequestConfig } from 'axios';
import type { RequestClient } from '../request-client';
import type { RequestResponse } from '../types';
import type { RequestClientConfig } from '../types';
type DownloadRequestConfig = {
/**
* 定义期望获得的数据类型。
* raw: 原始的AxiosResponse包括headers、status等。
* body: 只返回响应数据的BODY部分(Blob)
*/
responseReturn?: 'body' | 'raw';
} & Omit<RequestClientConfig, 'responseReturn'>;
class FileDownloader {
private client: RequestClient;
@@ -9,20 +16,23 @@ class FileDownloader {
constructor(client: RequestClient) {
this.client = client;
}
public async download(
/**
* 下载文件
* @param url 文件的完整链接
* @param config 配置信息,可选。
* @returns 如果config.responseReturn为'body'则返回Blob(默认)否则返回RequestResponse<Blob>
*/
public async download<T = Blob>(
url: string,
config?: AxiosRequestConfig,
): Promise<RequestResponse<Blob>> {
const finalConfig: AxiosRequestConfig = {
config?: DownloadRequestConfig,
): Promise<T> {
const finalConfig: DownloadRequestConfig = {
responseReturn: 'body',
...config,
responseType: 'blob',
};
const response = await this.client.get<RequestResponse<Blob>>(
url,
finalConfig,
);
const response = await this.client.get<T>(url, finalConfig);
return response;
}