Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/jin-frame/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jin-frame",
"version": "5.0.1",
"version": "5.0.2",
"description": "Reusable HTTP API request definition library",
"packageManager": "pnpm@10.16.0",
"scripts": {
Expand Down
36 changes: 36 additions & 0 deletions packages/jin-frame/src/tools/json/safeParse.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { describe, expect, it } from 'vitest';
import { safeParse } from '#tools/json/safeParse';

describe('safeParse', () => {
it('should parse JSON object when valid JSON string provided', () => {
expect(safeParse('{"key":"value"}')).toEqual({ key: 'value' });
});

it('should parse JSON array when valid JSON array string provided', () => {
expect(safeParse('[1,2,3]')).toEqual([1, 2, 3]);
});

it('should parse boolean true when JSON boolean string provided', () => {
expect(safeParse('true')).toBe(true);
});

it('should parse boolean false when JSON boolean string provided', () => {
expect(safeParse('false')).toBe(false);
});

it('should parse number when JSON number string provided', () => {
expect(safeParse('42')).toBe(42);
});

it('should parse null when JSON null string provided', () => {
expect(safeParse('null')).toBeNull();
});

it('should return raw string when plain non-JSON string provided', () => {
expect(safeParse('hello')).toBe('hello');
});

it('should return raw string when API returns plain text response', () => {
expect(safeParse('plain text response')).toBe('plain text response');
});
});
3 changes: 2 additions & 1 deletion packages/jin-frame/src/tools/json/safeParse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export function safeParse<T = unknown>(value: string): T | undefined {
try {
return JSON.parse(value) as T;
} catch {
return undefined;
// Return raw string for plain-text responses (e.g. API returns "hello" without JSON encoding)
return value as unknown as T;
}
}
Loading