Skip to content
File

Blob: src/shared/web/json.ts

typescript18 lines
1export type JsonPrimitive = string | number | boolean | null;
2export type JsonValue = JsonPrimitive | JsonObject | JsonValue[];
3export type JsonObject = { [key: string]: JsonValue };
4 
5export function isJsonObject(value: JsonValue | null | undefined): value is JsonObject {
6 return typeof value === "object" && value !== null && !Array.isArray(value);
7}
8 
9export async function safeParseJsonRequest(request: Request): Promise<JsonValue | null> {
10 try {
11 const text = await request.text();
12 if (!text) return null;
13 return JSON.parse(text) as JsonValue;
14 } catch {
15 return null;
16 }
17}