made a pagination service

This commit is contained in:
2025-11-29 12:04:50 +05:30
parent e0b841b437
commit 264f2fa29c
6 changed files with 149 additions and 18 deletions

View File

@@ -0,0 +1,66 @@
// common/utils/pagination/pagination.service.ts
import { PaginationOptions, PaginationParams, PaginatedResponse } from './pagination.types';
export class PaginationService {
private readonly DEFAULT_PAGE = 1;
private readonly DEFAULT_LIMIT = 10;
private readonly MAX_LIMIT = 100;
/**
* Parse and validate pagination parameters
*/
parsePaginationParams(params: PaginationParams): PaginationOptions {
let page = Number(params.page) || this.DEFAULT_PAGE;
let limit = Number(params.limit) || this.DEFAULT_LIMIT;
// Validate and constrain values
page = Math.max(1, page);
limit = Math.max(1, Math.min(limit, this.MAX_LIMIT));
const skip = (page - 1) * limit;
return {
page,
limit,
skip,
};
}
/**
* Create paginated response
*/
createPaginatedResponse<T>(
data: T[],
totalCount: number,
paginationOptions: PaginationOptions,
): PaginatedResponse<T> {
const { page, limit } = paginationOptions;
const totalPages = Math.ceil(totalCount / limit);
return {
data,
pagination: {
currentPage: page,
pageSize: limit,
totalCount,
totalPages,
hasNext: page < totalPages,
hasPrevious: page > 1,
},
};
}
/**
* Extract pagination params from API Gateway event
*/
getPaginationFromEvent(event: any): PaginationParams {
const queryParams = event.queryStringParameters || {};
return {
page: queryParams.page,
limit: queryParams.limit,
};
}
}
export const paginationService = new PaginationService();

View File

@@ -0,0 +1,23 @@
// common/utils/pagination/pagination.types.ts
export interface PaginationOptions {
page: number;
limit: number;
skip: number;
}
export interface PaginatedResponse<T> {
data: T[];
pagination: {
currentPage: number;
pageSize: number;
totalCount: number;
totalPages: number;
hasNext: boolean;
hasPrevious: boolean;
};
}
export interface PaginationParams {
page?: string | number;
limit?: string | number;
}