1.0.4 • Published 2 years ago

@chris7ca/pagination_lib v1.0.4

Weekly downloads
-
License
MIT
Repository
-
Last release
2 years ago

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

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago