@russ-b/nestjs-common-tools 1.8.0 → 1.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,3 @@
1
+ export declare class OutOfRangeException extends Error {
2
+ message: string;
3
+ }
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.OutOfRangeException = void 0;
4
+ class OutOfRangeException extends Error {
5
+ constructor() {
6
+ super(...arguments);
7
+ this.message = 'Page is out of range';
8
+ }
9
+ }
10
+ exports.OutOfRangeException = OutOfRangeException;
11
+ //# sourceMappingURL=out-of-range.exception.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"out-of-range.exception.js","sourceRoot":"","sources":["../../../../src/common/pagination/exceptions/out-of-range.exception.ts"],"names":[],"mappings":";;;AAAA,MAAa,mBAAoB,SAAQ,KAAK;IAA9C;;QACE,YAAO,GAAG,sBAAsB,CAAC;IACnC,CAAC;CAAA;AAFD,kDAEC"}
@@ -10,6 +10,16 @@ export interface IPaginatedResponse<T> {
10
10
  data: T[];
11
11
  pagination: IPaginationData;
12
12
  }
13
+ export interface PaginatedResponse<T> {
14
+ data: T[];
15
+ pagination: PaginationMeta;
16
+ }
17
+ export interface PaginationMeta {
18
+ total: number;
19
+ pages: number;
20
+ page: number;
21
+ perPage: number;
22
+ }
13
23
  export interface IPaginationData {
14
24
  totalItems: number;
15
25
  totalPages: number;
@@ -1,5 +1,5 @@
1
- import { PaginatedResponseDto } from './dto/paginated-response.dto';
2
- import { CountableResponse, IPaginationRequestParams } from './interfaces/pagination.interface';
1
+ import { CountableResponse, IPaginatedResponse, IPaginationRequestParams, PaginatedResponse } from './interfaces/pagination.interface';
3
2
  export declare abstract class Pagination {
4
- static response<T>({ page, perPage }: IPaginationRequestParams, [data, total]: CountableResponse<T>): PaginatedResponseDto<T>;
3
+ static createResponse<T>({ page, perPage }: IPaginationRequestParams, [data, total]: CountableResponse<T>): PaginatedResponse<T>;
4
+ static response<T>({ page, perPage }: IPaginationRequestParams, [data, total]: CountableResponse<T>): IPaginatedResponse<T>;
5
5
  }
@@ -2,7 +2,23 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Pagination = void 0;
4
4
  const common_1 = require("@nestjs/common");
5
+ const out_of_range_exception_1 = require("./exceptions/out-of-range.exception");
5
6
  class Pagination {
7
+ static createResponse({ page, perPage }, [data, total]) {
8
+ const pages = Math.ceil(total / perPage);
9
+ if (page > pages && pages > 0) {
10
+ throw new out_of_range_exception_1.OutOfRangeException();
11
+ }
12
+ return {
13
+ data,
14
+ pagination: {
15
+ total,
16
+ pages,
17
+ perPage,
18
+ page,
19
+ },
20
+ };
21
+ }
6
22
  static response({ page, perPage }, [data, total]) {
7
23
  const totalPages = Math.ceil(total / perPage);
8
24
  if (page > totalPages && totalPages > 0) {
@@ -1 +1 @@
1
- {"version":3,"file":"pagination.js","sourceRoot":"","sources":["../../../src/common/pagination/pagination.ts"],"names":[],"mappings":";;;AACA,2CAAqD;AAMrD,MAAsB,UAAU;IAC9B,MAAM,CAAC,QAAQ,CACb,EAAE,IAAI,EAAE,OAAO,EAA4B,EAC3C,CAAC,IAAI,EAAE,KAAK,CAAuB;QAEnC,MAAM,UAAU,GAAG,IAAI,CAAC,IAAI,CAAC,KAAK,GAAG,OAAO,CAAC,CAAC;QAE9C,IAAI,IAAI,GAAG,UAAU,IAAI,UAAU,GAAG,CAAC,EAAE,CAAC;YACxC,MAAM,IAAI,4BAAmB,CAAC,sBAAsB,CAAC,CAAC;QACxD,CAAC;QAED,OAAO;YACL,IAAI;YACJ,UAAU,EAAE;gBACV,UAAU,EAAE,KAAK;gBACjB,UAAU;gBACV,OAAO;gBACP,IAAI;aACL;SACF,CAAC;IACJ,CAAC;CACF;AArBD,gCAqBC"}
1
+ {"version":3,"file":"pagination.js","sourceRoot":"","sources":["../../../src/common/pagination/pagination.ts"],"names":[],"mappings":";;;AAAA,2CAAqD;AAOrD,gFAA0E;AAE1E,MAAsB,UAAU;IAC9B,MAAM,CAAC,cAAc,CACnB,EAAE,IAAI,EAAE,OAAO,EAA4B,EAC3C,CAAC,IAAI,EAAE,KAAK,CAAuB;QAEnC,MAAM,KAAK,GAAG,IAAI,CAAC,IAAI,CAAC,KAAK,GAAG,OAAO,CAAC,CAAC;QAEzC,IAAI,IAAI,GAAG,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,CAAC;YAC9B,MAAM,IAAI,4CAAmB,EAAE,CAAC;QAClC,CAAC;QAED,OAAO;YACL,IAAI;YACJ,UAAU,EAAE;gBACV,KAAK;gBACL,KAAK;gBACL,OAAO;gBACP,IAAI;aACL;SACF,CAAC;IACJ,CAAC;IAKD,MAAM,CAAC,QAAQ,CACb,EAAE,IAAI,EAAE,OAAO,EAA4B,EAC3C,CAAC,IAAI,EAAE,KAAK,CAAuB;QAEnC,MAAM,UAAU,GAAG,IAAI,CAAC,IAAI,CAAC,KAAK,GAAG,OAAO,CAAC,CAAC;QAE9C,IAAI,IAAI,GAAG,UAAU,IAAI,UAAU,GAAG,CAAC,EAAE,CAAC;YACxC,MAAM,IAAI,4BAAmB,CAAC,sBAAsB,CAAC,CAAC;QACxD,CAAC;QAED,OAAO;YACL,IAAI;YACJ,UAAU,EAAE;gBACV,UAAU,EAAE,KAAK;gBACjB,UAAU;gBACV,OAAO;gBACP,IAAI;aACL;SACF,CAAC;IACJ,CAAC;CACF;AA7CD,gCA6CC"}