UNPKG

1.67 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const constants_1 = require("../../constants");
4const extend_metadata_util_1 = require("../../utils/extend-metadata.util");
5const shared_utils_1 = require("../../utils/shared.utils");
6const validate_each_util_1 = require("../../utils/validate-each.util");
7/**
8 * Decorator that binds pipes to the scope of the controller or method,
9 * depending on its context.
10 *
11 * When `@UsePipes` is used at the controller level, the pipe will be
12 * applied to every handler (method) in the controller.
13 *
14 * When `@UsePipes` is used at the individual handler level, the pipe
15 * will apply only to that specific method.
16 *
17 * @param pipes a single pipe instance or class, or a list of pipe instances or
18 * classes.
19 *
20 * @see [Pipes](https://docs.nestjs.com/pipes)
21 *
22 * @usageNotes
23 * Pipes can also be set up globally for all controllers and routes
24 * using `app.useGlobalPipes()`. [See here for details](https://docs.nestjs.com/pipes#class-validator)
25 *
26 * @publicApi
27 */
28function UsePipes(...pipes) {
29 return (target, key, descriptor) => {
30 const isPipeValid = (pipe) => pipe &&
31 (shared_utils_1.isFunction(pipe) || shared_utils_1.isFunction(pipe.transform));
32 if (descriptor) {
33 extend_metadata_util_1.extendArrayMetadata(constants_1.PIPES_METADATA, pipes, descriptor.value);
34 return descriptor;
35 }
36 validate_each_util_1.validateEach(target, pipes, isPipeValid, '@UsePipes', 'pipe');
37 extend_metadata_util_1.extendArrayMetadata(constants_1.PIPES_METADATA, pipes, target);
38 return target;
39 };
40}
41exports.UsePipes = UsePipes;