From f2fc40605eebdb56810829a876901d44c0035026 Mon Sep 17 00:00:00 2001 From: ouyangxq <775153912@qq.com> Date: Thu, 30 Jul 2026 20:34:08 +0800 Subject: [PATCH] 1 --- src/api/http.safe-integer.test.ts | 15 +++++++++++++++ src/api/http.ts | 2 ++ 2 files changed, 17 insertions(+) create mode 100644 src/api/http.safe-integer.test.ts diff --git a/src/api/http.safe-integer.test.ts b/src/api/http.safe-integer.test.ts new file mode 100644 index 00000000..b820c4e8 --- /dev/null +++ b/src/api/http.safe-integer.test.ts @@ -0,0 +1,15 @@ +import { describe, expect, it } from 'vitest' +import { parseApiJsonResponse, parseJsonWithSafeIntegers } from '@chongchong/shared' + +describe('safe integer API parsing', () => { + it('preserves backend long IDs as strings', () => { + expect(parseJsonWithSafeIntegers('{"id":2082674456475418625,"page":1}')).toEqual({ + id: '2082674456475418625', + page: 1, + }) + }) + + it('leaves non-JSON responses untouched', () => { + expect(parseApiJsonResponse('plain text')).toBe('plain text') + }) +}) diff --git a/src/api/http.ts b/src/api/http.ts index 87552097..81d14c6a 100644 --- a/src/api/http.ts +++ b/src/api/http.ts @@ -1,4 +1,5 @@ import axios, { type InternalAxiosRequestConfig } from 'axios' +import { parseApiJsonResponse } from '@chongchong/shared' export interface ApiEnvelope { code: string @@ -37,6 +38,7 @@ export interface ApiErrorDetails { export const http = axios.create({ baseURL: import.meta.env.VITE_API_BASE_URL || '/api', timeout: 15_000, + transformResponse: [(data: unknown) => parseApiJsonResponse(data)], }) type RetryableRequestConfig = InternalAxiosRequestConfig & { _authRetry?: boolean }