chore: lazy load request helpers
This commit is contained in:
@@ -6,10 +6,11 @@ import { bindMethods, isString, merge } from '@vben/utils';
|
|||||||
|
|
||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
|
|
||||||
import { FileDownloader } from './modules/downloader';
|
|
||||||
import { InterceptorManager } from './modules/interceptor';
|
import { InterceptorManager } from './modules/interceptor';
|
||||||
import { SSE } from './modules/sse';
|
|
||||||
import { FileUploader } from './modules/uploader';
|
import type { FileDownloader } from './modules/downloader';
|
||||||
|
import type { SSE } from './modules/sse';
|
||||||
|
import type { FileUploader } from './modules/uploader';
|
||||||
|
|
||||||
function stringifyQuery(
|
function stringifyQuery(
|
||||||
params: Record<string, any>,
|
params: Record<string, any>,
|
||||||
@@ -74,18 +75,13 @@ class RequestClient {
|
|||||||
public addRequestInterceptor: InterceptorManager['addRequestInterceptor'];
|
public addRequestInterceptor: InterceptorManager['addRequestInterceptor'];
|
||||||
|
|
||||||
public addResponseInterceptor: InterceptorManager['addResponseInterceptor'];
|
public addResponseInterceptor: InterceptorManager['addResponseInterceptor'];
|
||||||
public download: FileDownloader['download'];
|
|
||||||
|
|
||||||
public readonly instance: AxiosInstance;
|
public readonly instance: AxiosInstance;
|
||||||
// 是否正在刷新token
|
// 是否正在刷新token
|
||||||
public isRefreshing = false;
|
public isRefreshing = false;
|
||||||
public postSSE: SSE['postSSE'];
|
|
||||||
// 刷新token队列
|
// 刷新token队列
|
||||||
public refreshTokenQueue: ((token: string) => void)[] = [];
|
public refreshTokenQueue: ((token: string) => void)[] = [];
|
||||||
// SSE 401 时用于刷新 token 的回调
|
// SSE 401 时用于刷新 token 的回调
|
||||||
public refreshToken?: () => Promise<string>;
|
public refreshToken?: () => Promise<string>;
|
||||||
public requestSSE: SSE['requestSSE'];
|
|
||||||
public upload: FileUploader['upload'];
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 构造函数,用于创建Axios实例
|
* 构造函数,用于创建Axios实例
|
||||||
@@ -117,16 +113,6 @@ class RequestClient {
|
|||||||
this.addResponseInterceptor =
|
this.addResponseInterceptor =
|
||||||
interceptorManager.addResponseInterceptor.bind(interceptorManager);
|
interceptorManager.addResponseInterceptor.bind(interceptorManager);
|
||||||
|
|
||||||
// 实例化文件上传器
|
|
||||||
const fileUploader = new FileUploader(this);
|
|
||||||
this.upload = fileUploader.upload.bind(fileUploader);
|
|
||||||
// 实例化文件下载器
|
|
||||||
const fileDownloader = new FileDownloader(this);
|
|
||||||
this.download = fileDownloader.download.bind(fileDownloader);
|
|
||||||
// 实例化SSE模块
|
|
||||||
const sse = new SSE(this);
|
|
||||||
this.postSSE = sse.postSSE.bind(sse);
|
|
||||||
this.requestSSE = sse.requestSSE.bind(sse);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -153,6 +139,14 @@ class RequestClient {
|
|||||||
return this.instance.defaults.baseURL;
|
return this.instance.defaults.baseURL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async download<T = Blob>(
|
||||||
|
url: string,
|
||||||
|
config?: Parameters<FileDownloader['download']>[1],
|
||||||
|
): Promise<T> {
|
||||||
|
const { FileDownloader } = await import('./modules/downloader');
|
||||||
|
return new FileDownloader(this).download<T>(url, config);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* POST请求方法
|
* POST请求方法
|
||||||
*/
|
*/
|
||||||
@@ -186,6 +180,15 @@ class RequestClient {
|
|||||||
return this.request<T>(url, { ...config, data, method: 'PATCH' });
|
return this.request<T>(url, { ...config, data, method: 'PATCH' });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async postSSE(
|
||||||
|
url: string,
|
||||||
|
data?: any,
|
||||||
|
requestOptions?: Parameters<SSE['postSSE']>[2],
|
||||||
|
) {
|
||||||
|
const { SSE } = await import('./modules/sse');
|
||||||
|
return new SSE(this).postSSE(url, data, requestOptions);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 通用的请求方法
|
* 通用的请求方法
|
||||||
*/
|
*/
|
||||||
@@ -206,6 +209,24 @@ class RequestClient {
|
|||||||
throw error.response ? error.response.data : error;
|
throw error.response ? error.response.data : error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async requestSSE(
|
||||||
|
url: string,
|
||||||
|
data?: any,
|
||||||
|
requestOptions?: Parameters<SSE['requestSSE']>[2],
|
||||||
|
) {
|
||||||
|
const { SSE } = await import('./modules/sse');
|
||||||
|
return new SSE(this).requestSSE(url, data, requestOptions);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async upload<T = any>(
|
||||||
|
url: string,
|
||||||
|
data: Parameters<FileUploader['upload']>[1],
|
||||||
|
config?: RequestClientConfig,
|
||||||
|
): Promise<T> {
|
||||||
|
const { FileUploader } = await import('./modules/uploader');
|
||||||
|
return new FileUploader(this).upload<T>(url, data, config);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export { RequestClient };
|
export { RequestClient };
|
||||||
|
|||||||
Reference in New Issue
Block a user