@hedhog/pagination 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1 +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"}
1
+ {"version":3,"file":"pagination.decorator.d.ts","sourceRoot":"","sources":["../../src/decorator/pagination.decorator.ts"],"names":[],"mappings":"AAYA,OAAO,EAAsB,eAAe,EAAE,MAAM,2BAA2B,CAAC;AAGhF,eAAO,MAAM,UAAU,kMAoEtB,CAAC"}
@@ -2,11 +2,11 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Pagination = void 0;
4
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
5
  const class_transformer_1 = require("class-transformer");
8
- const pagination_dto_1 = require("../dto/pagination.dto");
9
6
  const class_validator_1 = require("class-validator");
7
+ const pagination_constants_1 = require("../constants/pagination.constants");
8
+ const pagination_dto_1 = require("../dto/pagination.dto");
9
+ const patination_enums_1 = require("../enums/patination.enums");
10
10
  exports.Pagination = (0, common_1.createParamDecorator)((data, ctx) => {
11
11
  const request = ctx.switchToHttp().getRequest();
12
12
  const defaultOptions = {
@@ -1 +1 @@
1
- {"version":3,"file":"pagination.decorator.js","sourceRoot":"","sources":["../../src/decorator/pagination.decorator.ts"],"names":[],"mappings":";;;AAAA,2CAIwB;AACxB,4EAG2C;AAC3C,gEAAgF;AAEhF,yDAAiD;AACjD,0DAAsD;AACtD,qDAA+C;AAElC,QAAA,UAAU,GAAG,IAAA,6BAAoB,EAC5C,CAAC,IAAqB,EAAE,GAAqB,EAAkB,EAAE;IAC/D,MAAM,OAAO,GAAG,GAAG,CAAC,YAAY,EAAE,CAAC,UAAU,EAAE,CAAC;IAEh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nE,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"}
@@ -3,5 +3,7 @@ export declare class PaginationService {
3
3
  private readonly logger;
4
4
  paginate<T, M extends BaseModel>(model: M, paginationParams: PaginationParams, customQuery?: FindManyArgs<M>): Promise<PaginatedResult<T>>;
5
5
  extractFieldNames(model: Record<string, any>): string[];
6
+ isInvalidField(sortField: string, model: BaseModel): boolean;
7
+ isInvalidFields(fields: string[], model: BaseModel): boolean;
6
8
  }
7
9
  //# sourceMappingURL=pagination.service.d.ts.map
@@ -1 +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"}
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;IAuG9B,iBAAiB,CAAC,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,MAAM,EAAE;IAcvD,cAAc,CAAC,SAAS,EAAE,MAAM,EAAE,KAAK,EAAE,SAAS,GAAG,OAAO;IAI5D,eAAe,CAAC,MAAM,EAAE,MAAM,EAAE,EAAE,KAAK,EAAE,SAAS,GAAG,OAAO;CAG7D"}
@@ -32,23 +32,25 @@ let PaginationService = PaginationService_1 = class PaginationService {
32
32
  let sortOrderCondition = {
33
33
  id: paginationParams.sortOrder || patination_enums_1.PageOrderDirection.Asc,
34
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
35
  if (sortField) {
36
+ const invalid = this.isInvalidField(sortField, model);
37
+ if (invalid) {
38
+ this.logger.error(`Invalid field: ${sortField}`);
39
+ throw new common_1.BadRequestException(`Invalid field: ${sortField}. Valid columns are: ${this.extractFieldNames(model).join(', ')}`);
40
+ }
45
41
  sortOrderCondition = { [sortField]: sortOrder };
46
42
  }
43
+ if (search) {
44
+ if (typeof search !== 'string') {
45
+ this.logger.error('Search must be a string');
46
+ throw new common_1.BadRequestException('Search must be a string');
47
+ }
48
+ }
47
49
  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(', ')}`);
50
+ const invalidFields = this.isInvalidFields(fields, model);
51
+ if (invalidFields) {
52
+ this.logger.error(`Invalid fields: ${sortField}. Valid columns are: ${this.extractFieldNames(model).join(', ')}`);
53
+ throw new common_1.BadRequestException(`Invalid fields: ${sortField}. Valid columns are: ${this.extractFieldNames(model).join(', ')}`);
52
54
  }
53
55
  selectCondition = fields.reduce((acc, field) => {
54
56
  acc[field] = true;
@@ -96,6 +98,12 @@ let PaginationService = PaginationService_1 = class PaginationService {
96
98
  }
97
99
  return fieldNames;
98
100
  }
101
+ isInvalidField(sortField, model) {
102
+ return !model.fields[sortField];
103
+ }
104
+ isInvalidFields(fields, model) {
105
+ return !fields.every((field) => model.fields[field]);
106
+ }
99
107
  };
100
108
  exports.PaginationService = PaginationService;
101
109
  exports.PaginationService = PaginationService = PaginationService_1 = __decorate([
@@ -1 +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,IA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C;YACvB,CAAC;QACH,CAAC;QAED,OAAO,UAAU,CAAC;IACpB,CAAC;IAED,cAAc,CAAC,SAAiB,EAAE,KAAgB;QAChD,OAAO,CAAC,KAAK,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;IAClC,CAAC;IAED,eAAe,CAAC,MAAgB,EAAE,KAAgB;QAChD,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC;IACvD,CAAC;CACF,CAAA;AAnIY,8CAAiB;4BAAjB,iBAAiB;IAD7B,IAAA,mBAAU,GAAE;GACA,iBAAiB,CAmI7B"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hedhog/pagination",
3
- "version": "0.0.2",
3
+ "version": "0.0.3",
4
4
  "private": false,
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -3,15 +3,15 @@ import {
3
3
  createParamDecorator,
4
4
  ExecutionContext,
5
5
  } from '@nestjs/common';
6
+ import { plainToClass } from 'class-transformer';
7
+ import { validateSync } from 'class-validator';
6
8
  import {
7
9
  DEFAULT_PAGE,
8
10
  DEFAULT_PAGE_SIZE,
9
11
  } from '../constants/pagination.constants';
12
+ import { PaginationDTO } from '../dto/pagination.dto';
10
13
  import { PageOrderDirection, PaginationField } from '../enums/patination.enums';
11
14
  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';
15
15
 
16
16
  export const Pagination = createParamDecorator(
17
17
  (data: PaginationField, ctx: ExecutionContext): PaginationType => {
@@ -41,34 +41,44 @@ export class PaginationService {
41
41
  id: paginationParams.sortOrder || PageOrderDirection.Asc,
42
42
  };
43
43
 
44
- if (search && sortField) {
45
- const fieldNames = this.extractFieldNames(model);
46
-
47
- if (!fieldNames.includes(sortField)) {
44
+ if (sortField) {
45
+ const invalid = this.isInvalidField(sortField, model);
46
+ if (invalid) {
47
+ this.logger.error(`Invalid field: ${sortField}`);
48
48
  throw new BadRequestException(
49
- `Invalid field: ${sortField}. Valid columns are: ${fieldNames.join(', ')}`,
49
+ `Invalid field: ${sortField}. Valid columns are: ${this.extractFieldNames(
50
+ model,
51
+ ).join(', ')}`,
50
52
  );
51
53
  }
52
54
 
53
- if (typeof sortField !== 'string') {
54
- throw new BadRequestException('Field must be a string');
55
- }
55
+ sortOrderCondition = { [sortField]: sortOrder };
56
56
  }
57
57
 
58
- if (sortField) {
59
- sortOrderCondition = { [sortField]: sortOrder };
58
+ if (search) {
59
+ if (typeof search !== 'string') {
60
+ this.logger.error('Search must be a string');
61
+ throw new BadRequestException('Search must be a string');
62
+ }
60
63
  }
61
64
 
62
65
  if (fields) {
63
- const fieldNames = this.extractFieldNames(model);
64
- const invalidFields = fields.filter(
65
- (field) => !fieldNames.includes(field),
66
- );
67
- if (invalidFields.length > 0) {
66
+ const invalidFields = this.isInvalidFields(fields, model);
67
+
68
+ if (invalidFields) {
69
+ this.logger.error(
70
+ `Invalid fields: ${sortField}. Valid columns are: ${this.extractFieldNames(
71
+ model,
72
+ ).join(', ')}`,
73
+ );
74
+
68
75
  throw new BadRequestException(
69
- `Invalid fields: ${invalidFields.join(', ')}. Valid columns are: ${fieldNames.join(', ')}`,
76
+ `Invalid fields: ${sortField}. Valid columns are: ${this.extractFieldNames(
77
+ model,
78
+ ).join(', ')}`,
70
79
  );
71
80
  }
81
+
72
82
  selectCondition = fields.reduce((acc, field) => {
73
83
  acc[field] = true;
74
84
  return acc;
@@ -125,4 +135,12 @@ export class PaginationService {
125
135
 
126
136
  return fieldNames;
127
137
  }
138
+
139
+ isInvalidField(sortField: string, model: BaseModel): boolean {
140
+ return !model.fields[sortField];
141
+ }
142
+
143
+ isInvalidFields(fields: string[], model: BaseModel): boolean {
144
+ return !fields.every((field) => model.fields[field]);
145
+ }
128
146
  }