eh-commons 0.0.1-testing.21 → 0.0.1-testing.23

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.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "eh-commons",
3
3
  "type": "commonjs",
4
- "version": "0.0.1-testing.21",
4
+ "version": "0.0.1-testing.23",
5
5
  "description": "eh-commons",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",
package/src/index.ts CHANGED
@@ -10,9 +10,11 @@ import { StateParamDTO } from './models/dtos/state-params.dto';
10
10
  import { RecordState } from './models/enums/record-state.enum';
11
11
  import { IRecord } from './models/interfaces/record.interface';
12
12
  import {
13
+ IMongoPage,
13
14
  RESTError,
14
15
  RESTPage,
15
16
  RESTPaging,
17
+ RESTQueryParams,
16
18
  RESTRequestBody,
17
19
  RESTResponseBody,
18
20
  RESTResult,
@@ -265,4 +267,6 @@ export {
265
267
  Dictionary,
266
268
  DictionarySchema,
267
269
  DictionaryDocument,
270
+ RESTQueryParams,
271
+ IMongoPage,
268
272
  };
@@ -1,6 +1,6 @@
1
1
  import { HttpStatus } from '@nestjs/common';
2
2
  import { Type } from 'class-transformer';
3
- import { IsArray, IsEnum, IsNotEmpty, IsNumber, IsOptional, IsString, ValidateNested } from 'class-validator';
3
+ import { IsArray, IsEnum, IsInt, IsNotEmpty, IsNumber, IsOptional, IsString, Max, Min, ValidateNested } from 'class-validator';
4
4
 
5
5
  export enum SortDirection {
6
6
  asc = 1,
@@ -18,19 +18,34 @@ export class RESTSort {
18
18
  direction: SortDirection;
19
19
  }
20
20
 
21
+ export interface IMongoPage {
22
+ skip: number;
23
+ limit: number;
24
+ }
25
+
21
26
  export class RESTPaging {
22
27
  @IsNotEmpty()
23
- @IsNumber()
28
+ @IsInt()
29
+ @Type(() => Number)
30
+ @Min(1)
24
31
  page: number;
25
32
 
26
33
  @IsNotEmpty()
27
- @IsNumber()
34
+ @IsInt()
35
+ @Type(() => Number)
36
+ @Min(1)
37
+ @Max(100)
28
38
  size: number;
29
39
 
30
40
  public extract() {
31
41
  const skip = (this.page - 1) * this.size;
32
42
  return { skip, limit: this.size };
33
43
  }
44
+
45
+ public static generate(page: number, size: number): IMongoPage {
46
+ const skip = (page - 1) * size;
47
+ return { skip, limit: size };
48
+ }
34
49
  }
35
50
 
36
51
  export abstract class RESTRequestBody {
@@ -121,11 +136,15 @@ export class RESTError {
121
136
  validation?: any;
122
137
  statusCode?: HttpStatus;
123
138
 
124
- constructor(keyword: string, debug?: any) {
139
+ constructor(keyword: string, debug?: any, statusCode?: HttpStatus) {
125
140
  this.keyword = keyword;
126
141
  this.debug = debug;
142
+ this.statusCode = statusCode;
127
143
  }
128
144
 
145
+ /**
146
+ * @deprecated The method should not be used.. use new RESTError('error_keyword', 'debug message', HttpStatus.BAD_REQUEST);
147
+ */
129
148
  public static throw(keyword: string, statusCode?: HttpStatus, debug?: any) {
130
149
  const error = new RESTError(keyword, debug);
131
150
  error.statusCode = statusCode || HttpStatus.BAD_REQUEST;
@@ -133,7 +152,7 @@ export class RESTError {
133
152
  }
134
153
 
135
154
  public static dataValidation(validation: any) {
136
- const error = new RESTError('body_validation_error', null);
155
+ const error = new RESTError('body_validation_error', null, 422);
137
156
  error.validation = validation;
138
157
  return error;
139
158
  }
@@ -170,3 +189,49 @@ export class RESTResponseBody<T> {
170
189
  return response;
171
190
  }
172
191
  }
192
+
193
+ export class RESTQueryParams extends RESTPaging {
194
+
195
+ @IsOptional()
196
+ @IsString({ each: true })
197
+ sort: string[];
198
+
199
+ public getRESTSort(): RESTSort[] {
200
+ const result = [];
201
+ const items = [];
202
+ if (this.sort) {
203
+ if (this.sort instanceof Array) {
204
+ items.push(...this.sort);
205
+ } else {
206
+ items.push(this.sort);
207
+ }
208
+ }
209
+
210
+ for (let item of items) {
211
+ const pair = item.split(':');
212
+ if (pair.length != 2) {
213
+ throw new RESTError('invalid_sorting_pair', pair, 422);
214
+ }
215
+
216
+ if (['asc', 'desc'].indexOf(pair[1]) === -1) {
217
+ throw new RESTError('invalid_sorting_direction', pair[1], 422);
218
+ }
219
+
220
+ result.push({
221
+ property: pair[0],
222
+ direction: pair[1] === 'asc' ? 1 : -1,
223
+ });
224
+ }
225
+
226
+ return result;
227
+ }
228
+
229
+ public extractSort() {
230
+ const sorts = this.getRESTSort();
231
+ const result = {};
232
+ for (let item of sorts) {
233
+ result[item.property] = item.direction;
234
+ }
235
+ return result;
236
+ }
237
+ }