UNPKG

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