made a pagination service
This commit is contained in:
66
src/common/utils/pagination/pagination.service.ts
Normal file
66
src/common/utils/pagination/pagination.service.ts
Normal 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();
|
||||
23
src/common/utils/pagination/pagination.types.ts
Normal file
23
src/common/utils/pagination/pagination.types.ts
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user