杂务:新增声明级注释覆盖率
This commit is contained in:
@@ -15,9 +15,21 @@ import { isApiAuthFreePath } from './auth-free-paths'
|
||||
|
||||
// 防重复提示标志
|
||||
let lastErrorTime = 0
|
||||
/**
|
||||
* Purpose: lastErrorMessage supports index.ts by holding or executing the last error message page or API role.
|
||||
* Usage: import, call, read, or update this let from the owning page/module while preserving the surrounding loading and permission flow.
|
||||
*/
|
||||
let lastErrorMessage = ''
|
||||
/**
|
||||
* Purpose: ERROR_DEBOUNCE_TIME supports index.ts by holding or executing the error debounce time page or API role.
|
||||
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
|
||||
*/
|
||||
const ERROR_DEBOUNCE_TIME = 1000 // 1秒内不重复显示相同错误
|
||||
|
||||
/**
|
||||
* Purpose: api supports index.ts by holding or executing the api page or API role.
|
||||
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
|
||||
*/
|
||||
const api = axios.create({
|
||||
baseURL: API_BASE_URL,
|
||||
timeout: 10000,
|
||||
@@ -26,24 +38,40 @@ const api = axios.create({
|
||||
}
|
||||
})
|
||||
|
||||
/**
|
||||
* Purpose: isRefreshing supports index.ts by holding or executing the is refreshing page or API role.
|
||||
* Usage: import, call, read, or update this let from the owning page/module while preserving the surrounding loading and permission flow.
|
||||
*/
|
||||
let isRefreshing = false
|
||||
let failedQueue: Array<{
|
||||
resolve: (value?: any) => void
|
||||
reject: (reason?: any) => void
|
||||
}> = []
|
||||
|
||||
/**
|
||||
* Purpose: getStoredAccessToken supports index.ts by holding or executing the get stored access token page or API role.
|
||||
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
|
||||
*/
|
||||
const getStoredAccessToken = (isAdmin = false) => {
|
||||
return isAdmin
|
||||
? (localStorage.getItem('adminAccessToken') || sessionStorage.getItem('adminAccessToken'))
|
||||
: (localStorage.getItem('accessToken') || sessionStorage.getItem('accessToken'))
|
||||
}
|
||||
|
||||
/**
|
||||
* Purpose: getStoredRefreshToken supports index.ts by holding or executing the get stored refresh token page or API role.
|
||||
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
|
||||
*/
|
||||
const getStoredRefreshToken = (isAdmin = false) => {
|
||||
return isAdmin
|
||||
? (localStorage.getItem('adminRefreshToken') || sessionStorage.getItem('adminRefreshToken'))
|
||||
: (localStorage.getItem('refreshToken') || sessionStorage.getItem('refreshToken'))
|
||||
}
|
||||
|
||||
/**
|
||||
* Purpose: getRequestToken supports index.ts by holding or executing the get request token page or API role.
|
||||
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
|
||||
*/
|
||||
const getRequestToken = (request: any) => {
|
||||
if (request?._authToken) {
|
||||
return request._authToken as string
|
||||
@@ -57,14 +85,26 @@ const getRequestToken = (request: any) => {
|
||||
return authHeader.startsWith('Bearer ') ? authHeader.slice('Bearer '.length) : null
|
||||
}
|
||||
|
||||
/**
|
||||
* Purpose: isCurrentStoredToken supports index.ts by holding or executing the is current stored token page or API role.
|
||||
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
|
||||
*/
|
||||
const isCurrentStoredToken = (token: string | null, isAdmin = false) => {
|
||||
return Boolean(token && token === getStoredAccessToken(isAdmin))
|
||||
}
|
||||
|
||||
/**
|
||||
* Purpose: getResponseMessage supports index.ts by holding or executing the get response message page or API role.
|
||||
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
|
||||
*/
|
||||
const getResponseMessage = (error: AxiosError) => {
|
||||
return (error.response?.data as any)?.message || error.message || '请求失败'
|
||||
}
|
||||
|
||||
/**
|
||||
* Purpose: isTokenExpiredResponse supports index.ts by holding or executing the is token expired response page or API role.
|
||||
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
|
||||
*/
|
||||
const isTokenExpiredResponse = (error: AxiosError, requestToken: string | null) => {
|
||||
const messageText = getResponseMessage(error)
|
||||
return Boolean(
|
||||
@@ -81,6 +121,10 @@ const isTokenExpiredResponse = (error: AxiosError, requestToken: string | null)
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Purpose: processQueue supports index.ts by holding or executing the process queue page or API role.
|
||||
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
|
||||
*/
|
||||
const processQueue = (error: any, token: string | null = null) => {
|
||||
failedQueue.forEach(prom => {
|
||||
if (error) {
|
||||
@@ -102,10 +146,18 @@ const showError = (msg: string) => {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Purpose: isAuthFreePath supports index.ts by holding or executing the is auth free path page or API role.
|
||||
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
|
||||
*/
|
||||
const isAuthFreePath = (url?: string) => {
|
||||
return isApiAuthFreePath(url)
|
||||
}
|
||||
|
||||
/**
|
||||
* Purpose: handleTokenExpired supports index.ts by holding or executing the handle token expired page or API role.
|
||||
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
|
||||
*/
|
||||
const handleTokenExpired = (isAdmin = false, failedToken: string | null = null) => {
|
||||
if (!isCurrentStoredToken(failedToken, isAdmin)) {
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user