@russ-b/nestjs-common-tools 1.3.5 → 1.4.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,7 @@
1
+ import { IPaginationParams, IPaginationRequestParams } from '../interfaces/pagination.interface';
2
+ export declare class PaginatedRequestDto implements IPaginationRequestParams {
3
+ page: number;
4
+ perPage: number;
5
+ get offset(): number;
6
+ getPaginationParams(): IPaginationParams;
7
+ }
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.PaginatedRequestDto = void 0;
13
+ const class_validator_1 = require("class-validator");
14
+ const class_transformer_1 = require("class-transformer");
15
+ class PaginatedRequestDto {
16
+ constructor() {
17
+ this.page = 1;
18
+ this.perPage = 50;
19
+ }
20
+ get offset() {
21
+ return (this.page - 1) * this.perPage;
22
+ }
23
+ getPaginationParams() {
24
+ return {
25
+ perPage: this.perPage,
26
+ offset: this.offset,
27
+ };
28
+ }
29
+ }
30
+ exports.PaginatedRequestDto = PaginatedRequestDto;
31
+ __decorate([
32
+ (0, class_validator_1.IsOptional)(),
33
+ (0, class_validator_1.IsNumber)(),
34
+ (0, class_validator_1.Min)(1),
35
+ (0, class_transformer_1.Type)(() => Number),
36
+ __metadata("design:type", Number)
37
+ ], PaginatedRequestDto.prototype, "page", void 0);
38
+ __decorate([
39
+ (0, class_validator_1.IsOptional)(),
40
+ (0, class_validator_1.IsNumber)(),
41
+ (0, class_validator_1.Max)(1000),
42
+ (0, class_validator_1.Min)(1),
43
+ (0, class_transformer_1.Type)(() => Number),
44
+ __metadata("design:type", Number)
45
+ ], PaginatedRequestDto.prototype, "perPage", void 0);
46
+ //# sourceMappingURL=paginated-request.dto.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"paginated-request.dto.js","sourceRoot":"","sources":["../../../../src/common/pagination/dto/paginated-request.dto.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,qDAAiE;AACjE,yDAAyC;AAMzC,MAAa,mBAAmB;IAAhC;QAKE,SAAI,GAAW,CAAC,CAAC;QAOjB,YAAO,GAAW,EAAE,CAAC;IAYvB,CAAC;IAVC,IAAI,MAAM;QACR,OAAO,CAAC,IAAI,CAAC,IAAI,GAAG,CAAC,CAAC,GAAG,IAAI,CAAC,OAAO,CAAC;IACxC,CAAC;IAED,mBAAmB;QACjB,OAAO;YACL,OAAO,EAAE,IAAI,CAAC,OAAO;YACrB,MAAM,EAAE,IAAI,CAAC,MAAM;SACpB,CAAC;IACJ,CAAC;CACF;AAxBD,kDAwBC;AAnBC;IAJC,IAAA,4BAAU,GAAE;IACZ,IAAA,0BAAQ,GAAE;IACV,IAAA,qBAAG,EAAC,CAAC,CAAC;IACN,IAAA,wBAAI,EAAC,GAAG,EAAE,CAAC,MAAM,CAAC;;iDACF;AAOjB;IALC,IAAA,4BAAU,GAAE;IACZ,IAAA,0BAAQ,GAAE;IACV,IAAA,qBAAG,EAAC,IAAI,CAAC;IACT,IAAA,qBAAG,EAAC,CAAC,CAAC;IACN,IAAA,wBAAI,EAAC,GAAG,EAAE,CAAC,MAAM,CAAC;;oDACE"}
@@ -0,0 +1,12 @@
1
+ import { IPaginatedResponse, IPaginationData } from '../interfaces/pagination.interface';
2
+ declare class PaginationDataDto implements IPaginationData {
3
+ totalItems: number;
4
+ totalPages: number;
5
+ page: number;
6
+ perPage: number;
7
+ }
8
+ export declare class PaginatedResponseDto<T> implements IPaginatedResponse<T> {
9
+ pagination: PaginationDataDto;
10
+ data: T[];
11
+ }
12
+ export {};
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.PaginatedResponseDto = void 0;
13
+ const class_transformer_1 = require("class-transformer");
14
+ let PaginationDataDto = class PaginationDataDto {
15
+ };
16
+ __decorate([
17
+ (0, class_transformer_1.Expose)(),
18
+ __metadata("design:type", Number)
19
+ ], PaginationDataDto.prototype, "totalItems", void 0);
20
+ __decorate([
21
+ (0, class_transformer_1.Expose)(),
22
+ __metadata("design:type", Number)
23
+ ], PaginationDataDto.prototype, "totalPages", void 0);
24
+ __decorate([
25
+ (0, class_transformer_1.Expose)(),
26
+ __metadata("design:type", Number)
27
+ ], PaginationDataDto.prototype, "page", void 0);
28
+ __decorate([
29
+ (0, class_transformer_1.Expose)(),
30
+ __metadata("design:type", Number)
31
+ ], PaginationDataDto.prototype, "perPage", void 0);
32
+ PaginationDataDto = __decorate([
33
+ (0, class_transformer_1.Exclude)()
34
+ ], PaginationDataDto);
35
+ let PaginatedResponseDto = class PaginatedResponseDto {
36
+ };
37
+ exports.PaginatedResponseDto = PaginatedResponseDto;
38
+ __decorate([
39
+ (0, class_transformer_1.Expose)(),
40
+ (0, class_transformer_1.Type)(() => PaginationDataDto),
41
+ __metadata("design:type", PaginationDataDto)
42
+ ], PaginatedResponseDto.prototype, "pagination", void 0);
43
+ __decorate([
44
+ (0, class_transformer_1.Expose)(),
45
+ __metadata("design:type", Array)
46
+ ], PaginatedResponseDto.prototype, "data", void 0);
47
+ exports.PaginatedResponseDto = PaginatedResponseDto = __decorate([
48
+ (0, class_transformer_1.Exclude)()
49
+ ], PaginatedResponseDto);
50
+ //# sourceMappingURL=paginated-response.dto.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"paginated-response.dto.js","sourceRoot":"","sources":["../../../../src/common/pagination/dto/paginated-response.dto.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,yDAA0D;AAO1D,IAAM,iBAAiB,GAAvB,MAAM,iBAAiB;CAYtB,CAAA;AAVC;IADC,IAAA,0BAAM,GAAE;;qDACU;AAGnB;IADC,IAAA,0BAAM,GAAE;;qDACU;AAGnB;IADC,IAAA,0BAAM,GAAE;;+CACI;AAGb;IADC,IAAA,0BAAM,GAAE;;kDACO;AAXZ,iBAAiB;IADtB,IAAA,2BAAO,GAAE;GACJ,iBAAiB,CAYtB;AAGM,IAAM,oBAAoB,GAA1B,MAAM,oBAAoB;CAOhC,CAAA;AAPY,oDAAoB;AAG/B;IAFC,IAAA,0BAAM,GAAE;IACR,IAAA,wBAAI,EAAC,GAAG,EAAE,CAAC,iBAAiB,CAAC;8BAClB,iBAAiB;wDAAC;AAG9B;IADC,IAAA,0BAAM,GAAE;;kDACC;+BANC,oBAAoB;IADhC,IAAA,2BAAO,GAAE;GACG,oBAAoB,CAOhC"}
@@ -0,0 +1,4 @@
1
+ export * from './pagination';
2
+ export * from './dto/paginated-request.dto';
3
+ export * from './dto/paginated-response.dto';
4
+ export * from './interfaces/pagination.interface';
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./pagination"), exports);
18
+ __exportStar(require("./dto/paginated-request.dto"), exports);
19
+ __exportStar(require("./dto/paginated-response.dto"), exports);
20
+ __exportStar(require("./interfaces/pagination.interface"), exports);
21
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/common/pagination/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,+CAA6B;AAC7B,8DAA4C;AAC5C,+DAA6C;AAC7C,oEAAkD"}
@@ -0,0 +1,19 @@
1
+ export interface IPaginationRequestParams {
2
+ page: number;
3
+ perPage: number;
4
+ }
5
+ export interface IPaginationParams {
6
+ perPage: number;
7
+ offset: number;
8
+ }
9
+ export interface IPaginatedResponse<T> {
10
+ data: T[];
11
+ pagination: IPaginationData;
12
+ }
13
+ export interface IPaginationData {
14
+ totalItems: number;
15
+ totalPages: number;
16
+ page: number;
17
+ perPage: number;
18
+ }
19
+ export type CountableResponse<T> = [T[], number];
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=pagination.interface.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pagination.interface.js","sourceRoot":"","sources":["../../../../src/common/pagination/interfaces/pagination.interface.ts"],"names":[],"mappings":""}
@@ -0,0 +1,5 @@
1
+ import { PaginatedResponseDto } from './dto/paginated-response.dto';
2
+ import { CountableResponse, IPaginationRequestParams } from './interfaces/pagination.interface';
3
+ export declare abstract class Pagination {
4
+ static response<T>({ page, perPage }: IPaginationRequestParams, [data, total]: CountableResponse<T>): PaginatedResponseDto<T>;
5
+ }
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Pagination = void 0;
4
+ const common_1 = require("@nestjs/common");
5
+ class Pagination {
6
+ static response({ page, perPage }, [data, total]) {
7
+ const totalPages = Math.ceil(total / perPage);
8
+ if (page > totalPages && totalPages > 0) {
9
+ throw new common_1.BadRequestException('Page is out of range');
10
+ }
11
+ return {
12
+ data,
13
+ pagination: {
14
+ totalItems: total,
15
+ totalPages,
16
+ perPage,
17
+ page,
18
+ },
19
+ };
20
+ }
21
+ }
22
+ exports.Pagination = Pagination;
23
+ //# sourceMappingURL=pagination.js.map
@@ -0,0 +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"}