mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-16 04:27:21 +00:00
refactor for better precision
This commit is contained in:
@@ -5,9 +5,9 @@ describe("DataApi", () => {
|
|||||||
it("should switch to post for long url reads", async () => {
|
it("should switch to post for long url reads", async () => {
|
||||||
const api = new DataApi();
|
const api = new DataApi();
|
||||||
|
|
||||||
const get = api.readMany("a".repeat(100), { select: ["id", "name"] });
|
const get = api.readMany("a".repeat(300), { select: ["id", "name"] });
|
||||||
expect(get.request.method).toBe("GET");
|
expect(get.request.method).toBe("GET");
|
||||||
expect(new URL(get.request.url).pathname).toBe(`/api/data/${"a".repeat(100)}`);
|
expect(new URL(get.request.url).pathname).toBe(`/api/data/${"a".repeat(300)}`);
|
||||||
|
|
||||||
const post = api.readMany("a".repeat(1000), { select: ["id", "name"] });
|
const post = api.readMany("a".repeat(1000), { select: ["id", "name"] });
|
||||||
expect(post.request.method).toBe("POST");
|
expect(post.request.method).toBe("POST");
|
||||||
|
|||||||
@@ -33,13 +33,13 @@ export class DataApi extends ModuleApi<DataApiOptions> {
|
|||||||
type T = Pick<RepositoryResponse<Data[]>, "meta" | "data">;
|
type T = Pick<RepositoryResponse<Data[]>, "meta" | "data">;
|
||||||
|
|
||||||
const input = query ?? this.options.defaultQuery;
|
const input = query ?? this.options.defaultQuery;
|
||||||
const exceeds = JSON.stringify([entity, input]).length > this.options.queryLengthLimit;
|
const req = this.get<T>([entity as any], input);
|
||||||
|
|
||||||
if (exceeds) {
|
if (req.request.url.length <= this.options.queryLengthLimit) {
|
||||||
return this.post<T>([entity as any, "query"], input);
|
return req;
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.get<T>([entity as any], input);
|
return this.post<T>([entity as any, "query"], input);
|
||||||
}
|
}
|
||||||
|
|
||||||
readManyByReference<
|
readManyByReference<
|
||||||
|
|||||||
Reference in New Issue
Block a user