DataApi: automatically switch to POST if the URL is too long

This commit is contained in:
dswbx
2025-01-16 17:18:30 +01:00
parent c6cbd36231
commit 69ea5a00ee
3 changed files with 33 additions and 5 deletions

View File

@@ -0,0 +1,16 @@
import { describe, expect, it } from "bun:test";
import { DataApi } from "../../src/data/api/DataApi";
describe("DataApi", () => {
it("should switch to post for long url reads", async () => {
const api = new DataApi();
const get = api.readMany("a".repeat(100), { select: ["id", "name"] });
expect(get.request.method).toBe("GET");
expect(new URL(get.request.url).pathname).toBe(`/api/data/${"a".repeat(100)}`);
const post = api.readMany("a".repeat(1000), { select: ["id", "name"] });
expect(post.request.method).toBe("POST");
expect(new URL(post.request.url).pathname).toBe(`/api/data/${"a".repeat(1000)}/query`);
});
});

View File

@@ -28,6 +28,8 @@ describe("ModuleApi", () => {
it("fetches endpoint", async () => {
const app = new Hono().get("/endpoint", (c) => c.json({ foo: "bar" }));
const api = new Api({ host });
// @ts-expect-error it's protected
api.fetcher = app.request as typeof fetch;
const res = await api.get("/endpoint");
@@ -40,6 +42,8 @@ describe("ModuleApi", () => {
it("has accessible request", async () => {
const app = new Hono().get("/endpoint", (c) => c.json({ foo: "bar" }));
const api = new Api({ host });
// @ts-expect-error it's protected
api.fetcher = app.request as typeof fetch;
const promise = api.get("/endpoint");