1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.ParseIntPipe = void 0;
|
4 | const tslib_1 = require("tslib");
|
5 | const injectable_decorator_1 = require("../decorators/core/injectable.decorator");
|
6 | const optional_decorator_1 = require("../decorators/core/optional.decorator");
|
7 | const http_status_enum_1 = require("../enums/http-status.enum");
|
8 | const http_error_by_code_util_1 = require("../utils/http-error-by-code.util");
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | let ParseIntPipe = class ParseIntPipe {
|
17 | constructor(options) {
|
18 | options = options || {};
|
19 | const { exceptionFactory, errorHttpStatusCode = http_status_enum_1.HttpStatus.BAD_REQUEST } = options;
|
20 | this.exceptionFactory =
|
21 | exceptionFactory ||
|
22 | (error => new http_error_by_code_util_1.HttpErrorByCode[errorHttpStatusCode](error));
|
23 | }
|
24 | |
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | async transform(value, metadata) {
|
32 | if (!this.isNumeric(value)) {
|
33 | throw this.exceptionFactory('Validation failed (numeric string is expected)');
|
34 | }
|
35 | return parseInt(value, 10);
|
36 | }
|
37 | |
38 |
|
39 |
|
40 |
|
41 | isNumeric(value) {
|
42 | return (['string', 'number'].includes(typeof value) &&
|
43 | /^-?\d+$/.test(value) &&
|
44 | isFinite(value));
|
45 | }
|
46 | };
|
47 | ParseIntPipe = tslib_1.__decorate([
|
48 | (0, injectable_decorator_1.Injectable)(),
|
49 | tslib_1.__param(0, (0, optional_decorator_1.Optional)()),
|
50 | tslib_1.__metadata("design:paramtypes", [Object])
|
51 | ], ParseIntPipe);
|
52 | exports.ParseIntPipe = ParseIntPipe;
|