UNPKG

2.34 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const constants_1 = require("../../constants");
4const shared_utils_1 = require("../../utils/shared.utils");
5/**
6 * Decorator that marks a constructor parameter as a target for
7 * [Dependency Injection (DI)](https://docs.nestjs.com/providers#dependency-injection).
8 *
9 * Any injected provider must be visible within the module scope (loosely
10 * speaking, the containing module) of the class it is being injected into. This
11 * can be done by:
12 *
13 * - defining the provider in the same module scope
14 * - exporting the provider from one module scope and importing that module into the
15 * module scope of the class being injected into
16 * - exporting the provider from a module that is marked as global using the
17 * `@Global()` decorator
18 *
19 * #### Injection tokens
20 * Can be *types* (class names), *strings* or *symbols*. This depends on how the
21 * provider with which it is associated was defined. Providers defined with the
22 * `@Injectable()` decorator use the class name. Custom Providers may use strings
23 * or symbols as the injection token.
24 *
25 * @param token lookup key for the provider to be injected (assigned to the constructor
26 * parameter).
27 *
28 * @see [Providers](https://docs.nestjs.com/providers)
29 * @see [Custom Providers](https://docs.nestjs.com/fundamentals/custom-providers)
30 * @see [Injection Scopes](https://docs.nestjs.com/fundamentals/injection-scopes)
31 *
32 * @publicApi
33 */
34function Inject(token) {
35 return (target, key, index) => {
36 token = token || Reflect.getMetadata('design:type', target, key);
37 const type = token && shared_utils_1.isFunction(token) ? token.name : token;
38 if (!shared_utils_1.isUndefined(index)) {
39 let dependencies = Reflect.getMetadata(constants_1.SELF_DECLARED_DEPS_METADATA, target) || [];
40 dependencies = [...dependencies, { index, param: type }];
41 Reflect.defineMetadata(constants_1.SELF_DECLARED_DEPS_METADATA, dependencies, target);
42 return;
43 }
44 let properties = Reflect.getMetadata(constants_1.PROPERTY_DEPS_METADATA, target.constructor) || [];
45 properties = [...properties, { key, type }];
46 Reflect.defineMetadata(constants_1.PROPERTY_DEPS_METADATA, properties, target.constructor);
47 };
48}
49exports.Inject = Inject;