@liujitcn/kratos-taro-app-core 0.0.64 → 0.0.65

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/utils/http.ts +19 -2
package/package.json CHANGED
@@ -7,7 +7,7 @@
7
7
  "type": "git",
8
8
  "url": "git@github.com:liujitcn/kratos-admin.git"
9
9
  },
10
- "version": "0.0.64",
10
+ "version": "0.0.65",
11
11
  "type": "module",
12
12
  "files": [
13
13
  "dist",
package/src/utils/http.ts CHANGED
@@ -56,6 +56,23 @@ export type AuthMode = 'none' | 'optional' | 'required'
56
56
  /** Taro 请求参数扩展。 */
57
57
  export type HttpRequestOptions = Taro.request.Option & { authMode?: AuthMode }
58
58
 
59
+ /** 认证缺失或失效错误:抛出前请求层已弹窗引导重新登录,页面侧无需重复提示。 */
60
+ export class AuthPromptError extends Error {
61
+ constructor() {
62
+ super(t('core.auth.session_expired'))
63
+ this.name = 'AuthPromptError'
64
+ }
65
+ }
66
+
67
+ /** 判断错误是否为登录态失效(请求层已弹窗提示,或响应为 401/403)。 */
68
+ export function isAuthExpiredError(error: unknown): boolean {
69
+ if (error && typeof error === 'object') {
70
+ const statusCode = (error as { statusCode?: number }).statusCode
71
+ if (statusCode === 401 || statusCode === 403) return true
72
+ }
73
+ return error instanceof AuthPromptError
74
+ }
75
+
59
76
  type ErrorData = {
60
77
  code?: string | number
61
78
  message?: string
@@ -179,7 +196,7 @@ async function getAccessTokenByMode(authMode: AuthMode): Promise<string> {
179
196
  if (!getToken()) {
180
197
  if (authMode === 'required') {
181
198
  await promptRelogin()
182
- throw new Error('auth required')
199
+ throw new AuthPromptError()
183
200
  }
184
201
  return ''
185
202
  }
@@ -193,7 +210,7 @@ async function getAccessTokenByMode(authMode: AuthMode): Promise<string> {
193
210
  if (hasValidToken()) return getToken()
194
211
  if (authMode === 'required') {
195
212
  await promptRelogin()
196
- throw new Error('auth expired')
213
+ throw new AuthPromptError()
197
214
  }
198
215
  silentClearAuthData()
199
216
  return ''