UNPKG

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