UNPKG

2.1 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.UseInterceptors = 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 interceptors to the scope of the controller or method,
10 * depending on its context.
11 *
12 * When `@UseInterceptors` is used at the controller level, the interceptor will
13 * be applied to every handler (method) in the controller.
14 *
15 * When `@UseInterceptors` is used at the individual handler level, the interceptor
16 * will apply only to that specific method.
17 *
18 * @param interceptors a single interceptor instance or class, or a list of
19 * interceptor instances or classes.
20 *
21 * @see [Interceptors](https://docs.nestjs.com/interceptors)
22 *
23 * @usageNotes
24 * Interceptors can also be set up globally for all controllers and routes
25 * using `app.useGlobalInterceptors()`. [See here for details](https://docs.nestjs.com/interceptors#binding-interceptors)
26 *
27 * @publicApi
28 */
29function UseInterceptors(...interceptors) {
30 return (target, key, descriptor) => {
31 const isInterceptorValid = (interceptor) => interceptor &&
32 ((0, shared_utils_1.isFunction)(interceptor) ||
33 (0, shared_utils_1.isFunction)(interceptor.intercept));
34 if (descriptor) {
35 (0, validate_each_util_1.validateEach)(target.constructor, interceptors, isInterceptorValid, '@UseInterceptors', 'interceptor');
36 (0, extend_metadata_util_1.extendArrayMetadata)(constants_1.INTERCEPTORS_METADATA, interceptors, descriptor.value);
37 return descriptor;
38 }
39 (0, validate_each_util_1.validateEach)(target, interceptors, isInterceptorValid, '@UseInterceptors', 'interceptor');
40 (0, extend_metadata_util_1.extendArrayMetadata)(constants_1.INTERCEPTORS_METADATA, interceptors, target);
41 return target;
42 };
43}
44exports.UseInterceptors = UseInterceptors;