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
}
},
)
+26
View File
@@ -0,0 +1,26 @@
import assert from 'node:assert/strict'
import { readFile } from 'node:fs/promises'
import test from 'node:test'
import ts from 'typescript'
const source = await readFile(new URL('../../../../packages/shared/src/index.ts', import.meta.url), 'utf8')
const compiled = ts.transpileModule(source, {
compilerOptions: { module: ts.ModuleKind.ESNext, target: ts.ScriptTarget.ES2022 },
}).outputText
const parser = await import(`data:text/javascript;base64,${Buffer.from(compiled).toString('base64')}`)
test('preserves integers outside the JavaScript safe range as strings', () => {
const value = parser.parseJsonWithSafeIntegers('{"id":2082674456475418625,"nested":[-2082674456475418625]}')
assert.deepEqual(value, { id: '2082674456475418625', nested: ['-2082674456475418625'] })
})
test('keeps safe integers and decimals numeric without changing numeric strings', () => {
const value = parser.parseJsonWithSafeIntegers('{"page":1,"weight":12.5,"id":"2082674456475418625"}')
assert.deepEqual(value, { page: 1, weight: 12.5, id: '2082674456475418625' })
})
test('leaves non-JSON and binary response values untouched', () => {
assert.equal(parser.parseApiJsonResponse('plain text'), 'plain text')
const binary = new Uint8Array([1, 2, 3])
assert.equal(parser.parseApiJsonResponse(binary), binary)
})