From 0bc87161cc71aceb2e2f3b5b1a9f770dba2c0560 Mon Sep 17 00:00:00 2001 From: EduardF1 <50618110+EduardF1@users.noreply.github.com> Date: Mon, 4 May 2026 23:56:21 +0200 Subject: [PATCH 1/2] fix: correct isJSONSerializable to handle null without throwing --- src/utils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils.ts b/src/utils.ts index a773431b..408b346e 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -19,7 +19,7 @@ export function isJSONSerializable(value: any): boolean { return false; } const t = typeof value; - if (t === "string" || t === "number" || t === "boolean" || t === null) { + if (t === "string" || t === "number" || t === "boolean" || value === null) { return true; } if (t !== "object") { From a6cf69a7b1371ff4c0b6d0d96cea92886c98758f Mon Sep 17 00:00:00 2001 From: EduardF1 <50618110+EduardF1@users.noreply.github.com> Date: Tue, 30 Jun 2026 14:50:08 +0200 Subject: [PATCH 2/2] test: cover null JSON serialization --- test/index.test.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/index.test.ts b/test/index.test.ts index 5ac20b07..5ac7c4d1 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -10,6 +10,7 @@ import { import { Readable } from "node:stream"; import { H3, HTTPError, readBody, serve } from "h3"; import { $fetch } from "../src/index.ts"; +import { isJSONSerializable } from "../src/utils.ts"; describe("ofetch", () => { let listener: ReturnType; @@ -158,6 +159,10 @@ describe("ofetch", () => { } }); + it("treats null as JSON serializable", () => { + expect(isJSONSerializable(null)).toBe(true); // eslint-disable-line unicorn/no-null + }); + it("does not stringify body when content type != application/json", async () => { const message = '"Hallo von Pascal"'; const { body } = await $fetch(getURL("echo"), {