Reorganize repo structure and standards references
This commit is contained in:
1 parent
3cb93a81be
commit
be2054274d
5885 files changed
+2588
-195
No files matched your search
@@ -0,0 +1,43 @@
|
||||
import { describe, expect, test } from "vitest";
|
||||
import { ROWS_PAGINATION_ERROR_MESSAGE, parseRowsPaginationFromQuery } from "./rows.js";
|
||||
|
||||
function parseQuery(values: Record<string, string | undefined>) {
|
||||
return parseRowsPaginationFromQuery((name) => values[name]);
|
||||
}
|
||||
|
||||
const invalidResult = { ok: false, error: ROWS_PAGINATION_ERROR_MESSAGE };
|
||||
|
||||
describe("row pagination parser", () => {
|
||||
test("uses default pagination when query parameters are absent", () => {
|
||||
expect(parseQuery({})).toEqual({ ok: true, value: { page: 1, pageSize: 100 } });
|
||||
});
|
||||
|
||||
test("accepts explicit positive integer pagination values", () => {
|
||||
expect(parseQuery({ page: "2", pageSize: "25" })).toEqual({ ok: true, value: { page: 2, pageSize: 25 } });
|
||||
expect(parseQuery({ page: "1000000", pageSize: "100" })).toEqual({ ok: true, value: { page: 1000000, pageSize: 100 } });
|
||||
});
|
||||
|
||||
test("rejects non-digit and otherwise untrusted query values without coercion", () => {
|
||||
for (const value of ["abc", "1.5", "+1", "-1", " 1", "", "Infinity"]) {
|
||||
expect(parseQuery({ page: value })).toEqual(invalidResult);
|
||||
expect(parseQuery({ pageSize: value })).toEqual(invalidResult);
|
||||
}
|
||||
});
|
||||
|
||||
test("rejects zero and negative values", () => {
|
||||
expect(parseQuery({ page: "0" })).toEqual(invalidResult);
|
||||
expect(parseQuery({ pageSize: "0" })).toEqual(invalidResult);
|
||||
expect(parseQuery({ page: "-1" })).toEqual(invalidResult);
|
||||
expect(parseQuery({ pageSize: "-1" })).toEqual(invalidResult);
|
||||
});
|
||||
|
||||
test("rejects unsafe and out-of-range page values", () => {
|
||||
expect(parseQuery({ page: "9007199254740992" })).toEqual(invalidResult);
|
||||
expect(parseQuery({ page: "1000001" })).toEqual(invalidResult);
|
||||
});
|
||||
|
||||
test("rejects oversized pageSize values", () => {
|
||||
expect(parseQuery({ pageSize: "101" })).toEqual(invalidResult);
|
||||
expect(parseQuery({ pageSize: "9007199254740992" })).toEqual(invalidResult);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,38 @@
|
||||
export type RowsPagination = {
|
||||
page: number;
|
||||
pageSize: number;
|
||||
};
|
||||
|
||||
export type RowsPaginationResult = { ok: true; value: RowsPagination } | { ok: false; error: string };
|
||||
|
||||
export type RowsPaginationQueryReader = (name: string) => string | undefined;
|
||||
|
||||
export const ROWS_PAGINATION_DEFAULT_PAGE = 1;
|
||||
export const ROWS_PAGINATION_DEFAULT_PAGE_SIZE = 100;
|
||||
export const ROWS_PAGINATION_MAX_PAGE = 1000000;
|
||||
export const ROWS_PAGINATION_MAX_PAGE_SIZE = 100;
|
||||
export const ROWS_PAGINATION_ERROR_MESSAGE = "page and pageSize must be positive integers; pageSize must be at most 100.";
|
||||
|
||||
export function parseRowsPaginationFromQuery(query: RowsPaginationQueryReader): RowsPaginationResult {
|
||||
const page = parseBoundedInteger(query("page"), ROWS_PAGINATION_DEFAULT_PAGE, 1, ROWS_PAGINATION_MAX_PAGE);
|
||||
const pageSize = parseBoundedInteger(query("pageSize"), ROWS_PAGINATION_DEFAULT_PAGE_SIZE, 1, ROWS_PAGINATION_MAX_PAGE_SIZE);
|
||||
|
||||
if (page === null || pageSize === null) {
|
||||
return { ok: false, error: ROWS_PAGINATION_ERROR_MESSAGE };
|
||||
}
|
||||
|
||||
return { ok: true, value: { page, pageSize } };
|
||||
}
|
||||
|
||||
function parseBoundedInteger(value: string | undefined, defaultValue: number, min: number, max: number) {
|
||||
if (value === undefined) {
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
if (!/^\d+$/.test(value)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const parsed = Number(value);
|
||||
return Number.isSafeInteger(parsed) && parsed >= min && parsed <= max ? parsed : null;
|
||||
}
|
||||
Reference in new issue
Block a user