spawn/packages/shared/src/parse.ts
L 2da9e6cd46
refactor: restore @openrouter/spawn-shared workspace package (#2405)
* refactor: restore @openrouter/spawn-shared workspace package

Restore packages/shared/ as canonical location for parse.ts, result.ts,
and type-guards.ts. CLI shared files become thin re-exports, preserving
all existing import paths. SPA imports switch from fragile relative paths
to the workspace package.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* fix: sort exports in shared package barrel to satisfy biome

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* fix: sort SPA imports to satisfy biome organizeImports

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-09 17:14:26 -07:00

35 lines
959 B
TypeScript

// shared/parse.ts — Schema-validated JSON parsing (replaces unsafe `as` casts)
import * as v from "valibot";
/**
* Parse a JSON string and validate it against a valibot schema.
* Returns the validated value, or null if parsing/validation fails.
*/
export function parseJsonWith<T extends v.BaseSchema<unknown, unknown, v.BaseIssue<unknown>>>(
text: string,
schema: T,
): v.InferOutput<T> | null {
try {
return v.parse(schema, JSON.parse(text));
} catch {
return null;
}
}
/**
* Parse a JSON string and return it as a Record<string, unknown> or null.
* Rejects non-object results (arrays, primitives).
* Use for API responses that are always a JSON object.
*/
export function parseJsonObj(text: string): Record<string, unknown> | null {
try {
const val = JSON.parse(text);
if (val !== null && typeof val === "object" && !Array.isArray(val)) {
return val;
}
return null;
} catch {
return null;
}
}