initial commit
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
28
src/utils/pagination.ts
Normal file
28
src/utils/pagination.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import { PaginationQuery, PaginationMeta } from '../types';
|
||||
|
||||
export function parsePagination(query: Record<string, unknown>): {
|
||||
page: number;
|
||||
limit: number;
|
||||
skip: number;
|
||||
sort: string;
|
||||
order: 'asc' | 'desc';
|
||||
search: string;
|
||||
} {
|
||||
const page = Math.max(1, parseInt(String(query.page || '1'), 10) || 1);
|
||||
const limit = Math.min(100, Math.max(1, parseInt(String(query.limit || query.per_page || '25'), 10) || 25));
|
||||
const sort = String(query.sort || 'id');
|
||||
const order = String(query.order || '').toLowerCase() === 'asc' ? 'asc' : 'desc';
|
||||
const search = String(query.search || '');
|
||||
|
||||
return { page, limit, skip: (page - 1) * limit, sort, order, search };
|
||||
}
|
||||
|
||||
export function buildPaginationMeta(total: number, page: number, limit: number): PaginationMeta {
|
||||
return {
|
||||
page,
|
||||
limit,
|
||||
per_page: limit,
|
||||
total,
|
||||
total_pages: Math.ceil(total / limit),
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user