This commit is contained in:
2026-07-30 21:23:06 +08:00
parent 4e24d381c2
commit 4cef53e842
23 changed files with 493 additions and 54 deletions
+8 -3
View File
@@ -1,10 +1,12 @@
import axios, { AxiosError, type AxiosRequestConfig, type InternalAxiosRequestConfig } from 'axios'
import { parseApiJsonResponse } from '@chongchong/shared'
import { clearSessionTokens, getAccessToken, getRefreshToken, persistTokenPair, type TokenPair } from '@/api/session'
import { ApiError, asFieldErrors, isApiEnvelope, type ApiEnvelope } from '@/api/types'
const transport = axios.create({
baseURL: import.meta.env.VITE_API_BASE_URL || '/api',
timeout: 15_000,
transformResponse: [(data: unknown) => parseApiJsonResponse(data)],
})
interface RetryableRequest extends InternalAxiosRequestConfig {
@@ -79,9 +81,12 @@ transport.interceptors.response.use(
request.headers.Authorization = `Bearer ${tokens.accessToken}`
return await transport.request(request)
} catch (refreshError) {
clearSessionTokens()
window.dispatchEvent(new CustomEvent('cc-client-session-expired'))
throw mapError(refreshError)
const mappedError = mapError(refreshError)
if (mappedError.status === 401 || mappedError.code === 'A0401') {
clearSessionTokens()
window.dispatchEvent(new CustomEvent('cc-client-session-expired'))
}
throw mappedError
}
},
)