v3: code quality pass, various bug fixes
This commit is contained in:
22
api/lib/pagination.ts
Normal file
22
api/lib/pagination.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
/**
|
||||
* Parses page/limit query parameters with sensible defaults and bounds.
|
||||
* page: clamped to [1, ∞)
|
||||
* limit: clamped to [1, 100], defaults to 20
|
||||
*/
|
||||
export function parsePagination(
|
||||
params: URLSearchParams,
|
||||
defaultLimit = 20,
|
||||
): { page: number; limit: number } {
|
||||
const page = Math.max(
|
||||
1,
|
||||
parseInt(params.get("page") ?? "1") || 1,
|
||||
);
|
||||
const limit = Math.min(
|
||||
Math.max(
|
||||
1,
|
||||
parseInt(params.get("limit") ?? String(defaultLimit)) || defaultLimit,
|
||||
),
|
||||
100,
|
||||
);
|
||||
return { page, limit };
|
||||
}
|
||||
Reference in New Issue
Block a user