Skip to content

Commit

Permalink
Make limit parameter default to config.maxLimit
Browse files Browse the repository at this point in the history
  • Loading branch information
0237h committed Apr 19, 2024
1 parent b507988 commit 7e6f6b1
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 15 deletions.
7 changes: 4 additions & 3 deletions src/queries.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
getTransfers,
addAmountFilter,
} from "./queries.js";
import { config } from "./config.js";

const contract = "eosio.token";
const account = "push.sx";
Expand Down Expand Up @@ -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", () => {
Expand Down Expand Up @@ -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", () => {
Expand Down Expand Up @@ -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}`));
});
16 changes: 8 additions & 8 deletions src/queries.ts
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -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)} `;

Expand Down Expand Up @@ -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)} `;
Expand Down Expand Up @@ -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)} `;
Expand Down
2 changes: 1 addition & 1 deletion src/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down
6 changes: 3 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 7e6f6b1

Please sign in to comment.