nestjs-config-extended 0.2.2 → 0.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -71,6 +71,7 @@ To customize module's configuration, you can setup few options by importing it u
71
71
  | `cache` | `true` | `false` | `boolean` | If `false`, values will be stored directly in process.env object. <br> If `true`, values will be stored only in the memory. |
72
72
  | `debug` | `false` | `false` | `boolean` | If `true`, module will log some internal process information. |
73
73
  | `isGlobal` | `false` | `false` | `boolean` | If `true`, registers `ExtendedConfigModule` as a global module. |
74
+ | `preload` | `false` | `false` | `boolean` | If `true`, variables load will be done by the module and injected on service. This option is required if you need to inject the service on a custom provider and retrieve a variable before application is totally ready. |
74
75
 
75
76
  ### Strategies
76
77
 
@@ -145,6 +146,7 @@ import {
145
146
  cache: true,
146
147
  debug: false,
147
148
  isGlobal: true,
149
+ preload: true,
148
150
  strategies: [
149
151
  {
150
152
  identifier: 'DOT_ENV_STRATEGY',
@@ -1,6 +1,8 @@
1
1
  export declare const CRON_SCHEDULER = "CRON_SCHEDULER";
2
2
  export declare const EXTENDED_CONFIG_OPTIONS = "EXTENDED_CONFIG_OPTIONS";
3
+ export declare const PRE_LOADED_VALUES = "PRE_LOADED_VALUES";
3
4
  export declare const NOT_IDENTIFIED = "Not identified";
5
+ export declare const VARIABLES_PRELOADED_BY_MODULE = "Variables was preloaded by module from enabled strategies";
4
6
  export declare const FIRST_LOAD_REQUESTED = "Starting variables loading process";
5
7
  export declare const LOAD_REQUEST_IGNORED_DUE_ALREADY_LOADED = "Load request was ignored since variables already was loaded";
6
8
  export declare const LOAD_REQUEST_IGNORED_DUE_IN_PROGRESS = "Load request was ignored since a loading process is in progress at this moment";
@@ -1,9 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.NOT_LOADED_STRATEGY_DISABLED = exports.ALL_STRATEGIES = exports.STARTING_RELOAD = exports.ASSIGNING_VARIABLES_TO_CACHE = exports.ASSIGNING_VARIABLES_TO_PROCESS_ENV = exports.TRANSFORMING_VARIABLES_FROM_STRATEGY = exports.VALIDATING_VARIABLES_FROM_STRATEGY = exports.RELOADING_VARIABLES_FROM_STRATEGY_BY_SCHEDULER = exports.RELOADING_VARIABLES_FROM_STRATEGY = exports.LOADING_VARIABLES_FROM_STRATEGY = exports.STARTING_STRATEGY = exports.INVALID_VARIABLES = exports.LOAD_REQUEST_IGNORED_DUE_IN_PROGRESS = exports.LOAD_REQUEST_IGNORED_DUE_ALREADY_LOADED = exports.FIRST_LOAD_REQUESTED = exports.NOT_IDENTIFIED = exports.EXTENDED_CONFIG_OPTIONS = exports.CRON_SCHEDULER = void 0;
3
+ exports.NOT_LOADED_STRATEGY_DISABLED = exports.ALL_STRATEGIES = exports.STARTING_RELOAD = exports.ASSIGNING_VARIABLES_TO_CACHE = exports.ASSIGNING_VARIABLES_TO_PROCESS_ENV = exports.TRANSFORMING_VARIABLES_FROM_STRATEGY = exports.VALIDATING_VARIABLES_FROM_STRATEGY = exports.RELOADING_VARIABLES_FROM_STRATEGY_BY_SCHEDULER = exports.RELOADING_VARIABLES_FROM_STRATEGY = exports.LOADING_VARIABLES_FROM_STRATEGY = exports.STARTING_STRATEGY = exports.INVALID_VARIABLES = exports.LOAD_REQUEST_IGNORED_DUE_IN_PROGRESS = exports.LOAD_REQUEST_IGNORED_DUE_ALREADY_LOADED = exports.FIRST_LOAD_REQUESTED = exports.VARIABLES_PRELOADED_BY_MODULE = exports.NOT_IDENTIFIED = exports.PRE_LOADED_VALUES = exports.EXTENDED_CONFIG_OPTIONS = exports.CRON_SCHEDULER = void 0;
4
4
  exports.CRON_SCHEDULER = 'CRON_SCHEDULER';
5
5
  exports.EXTENDED_CONFIG_OPTIONS = 'EXTENDED_CONFIG_OPTIONS';
6
+ exports.PRE_LOADED_VALUES = 'PRE_LOADED_VALUES';
6
7
  exports.NOT_IDENTIFIED = 'Not identified';
8
+ exports.VARIABLES_PRELOADED_BY_MODULE = 'Variables was preloaded by module from enabled strategies';
7
9
  exports.FIRST_LOAD_REQUESTED = 'Starting variables loading process';
8
10
  exports.LOAD_REQUEST_IGNORED_DUE_ALREADY_LOADED = 'Load request was ignored since variables already was loaded';
9
11
  exports.LOAD_REQUEST_IGNORED_DUE_IN_PROGRESS = 'Load request was ignored since a loading process is in progress at this moment';
@@ -4,7 +4,7 @@ import { ExtendedConfigModuleSyncOptions, ExtendedConfigModuleAsyncOptions } fro
4
4
  export declare class ExtendedConfigModule {
5
5
  private extendedConfigService;
6
6
  constructor(extendedConfigService: ExtendedConfigService);
7
- static forRoot(options: ExtendedConfigModuleSyncOptions): DynamicModule;
7
+ static forRoot(options: ExtendedConfigModuleSyncOptions): Promise<DynamicModule>;
8
8
  static forRootAsync(options: ExtendedConfigModuleAsyncOptions): DynamicModule;
9
9
  private static createAsyncProviders;
10
10
  protected onModuleInit(): Promise<void>;
@@ -36,6 +36,9 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
36
36
  step((generator = generator.apply(thisArg, _arguments || [])).next());
37
37
  });
38
38
  };
39
+ var __importDefault = (this && this.__importDefault) || function (mod) {
40
+ return (mod && mod.__esModule) ? mod : { "default": mod };
41
+ };
39
42
  var ExtendedConfigModule_1;
40
43
  Object.defineProperty(exports, "__esModule", { value: true });
41
44
  exports.ExtendedConfigModule = void 0;
@@ -43,23 +46,49 @@ const common_1 = require("@nestjs/common");
43
46
  const config_constants_1 = require("./config.constants");
44
47
  const extended_config_service_1 = require("./extended-config.service");
45
48
  const cron = __importStar(require("node-cron"));
49
+ const loader_util_1 = require("./util/loader.util");
50
+ const lodash_set_1 = __importDefault(require("lodash.set"));
46
51
  let ExtendedConfigModule = ExtendedConfigModule_1 = class ExtendedConfigModule {
47
52
  constructor(extendedConfigService) {
48
53
  this.extendedConfigService = extendedConfigService;
49
54
  }
50
55
  static forRoot(options) {
51
- return {
52
- module: ExtendedConfigModule_1,
53
- global: options.isGlobal,
54
- providers: [
55
- {
56
- provide: config_constants_1.EXTENDED_CONFIG_OPTIONS,
57
- useValue: options,
58
- },
59
- extended_config_service_1.ExtendedConfigService,
60
- ],
61
- exports: [extended_config_service_1.ExtendedConfigService],
62
- };
56
+ return __awaiter(this, void 0, void 0, function* () {
57
+ let preLoadedVariables = null;
58
+ if (options === null || options === void 0 ? void 0 : options.preload) {
59
+ preLoadedVariables = {};
60
+ for (const strategy of options.strategies || []) {
61
+ const { registerAs, disable } = strategy;
62
+ if (!disable) {
63
+ const variables = yield loader_util_1.retrieveVariablesByStrategy(strategy);
64
+ if (registerAs) {
65
+ lodash_set_1.default(preLoadedVariables, registerAs, variables);
66
+ }
67
+ else {
68
+ Object.keys(variables).forEach((key) => {
69
+ lodash_set_1.default(preLoadedVariables, key, variables[key]);
70
+ });
71
+ }
72
+ }
73
+ }
74
+ }
75
+ return {
76
+ module: ExtendedConfigModule_1,
77
+ global: options.isGlobal,
78
+ providers: [
79
+ {
80
+ provide: config_constants_1.EXTENDED_CONFIG_OPTIONS,
81
+ useValue: options,
82
+ },
83
+ {
84
+ provide: config_constants_1.PRE_LOADED_VALUES,
85
+ useValue: preLoadedVariables,
86
+ },
87
+ extended_config_service_1.ExtendedConfigService,
88
+ ],
89
+ exports: [extended_config_service_1.ExtendedConfigService],
90
+ };
91
+ });
63
92
  }
64
93
  static forRootAsync(options) {
65
94
  return {
@@ -84,7 +113,8 @@ let ExtendedConfigModule = ExtendedConfigModule_1 = class ExtendedConfigModule {
84
113
  }
85
114
  onModuleInit() {
86
115
  return __awaiter(this, void 0, void 0, function* () {
87
- yield this.extendedConfigService.load();
116
+ const ensureLoad = true;
117
+ yield this.extendedConfigService.load(ensureLoad);
88
118
  });
89
119
  }
90
120
  };
@@ -100,6 +130,10 @@ ExtendedConfigModule = ExtendedConfigModule_1 = __decorate([
100
130
  provide: config_constants_1.EXTENDED_CONFIG_OPTIONS,
101
131
  useValue: {},
102
132
  },
133
+ {
134
+ provide: config_constants_1.PRE_LOADED_VALUES,
135
+ useValue: null,
136
+ },
103
137
  ],
104
138
  }),
105
139
  __metadata("design:paramtypes", [extended_config_service_1.ExtendedConfigService])
@@ -8,7 +8,7 @@ export declare class ExtendedConfigService<K = Record<string, any>> {
8
8
  private loaded;
9
9
  private loading;
10
10
  private readonly cache;
11
- constructor(options: ExtendedConfigModuleOptions, scheduler: typeof cron, logger: Logger);
11
+ constructor(options: ExtendedConfigModuleOptions, scheduler: typeof cron, logger: Logger, preLoadedValues?: Record<string, any>);
12
12
  private get isLoaded();
13
13
  private set isLoaded(value);
14
14
  private get isLoading();
@@ -22,13 +22,12 @@ export declare class ExtendedConfigService<K = Record<string, any>> {
22
22
  has(propertyPath: keyof K): boolean;
23
23
  private set;
24
24
  private get isCacheEnabled();
25
- private get isDebugEnabled();
26
25
  private loadVariables;
26
+ private configureSchedulerOnPreload;
27
+ private configureScheduler;
27
28
  reload(strategyIdentifier?: string | Symbol): Promise<void>;
28
29
  private loadVariablesByStrategy;
29
30
  private retrieveVariables;
30
- private transformVariables;
31
- private validateVariables;
32
31
  private assignVariables;
33
32
  private debug;
34
33
  }
@@ -33,21 +33,29 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
33
33
  Object.defineProperty(exports, "__esModule", { value: true });
34
34
  exports.ExtendedConfigService = void 0;
35
35
  const common_1 = require("@nestjs/common");
36
- const lodash_isfunction_1 = __importDefault(require("lodash.isfunction"));
37
36
  const util_1 = require("util");
38
37
  const lodash_get_1 = __importDefault(require("lodash.get"));
39
38
  const lodash_has_1 = __importDefault(require("lodash.has"));
40
39
  const lodash_set_1 = __importDefault(require("lodash.set"));
41
40
  const config_constants_1 = require("./config.constants");
42
41
  const exceptions_1 = require("./exceptions");
42
+ const loader_util_1 = require("./util/loader.util");
43
43
  let ExtendedConfigService = class ExtendedConfigService {
44
- constructor(options, scheduler, logger) {
44
+ constructor(options, scheduler, logger, preLoadedValues) {
45
45
  this.options = options;
46
46
  this.scheduler = scheduler;
47
47
  this.logger = logger;
48
48
  this.loaded = false;
49
49
  this.loading = false;
50
50
  this.cache = {};
51
+ if (preLoadedValues) {
52
+ this.debug(config_constants_1.VARIABLES_PRELOADED_BY_MODULE, config_constants_1.ALL_STRATEGIES);
53
+ for (const key of Object.keys(preLoadedValues)) {
54
+ this.set(key, preLoadedValues[key]);
55
+ }
56
+ this.isLoaded = true;
57
+ this.configureSchedulerOnPreload();
58
+ }
51
59
  }
52
60
  get isLoaded() {
53
61
  return this.loaded;
@@ -131,30 +139,18 @@ let ExtendedConfigService = class ExtendedConfigService {
131
139
  get isCacheEnabled() {
132
140
  return this.options.cache === false ? this.options.cache : true;
133
141
  }
134
- get isDebugEnabled() {
135
- return this.options.debug || false;
136
- }
137
142
  loadVariables(onlyReloadable = false) {
138
143
  var e_1, _a;
139
144
  return __awaiter(this, void 0, void 0, function* () {
140
145
  try {
141
146
  for (var _b = __asyncValues(this.options.strategies || []), _c; _c = yield _b.next(), !_c.done;) {
142
147
  const strategy = _c.value;
143
- const { reloadable, identifier, schedule, scheduleTimezone, disable } = strategy;
148
+ const { reloadable, identifier } = strategy;
144
149
  if (!onlyReloadable || reloadable) {
145
150
  this.debug(config_constants_1.STARTING_STRATEGY, identifier);
146
151
  yield this.loadVariablesByStrategy(strategy);
147
- if (!disable && !onlyReloadable && schedule && reloadable) {
148
- if (!this.scheduler.validate(schedule)) {
149
- throw new exceptions_1.InvalidScheduleException(identifier);
150
- }
151
- this.scheduler.schedule(schedule, () => __awaiter(this, void 0, void 0, function* () {
152
- this.debug(config_constants_1.RELOADING_VARIABLES_FROM_STRATEGY_BY_SCHEDULER, identifier);
153
- yield this.loadVariablesByStrategy(strategy);
154
- }), {
155
- scheduled: true,
156
- timezone: scheduleTimezone,
157
- });
152
+ if (!onlyReloadable) {
153
+ this.configureScheduler(strategy);
158
154
  }
159
155
  }
160
156
  }
@@ -168,6 +164,26 @@ let ExtendedConfigService = class ExtendedConfigService {
168
164
  }
169
165
  });
170
166
  }
167
+ configureSchedulerOnPreload() {
168
+ for (const strategy of this.options.strategies || []) {
169
+ this.configureScheduler(strategy);
170
+ }
171
+ }
172
+ configureScheduler(strategy) {
173
+ const { reloadable, identifier, schedule, scheduleTimezone, disable, } = strategy;
174
+ if (!disable && schedule && reloadable) {
175
+ if (!this.scheduler.validate(schedule)) {
176
+ throw new exceptions_1.InvalidScheduleException(identifier);
177
+ }
178
+ this.scheduler.schedule(schedule, () => __awaiter(this, void 0, void 0, function* () {
179
+ this.debug(config_constants_1.RELOADING_VARIABLES_FROM_STRATEGY_BY_SCHEDULER, identifier);
180
+ yield this.loadVariablesByStrategy(strategy);
181
+ }), {
182
+ scheduled: true,
183
+ timezone: scheduleTimezone,
184
+ });
185
+ }
186
+ }
171
187
  reload(strategyIdentifier) {
172
188
  var _a, _b;
173
189
  return __awaiter(this, void 0, void 0, function* () {
@@ -198,85 +214,12 @@ let ExtendedConfigService = class ExtendedConfigService {
198
214
  });
199
215
  }
200
216
  retrieveVariables(strategy) {
217
+ var _a;
201
218
  return __awaiter(this, void 0, void 0, function* () {
202
- const { loader, identifier, options } = strategy;
203
- let loadedVariables = {};
204
- try {
205
- this.debug(config_constants_1.LOADING_VARIABLES_FROM_STRATEGY, identifier);
206
- if (lodash_isfunction_1.default(loader)) {
207
- loadedVariables = yield loader(options);
208
- }
209
- else if (lodash_isfunction_1.default(loader === null || loader === void 0 ? void 0 : loader.load)) {
210
- loadedVariables = yield loader.load(options);
211
- }
212
- else {
213
- throw new exceptions_1.InvalidLoaderException(identifier);
214
- }
215
- }
216
- catch (err) {
217
- if (err instanceof exceptions_1.InvalidLoaderException) {
218
- throw err;
219
- }
220
- throw new exceptions_1.LoadingException(err, identifier);
221
- }
222
- yield this.validateVariables(strategy, loadedVariables);
223
- loadedVariables = yield this.transformVariables(strategy, loadedVariables);
224
- return loadedVariables;
225
- });
226
- }
227
- transformVariables(strategy, variables) {
228
- return __awaiter(this, void 0, void 0, function* () {
229
- const { transformer, identifier } = strategy;
230
- if (transformer) {
231
- this.debug(config_constants_1.TRANSFORMING_VARIABLES_FROM_STRATEGY, identifier);
232
- try {
233
- if (lodash_isfunction_1.default(transformer)) {
234
- variables = yield transformer(variables);
235
- }
236
- else if (lodash_isfunction_1.default(transformer === null || transformer === void 0 ? void 0 : transformer.transform)) {
237
- variables = yield transformer.transform(variables);
238
- }
239
- else {
240
- throw new exceptions_1.InvalidTransformerException(identifier);
241
- }
242
- }
243
- catch (err) {
244
- if (err instanceof exceptions_1.InvalidTransformerException) {
245
- throw err;
246
- }
247
- throw new exceptions_1.TransformationException(err, identifier);
248
- }
249
- }
250
- return variables;
251
- });
252
- }
253
- validateVariables(strategy, variables) {
254
- return __awaiter(this, void 0, void 0, function* () {
255
- const { validator, identifier } = strategy;
256
- let isValid = true;
257
- try {
258
- if (validator) {
259
- this.debug(config_constants_1.VALIDATING_VARIABLES_FROM_STRATEGY, identifier);
260
- if (lodash_isfunction_1.default(validator)) {
261
- isValid = yield validator(variables);
262
- }
263
- else if (lodash_isfunction_1.default(validator === null || validator === void 0 ? void 0 : validator.validate)) {
264
- isValid = yield validator.validate(variables);
265
- }
266
- else {
267
- throw new exceptions_1.InvalidValidatorException(identifier);
268
- }
269
- }
270
- }
271
- catch (err) {
272
- if (err instanceof exceptions_1.InvalidValidatorException) {
273
- throw err;
274
- }
275
- throw new exceptions_1.ValidationException(err, identifier);
276
- }
277
- if (!isValid) {
278
- throw new exceptions_1.ValidationException(config_constants_1.INVALID_VARIABLES, identifier);
219
+ return loader_util_1.retrieveVariablesByStrategy(strategy, ((_a = this.options) === null || _a === void 0 ? void 0 : _a.debug) ? (message, identifier) => {
220
+ this.logger.debug(`[Extended Config Module] ${identifier || config_constants_1.NOT_IDENTIFIED}: ${message}`);
279
221
  }
222
+ : undefined);
280
223
  });
281
224
  }
282
225
  assignVariables(strategy, variables) {
@@ -297,7 +240,8 @@ let ExtendedConfigService = class ExtendedConfigService {
297
240
  }
298
241
  }
299
242
  debug(message, identifier) {
300
- if (this.isDebugEnabled) {
243
+ var _a;
244
+ if ((_a = this.options) === null || _a === void 0 ? void 0 : _a.debug) {
301
245
  this.logger.debug(`[Extended Config Module] ${identifier || config_constants_1.NOT_IDENTIFIED}: ${message}`);
302
246
  }
303
247
  }
@@ -306,6 +250,7 @@ ExtendedConfigService = __decorate([
306
250
  common_1.Injectable(),
307
251
  __param(0, common_1.Inject(config_constants_1.EXTENDED_CONFIG_OPTIONS)),
308
252
  __param(1, common_1.Inject(config_constants_1.CRON_SCHEDULER)),
309
- __metadata("design:paramtypes", [Object, Object, common_1.Logger])
253
+ __param(3, common_1.Inject(config_constants_1.PRE_LOADED_VALUES)),
254
+ __metadata("design:paramtypes", [Object, Object, common_1.Logger, Object])
310
255
  ], ExtendedConfigService);
311
256
  exports.ExtendedConfigService = ExtendedConfigService;
@@ -7,6 +7,7 @@ export interface ExtendedConfigModuleOptions {
7
7
  }
8
8
  export interface ExtendedConfigModuleSyncOptions extends ExtendedConfigModuleOptions {
9
9
  isGlobal?: boolean;
10
+ preload?: boolean;
10
11
  }
11
12
  export interface ExtendedConfigModuleAsyncOptions extends Pick<ModuleMetadata, 'imports'> {
12
13
  isGlobal?: boolean;
@@ -0,0 +1,4 @@
1
+ import { ConfigLoaderStrategy } from '../interfaces';
2
+ export declare const retrieveVariablesByStrategy: (strategy: ConfigLoaderStrategy, debug?: ((message: string, identifier?: string | Symbol | undefined) => void) | undefined) => Promise<Record<string, any>>;
3
+ export declare const validateVariablesByStrategy: (strategy: ConfigLoaderStrategy, variables: Record<string, any>, debug?: ((message: string, identifier?: string | Symbol | undefined) => void) | undefined) => Promise<void>;
4
+ export declare const transformVariablesByStrategy: (strategy: ConfigLoaderStrategy, variables: Record<string, any>, debug?: ((message: string, identifier?: string | Symbol | undefined) => void) | undefined) => Promise<Record<string, any>>;
@@ -0,0 +1,103 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.transformVariablesByStrategy = exports.validateVariablesByStrategy = exports.retrieveVariablesByStrategy = void 0;
16
+ const lodash_isfunction_1 = __importDefault(require("lodash.isfunction"));
17
+ const config_constants_1 = require("../config.constants");
18
+ const exceptions_1 = require("../exceptions");
19
+ const retrieveVariablesByStrategy = (strategy, debug) => __awaiter(void 0, void 0, void 0, function* () {
20
+ const { loader, identifier, options } = strategy;
21
+ let loadedVariables = {};
22
+ try {
23
+ if (debug) {
24
+ debug(config_constants_1.LOADING_VARIABLES_FROM_STRATEGY, identifier);
25
+ }
26
+ if (lodash_isfunction_1.default(loader)) {
27
+ loadedVariables = yield loader(options);
28
+ }
29
+ else if (lodash_isfunction_1.default(loader === null || loader === void 0 ? void 0 : loader.load)) {
30
+ loadedVariables = yield loader.load(options);
31
+ }
32
+ else {
33
+ throw new exceptions_1.InvalidLoaderException(identifier);
34
+ }
35
+ }
36
+ catch (err) {
37
+ if (err instanceof exceptions_1.InvalidLoaderException) {
38
+ throw err;
39
+ }
40
+ throw new exceptions_1.LoadingException(err, identifier);
41
+ }
42
+ yield exports.validateVariablesByStrategy(strategy, loadedVariables, debug);
43
+ loadedVariables = yield exports.transformVariablesByStrategy(strategy, loadedVariables, debug);
44
+ return loadedVariables;
45
+ });
46
+ exports.retrieveVariablesByStrategy = retrieveVariablesByStrategy;
47
+ const validateVariablesByStrategy = (strategy, variables, debug) => __awaiter(void 0, void 0, void 0, function* () {
48
+ const { validator, identifier } = strategy;
49
+ let isValid = true;
50
+ try {
51
+ if (validator) {
52
+ if (debug) {
53
+ debug(config_constants_1.VALIDATING_VARIABLES_FROM_STRATEGY, identifier);
54
+ }
55
+ if (lodash_isfunction_1.default(validator)) {
56
+ isValid = yield validator(variables);
57
+ }
58
+ else if (lodash_isfunction_1.default(validator === null || validator === void 0 ? void 0 : validator.validate)) {
59
+ isValid = yield validator.validate(variables);
60
+ }
61
+ else {
62
+ throw new exceptions_1.InvalidValidatorException(identifier);
63
+ }
64
+ }
65
+ }
66
+ catch (err) {
67
+ if (err instanceof exceptions_1.InvalidValidatorException) {
68
+ throw err;
69
+ }
70
+ throw new exceptions_1.ValidationException(err, identifier);
71
+ }
72
+ if (!isValid) {
73
+ throw new exceptions_1.ValidationException(config_constants_1.INVALID_VARIABLES, identifier);
74
+ }
75
+ });
76
+ exports.validateVariablesByStrategy = validateVariablesByStrategy;
77
+ const transformVariablesByStrategy = (strategy, variables, debug) => __awaiter(void 0, void 0, void 0, function* () {
78
+ const { transformer, identifier } = strategy;
79
+ if (transformer) {
80
+ if (debug) {
81
+ debug(config_constants_1.TRANSFORMING_VARIABLES_FROM_STRATEGY, identifier);
82
+ }
83
+ try {
84
+ if (lodash_isfunction_1.default(transformer)) {
85
+ variables = yield transformer(variables);
86
+ }
87
+ else if (lodash_isfunction_1.default(transformer === null || transformer === void 0 ? void 0 : transformer.transform)) {
88
+ variables = yield transformer.transform(variables);
89
+ }
90
+ else {
91
+ throw new exceptions_1.InvalidTransformerException(identifier);
92
+ }
93
+ }
94
+ catch (err) {
95
+ if (err instanceof exceptions_1.InvalidTransformerException) {
96
+ throw err;
97
+ }
98
+ throw new exceptions_1.TransformationException(err, identifier);
99
+ }
100
+ }
101
+ return variables;
102
+ });
103
+ exports.transformVariablesByStrategy = transformVariablesByStrategy;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nestjs-config-extended",
3
- "version": "0.2.2",
3
+ "version": "0.3.3",
4
4
  "description": "Extended Configuration Module for Nest to load environment variables package",
5
5
  "author": "Pedro Sodré",
6
6
  "license": "MIT",
@@ -30,8 +30,8 @@
30
30
  "uuid": "8.3.2"
31
31
  },
32
32
  "devDependencies": {
33
- "@nestjs/common": "7.6.5",
34
- "@nestjs/core": "7.6.5",
33
+ "@nestjs/common": "^7.6.5",
34
+ "@nestjs/core": "^7.6.5",
35
35
  "@nestjs/platform-express": "^7.6.5",
36
36
  "@nestjs/testing": "^7.6.5",
37
37
  "@types/jest": "^26.0.20",