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
6 changes: 3 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ export function isJSONSerializable(value: any): boolean {
if (Array.isArray(value)) {
return true;
}
if (value.buffer) {
return false;
}
// `FormData` and `URLSearchParams` should't have a `toJSON` method,
// but Bun adds it, which is non-standard.
if (value instanceof FormData || value instanceof URLSearchParams) {
return false;
}
if (value.buffer) {
return false;
}
return (
(value.constructor && value.constructor.name === "Object") ||
typeof value.toJSON === "function"
Expand Down
45 changes: 45 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 @@ -525,3 +526,47 @@ describe("ofetch", () => {
});
});
});

describe("isJSONSerializable", () => {
it("returns false for FormData (non-empty)", () => {
const fd = new FormData();
fd.append("key", "value");
expect(isJSONSerializable(fd)).toBe(false);
});

it("returns false for empty FormData", () => {
expect(isJSONSerializable(new FormData())).toBe(false);
});

it("returns false for URLSearchParams (non-empty)", () => {
expect(isJSONSerializable(new URLSearchParams({ foo: "bar" }))).toBe(false);
});

it("returns false for empty URLSearchParams", () => {
expect(isJSONSerializable(new URLSearchParams())).toBe(false);
});

it("returns false for ArrayBuffer", () => {
expect(isJSONSerializable(new ArrayBuffer(8))).toBe(false);
});

it("returns false for Uint8Array", () => {
expect(isJSONSerializable(new Uint8Array([1, 2, 3]))).toBe(false);
});

it("returns true for plain objects", () => {
expect(isJSONSerializable({ a: 1 })).toBe(true);
});

it("returns true for arrays", () => {
expect(isJSONSerializable([1, 2, 3])).toBe(true);
});

it("returns true for objects with toJSON method", () => {
expect(isJSONSerializable({ toJSON: () => ({}) })).toBe(true);
});

it("returns false for undefined", () => {
expect(isJSONSerializable(undefined)).toBe(false);
});
});