|
|
@ -1,5 +1,9 @@ |
|
|
import type { RequestClient } from './request-client'; |
|
|
import type { RequestClient } from './request-client'; |
|
|
import type { MakeErrorMessageFn, ResponseInterceptorConfig } from './types'; |
|
|
import type { |
|
|
|
|
|
MakeErrorMessageFn, |
|
|
|
|
|
RequestClientConfig, |
|
|
|
|
|
ResponseInterceptorConfig, |
|
|
|
|
|
} from './types'; |
|
|
|
|
|
|
|
|
import { $t } from '@vben/locales'; |
|
|
import { $t } from '@vben/locales'; |
|
|
import { isFunction } from '@vben/utils'; |
|
|
import { isFunction } from '@vben/utils'; |
|
|
@ -52,7 +56,7 @@ export const authenticateResponseInterceptor = ({ |
|
|
formatToken, |
|
|
formatToken, |
|
|
}: { |
|
|
}: { |
|
|
client: RequestClient; |
|
|
client: RequestClient; |
|
|
doReAuthenticate: () => Promise<void>; |
|
|
doReAuthenticate: (config?: RequestClientConfig) => Promise<void>; |
|
|
doRefreshToken: () => Promise<string>; |
|
|
doRefreshToken: () => Promise<string>; |
|
|
enableRefreshToken: boolean; |
|
|
enableRefreshToken: boolean; |
|
|
formatToken: (token: string) => null | string; |
|
|
formatToken: (token: string) => null | string; |
|
|
@ -67,7 +71,7 @@ export const authenticateResponseInterceptor = ({ |
|
|
// 判断是否启用了 refreshToken 功能
|
|
|
// 判断是否启用了 refreshToken 功能
|
|
|
// 如果没有启用或者已经是重试请求了,直接跳转到重新登录
|
|
|
// 如果没有启用或者已经是重试请求了,直接跳转到重新登录
|
|
|
if (!enableRefreshToken || config.__isRetryRequest) { |
|
|
if (!enableRefreshToken || config.__isRetryRequest) { |
|
|
await doReAuthenticate(); |
|
|
await doReAuthenticate(config); |
|
|
throw error; |
|
|
throw error; |
|
|
} |
|
|
} |
|
|
// 如果正在刷新 token,则将请求加入队列,等待刷新完成
|
|
|
// 如果正在刷新 token,则将请求加入队列,等待刷新完成
|
|
|
@ -99,7 +103,7 @@ export const authenticateResponseInterceptor = ({ |
|
|
client.refreshTokenQueue.forEach((callback) => callback('')); |
|
|
client.refreshTokenQueue.forEach((callback) => callback('')); |
|
|
client.refreshTokenQueue = []; |
|
|
client.refreshTokenQueue = []; |
|
|
console.error('Refresh token failed, please login again.'); |
|
|
console.error('Refresh token failed, please login again.'); |
|
|
await doReAuthenticate(); |
|
|
await doReAuthenticate(config); |
|
|
|
|
|
|
|
|
throw refreshError; |
|
|
throw refreshError; |
|
|
} finally { |
|
|
} finally { |
|
|
@ -118,6 +122,11 @@ export const errorMessageResponseInterceptor = ( |
|
|
return Promise.reject(error); |
|
|
return Promise.reject(error); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 如果配置了错误消息提示模式为 none,则跳过错误提示
|
|
|
|
|
|
if (error?.config?.errorMessageMode === 'none') { |
|
|
|
|
|
return Promise.reject(error); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
const err: string = error?.toString?.() ?? ''; |
|
|
const err: string = error?.toString?.() ?? ''; |
|
|
let errMsg = ''; |
|
|
let errMsg = ''; |
|
|
if (err?.includes('Network Error')) { |
|
|
if (err?.includes('Network Error')) { |
|
|
|