Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down
5 changes: 5 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof serve>;
Expand Down Expand Up @@ -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"), {
Expand Down
Loading