UNPKG

4.59 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.ParseArrayPipe = void 0;
4const tslib_1 = require("tslib");
5const injectable_decorator_1 = require("../decorators/core/injectable.decorator");
6const optional_decorator_1 = require("../decorators/core/optional.decorator");
7const http_status_enum_1 = require("../enums/http-status.enum");
8const http_error_by_code_util_1 = require("../utils/http-error-by-code.util");
9const shared_utils_1 = require("../utils/shared.utils");
10const validation_pipe_1 = require("./validation.pipe");
11const VALIDATION_ERROR_MESSAGE = 'Validation failed (parsable array expected)';
12const DEFAULT_ARRAY_SEPARATOR = ',';
13/**
14 * Defines the built-in ParseArray Pipe
15 *
16 * @see [Built-in Pipes](https://docs.nestjs.com/pipes#built-in-pipes)
17 *
18 * @publicApi
19 */
20let 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 * Method that accesses and performs optional transformation on argument for
31 * in-flight requests.
32 *
33 * @param value currently processed route argument
34 * @param metadata contains metadata about the currently processed route argument
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 // strict compare to "false" to make sure
72 // that this option is disabled by default
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};
108ParseArrayPipe = 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);
113exports.ParseArrayPipe = ParseArrayPipe;