1 | "use strict";
|
2 | var __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 | };
|
8 | var __metadata = (this && this.__metadata) || function (k, v) {
|
9 | if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
10 | };
|
11 | var __param = (this && this.__param) || function (paramIndex, decorator) {
|
12 | return function (target, key) { decorator(target, key, paramIndex); }
|
13 | };
|
14 | Object.defineProperty(exports, "__esModule", { value: true });
|
15 | exports.ConfigService = void 0;
|
16 | const common_1 = require("@nestjs/common");
|
17 | const shared_utils_1 = require("@nestjs/common/utils/shared.utils");
|
18 | const lodash_1 = require("lodash");
|
19 | const config_constants_1 = require("./config.constants");
|
20 | let 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 |
|
34 |
|
35 |
|
36 |
|
37 |
|
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 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 | getOrThrow(propertyPath, defaultValueOrOptions, options) {
|
66 |
|
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 | };
|
107 | ConfigService = __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);
|
113 | exports.ConfigService = ConfigService;
|