UNPKG

@motech-development/pagination

Version:
146 lines (125 loc) 3.42 kB
import { createParamDecorator, Injectable, Module } from '@nestjs/common'; import { map } from 'rxjs/operators'; var PaginationEnum; (function (PaginationEnum) { PaginationEnum["Asc"] = "asc"; PaginationEnum["Desc"] = "desc"; PaginationEnum[PaginationEnum["Max"] = 50] = "Max"; PaginationEnum[PaginationEnum["Min"] = 5] = "Min"; PaginationEnum[PaginationEnum["Page"] = 1] = "Page"; PaginationEnum[PaginationEnum["Size"] = 10] = "Size"; })(PaginationEnum || (PaginationEnum = {})); var PaginationEnum$1 = PaginationEnum; /** * Calculates the number of items to take and ensures min and max ranges are respected. * * @param limit - Number of items to return. * @returns Number of items to take. */ const calculateTake = limit => { const value = Number(limit); if (value > PaginationEnum$1.Max) { return PaginationEnum$1.Max; } if (value < PaginationEnum$1.Min) { return PaginationEnum$1.Min; } return value; }; /** * Calculates how many items to skip based on page number and items to take. * * @param page - Page number. * @param take - Number of items to take. * @returns Number of items to skip. */ const calculateSkip = (page, take) => { const value = Number(page); return take * (value - 1); }; /** Ordering object. */ /** * Generates order by object. * * @param dir - Direction to order results by. * @param sortBy - Property to sort by. * @returns Order by object */ const calculateOrderBy = (dir, sortBy) => { if (sortBy) { const value = String(sortBy); return { [value]: dir }; } return {}; }; const calculatePages = (total, limit) => total > 0 ? Math.ceil(total / limit) : 0; /** Paginated query object. */ /** * Pagination decorator factory. * * @param _ - Decorator data. * @param ctx - Execution context. * @returns Pagination query object. */ const paginationFactory = (_, ctx) => { const { query } = ctx.switchToHttp().getRequest(); const { dir = PaginationEnum$1.Desc, limit = PaginationEnum$1.Size, page = PaginationEnum$1.Page, sortBy } = query; const take = calculateTake(limit); const skip = calculateSkip(page, take); const orderBy = calculateOrderBy(dir, sortBy); return { orderBy, skip, take }; }; /** * Pagination decorator. Generates paginated query from query params. */ const Pagination = createParamDecorator(paginationFactory); var _dec$1, _class$1; /** Paginated result DTO. */ /** * Transforms response into a paginated response. */ let PaginationInterceptor = (_dec$1 = Injectable(), _dec$1(_class$1 = class PaginationInterceptor { intercept(ctx, next) { const { query } = ctx.switchToHttp().getRequest(); const { limit = PaginationEnum$1.Size, page = PaginationEnum$1.Page } = query; const take = calculateTake(limit); return next.handle().pipe(map(([total, items]) => { const pages = calculatePages(total, take); return { items, limit: take, page: Number(page), pages, total }; })); } }) || _class$1); var _dec, _class; /** * Pagination module. */ let PaginationModule = (_dec = Module({ exports: [PaginationInterceptor], providers: [PaginationInterceptor] }), _dec(_class = class PaginationModule {}) || _class); export { Pagination, PaginationInterceptor, PaginationModule }; //# sourceMappingURL=index.esm.js.map