UNPKG

1.12 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 * Parameter decorator for an injected dependency marking the
7 * dependency as optional.
8 *
9 * For example:
10 * ```typescript
11 * constructor(@Optional() @Inject('HTTP_OPTIONS')private readonly httpClient: T) {}
12 * ```
13 *
14 * @see [Optional providers](https://docs.nestjs.com/providers#optional-providers)
15 *
16 * @publicApi
17 */
18function Optional() {
19 return (target, key, index) => {
20 if (!shared_utils_1.isUndefined(index)) {
21 const args = Reflect.getMetadata(constants_1.OPTIONAL_DEPS_METADATA, target) || [];
22 Reflect.defineMetadata(constants_1.OPTIONAL_DEPS_METADATA, [...args, index], target);
23 return;
24 }
25 const properties = Reflect.getMetadata(constants_1.OPTIONAL_PROPERTY_DEPS_METADATA, target.constructor) || [];
26 Reflect.defineMetadata(constants_1.OPTIONAL_PROPERTY_DEPS_METADATA, [...properties, key], target.constructor);
27 };
28}
29exports.Optional = Optional;