File
Blob: src/shared/web/json.ts
| 1 | export type JsonPrimitive = string | number | boolean | null; |
| 2 | export type JsonValue = JsonPrimitive | JsonObject | JsonValue[]; |
| 3 | export type JsonObject = { [key: string]: JsonValue }; |
| 4 | |
| 5 | export function isJsonObject(value: JsonValue | null | undefined): value is JsonObject { |
| 6 | return typeof value === "object" && value !== null && !Array.isArray(value); |
| 7 | } |
| 8 | |
| 9 | export 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 | } |