1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.ParseArrayPipe = 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 | const shared_utils_1 = require("../utils/shared.utils");
|
10 | const validation_pipe_1 = require("./validation.pipe");
|
11 | const VALIDATION_ERROR_MESSAGE = 'Validation failed (parsable array expected)';
|
12 | const DEFAULT_ARRAY_SEPARATOR = ',';
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | let ParseArrayPipe = class ParseArrayPipe {
|
21 | constructor(options = {}) {
|
22 | this.options = options;
|
23 | this.validationPipe = new validation_pipe_1.ValidationPipe(Object.assign({ transform: true, validateCustomDecorators: true }, options));
|
24 | const { exceptionFactory, errorHttpStatusCode = http_status_enum_1.HttpStatus.BAD_REQUEST, } = options;
|
25 | this.exceptionFactory =
|
26 | exceptionFactory ||
|
27 | (error => new http_error_by_code_util_1.HttpErrorByCode[errorHttpStatusCode](error));
|
28 | }
|
29 | |
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 | async transform(value, metadata) {
|
37 | if (!value && !this.options.optional) {
|
38 | throw this.exceptionFactory(VALIDATION_ERROR_MESSAGE);
|
39 | }
|
40 | else if (shared_utils_1.isNil(value) && this.options.optional) {
|
41 | return value;
|
42 | }
|
43 | if (!Array.isArray(value)) {
|
44 | if (!shared_utils_1.isString(value)) {
|
45 | throw this.exceptionFactory(VALIDATION_ERROR_MESSAGE);
|
46 | }
|
47 | else {
|
48 | try {
|
49 | value = value
|
50 | .trim()
|
51 | .split(this.options.separator || DEFAULT_ARRAY_SEPARATOR);
|
52 | }
|
53 | catch (_a) {
|
54 | throw this.exceptionFactory(VALIDATION_ERROR_MESSAGE);
|
55 | }
|
56 | }
|
57 | }
|
58 | if (this.options.items) {
|
59 | const validationMetadata = {
|
60 | metatype: this.options.items,
|
61 | type: 'query',
|
62 | };
|
63 | const toClassInstance = (item) => {
|
64 | try {
|
65 | item = JSON.parse(item);
|
66 | }
|
67 | catch (_a) { }
|
68 | return this.validationPipe.transform(item, validationMetadata);
|
69 | };
|
70 | if (this.options.stopAtFirstError === false) {
|
71 |
|
72 |
|
73 | let errors = [];
|
74 | const targetArray = value;
|
75 | for (let i = 0; i < targetArray.length; i++) {
|
76 | try {
|
77 | targetArray[i] = await toClassInstance(targetArray[i]);
|
78 | }
|
79 | catch (err) {
|
80 | let message;
|
81 | if (err.getResponse) {
|
82 | const response = err.getResponse();
|
83 | if (Array.isArray(response.message)) {
|
84 | message = response.message.map((item) => `[${i}] ${item}`);
|
85 | }
|
86 | else {
|
87 | message = `[${i}] ${response.message}`;
|
88 | }
|
89 | }
|
90 | else {
|
91 | message = err;
|
92 | }
|
93 | errors = errors.concat(message);
|
94 | }
|
95 | }
|
96 | if (errors.length > 0) {
|
97 | throw this.exceptionFactory(errors);
|
98 | }
|
99 | return targetArray;
|
100 | }
|
101 | else {
|
102 | value = await Promise.all(value.map(toClassInstance));
|
103 | }
|
104 | }
|
105 | return value;
|
106 | }
|
107 | };
|
108 | ParseArrayPipe = tslib_1.__decorate([
|
109 | injectable_decorator_1.Injectable(),
|
110 | tslib_1.__param(0, optional_decorator_1.Optional()),
|
111 | tslib_1.__metadata("design:paramtypes", [Object])
|
112 | ], ParseArrayPipe);
|
113 | exports.ParseArrayPipe = ParseArrayPipe;
|