mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-17 04:46:05 +00:00
DataApi: automatically switch to POST if the URL is too long
This commit is contained in:
16
app/__test__/api/DataApi.spec.ts
Normal file
16
app/__test__/api/DataApi.spec.ts
Normal 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`);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -28,6 +28,8 @@ describe("ModuleApi", () => {
|
|||||||
it("fetches endpoint", async () => {
|
it("fetches endpoint", async () => {
|
||||||
const app = new Hono().get("/endpoint", (c) => c.json({ foo: "bar" }));
|
const app = new Hono().get("/endpoint", (c) => c.json({ foo: "bar" }));
|
||||||
const api = new Api({ host });
|
const api = new Api({ host });
|
||||||
|
|
||||||
|
// @ts-expect-error it's protected
|
||||||
api.fetcher = app.request as typeof fetch;
|
api.fetcher = app.request as typeof fetch;
|
||||||
|
|
||||||
const res = await api.get("/endpoint");
|
const res = await api.get("/endpoint");
|
||||||
@@ -40,6 +42,8 @@ describe("ModuleApi", () => {
|
|||||||
it("has accessible request", async () => {
|
it("has accessible request", async () => {
|
||||||
const app = new Hono().get("/endpoint", (c) => c.json({ foo: "bar" }));
|
const app = new Hono().get("/endpoint", (c) => c.json({ foo: "bar" }));
|
||||||
const api = new Api({ host });
|
const api = new Api({ host });
|
||||||
|
|
||||||
|
// @ts-expect-error it's protected
|
||||||
api.fetcher = app.request as typeof fetch;
|
api.fetcher = app.request as typeof fetch;
|
||||||
|
|
||||||
const promise = api.get("/endpoint");
|
const promise = api.get("/endpoint");
|
||||||
|
|||||||
@@ -3,13 +3,15 @@ import type { EntityData, RepoQuery, RepoQueryIn, RepositoryResponse } from "dat
|
|||||||
import { type BaseModuleApiOptions, ModuleApi, type PrimaryFieldType } from "modules";
|
import { type BaseModuleApiOptions, ModuleApi, type PrimaryFieldType } from "modules";
|
||||||
|
|
||||||
export type DataApiOptions = BaseModuleApiOptions & {
|
export type DataApiOptions = BaseModuleApiOptions & {
|
||||||
defaultQuery?: Partial<RepoQuery>;
|
queryLengthLimit: number;
|
||||||
|
defaultQuery: Partial<RepoQuery>;
|
||||||
};
|
};
|
||||||
|
|
||||||
export class DataApi extends ModuleApi<DataApiOptions> {
|
export class DataApi extends ModuleApi<DataApiOptions> {
|
||||||
protected override getDefaultOptions(): Partial<DataApiOptions> {
|
protected override getDefaultOptions(): Partial<DataApiOptions> {
|
||||||
return {
|
return {
|
||||||
basepath: "/api/data",
|
basepath: "/api/data",
|
||||||
|
queryLengthLimit: 1000,
|
||||||
defaultQuery: {
|
defaultQuery: {
|
||||||
limit: 10
|
limit: 10
|
||||||
}
|
}
|
||||||
@@ -28,10 +30,16 @@ export class DataApi extends ModuleApi<DataApiOptions> {
|
|||||||
entity: E,
|
entity: E,
|
||||||
query: RepoQueryIn = {}
|
query: RepoQueryIn = {}
|
||||||
) {
|
) {
|
||||||
return this.get<Pick<RepositoryResponse<Data[]>, "meta" | "data">>(
|
type T = Pick<RepositoryResponse<Data[]>, "meta" | "data">;
|
||||||
[entity as any],
|
|
||||||
query ?? this.options.defaultQuery
|
const input = query ?? this.options.defaultQuery;
|
||||||
);
|
const exceeds = JSON.stringify([entity, input]).length > this.options.queryLengthLimit;
|
||||||
|
|
||||||
|
if (exceeds) {
|
||||||
|
return this.post<T>([entity as any, "query"], input);
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.get<T>([entity as any], input);
|
||||||
}
|
}
|
||||||
|
|
||||||
readManyByReference<
|
readManyByReference<
|
||||||
|
|||||||
Reference in New Issue
Block a user