@sihay.ztch/pagination_lib
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

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;
}

Readme

Keywords

Package Sidebar

Install

npm i @sihay.ztch/pagination_lib

Weekly Downloads

2

Version

1.0.1

License

MIT

Unpacked Size

18.3 kB

Total Files

42

Last publish

Collaborators

  • sihay.ztch