Merge pull request #40 from vbenjs/small-change

refactor: small refactor in @vben-core/request
pull/48/MERGE
vince 2024-07-12 10:12:16 +08:00 committed by GitHub
commit 61dbb05b5d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 16 additions and 13 deletions

View File

@ -92,10 +92,9 @@ function setupAccessGuard(router: Router) {
// 生成路由表 // 生成路由表
// 当前登录用户拥有的角色标识列表 // 当前登录用户拥有的角色标识列表
let userRoles: string[] = [];
const userInfo = const userInfo =
accessStore.userInfo || (await accessStore.fetchUserInfo()); accessStore.userInfo || (await accessStore.fetchUserInfo());
userRoles = userInfo.roles ?? []; const userRoles = userInfo.roles ?? [];
// 生成菜单和路由 // 生成菜单和路由
const { accessibleMenus, accessibleRoutes } = await generateAccess({ const { accessibleMenus, accessibleRoutes } = await generateAccess({

View File

@ -6,7 +6,11 @@ import type {
InternalAxiosRequestConfig, InternalAxiosRequestConfig,
} from 'axios'; } from 'axios';
import type { MakeAuthorizationFn, RequestClientOptions } from './types'; import type {
MakeAuthorizationFn,
MakeErrorMessageFn,
RequestClientOptions,
} from './types';
import { $t } from '@vben-core/locales'; import { $t } from '@vben-core/locales';
import { merge } from '@vben-core/toolkit'; import { merge } from '@vben-core/toolkit';
@ -20,7 +24,8 @@ import { FileUploader } from './modules/uploader';
class RequestClient { class RequestClient {
private instance: AxiosInstance; private instance: AxiosInstance;
private makeAuthorization: MakeAuthorizationFn | undefined; private makeAuthorization: MakeAuthorizationFn | undefined;
private options: RequestClientOptions; private makeErrorMessage: MakeErrorMessageFn | undefined;
public addRequestInterceptor: InterceptorManager['addRequestInterceptor']; public addRequestInterceptor: InterceptorManager['addRequestInterceptor'];
public addResponseInterceptor: InterceptorManager['addResponseInterceptor']; public addResponseInterceptor: InterceptorManager['addResponseInterceptor'];
public download: FileDownloader['download']; public download: FileDownloader['download'];
@ -40,12 +45,12 @@ class RequestClient {
// 默认超时时间 // 默认超时时间
timeout: 10_000, timeout: 10_000,
}; };
const { makeAuthorization, ...axiosConfig } = options; const { makeAuthorization, makeErrorMessage, ...axiosConfig } = options;
this.options = options;
const requestConfig = merge(axiosConfig, defaultConfig); const requestConfig = merge(axiosConfig, defaultConfig);
this.instance = axios.create(requestConfig); this.instance = axios.create(requestConfig);
this.makeAuthorization = makeAuthorization; this.makeAuthorization = makeAuthorization;
this.makeErrorMessage = makeErrorMessage;
// 实例化拦截器管理器 // 实例化拦截器管理器
const interceptorManager = new InterceptorManager(this.instance); const interceptorManager = new InterceptorManager(this.instance);
@ -111,9 +116,8 @@ class RequestClient {
} else if (error?.message?.includes?.('timeout')) { } else if (error?.message?.includes?.('timeout')) {
errMsg = $t('fallback.http.requestTimeout'); errMsg = $t('fallback.http.requestTimeout');
} }
const { makeAuthorization, makeErrorMessage } = this.options;
if (errMsg) { if (errMsg) {
makeErrorMessage?.(errMsg); this.makeErrorMessage?.(errMsg);
return Promise.reject(error); return Promise.reject(error);
} }
@ -128,7 +132,7 @@ class RequestClient {
case 401: { case 401: {
errorMessage = $t('fallback.http.unauthorized'); errorMessage = $t('fallback.http.unauthorized');
makeAuthorization?.().unAuthorizedHandler?.(); this.makeAuthorization?.().unAuthorizedHandler?.();
break; break;
} }
case 403: { case 403: {
@ -150,7 +154,7 @@ class RequestClient {
} }
} }
makeErrorMessage?.(errorMessage); this.makeErrorMessage?.(errorMessage);
return Promise.reject(error); return Promise.reject(error);
}, },
); );

View File

@ -16,7 +16,7 @@ type MakeAuthorizationFn = (
config?: InternalAxiosRequestConfig, config?: InternalAxiosRequestConfig,
) => MakeAuthorization; ) => MakeAuthorization;
type ErrorMessageFn = (message: string) => void; type MakeErrorMessageFn = (message: string) => void;
interface RequestClientOptions extends CreateAxiosDefaults { interface RequestClientOptions extends CreateAxiosDefaults {
/** /**
@ -26,7 +26,7 @@ interface RequestClientOptions extends CreateAxiosDefaults {
/** /**
* *
*/ */
makeErrorMessage?: ErrorMessageFn; makeErrorMessage?: MakeErrorMessageFn;
} }
interface HttpResponse<T = any> { interface HttpResponse<T = any> {
@ -42,6 +42,7 @@ interface HttpResponse<T = any> {
export type { export type {
HttpResponse, HttpResponse,
MakeAuthorizationFn, MakeAuthorizationFn,
MakeErrorMessageFn,
RequestClientOptions, RequestClientOptions,
RequestContentType, RequestContentType,
}; };

View File

@ -1,4 +1,3 @@
// 修改过的button
export * from './alert-dialog'; export * from './alert-dialog';
export * from './avatar'; export * from './avatar';
export * from './back-top'; export * from './back-top';