JSPM

@sihay.ztch/pagination_lib

1.0.1
    • ESM via JSPM
    • ES Module Entrypoint
    • Export Map
    • Keywords
    • License
    • Repository URL
    • TypeScript Types
    • README
    • Created
    • Published
    • 0
    • Score
      100M100P100Q48637F
    • License MIT

    Pagination library

    Package Exports

    • @sihay.ztch/pagination_lib
    • @sihay.ztch/pagination_lib/dist/index.js

    This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@sihay.ztch/pagination_lib) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

    Readme

    Pagination Library

    Help libary for pagination

    Installation

    npm install @sihay.ztch/pagination_lib

    Note: The class PaginationQueryDto use class-validator and class-transformer dependencies

    Utilities

    DTO Validator

    You can extends of this class it is necessary

    class PaginationQueryDto {
      @IsOptional()
      @IsNumber()
      @Min(1)
      @Type(() => Number)
      page: number;
    
      @IsOptional()
      @IsNumber()
      @Min(2)
      @Type(() => Number)
      perPage: number;
    }

    Exceptions

    When current page is more granther than last page throws a PaginationExcepetion

    class PaginationException {
      private code = 400;
      private message = 'Page out of range';
      private error = 'Bad request';
    
      getResponse(): IPaginationExcepion {
        return {
          code: this.code,
          message: this.message,
          error: this.error,
        };
      }
    }

    Paginator

    This function returns a paginated response

    import { paginateResponse } from '@sihay.ztch/pagination_lib';
    
    const data = new Array(100);
    const total = data.length;
    const perPage = 10;
    const currentPage = 1;
    const portionOfData = new Array(perPage);
    
    const result = paginateResponse(portionOfData, total, currentPage, perPage);

    paginateResponse return an object who implements PaginationResponse interface:

    interface PaginationResponse {
      data: any[];
      total: number;
      current_page: number;
      next_page: number | null;
      prev_page: number | null;
      last_page: number | null;
    }