|
@@ -1,11 +1,10 @@
|
|
-import type {
|
|
|
|
- AxiosInstance,
|
|
|
|
- AxiosRequestConfig,
|
|
|
|
- AxiosResponse,
|
|
|
|
- CreateAxiosDefaults,
|
|
|
|
-} from 'axios';
|
|
|
|
|
|
+import type { AxiosInstance, AxiosResponse } from 'axios';
|
|
|
|
|
|
-import type { RequestClientOptions } from './types';
|
|
|
|
|
|
+import type {
|
|
|
|
+ HttpResponse,
|
|
|
|
+ RequestClientConfig,
|
|
|
|
+ RequestClientOptions,
|
|
|
|
+} from './types';
|
|
|
|
|
|
import { bindMethods, merge } from '@vben/utils';
|
|
import { bindMethods, merge } from '@vben/utils';
|
|
|
|
|
|
@@ -34,10 +33,11 @@ class RequestClient {
|
|
*/
|
|
*/
|
|
constructor(options: RequestClientOptions = {}) {
|
|
constructor(options: RequestClientOptions = {}) {
|
|
// 合并默认配置和传入的配置
|
|
// 合并默认配置和传入的配置
|
|
- const defaultConfig: CreateAxiosDefaults = {
|
|
|
|
|
|
+ const defaultConfig: RequestClientOptions = {
|
|
headers: {
|
|
headers: {
|
|
'Content-Type': 'application/json;charset=utf-8',
|
|
'Content-Type': 'application/json;charset=utf-8',
|
|
},
|
|
},
|
|
|
|
+ responseReturn: 'raw',
|
|
// 默认超时时间
|
|
// 默认超时时间
|
|
timeout: 10_000,
|
|
timeout: 10_000,
|
|
};
|
|
};
|
|
@@ -54,6 +54,24 @@ class RequestClient {
|
|
this.addResponseInterceptor =
|
|
this.addResponseInterceptor =
|
|
interceptorManager.addResponseInterceptor.bind(interceptorManager);
|
|
interceptorManager.addResponseInterceptor.bind(interceptorManager);
|
|
|
|
|
|
|
|
+ // 添加基础的响应处理,根据设置决定返回响应的哪一部分
|
|
|
|
+ this.addResponseInterceptor<HttpResponse>({
|
|
|
|
+ fulfilled: (response) => {
|
|
|
|
+ const { config, data: responseData, status } = response;
|
|
|
|
+
|
|
|
|
+ if (config.responseReturn === 'raw') {
|
|
|
|
+ return response;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ const { code, data } = responseData;
|
|
|
|
+
|
|
|
|
+ if (status >= 200 && status < 400 && code === 0) {
|
|
|
|
+ return config.responseReturn === 'body' ? responseData : data;
|
|
|
|
+ }
|
|
|
|
+ throw Object.assign({}, response, { response });
|
|
|
|
+ },
|
|
|
|
+ });
|
|
|
|
+
|
|
// 实例化文件上传器
|
|
// 实例化文件上传器
|
|
const fileUploader = new FileUploader(this);
|
|
const fileUploader = new FileUploader(this);
|
|
this.upload = fileUploader.upload.bind(fileUploader);
|
|
this.upload = fileUploader.upload.bind(fileUploader);
|
|
@@ -65,14 +83,17 @@ class RequestClient {
|
|
/**
|
|
/**
|
|
* DELETE请求方法
|
|
* DELETE请求方法
|
|
*/
|
|
*/
|
|
- public delete<T = any>(url: string, config?: AxiosRequestConfig): Promise<T> {
|
|
|
|
|
|
+ public delete<T = any>(
|
|
|
|
+ url: string,
|
|
|
|
+ config?: RequestClientConfig,
|
|
|
|
+ ): Promise<T> {
|
|
return this.request<T>(url, { ...config, method: 'DELETE' });
|
|
return this.request<T>(url, { ...config, method: 'DELETE' });
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
* GET请求方法
|
|
* GET请求方法
|
|
*/
|
|
*/
|
|
- public get<T = any>(url: string, config?: AxiosRequestConfig): Promise<T> {
|
|
|
|
|
|
+ public get<T = any>(url: string, config?: RequestClientConfig): Promise<T> {
|
|
return this.request<T>(url, { ...config, method: 'GET' });
|
|
return this.request<T>(url, { ...config, method: 'GET' });
|
|
}
|
|
}
|
|
|
|
|
|
@@ -82,7 +103,7 @@ class RequestClient {
|
|
public post<T = any>(
|
|
public post<T = any>(
|
|
url: string,
|
|
url: string,
|
|
data?: any,
|
|
data?: any,
|
|
- config?: AxiosRequestConfig,
|
|
|
|
|
|
+ config?: RequestClientConfig,
|
|
): Promise<T> {
|
|
): Promise<T> {
|
|
return this.request<T>(url, { ...config, data, method: 'POST' });
|
|
return this.request<T>(url, { ...config, data, method: 'POST' });
|
|
}
|
|
}
|
|
@@ -93,7 +114,7 @@ class RequestClient {
|
|
public put<T = any>(
|
|
public put<T = any>(
|
|
url: string,
|
|
url: string,
|
|
data?: any,
|
|
data?: any,
|
|
- config?: AxiosRequestConfig,
|
|
|
|
|
|
+ config?: RequestClientConfig,
|
|
): Promise<T> {
|
|
): Promise<T> {
|
|
return this.request<T>(url, { ...config, data, method: 'PUT' });
|
|
return this.request<T>(url, { ...config, data, method: 'PUT' });
|
|
}
|
|
}
|
|
@@ -101,7 +122,10 @@ class RequestClient {
|
|
/**
|
|
/**
|
|
* 通用的请求方法
|
|
* 通用的请求方法
|
|
*/
|
|
*/
|
|
- public async request<T>(url: string, config: AxiosRequestConfig): Promise<T> {
|
|
|
|
|
|
+ public async request<T>(
|
|
|
|
+ url: string,
|
|
|
|
+ config: RequestClientConfig,
|
|
|
|
+ ): Promise<T> {
|
|
try {
|
|
try {
|
|
const response: AxiosResponse<T> = await this.instance({
|
|
const response: AxiosResponse<T> = await this.instance({
|
|
url,
|
|
url,
|