feat: complete admin API integration

This commit is contained in:
2026-07-31 13:43:16 +08:00
parent bb09c7d6a0
commit d0f94d95d7
24 changed files with 1834 additions and 113 deletions
+42 -2
View File
@@ -1,6 +1,21 @@
import axios, { AxiosHeaders } from 'axios'
import { describe, expect, it } from 'vitest'
import { ApiResponseError, getApiErrorDetails, unwrapApiResponse } from './http'
import { afterEach, describe, expect, it, vi } from 'vitest'
import {
ApiResponseError,
getApiErrorDetails,
http,
registerAuthRefreshHandler,
registerAuthSessionExpiredHandler,
unwrapApiResponse,
} from './http'
const originalAdapter = http.defaults.adapter
afterEach(() => {
http.defaults.adapter = originalAdapter
registerAuthRefreshHandler(async () => { throw new Error('refresh handler is not configured') })
registerAuthSessionExpiredHandler(() => undefined)
})
describe('API error diagnostics', () => {
it('keeps business request and trace identifiers when unwrapping fails', () => {
@@ -40,4 +55,29 @@ describe('API error diagnostics', () => {
traceId: 'trace-2',
})
})
it('notifies session expiry once when concurrent 401 requests share a failed refresh', async () => {
const refresh = vi.fn(async () => { throw new Error('refresh token expired') })
const expired = vi.fn()
registerAuthRefreshHandler(refresh)
registerAuthSessionExpiredHandler(expired)
http.defaults.adapter = async (config) => {
throw new axios.AxiosError('Unauthorized', 'ERR_BAD_REQUEST', config, undefined, {
status: 401,
statusText: 'Unauthorized',
headers: {},
config,
data: { code: 'A0401', message: '登录状态已过期' },
})
}
const results = await Promise.allSettled([
http.get('/platform/v1/orders'),
http.get('/platform/v1/warehouses'),
])
expect(results.every((result) => result.status === 'rejected')).toBe(true)
expect(refresh).toHaveBeenCalledTimes(1)
expect(expired).toHaveBeenCalledTimes(1)
})
})