@hedhog/pagination 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/constants/pagination.constants.d.ts +3 -0
- package/dist/constants/pagination.constants.d.ts.map +1 -0
- package/dist/constants/pagination.constants.js +6 -0
- package/dist/constants/pagination.constants.js.map +1 -0
- package/dist/decorator/pagination.decorator.d.ts +3 -0
- package/dist/decorator/pagination.decorator.d.ts.map +1 -0
- package/dist/decorator/pagination.decorator.js +54 -0
- package/dist/decorator/pagination.decorator.js.map +1 -0
- package/dist/dto/pagination.dto.d.ts +10 -0
- package/dist/dto/pagination.dto.d.ts.map +1 -0
- package/dist/dto/pagination.dto.js +52 -0
- package/dist/dto/pagination.dto.js.map +1 -0
- package/dist/enums/patination.enums.d.ts +12 -0
- package/dist/enums/patination.enums.d.ts.map +1 -0
- package/dist/enums/patination.enums.js +17 -0
- package/dist/enums/patination.enums.js.map +1 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +23 -0
- package/dist/index.js.map +1 -0
- package/dist/pagination.module.d.ts +3 -0
- package/dist/pagination.module.d.ts.map +1 -0
- package/dist/pagination.module.js +22 -0
- package/dist/pagination.module.js.map +1 -0
- package/dist/pagination.service.d.ts +7 -0
- package/dist/pagination.service.d.ts.map +1 -0
- package/dist/pagination.service.js +104 -0
- package/dist/pagination.service.js.map +1 -0
- package/dist/pagination.service.spec.d.ts +2 -0
- package/dist/pagination.service.spec.d.ts.map +1 -0
- package/dist/pagination.service.spec.js +17 -0
- package/dist/pagination.service.spec.js.map +1 -0
- package/dist/types/pagination.types.d.ts +33 -0
- package/dist/types/pagination.types.d.ts.map +1 -0
- package/dist/types/pagination.types.js +3 -0
- package/dist/types/pagination.types.js.map +1 -0
- package/package.json +2 -2
- package/src/decorator/pagination.decorator.ts +35 -13
- package/src/dto/pagination.dto.ts +2 -2
- package/src/enums/patination.enums.ts +2 -2
- package/src/pagination.service.ts +1 -1
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"pagination.constants.d.ts","sourceRoot":"","sources":["../../src/constants/pagination.constants.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,YAAY,IAAI,CAAC;AAC9B,eAAO,MAAM,iBAAiB,KAAK,CAAC"}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"pagination.constants.js","sourceRoot":"","sources":["../../src/constants/pagination.constants.ts"],"names":[],"mappings":";;;AAAa,QAAA,YAAY,GAAG,CAAC,CAAC;AACjB,QAAA,iBAAiB,GAAG,EAAE,CAAC"}
|
@@ -0,0 +1,3 @@
|
|
1
|
+
import { PaginationField } from '../enums/patination.enums';
|
2
|
+
export declare const Pagination: (...dataOrPipes: (import("@nestjs/common").PipeTransform<any, any> | import("@nestjs/common").Type<import("@nestjs/common").PipeTransform<any, any>> | PaginationField)[]) => ParameterDecorator;
|
3
|
+
//# sourceMappingURL=pagination.decorator.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"pagination.decorator.d.ts","sourceRoot":"","sources":["../../src/decorator/pagination.decorator.ts"],"names":[],"mappings":"AASA,OAAO,EAAsB,eAAe,EAAE,MAAM,2BAA2B,CAAC;AAMhF,eAAO,MAAM,UAAU,kMAoEtB,CAAC"}
|
@@ -0,0 +1,54 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.Pagination = void 0;
|
4
|
+
const common_1 = require("@nestjs/common");
|
5
|
+
const pagination_constants_1 = require("../constants/pagination.constants");
|
6
|
+
const patination_enums_1 = require("../enums/patination.enums");
|
7
|
+
const class_transformer_1 = require("class-transformer");
|
8
|
+
const pagination_dto_1 = require("../dto/pagination.dto");
|
9
|
+
const class_validator_1 = require("class-validator");
|
10
|
+
exports.Pagination = (0, common_1.createParamDecorator)((data, ctx) => {
|
11
|
+
const request = ctx.switchToHttp().getRequest();
|
12
|
+
const defaultOptions = {
|
13
|
+
page: pagination_constants_1.DEFAULT_PAGE,
|
14
|
+
pageSize: pagination_constants_1.DEFAULT_PAGE_SIZE,
|
15
|
+
search: '',
|
16
|
+
sortField: 'id',
|
17
|
+
sortOrder: patination_enums_1.PageOrderDirection.Asc,
|
18
|
+
fields: '',
|
19
|
+
};
|
20
|
+
const requestData = Object.assign(Object.assign(Object.assign({}, defaultOptions), (request.body || {})), (request.query || {}));
|
21
|
+
const { page = defaultOptions.page, pageSize = defaultOptions.pageSize, search = defaultOptions.search, sortField = defaultOptions.sortField, sortOrder = defaultOptions.sortOrder, fields = defaultOptions.fields, } = requestData;
|
22
|
+
const validSortOrder = Object.values(patination_enums_1.PageOrderDirection).includes(sortOrder)
|
23
|
+
? sortOrder
|
24
|
+
: defaultOptions.sortOrder;
|
25
|
+
const finalData = {
|
26
|
+
page,
|
27
|
+
pageSize,
|
28
|
+
search,
|
29
|
+
sortField,
|
30
|
+
sortOrder: validSortOrder,
|
31
|
+
fields,
|
32
|
+
};
|
33
|
+
const paginationDtoInstance = (0, class_transformer_1.plainToClass)(pagination_dto_1.PaginationDTO, finalData);
|
34
|
+
const errors = (0, class_validator_1.validateSync)(paginationDtoInstance);
|
35
|
+
if (errors.length > 0) {
|
36
|
+
throw new common_1.BadRequestException('Pagination data is not valid according to PaginationDto: ' +
|
37
|
+
errors
|
38
|
+
.map((error) => Object.values(error.constraints).join(', '))
|
39
|
+
.join(', '));
|
40
|
+
}
|
41
|
+
if (data) {
|
42
|
+
switch (data) {
|
43
|
+
case patination_enums_1.PaginationField.Page:
|
44
|
+
case patination_enums_1.PaginationField.PageSize:
|
45
|
+
return finalData[data] ? +finalData[data] : defaultOptions[data];
|
46
|
+
case patination_enums_1.PaginationField.SortOrder:
|
47
|
+
return validSortOrder || defaultOptions[data];
|
48
|
+
default:
|
49
|
+
return finalData[data];
|
50
|
+
}
|
51
|
+
}
|
52
|
+
return finalData;
|
53
|
+
});
|
54
|
+
//# sourceMappingURL=pagination.decorator.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"pagination.dec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BAAa,EAAE,SAAS,CAAC,CAAC;IAErE,MAAM,MAAM,GAAG,IAAA,8BAAY,EAAC,qBAAqB,CAAC,CAAC;IAEnD,IAAI,MAAM,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QACtB,MAAM,IAAI,4BAAmB,CAC3B,2DAA2D;YACzD,MAAM;iBACH,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,WAAW,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;iBAC3D,IAAI,CAAC,IAAI,CAAC,CAChB,CAAC;IACJ,CAAC;IAED,IAAI,IAAI,EAAE,CAAC;QACT,QAAQ,IAAI,EAAE,CAAC;YACb,KAAK,kCAAe,CAAC,IAAI,CAAC;YAC1B,KAAK,kCAAe,CAAC,QAAQ;gBAC3B,OAAO,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,cAAc,CAAC,IAAI,CAAC,CAAC;YACnE,KAAK,kCAAe,CAAC,SAAS;gBAC5B,OAAO,cAAc,IAAI,cAAc,CAAC,IAAI,CAAC,CAAC;YAChD;gBACE,OAAO,SAAS,CAAC,IAAI,CAAC,CAAC;QAC3B,CAAC;IACH,CAAC;IAED,OAAO,SAAS,CAAC;AACnB,CAAC,CACF,CAAC"}
|
@@ -0,0 +1,10 @@
|
|
1
|
+
import { PageOrderDirection } from '../enums/patination.enums';
|
2
|
+
export declare class PaginationDTO {
|
3
|
+
page: number;
|
4
|
+
pageSize: number;
|
5
|
+
search: string;
|
6
|
+
sortField: string;
|
7
|
+
sortOrder: PageOrderDirection;
|
8
|
+
fields: string;
|
9
|
+
}
|
10
|
+
//# sourceMappingURL=pagination.dto.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"pagination.dto.d.ts","sourceRoot":"","sources":["../../src/dto/pagination.dto.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,kBAAkB,EAAE,MAAM,2BAA2B,CAAC;AAC/D,qBAAa,aAAa;IAIxB,IAAI,EAAE,MAAM,CAAC;IAKb,QAAQ,EAAE,MAAM,CAAC;IAIjB,MAAM,EAAE,MAAM,CAAC;IAIf,SAAS,EAAE,MAAM,CAAC;IAKlB,SAAS,EAAE,kBAAkB,CAAC;IAI9B,MAAM,EAAE,MAAM,CAAC;CAChB"}
|
@@ -0,0 +1,52 @@
|
|
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.PaginationDTO = void 0;
|
13
|
+
const class_transformer_1 = require("class-transformer");
|
14
|
+
const class_validator_1 = require("class-validator");
|
15
|
+
const patination_enums_1 = require("../enums/patination.enums");
|
16
|
+
class PaginationDTO {
|
17
|
+
}
|
18
|
+
exports.PaginationDTO = PaginationDTO;
|
19
|
+
__decorate([
|
20
|
+
(0, class_validator_1.IsOptional)(),
|
21
|
+
(0, class_transformer_1.Transform)(({ value }) => Number(value)),
|
22
|
+
(0, class_validator_1.IsInt)({ message: 'page must be an integer' }),
|
23
|
+
__metadata("design:type", Number)
|
24
|
+
], PaginationDTO.prototype, "page", void 0);
|
25
|
+
__decorate([
|
26
|
+
(0, class_validator_1.IsOptional)(),
|
27
|
+
(0, class_transformer_1.Transform)(({ value }) => Number(value)),
|
28
|
+
(0, class_validator_1.IsInt)({ message: 'pageSize must be an integer' }),
|
29
|
+
__metadata("design:type", Number)
|
30
|
+
], PaginationDTO.prototype, "pageSize", void 0);
|
31
|
+
__decorate([
|
32
|
+
(0, class_validator_1.IsOptional)(),
|
33
|
+
(0, class_validator_1.IsString)({ message: 'search must be a string' }),
|
34
|
+
__metadata("design:type", String)
|
35
|
+
], PaginationDTO.prototype, "search", void 0);
|
36
|
+
__decorate([
|
37
|
+
(0, class_validator_1.IsOptional)(),
|
38
|
+
(0, class_validator_1.IsString)({ message: 'field must be a string' }),
|
39
|
+
__metadata("design:type", String)
|
40
|
+
], PaginationDTO.prototype, "sortField", void 0);
|
41
|
+
__decorate([
|
42
|
+
(0, class_validator_1.IsOptional)(),
|
43
|
+
(0, class_validator_1.IsString)({ message: 'sortOrder must be a string' }),
|
44
|
+
(0, class_validator_1.IsEnum)(patination_enums_1.PageOrderDirection, { message: 'sortOrder is not valid' }),
|
45
|
+
__metadata("design:type", String)
|
46
|
+
], PaginationDTO.prototype, "sortOrder", void 0);
|
47
|
+
__decorate([
|
48
|
+
(0, class_validator_1.IsOptional)(),
|
49
|
+
(0, class_validator_1.IsString)({ message: 'fields must be a string' }),
|
50
|
+
__metadata("design:type", String)
|
51
|
+
], PaginationDTO.prototype, "fields", void 0);
|
52
|
+
//# sourceMappingURL=pagination.dto.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"pagination.dto.js","sourceRoot":"","sources":["../../src/dto/pagination.dto.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,yDAA8C;AAC9C,qDAAsE;AACtE,gEAA+D;AAC/D,MAAa,aAAa;CA2BzB;AA3BD,sCA2BC;AAvBC;IAHC,IAAA,4BAAU,GAAE;IACZ,IAAA,6BAAS,EAAC,CAAC,EAAE,KAAK,EAAE,EAAE,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IACvC,IAAA,uBAAK,EAAC,EAAE,OAAO,EAAE,yBAAyB,EAAE,CAAC;;2CACjC;AAKb;IAHC,IAAA,4BAAU,GAAE;IACZ,IAAA,6BAAS,EAAC,CAAC,EAAE,KAAK,EAAE,EAAE,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IACvC,IAAA,uBAAK,EAAC,EAAE,OAAO,EAAE,6BAA6B,EAAE,CAAC;;+CACjC;AAIjB;IAFC,IAAA,4BAAU,GAAE;IACZ,IAAA,0BAAQ,EAAC,EAAE,OAAO,EAAE,yBAAyB,EAAE,CAAC;;6CAClC;AAIf;IAFC,IAAA,4BAAU,GAAE;IACZ,IAAA,0BAAQ,EAAC,EAAE,OAAO,EAAE,wBAAwB,EAAE,CAAC;;gDAC9B;AAKlB;IAHC,IAAA,4BAAU,GAAE;IACZ,IAAA,0BAAQ,EAAC,EAAE,OAAO,EAAE,4BAA4B,EAAE,CAAC;IACnD,IAAA,wBAAM,EAAC,qCAAkB,EAAE,EAAE,OAAO,EAAE,wBAAwB,EAAE,CAAC;;gDACpC;AAI9B;IAFC,IAAA,4BAAU,GAAE;IACZ,IAAA,0BAAQ,EAAC,EAAE,OAAO,EAAE,yBAAyB,EAAE,CAAC;;6CAClC"}
|
@@ -0,0 +1,12 @@
|
|
1
|
+
export declare enum PageOrderDirection {
|
2
|
+
Asc = "asc",
|
3
|
+
Desc = "desc"
|
4
|
+
}
|
5
|
+
export declare enum PaginationField {
|
6
|
+
Page = "page",
|
7
|
+
PageSize = "pageSize",
|
8
|
+
SortField = "sortOrder",
|
9
|
+
SortOrder = "sortOrder",
|
10
|
+
Search = "search"
|
11
|
+
}
|
12
|
+
//# sourceMappingURL=patination.enums.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"patination.enums.d.ts","sourceRoot":"","sources":["../../src/enums/patination.enums.ts"],"names":[],"mappings":"AAAA,oBAAY,kBAAkB;IAC5B,GAAG,QAAQ;IACX,IAAI,SAAS;CACd;AAED,oBAAY,eAAe;IACzB,IAAI,SAAS;IACb,QAAQ,aAAa;IACrB,SAAS,cAAc;IACvB,SAAS,cAAc;IACvB,MAAM,WAAW;CAClB"}
|
@@ -0,0 +1,17 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.PaginationField = exports.PageOrderDirection = void 0;
|
4
|
+
var PageOrderDirection;
|
5
|
+
(function (PageOrderDirection) {
|
6
|
+
PageOrderDirection["Asc"] = "asc";
|
7
|
+
PageOrderDirection["Desc"] = "desc";
|
8
|
+
})(PageOrderDirection || (exports.PageOrderDirection = PageOrderDirection = {}));
|
9
|
+
var PaginationField;
|
10
|
+
(function (PaginationField) {
|
11
|
+
PaginationField["Page"] = "page";
|
12
|
+
PaginationField["PageSize"] = "pageSize";
|
13
|
+
PaginationField["SortField"] = "sortOrder";
|
14
|
+
PaginationField["SortOrder"] = "sortOrder";
|
15
|
+
PaginationField["Search"] = "search";
|
16
|
+
})(PaginationField || (exports.PaginationField = PaginationField = {}));
|
17
|
+
//# sourceMappingURL=patination.enums.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"patination.enums.js","sourceRoot":"","sources":["../../src/enums/patination.enums.ts"],"names":[],"mappings":";;;AAAA,IAAY,kBAGX;AAHD,WAAY,kBAAkB;IAC5B,iCAAW,CAAA;IACX,mCAAa,CAAA;AACf,CAAC,EAHW,kBAAkB,kCAAlB,kBAAkB,QAG7B;AAED,IAAY,eAMX;AAND,WAAY,eAAe;IACzB,gCAAa,CAAA;IACb,wCAAqB,CAAA;IACrB,0CAAuB,CAAA;IACvB,0CAAuB,CAAA;IACvB,oCAAiB,CAAA;AACnB,CAAC,EANW,eAAe,+BAAf,eAAe,QAM1B"}
|
package/dist/index.d.ts
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
export * from './constants/pagination.constants';
|
2
|
+
export * from './decorator/pagination.decorator';
|
3
|
+
export * from './dto/pagination.dto';
|
4
|
+
export * from './enums/patination.enums';
|
5
|
+
export * from './pagination.module';
|
6
|
+
export * from './pagination.service';
|
7
|
+
export type * from './types/pagination.types';
|
8
|
+
//# sourceMappingURL=index.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,kCAAkC,CAAC;AACjD,cAAc,kCAAkC,CAAC;AACjD,cAAc,sBAAsB,CAAC;AACrC,cAAc,0BAA0B,CAAC;AACzC,cAAc,qBAAqB,CAAC;AACpC,cAAc,sBAAsB,CAAC;AACrC,mBAAmB,0BAA0B,CAAC"}
|
package/dist/index.js
ADDED
@@ -0,0 +1,23 @@
|
|
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("./constants/pagination.constants"), exports);
|
18
|
+
__exportStar(require("./decorator/pagination.decorator"), exports);
|
19
|
+
__exportStar(require("./dto/pagination.dto"), exports);
|
20
|
+
__exportStar(require("./enums/patination.enums"), exports);
|
21
|
+
__exportStar(require("./pagination.module"), exports);
|
22
|
+
__exportStar(require("./pagination.service"), exports);
|
23
|
+
//# sourceMappingURL=index.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,mEAAiD;AACjD,mEAAiD;AACjD,uDAAqC;AACrC,2DAAyC;AACzC,sDAAoC;AACpC,uDAAqC"}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"pagination.module.d.ts","sourceRoot":"","sources":["../src/pagination.module.ts"],"names":[],"mappings":"AAGA,qBAKa,gBAAgB;CAAG"}
|
@@ -0,0 +1,22 @@
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
9
|
+
exports.PaginationModule = void 0;
|
10
|
+
const common_1 = require("@nestjs/common");
|
11
|
+
const pagination_service_1 = require("./pagination.service");
|
12
|
+
let PaginationModule = class PaginationModule {
|
13
|
+
};
|
14
|
+
exports.PaginationModule = PaginationModule;
|
15
|
+
exports.PaginationModule = PaginationModule = __decorate([
|
16
|
+
(0, common_1.Module)({
|
17
|
+
imports: [],
|
18
|
+
providers: [pagination_service_1.PaginationService],
|
19
|
+
exports: [pagination_service_1.PaginationService],
|
20
|
+
})
|
21
|
+
], PaginationModule);
|
22
|
+
//# sourceMappingURL=pagination.module.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"pagination.module.js","sourceRoot":"","sources":["../src/pagination.module.ts"],"names":[],"mappings":";;;;;;;;;AAAA,2CAAwC;AACxC,6DAAyD;AAOlD,IAAM,gBAAgB,GAAtB,MAAM,gBAAgB;CAAG,CAAA;AAAnB,4CAAgB;2BAAhB,gBAAgB;IAL5B,IAAA,eAAM,EAAC;QACN,OAAO,EAAE,EAAE;QACX,SAAS,EAAE,CAAC,sCAAiB,CAAC;QAC9B,OAAO,EAAE,CAAC,sCAAiB,CAAC;KAC7B,CAAC;GACW,gBAAgB,CAAG"}
|
@@ -0,0 +1,7 @@
|
|
1
|
+
import type { BaseModel, FindManyArgs, PaginatedResult, PaginationParams } from './types/pagination.types';
|
2
|
+
export declare class PaginationService {
|
3
|
+
private readonly logger;
|
4
|
+
paginate<T, M extends BaseModel>(model: M, paginationParams: PaginationParams, customQuery?: FindManyArgs<M>): Promise<PaginatedResult<T>>;
|
5
|
+
extractFieldNames(model: Record<string, any>): string[];
|
6
|
+
}
|
7
|
+
//# sourceMappingURL=pagination.service.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"pagination.service.d.ts","sourceRoot":"","sources":["../src/pagination.service.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EACV,SAAS,EACT,YAAY,EACZ,eAAe,EACf,gBAAgB,EACjB,MAAM,0BAA0B,CAAC;AAElC,qBACa,iBAAiB;IAC5B,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAsC;IAEvD,QAAQ,CAAC,CAAC,EAAE,CAAC,SAAS,SAAS,EACnC,KAAK,EAAE,CAAC,EACR,gBAAgB,EAAE,gBAAgB,EAClC,WAAW,CAAC,EAAE,YAAY,CAAC,CAAC,CAAC,GAC5B,OAAO,CAAC,eAAe,CAAC,CAAC,CAAC,CAAC;IA6F9B,iBAAiB,CAAC,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,MAAM,EAAE;CAaxD"}
|
@@ -0,0 +1,104 @@
|
|
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 PaginationService_1;
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
10
|
+
exports.PaginationService = void 0;
|
11
|
+
const common_1 = require("@nestjs/common");
|
12
|
+
const pagination_constants_1 = require("./constants/pagination.constants");
|
13
|
+
const patination_enums_1 = require("./enums/patination.enums");
|
14
|
+
let PaginationService = PaginationService_1 = class PaginationService {
|
15
|
+
constructor() {
|
16
|
+
this.logger = new common_1.Logger(PaginationService_1.name);
|
17
|
+
}
|
18
|
+
async paginate(model, paginationParams, customQuery) {
|
19
|
+
try {
|
20
|
+
const page = Number(paginationParams.page || pagination_constants_1.DEFAULT_PAGE);
|
21
|
+
const pageSize = Number(paginationParams.pageSize || pagination_constants_1.DEFAULT_PAGE_SIZE);
|
22
|
+
const search = paginationParams.search || null;
|
23
|
+
const sortField = paginationParams.sortField || null;
|
24
|
+
const sortOrder = paginationParams.sortOrder || patination_enums_1.PageOrderDirection.Asc;
|
25
|
+
const fields = paginationParams.fields
|
26
|
+
? paginationParams.fields.split(',')
|
27
|
+
: null;
|
28
|
+
if (page < 1 || pageSize < 1) {
|
29
|
+
throw new common_1.BadRequestException('Page and pageSize must be greater than 0');
|
30
|
+
}
|
31
|
+
let selectCondition = undefined;
|
32
|
+
let sortOrderCondition = {
|
33
|
+
id: paginationParams.sortOrder || patination_enums_1.PageOrderDirection.Asc,
|
34
|
+
};
|
35
|
+
if (search && sortField) {
|
36
|
+
const fieldNames = this.extractFieldNames(model);
|
37
|
+
if (!fieldNames.includes(sortField)) {
|
38
|
+
throw new common_1.BadRequestException(`Invalid field: ${sortField}. Valid columns are: ${fieldNames.join(', ')}`);
|
39
|
+
}
|
40
|
+
if (typeof sortField !== 'string') {
|
41
|
+
throw new common_1.BadRequestException('Field must be a string');
|
42
|
+
}
|
43
|
+
}
|
44
|
+
if (sortField) {
|
45
|
+
sortOrderCondition = { [sortField]: sortOrder };
|
46
|
+
}
|
47
|
+
if (fields) {
|
48
|
+
const fieldNames = this.extractFieldNames(model);
|
49
|
+
const invalidFields = fields.filter((field) => !fieldNames.includes(field));
|
50
|
+
if (invalidFields.length > 0) {
|
51
|
+
throw new common_1.BadRequestException(`Invalid fields: ${invalidFields.join(', ')}. Valid columns are: ${fieldNames.join(', ')}`);
|
52
|
+
}
|
53
|
+
selectCondition = fields.reduce((acc, field) => {
|
54
|
+
acc[field] = true;
|
55
|
+
return acc;
|
56
|
+
}, {});
|
57
|
+
}
|
58
|
+
const skip = page > 0 ? pageSize * (page - 1) : 0;
|
59
|
+
const query = {
|
60
|
+
select: selectCondition,
|
61
|
+
where: (customQuery === null || customQuery === void 0 ? void 0 : customQuery.where) || {},
|
62
|
+
orderBy: sortOrderCondition,
|
63
|
+
take: pageSize,
|
64
|
+
skip,
|
65
|
+
};
|
66
|
+
const [total, data] = await Promise.all([
|
67
|
+
model.count({ where: (customQuery === null || customQuery === void 0 ? void 0 : customQuery.where) || {} }),
|
68
|
+
model.findMany(query),
|
69
|
+
]);
|
70
|
+
const lastPage = Math.ceil(total / pageSize);
|
71
|
+
return {
|
72
|
+
total,
|
73
|
+
lastPage,
|
74
|
+
currentPage: page,
|
75
|
+
pageSize,
|
76
|
+
prev: page > 1 ? page - 1 : null,
|
77
|
+
next: page < lastPage ? page + 1 : null,
|
78
|
+
data,
|
79
|
+
};
|
80
|
+
}
|
81
|
+
catch (error) {
|
82
|
+
this.logger.error('Pagination Error:', error);
|
83
|
+
if (error instanceof common_1.BadRequestException) {
|
84
|
+
throw error;
|
85
|
+
}
|
86
|
+
throw new common_1.BadRequestException(`Failed to paginate: ${error}`);
|
87
|
+
}
|
88
|
+
}
|
89
|
+
extractFieldNames(model) {
|
90
|
+
const fieldNames = [];
|
91
|
+
const fields = model.fields;
|
92
|
+
for (const key in fields) {
|
93
|
+
if (fields.hasOwnProperty(key)) {
|
94
|
+
fieldNames.push(key);
|
95
|
+
}
|
96
|
+
}
|
97
|
+
return fieldNames;
|
98
|
+
}
|
99
|
+
};
|
100
|
+
exports.PaginationService = PaginationService;
|
101
|
+
exports.PaginationService = PaginationService = PaginationService_1 = __decorate([
|
102
|
+
(0, common_1.Injectable)()
|
103
|
+
], PaginationService);
|
104
|
+
//# sourceMappingURL=pagination.service.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"pagination.service.js","sourceRoot":"","sources":["../src/pagination.service.ts"],"names":[],"mappings":";;;;;;;;;;AAAA,2CAAyE;AACzE,2EAG0C;AAC1C,+DAA8D;AASvD,IAAM,iBAAiB,yBAAvB,MAAM,iBAAiB;IAAvB;QACY,WAAM,GAAG,IAAI,eAAM,CAAC,mBAAiB,CAAC,IAAI,CAAC,CAAC;IAgH/D,CAAC;IA9GC,KAAK,CAAC,QAAQ,CACZ,KAAQ,EACR,gBAAkC,EAClC,WAA6B;QAE7B,IAAI,CAAC;YACH,MAAM,IAAI,GAAG,MAAM,CAAC,gBAAgB,CAAC,IAAI,IAAI,mCAAY,CAAC,CAAC;YAC3D,MAAM,QAAQ,GAAG,MAAM,CAAC,gBAAgB,CAAC,QAAQ,IAAI,wCAAiB,CAAC,CAAC;YACxE,MAAM,MAAM,GAAG,gBAAgB,CAAC,MAAM,IAAI,IAAI,CAAC;YAC/C,MAAM,SAAS,GAAG,gBAAgB,CAAC,SAAS,IAAI,IAAI,CAAC;YACrD,MAAM,SAAS,GAAG,gBAAgB,CAAC,SAAS,IAAI,qCAAkB,CAAC,GAAG,CA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,CAAC,IAAI;gBAChC,IAAI,EAAE,IAAI,GAAG,QAAQ,CAAC,CAAC,CAAC,IAAI,GAAG,CAAC,CAAC,CAAC,CAAC,IAAI;gBACvC,IAAI;aACL,CAAC;QACJ,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,mBAAmB,EAAE,KAAK,CAAC,CAAC;YAE9C,IAAI,KAAK,YAAY,4BAAmB,EAAE,CAAC;gBACzC,MAAM,KAAK,CAAC;YACd,CAAC;YAED,MAAM,IAAI,4BAAmB,CAAC,uBAAuB,KAAK,EAAE,CAAC,CAAC;QAChE,CAAC;IACH,CAAC;IAED,iBAAiB,CAAC,KAA0B;QAC1C,MAAM,UAAU,GAAa,EAAE,CAAC;QAEhC,MAAM,MAAM,GAAG,KAAK,CAAC,MAAM,CAAC;QAE5B,KAAK,MAAM,GAAG,IAAI,MAAM,EAAE,CAAC;YACzB,IAAI,MAAM,CAAC,cAAc,CAAC,GAAG,CAAC,EAAE,CAAC;gBAC/B,UAAU,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;YACvB,CAAC;QACH,CAAC;QAED,OAAO,UAAU,CAAC;IACpB,CAAC;CACF,CAAA;AAjHY,8CAAiB;4BAAjB,iBAAiB;IAD7B,IAAA,mBAAU,GAAE;GACA,iBAAiB,CAiH7B"}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"pagination.service.spec.d.ts","sourceRoot":"","sources":["../src/pagination.service.spec.ts"],"names":[],"mappings":""}
|
@@ -0,0 +1,17 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
const testing_1 = require("@nestjs/testing");
|
4
|
+
const pagination_service_1 = require("./pagination.service");
|
5
|
+
describe('PaginationService', () => {
|
6
|
+
let service;
|
7
|
+
beforeEach(async () => {
|
8
|
+
const module = await testing_1.Test.createTestingModule({
|
9
|
+
providers: [pagination_service_1.PaginationService],
|
10
|
+
}).compile();
|
11
|
+
service = module.get(pagination_service_1.PaginationService);
|
12
|
+
});
|
13
|
+
it('should be defined', () => {
|
14
|
+
expect(service).toBeDefined();
|
15
|
+
});
|
16
|
+
});
|
17
|
+
//# sourceMappingURL=pagination.service.spec.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"pagination.service.spec.js","sourceRoot":"","sources":["../src/pagination.service.spec.ts"],"names":[],"mappings":";;AAAA,6CAAsD;AACtD,6DAAyD;AAEzD,QAAQ,CAAC,mBAAmB,EAAE,GAAG,EAAE;IACjC,IAAI,OAA0B,CAAC;IAE/B,UAAU,CAAC,KAAK,IAAI,EAAE;QACpB,MAAM,MAAM,GAAkB,MAAM,cAAI,CAAC,mBAAmB,CAAC;YAC3D,SAAS,EAAE,CAAC,sCAAiB,CAAC;SAC/B,CAAC,CAAC,OAAO,EAAE,CAAC;QAEb,OAAO,GAAG,MAAM,CAAC,GAAG,CAAoB,sCAAiB,CAAC,CAAC;IAC7D,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,mBAAmB,EAAE,GAAG,EAAE;QAC3B,MAAM,CAAC,OAAO,CAAC,CAAC,WAAW,EAAE,CAAC;IAChC,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
@@ -0,0 +1,33 @@
|
|
1
|
+
import { PageOrderDirection } from '../enums/patination.enums';
|
2
|
+
export type PaginatedResult<T> = {
|
3
|
+
total: number;
|
4
|
+
lastPage: number;
|
5
|
+
currentPage: number;
|
6
|
+
pageSize: number;
|
7
|
+
prev: number | null;
|
8
|
+
next: number | null;
|
9
|
+
data: T[];
|
10
|
+
};
|
11
|
+
export type PaginationType = string | number | PaginationParams;
|
12
|
+
export type PaginateFunction = <K, T>(model: any, args?: K, options?: PaginateOptions) => Promise<PaginatedResult<T>>;
|
13
|
+
export type PaginationParams = {
|
14
|
+
page?: number;
|
15
|
+
pageSize?: number;
|
16
|
+
search?: string;
|
17
|
+
sortField?: string;
|
18
|
+
sortOrder?: PageOrderDirection;
|
19
|
+
fields: string;
|
20
|
+
};
|
21
|
+
export type PaginateOptions = {
|
22
|
+
page?: number | string;
|
23
|
+
pageSize?: number | string;
|
24
|
+
};
|
25
|
+
export type BaseModel = {
|
26
|
+
findMany: (args: any) => Promise<any[]>;
|
27
|
+
count: (args: any) => Promise<number>;
|
28
|
+
fields?: Record<string, any>;
|
29
|
+
};
|
30
|
+
export type FindManyArgs<M> = M extends {
|
31
|
+
findMany: (args: infer A) => any;
|
32
|
+
} ? A : never;
|
33
|
+
//# sourceMappingURL=pagination.types.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"pagination.types.d.ts","sourceRoot":"","sources":["../../src/types/pagination.types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,2BAA2B,CAAC;AAE/D,MAAM,MAAM,eAAe,CAAC,CAAC,IAAI;IAC/B,KAAK,EAAE,MAAM,CAAC;IACd,QAAQ,EAAE,MAAM,CAAC;IACjB,WAAW,EAAE,MAAM,CAAC;IACpB,QAAQ,EAAE,MAAM,CAAC;IACjB,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IACpB,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IACpB,IAAI,EAAE,CAAC,EAAE,CAAC;CACX,CAAC;AAEF,MAAM,MAAM,cAAc,GAAG,MAAM,GAAG,MAAM,GAAG,gBAAgB,CAAC;AAEhE,MAAM,MAAM,gBAAgB,GAAG,CAAC,CAAC,EAAE,CAAC,EAClC,KAAK,EAAE,GAAG,EACV,IAAI,CAAC,EAAE,CAAC,EACR,OAAO,CAAC,EAAE,eAAe,KACtB,OAAO,CAAC,eAAe,CAAC,CAAC,CAAC,CAAC,CAAC;AAEjC,MAAM,MAAM,gBAAgB,GAAG;IAC7B,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,SAAS,CAAC,EAAE,kBAAkB,CAAC;IAC/B,MAAM,EAAE,MAAM,CAAC;CAChB,CAAC;AAEF,MAAM,MAAM,eAAe,GAAG;IAC5B,IAAI,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;IACvB,QAAQ,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;CAC5B,CAAC;AAEF,MAAM,MAAM,SAAS,GAAG;IACtB,QAAQ,EAAE,CAAC,IAAI,EAAE,GAAG,KAAK,OAAO,CAAC,GAAG,EAAE,CAAC,CAAC;IACxC,KAAK,EAAE,CAAC,IAAI,EAAE,GAAG,KAAK,OAAO,CAAC,MAAM,CAAC,CAAC;IACtC,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;CAC9B,CAAC;AAEF,MAAM,MAAM,YAAY,CAAC,CAAC,IAAI,CAAC,SAAS;IAAE,QAAQ,EAAE,CAAC,IAAI,EAAE,MAAM,CAAC,KAAK,GAAG,CAAA;CAAE,GACxE,CAAC,GACD,KAAK,CAAC"}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"pagination.types.js","sourceRoot":"","sources":["../../src/types/pagination.types.ts"],"names":[],"mappings":""}
|
package/package.json
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
{
|
2
2
|
"name": "@hedhog/pagination",
|
3
|
-
"version": "0.0.
|
3
|
+
"version": "0.0.2",
|
4
4
|
"private": false,
|
5
5
|
"main": "dist/index.js",
|
6
6
|
"scripts": {
|
7
|
-
"build": "tsc && npm version patch",
|
7
|
+
"build": "tsc --project tsconfig.production.json && npm version patch",
|
8
8
|
"prod": "npm run build && npm publish --access public"
|
9
9
|
},
|
10
10
|
"keywords": [],
|
@@ -1,10 +1,17 @@
|
|
1
|
-
import {
|
1
|
+
import {
|
2
|
+
BadRequestException,
|
3
|
+
createParamDecorator,
|
4
|
+
ExecutionContext,
|
5
|
+
} from '@nestjs/common';
|
2
6
|
import {
|
3
7
|
DEFAULT_PAGE,
|
4
8
|
DEFAULT_PAGE_SIZE,
|
5
9
|
} from '../constants/pagination.constants';
|
6
10
|
import { PageOrderDirection, PaginationField } from '../enums/patination.enums';
|
7
11
|
import { PaginationType } from '../types/pagination.types';
|
12
|
+
import { plainToClass } from 'class-transformer';
|
13
|
+
import { PaginationDTO } from '../dto/pagination.dto';
|
14
|
+
import { validateSync } from 'class-validator';
|
8
15
|
|
9
16
|
export const Pagination = createParamDecorator(
|
10
17
|
(data: PaginationField, ctx: ExecutionContext): PaginationType => {
|
@@ -38,25 +45,40 @@ export const Pagination = createParamDecorator(
|
|
38
45
|
? sortOrder
|
39
46
|
: defaultOptions.sortOrder;
|
40
47
|
|
48
|
+
const finalData = {
|
49
|
+
page,
|
50
|
+
pageSize,
|
51
|
+
search,
|
52
|
+
sortField,
|
53
|
+
sortOrder: validSortOrder,
|
54
|
+
fields,
|
55
|
+
};
|
56
|
+
|
57
|
+
const paginationDtoInstance = plainToClass(PaginationDTO, finalData);
|
58
|
+
|
59
|
+
const errors = validateSync(paginationDtoInstance);
|
60
|
+
|
61
|
+
if (errors.length > 0) {
|
62
|
+
throw new BadRequestException(
|
63
|
+
'Pagination data is not valid according to PaginationDto: ' +
|
64
|
+
errors
|
65
|
+
.map((error) => Object.values(error.constraints).join(', '))
|
66
|
+
.join(', '),
|
67
|
+
);
|
68
|
+
}
|
69
|
+
|
41
70
|
if (data) {
|
42
71
|
switch (data) {
|
43
72
|
case PaginationField.Page:
|
44
73
|
case PaginationField.PageSize:
|
45
|
-
return
|
46
|
-
case PaginationField.
|
47
|
-
return
|
74
|
+
return finalData[data] ? +finalData[data] : defaultOptions[data];
|
75
|
+
case PaginationField.SortOrder:
|
76
|
+
return validSortOrder || defaultOptions[data];
|
48
77
|
default:
|
49
|
-
return
|
78
|
+
return finalData[data];
|
50
79
|
}
|
51
80
|
}
|
52
81
|
|
53
|
-
return
|
54
|
-
page: +page,
|
55
|
-
pageSize: +pageSize,
|
56
|
-
search,
|
57
|
-
sortField,
|
58
|
-
sortOrder: validSortOrder,
|
59
|
-
fields,
|
60
|
-
};
|
82
|
+
return finalData;
|
61
83
|
},
|
62
84
|
);
|
@@ -3,12 +3,12 @@ import { IsEnum, IsInt, IsOptional, IsString } from 'class-validator';
|
|
3
3
|
import { PageOrderDirection } from '../enums/patination.enums';
|
4
4
|
export class PaginationDTO {
|
5
5
|
@IsOptional()
|
6
|
-
@Transform((value) => Number(value))
|
6
|
+
@Transform(({ value }) => Number(value))
|
7
7
|
@IsInt({ message: 'page must be an integer' })
|
8
8
|
page: number;
|
9
9
|
|
10
10
|
@IsOptional()
|
11
|
-
@Transform((value) => Number(value))
|
11
|
+
@Transform(({ value }) => Number(value))
|
12
12
|
@IsInt({ message: 'pageSize must be an integer' })
|
13
13
|
pageSize: number;
|
14
14
|
|