feat:统一前端请求层错误回显与长整型解析
This commit is contained in:
21
frontend/src/api/json.ts
Normal file
21
frontend/src/api/json.ts
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user