From 7e6f6b1615eecbe0a4c6f89ec2ff8e05276958e4 Mon Sep 17 00:00:00 2001 From: Etienne Donneger Date: Fri, 19 Apr 2024 15:38:01 -0400 Subject: [PATCH] Make `limit` parameter default to `config.maxLimit` --- src/queries.spec.ts | 7 ++++--- src/queries.ts | 16 ++++++++-------- src/utils.spec.ts | 2 +- src/utils.ts | 6 +++--- 4 files changed, 16 insertions(+), 15 deletions(-) diff --git a/src/queries.spec.ts b/src/queries.spec.ts index 8887815..42e9210 100644 --- a/src/queries.spec.ts +++ b/src/queries.spec.ts @@ -6,6 +6,7 @@ import { getTransfers, addAmountFilter, } from "./queries.js"; +import { config } from "./config.js"; const contract = "eosio.token"; const account = "push.sx"; @@ -64,7 +65,7 @@ test("getTotalSupply", () => { ) ); expect(query).toContain(formatSQL(`ORDER BY block_number DESC`)); - expect(query).toContain(formatSQL(`LIMIT 1`)); + expect(query).toContain(formatSQL(`LIMIT ${config.maxLimit}`)); }); test("getTotalSupply with options", () => { @@ -98,7 +99,7 @@ test("getBalanceChange", () => { ) ); expect(query).toContain(formatSQL(`ORDER BY timestamp DESC`)); - expect(query).toContain(formatSQL(`LIMIT 1`)); + expect(query).toContain(formatSQL(`LIMIT ${config.maxLimit}`)); }); test("getBalanceChanges with options", () => { @@ -133,5 +134,5 @@ test("getTransfers", () => { ) ); expect(query).toContain(formatSQL(`ORDER BY timestamp DESC`)); - expect(query).toContain(formatSQL(`LIMIT 100`)); + expect(query).toContain(formatSQL(`LIMIT ${config.maxLimit}`)); }); \ No newline at end of file diff --git a/src/queries.ts b/src/queries.ts index 4f42daa..5ff3b65 100644 --- a/src/queries.ts +++ b/src/queries.ts @@ -1,4 +1,4 @@ -import { DEFAULT_SORT_BY } from "./config.js"; +import { DEFAULT_SORT_BY, config } from "./config.js"; import { parseLimit, parsePage, parseTimestamp } from "./utils.js"; // For reference on Clickhouse Database tables: @@ -82,9 +82,9 @@ export function getTotalSupply(searchParams: URLSearchParams, example?: boolean) query += ` ORDER BY block_number ${sort_by ?? DEFAULT_SORT_BY} `; } - const limit = parseLimit(searchParams.get("limit")); - query += ` LIMIT ${limit} `; - + const limit = parseLimit(searchParams.get("limit"), config.maxLimit); + if (limit) query += ` LIMIT ${limit}`; + const page = parsePage(searchParams.get("page")); if (page) query += ` OFFSET ${limit * (page - 1)} `; @@ -115,8 +115,8 @@ export function getBalanceChanges(searchParams: URLSearchParams, example?: boole //if (contract && !account) query += `GROUP BY (contract, account) ORDER BY timestamp DESC`; } - const limit = parseLimit(searchParams.get("limit")); - query += ` LIMIT ${limit} `; + const limit = parseLimit(searchParams.get("limit"), config.maxLimit); + if (limit) query += ` LIMIT ${limit}`; const page = parsePage(searchParams.get("page")); if (page) query += ` OFFSET ${limit * (page - 1)} `; @@ -154,8 +154,8 @@ export function getTransfers(searchParams: URLSearchParams, example?: boolean) { query += ` ORDER BY timestamp DESC`; } - const limit = parseLimit(searchParams.get("limit"), 100); - query += ` LIMIT ${limit}`; + const limit = parseLimit(searchParams.get("limit"), config.maxLimit); + if (limit) query += ` LIMIT ${limit}`; const page = parsePage(searchParams.get("page")); if (page) query += ` OFFSET ${limit * (page - 1)} `; diff --git a/src/utils.spec.ts b/src/utils.spec.ts index 5b6911d..9d2f6e4 100644 --- a/src/utils.spec.ts +++ b/src/utils.spec.ts @@ -8,7 +8,7 @@ test("parseBlockId", () => { test("parseLimit", () => { expect(parseLimit("1")).toBe(1); - expect(parseLimit("0")).toBe(1); + expect(parseLimit("0")).toBe(0); expect(parseLimit(10)).toBe(10); expect(parseLimit(config.maxLimit + 1)).toBe(config.maxLimit); }); diff --git a/src/utils.ts b/src/utils.ts index 6b33936..667df3d 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -5,15 +5,15 @@ export function parseBlockId(block_id?: string | null) { } export function parseLimit(limit?: string | null | number, defaultLimit?: number) { - let value = 1 // default 1 + let value = 0; // default 0 (no limit) if (defaultLimit) value = defaultLimit; if (limit) { if (typeof limit === "string") value = parseInt(limit); if (typeof limit === "number") value = limit; } - // limit must be between 1 and maxLimit - if (value <= 0) value = 1; + // limit must be between 0 (no limit) and maxLimit + if (value < 0) value = 0; if (value > config.maxLimit) value = config.maxLimit; return value; }