-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Replace `offset` parameter with `page` for pagination of results Instead of taking the offset directly from the user query, we can compute it by multiplying the page number with the `limit` parameter set in the query or by default. Not having a `page` parameter in the query is equivalent to `page=1`. * Fix `limit <= 0` and add missing tests * Add `parsePage` tests * Add metadata information to API responses The metadata contains information for the pagination of results. The following fields are available: - `next_page`, capped to `total_pages` on reaching the last page - `previous_page`, set to 1 on the first page - `total_pages`, computed from the `limit` parameter - `total_results` returned from `rows_before_limit_at_least` of ClickHouse's response (https://clickhouse.com/docs/en/interfaces/formats#json) * Make `limit` parameter default to `config.maxLimit` Resolves #1
- Loading branch information
Showing
11 changed files
with
150 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { expect, test } from "bun:test"; | ||
import { addMetadata } from "./utils.js"; | ||
|
||
test("addMetadata pagination", () => { | ||
const limit = 5; | ||
const mock_query_reponse = { | ||
data: Array(limit), | ||
rows: limit, | ||
rows_before_limit_at_least: 5*limit, // Simulate query with more total results than the query limit making pagination relevant | ||
}; | ||
|
||
const first_page = addMetadata(mock_query_reponse.data, mock_query_reponse.rows_before_limit_at_least, limit, 1); | ||
expect(first_page.meta.next_page).toBe(2); | ||
expect(first_page.meta.previous_page).toBe(1); // Previous page should be set to 1 on first page | ||
expect(first_page.meta.total_pages).toBe(5); | ||
expect(first_page.meta.total_results).toBe(5*limit); | ||
|
||
const odd_page = addMetadata(mock_query_reponse.data, mock_query_reponse.rows_before_limit_at_least, limit, 3); | ||
expect(odd_page.meta.next_page).toBe(4); | ||
expect(odd_page.meta.previous_page).toBe(2); | ||
expect(odd_page.meta.total_pages).toBe(5); | ||
expect(odd_page.meta.total_results).toBe(5*limit); | ||
|
||
const even_page = addMetadata(mock_query_reponse.data, mock_query_reponse.rows_before_limit_at_least, limit, 4); | ||
expect(even_page.meta.next_page).toBe(5); | ||
expect(even_page.meta.previous_page).toBe(3); | ||
expect(even_page.meta.total_pages).toBe(5); | ||
expect(even_page.meta.total_results).toBe(5*limit); | ||
|
||
const last_page = addMetadata(mock_query_reponse.data, mock_query_reponse.rows_before_limit_at_least, limit, 5); | ||
expect(last_page.meta.next_page).toBe(last_page.meta.total_pages); // Next page should be capped to total_pages on last page | ||
expect(last_page.meta.previous_page).toBe(4); | ||
expect(last_page.meta.total_pages).toBe(5); | ||
expect(last_page.meta.total_results).toBe(5*limit); | ||
|
||
// TODO: Expect error message on beyond last page | ||
// const beyond_last_page = addMetadata(mock_query_reponse.data, mock_query_reponse.rows_before_limit_at_least, limit, 6); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,16 @@ | ||
export function toJSON(data: any, status: number = 200) { | ||
return new Response(JSON.stringify(data), { status, headers: { "Content-Type": "application/json" } }); | ||
} | ||
|
||
export function addMetadata(data: any[], total_before_limit: number, limit: number, page: number) { | ||
// TODO: Catch page number greater than total_pages and return error | ||
return { | ||
data, | ||
meta: { | ||
"next_page": (page * limit >= total_before_limit) ? page : page + 1, | ||
"previous_page": (page <= 1) ? page : page - 1, | ||
"total_pages": Math.ceil(total_before_limit / limit), | ||
"total_results": total_before_limit | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,27 @@ | ||
import { expect, test } from "bun:test"; | ||
import { parseBlockId, parseTimestamp } from "./utils.js"; | ||
import { parseBlockId, parseLimit, parsePage, parseTimestamp } from "./utils.js"; | ||
import { config } from "./config.js"; | ||
|
||
test("parseBlockId", () => { | ||
expect(parseBlockId("0x123") as string).toBe("123"); | ||
}); | ||
|
||
test("parseLimit", () => { | ||
expect(parseLimit("1")).toBe(1); | ||
expect(parseLimit("0")).toBe(0); | ||
expect(parseLimit(10)).toBe(10); | ||
expect(parseLimit(config.maxLimit + 1)).toBe(config.maxLimit); | ||
}); | ||
|
||
test("parsePage", () => { | ||
expect(parsePage("1")).toBe(1); | ||
expect(parsePage("0")).toBe(1); | ||
expect(parsePage(10)).toBe(10); | ||
}); | ||
|
||
test("parseTimestamp", () => { | ||
expect(parseTimestamp("1697587100")).toBe(1697587100); | ||
expect(parseTimestamp("1697587100000")).toBe(1697587100); | ||
expect(parseTimestamp("awdawd")).toBeNaN(); | ||
expect(parseTimestamp(null)).toBeUndefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters