UNPKG

5.26 kBJavaScriptView Raw
1"use strict";
2var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3 var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4 if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5 else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6 return c > 3 && r && Object.defineProperty(target, key, r), r;
7};
8var __metadata = (this && this.__metadata) || function (k, v) {
9 if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10};
11var __param = (this && this.__param) || function (paramIndex, decorator) {
12 return function (target, key) { decorator(target, key, paramIndex); }
13};
14Object.defineProperty(exports, "__esModule", { value: true });
15exports.ConfigService = void 0;
16const common_1 = require("@nestjs/common");
17const shared_utils_1 = require("@nestjs/common/utils/shared.utils");
18const lodash_1 = require("lodash");
19const config_constants_1 = require("./config.constants");
20let ConfigService = class ConfigService {
21 constructor(internalConfig = {}) {
22 this.internalConfig = internalConfig;
23 this.cache = {};
24 this._isCacheEnabled = false;
25 }
26 set isCacheEnabled(value) {
27 this._isCacheEnabled = value;
28 }
29 get isCacheEnabled() {
30 return this._isCacheEnabled;
31 }
32 /**
33 * Get a configuration value (either custom configuration or process environment variable)
34 * based on property path (you can use dot notation to traverse nested object, e.g. "database.host").
35 * It returns a default value if the key does not exist.
36 * @param propertyPath
37 * @param defaultValueOrOptions
38 */
39 get(propertyPath, defaultValueOrOptions, options) {
40 const validatedEnvValue = this.getFromValidatedEnv(propertyPath);
41 if (!(0, shared_utils_1.isUndefined)(validatedEnvValue)) {
42 return validatedEnvValue;
43 }
44 const defaultValue = this.isGetOptionsObject(defaultValueOrOptions) && !options
45 ? undefined
46 : defaultValueOrOptions;
47 const processEnvValue = this.getFromProcessEnv(propertyPath, defaultValue);
48 if (!(0, shared_utils_1.isUndefined)(processEnvValue)) {
49 return processEnvValue;
50 }
51 const internalValue = this.getFromInternalConfig(propertyPath);
52 if (!(0, shared_utils_1.isUndefined)(internalValue)) {
53 return internalValue;
54 }
55 return defaultValue;
56 }
57 /**
58 * Get a configuration value (either custom configuration or process environment variable)
59 * based on property path (you can use dot notation to traverse nested object, e.g. "database.host").
60 * It returns a default value if the key does not exist.
61 * If the default value is undefined an exception will be thrown.
62 * @param propertyPath
63 * @param defaultValueOrOptions
64 */
65 getOrThrow(propertyPath, defaultValueOrOptions, options) {
66 // @ts-expect-error Bypass method overloads
67 const value = this.get(propertyPath, defaultValueOrOptions, options);
68 if ((0, shared_utils_1.isUndefined)(value)) {
69 throw new TypeError(`Configuration key "${propertyPath.toString()}" does not exist`);
70 }
71 return value;
72 }
73 getFromCache(propertyPath, defaultValue) {
74 const cachedValue = (0, lodash_1.get)(this.cache, propertyPath);
75 return (0, shared_utils_1.isUndefined)(cachedValue)
76 ? defaultValue
77 : cachedValue;
78 }
79 getFromValidatedEnv(propertyPath) {
80 const validatedEnvValue = (0, lodash_1.get)(this.internalConfig[config_constants_1.VALIDATED_ENV_PROPNAME], propertyPath);
81 return validatedEnvValue;
82 }
83 getFromProcessEnv(propertyPath, defaultValue) {
84 if (this.isCacheEnabled &&
85 (0, lodash_1.has)(this.cache, propertyPath)) {
86 const cachedValue = this.getFromCache(propertyPath, defaultValue);
87 return !(0, shared_utils_1.isUndefined)(cachedValue) ? cachedValue : defaultValue;
88 }
89 const processValue = (0, lodash_1.get)(process.env, propertyPath);
90 this.setInCacheIfDefined(propertyPath, processValue);
91 return processValue;
92 }
93 getFromInternalConfig(propertyPath) {
94 const internalValue = (0, lodash_1.get)(this.internalConfig, propertyPath);
95 return internalValue;
96 }
97 setInCacheIfDefined(propertyPath, value) {
98 if (typeof value === 'undefined') {
99 return;
100 }
101 (0, lodash_1.set)(this.cache, propertyPath, value);
102 }
103 isGetOptionsObject(options) {
104 return options && (options === null || options === void 0 ? void 0 : options.infer) && Object.keys(options).length === 1;
105 }
106};
107ConfigService = __decorate([
108 (0, common_1.Injectable)(),
109 __param(0, (0, common_1.Optional)()),
110 __param(0, (0, common_1.Inject)(config_constants_1.CONFIGURATION_TOKEN)),
111 __metadata("design:paramtypes", [Object])
112], ConfigService);
113exports.ConfigService = ConfigService;