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") { 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"), {