Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change default limit for results to 1 #17

Merged
merged 2 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/fetch/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const timestampExamplesArrayFilter = ["greater_or_equals_by_timestamp", "greater
const blockExamplesArrayFilter = ["greater_or_equals_by_block", "greater_by_block", "less_or_equals_by_block", "less_by_block"];
const amountExamplesArrayFilter = ["amount_greater_or_equals", "amount_greater", "amount_less_or_equals", "amount_less"];

const supply_example = await makeQuery(getTotalSupply(new URLSearchParams({ limit: "2" }), true)).then(res => addMetadata(res, 2, 1));
const supply_example = await makeQuery(getTotalSupply(new URLSearchParams({ limit: "1" }), true)).then(res => addMetadata(res, 1, 1));
const balance_example = await makeQuery(getBalanceChanges(new URLSearchParams({ limit: "2" }), true)).then(res => addMetadata(res, 2, 1));
const transfers_example = await makeQuery(getTransfers(new URLSearchParams({ limit: "5" }), true)).then(res => addMetadata(res, 5, 1));

Expand Down
9 changes: 4 additions & 5 deletions src/queries.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
getTransfers,
addAmountFilter,
} from "./queries.js";
import { config } from "./config.js";

const contract = "eosio.token";
const account = "push.sx";
Expand Down Expand Up @@ -65,7 +64,7 @@ test("getTotalSupply", () => {
)
);
expect(query).toContain(formatSQL(`ORDER BY block_number DESC`));
expect(query).toContain(formatSQL(`LIMIT ${config.maxLimit}`));
expect(query).toContain(formatSQL(`LIMIT 1`));
});

test("getTotalSupply with options", () => {
Expand Down Expand Up @@ -99,7 +98,7 @@ test("getBalanceChange", () => {
)
);
expect(query).toContain(formatSQL(`ORDER BY timestamp DESC`));
expect(query).toContain(formatSQL(`LIMIT ${config.maxLimit}`));
expect(query).toContain(formatSQL(`LIMIT 1`));
});

test("getBalanceChanges with options", () => {
Expand Down Expand Up @@ -133,6 +132,6 @@ test("getTransfers", () => {
AND from == '${account}' AND to == '${account}' AND transaction == '${transaction_id}')`
)
);
expect(query).toContain(formatSQL(`ORDER BY timestamp DESC`));
expect(query).toContain(formatSQL(`LIMIT ${config.maxLimit}`));
expect(query).toContain(formatSQL(`ORDER BY block_num DESC`));
expect(query).toContain(formatSQL(`LIMIT 1`));
});
13 changes: 6 additions & 7 deletions src/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export function getTotalSupply(searchParams: URLSearchParams, example?: boolean)
query += ` ORDER BY block_number ${sort_by ?? DEFAULT_SORT_BY} `;
}

const limit = parseLimit(searchParams.get("limit"), config.maxLimit);
const limit = parseLimit(searchParams.get("limit"));
if (limit) query += ` LIMIT ${limit}`;

const page = parsePage(searchParams.get("page"));
Expand Down Expand Up @@ -115,7 +115,7 @@ 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"), config.maxLimit);
const limit = parseLimit(searchParams.get("limit"));
if (limit) query += ` LIMIT ${limit}`;

const page = parsePage(searchParams.get("page"));
Expand All @@ -132,10 +132,9 @@ export function getTransfers(searchParams: URLSearchParams, example?: boolean) {

let query = "SELECT * FROM ";

if (searchParams.get("greater_or_equals_by_block") && searchParams.get("less_or_equals_by_block")) query += "transfers_block_num"
else if (from && !to) query += "transfers_from"
if (from && !to) query += "transfers_from"
else if (!from && to) query += "transfers_to"
else query += "transfer_events"
else query += "transfers_block_num"

if (!example) {
// WHERE statements
Expand All @@ -151,10 +150,10 @@ export function getTransfers(searchParams: URLSearchParams, example?: boolean) {

if (where.length) query += ` WHERE(${where.join(' AND ')})`;

query += ` ORDER BY timestamp DESC`;
query += ` ORDER BY block_num DESC`;
}

const limit = parseLimit(searchParams.get("limit"), config.maxLimit);
const limit = parseLimit(searchParams.get("limit"));
if (limit) query += ` LIMIT ${limit}`;

const page = parsePage(searchParams.get("page"));
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(0);
expect(parseLimit("0")).toBe(1);
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 = 0; // default 0 (no limit)
let value = 1; // default 1
if (defaultLimit)
value = defaultLimit;
if (limit) {
if (typeof limit === "string") value = parseInt(limit);
if (typeof limit === "number") value = limit;
}
// limit must be between 0 (no limit) and maxLimit
if (value < 0) value = 0;
// limit must be between 1 and maxLimit
if (value <= 0) value = 1;
if (value > config.maxLimit) value = config.maxLimit;
return value;
}
Expand Down
Loading