UNPKG

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