UNPKG

1.15 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.Optional = void 0;
4const constants_1 = require("../../constants");
5const shared_utils_1 = require("../../utils/shared.utils");
6/**
7 * Parameter decorator for an injected dependency marking the
8 * dependency as optional.
9 *
10 * For example:
11 * ```typescript
12 * constructor(@Optional() @Inject('HTTP_OPTIONS')private readonly httpClient: T) {}
13 * ```
14 *
15 * @see [Optional providers](https://docs.nestjs.com/providers#optional-providers)
16 *
17 * @publicApi
18 */
19function Optional() {
20 return (target, key, index) => {
21 if (!(0, shared_utils_1.isUndefined)(index)) {
22 const args = Reflect.getMetadata(constants_1.OPTIONAL_DEPS_METADATA, target) || [];
23 Reflect.defineMetadata(constants_1.OPTIONAL_DEPS_METADATA, [...args, index], target);
24 return;
25 }
26 const properties = Reflect.getMetadata(constants_1.OPTIONAL_PROPERTY_DEPS_METADATA, target.constructor) || [];
27 Reflect.defineMetadata(constants_1.OPTIONAL_PROPERTY_DEPS_METADATA, [...properties, key], target.constructor);
28 };
29}
30exports.Optional = Optional;