import * as express from 'express' ; import { MAX_PER_PAGE } from '../config' ; import { DEFAULT_PAGE , DEFAULT_PER_PAGE } from '../constants' ; import { ValidationError , ValidationErrorCodes } from '../errors' ; export const paginationUtils = { param records param page param perPage paginate : < T > ( records , page , perPage ) => { return paginationUtils . paginateSerialize ( records . slice ( ( page - 1 ) * perPage , page * perPage ) , records . length , page , perPage ) ; } , paginateDBFilters : ( page , perPage ) => { return { skip : ( page - 1 ) * perPage , take : perPage , } ; } , paginateSerialize : < T > ( collection , total , page , perPage ) => { const paginated = { total , page , perPage , records : collection , } ; return paginated ; } , parsePaginationConfig : ( req ) : { page : number ; perPage : number ; } => { const page = req . query . page === undefined ? DEFAULT_PAGE : Number ( req . query . page ) ; const perPage = req . query . perPage === undefined ? DEFAULT_PER_PAGE : Number ( req . query . perPage ) ; if ( perPage > MAX_PER_PAGE ) { throw new ValidationError ( [ { field : 'perPage' , code : ValidationErrorCodes . ValueOutOfRange , reason : `perPage should be less or equal to ${ MAX_PER_PAGE }` , } , ] ) ; } return { page , perPage } ; } , } ;