-
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.
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)
- Loading branch information
Showing
6 changed files
with
94 additions
and
9 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
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 | ||
} | ||
} | ||
} |