feat:统一前端请求层错误回显与长整型解析

This commit is contained in:
zhiye.sun
2026-05-21 13:10:01 +08:00
parent 91e6d5bdd3
commit 1ada88c02a
9 changed files with 2602 additions and 1 deletions

21
frontend/src/api/json.ts Normal file
View File

@@ -0,0 +1,21 @@
import JSONBig from 'json-bigint';
const jsonParser = JSONBig({
storeAsString: true,
useNativeBigInt: false,
});
function looksLikeJsonPayload(data: string) {
const trimmed = data.trim();
return trimmed.startsWith('{') || trimmed.startsWith('[');
}
export function parseJsonPreservingLong<T>(data: unknown, contentType?: string): T | unknown {
if (typeof data !== 'string' || !looksLikeJsonPayload(data)) {
return data;
}
if (contentType && !contentType.toLowerCase().includes('application/json')) {
return data;
}
return jsonParser.parse(data) as T;
}