nestjs-config-extended 0.3.1 → 1.0.1
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 +102 -104
- package/dist/extended-config.service.d.ts +2 -0
- package/dist/extended-config.service.js +24 -12
- package/package.json +8 -8
package/README.md
CHANGED
|
@@ -17,12 +17,16 @@
|
|
|
17
17
|
<img src="https://img.shields.io/github/issues-raw/pedrosodre/nestjs-config-extended" alt="Issues" />
|
|
18
18
|
<img src="https://img.shields.io/github/last-commit/pedrosodre/nestjs-config-extended" alt="GitHub Last Commit" />
|
|
19
19
|
</div>
|
|
20
|
+
<br />
|
|
21
|
+
|
|
22
|
+
> :warning: **Attention:** To support NestJS 8, this library will keep two stable versions. For NestJS 7, use `0.x` version.
|
|
20
23
|
|
|
21
24
|
## Description
|
|
22
25
|
|
|
23
26
|
Extended Configuration Module for [Nest](https://github.com/nestjs/nest) based on the [Nest's Configuration Module](https://docs.nestjs.com/techniques/configuration) to load environment variables.
|
|
24
27
|
|
|
25
28
|
## Installation
|
|
29
|
+
|
|
26
30
|
To begin using it, we first install the module itself.
|
|
27
31
|
|
|
28
32
|
```bash
|
|
@@ -38,7 +42,7 @@ import { Module } from '@nestjs/common';
|
|
|
38
42
|
import { ExtendedConfigModule } from 'nestjs-config-extended';
|
|
39
43
|
|
|
40
44
|
@Module({
|
|
41
|
-
|
|
45
|
+
imports: [ExtendedConfigModule],
|
|
42
46
|
})
|
|
43
47
|
export class AppModule {}
|
|
44
48
|
```
|
|
@@ -50,11 +54,11 @@ import { Module } from '@nestjs/common';
|
|
|
50
54
|
import { ExtendedConfigModule } from 'nestjs-config-extended';
|
|
51
55
|
|
|
52
56
|
@Module({
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
57
|
+
imports: [
|
|
58
|
+
ExtendedConfigModule.forRoot({
|
|
59
|
+
cache: false,
|
|
60
|
+
}),
|
|
61
|
+
],
|
|
58
62
|
})
|
|
59
63
|
export class AppModule {}
|
|
60
64
|
```
|
|
@@ -65,30 +69,30 @@ If needed, you can also initialize `ExtendedConfigModule` using `.forRootAsync()
|
|
|
65
69
|
|
|
66
70
|
To customize module's configuration, you can setup few options by importing it using one of `.forRoot()` or `.forRootAsync()` method.
|
|
67
71
|
|
|
68
|
-
| Option
|
|
69
|
-
|
|
|
70
|
-
| `strategies` | `[]`
|
|
71
|
-
| `cache`
|
|
72
|
-
| `debug`
|
|
73
|
-
| `isGlobal`
|
|
74
|
-
| `preload`
|
|
72
|
+
| Option | Default value | Required | Type | Description |
|
|
73
|
+
| ------------ | ------------- | -------- | ------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
74
|
+
| `strategies` | `[]` | `false` | `ConfigLoaderStrategy[]` | Array with all strategies used to load configuration variables. |
|
|
75
|
+
| `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. |
|
|
76
|
+
| `debug` | `false` | `false` | `boolean` | If `true`, module will log some internal process information. |
|
|
77
|
+
| `isGlobal` | `false` | `false` | `boolean` | If `true`, registers `ExtendedConfigModule` as a global module. |
|
|
78
|
+
| `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. |
|
|
75
79
|
|
|
76
80
|
### Strategies
|
|
77
81
|
|
|
78
82
|
To keep everything as flexible as possible, `ExtendedConfigModule` works with an array of strategies, that are specialized objects in load environment variables. Each strategy has its own options, as described below:
|
|
79
83
|
|
|
80
|
-
| Option
|
|
81
|
-
|
|
|
82
|
-
| `loader`
|
|
83
|
-
| `identifier`
|
|
84
|
-
| `disable`
|
|
85
|
-
| `options`
|
|
86
|
-
| `reloadable`
|
|
87
|
-
| `schedule`
|
|
88
|
-
| `scheduleTimezone` | -
|
|
89
|
-
| `registerAs`
|
|
90
|
-
| `transformer`
|
|
91
|
-
| `validator`
|
|
84
|
+
| Option | Default value | Required | Type | Description |
|
|
85
|
+
| ------------------ | ------------- | -------- | --------------------- | ------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
86
|
+
| `loader` | - | `true` | `LoaderClass | LoaderMethod` | Class or function responsible for loading environment variables for this strategy. Accepts a custom function or class with load() method. |
|
|
87
|
+
| `identifier` | - | `false` | `string | Symbol` | Identify a strategy to allow module to manipulate it directly on helper functions. |
|
|
88
|
+
| `disable` | `false` | `false` | `boolean` | If `true`, this strategy will not used. This flag is useful to perform conditional strategy disabling. |
|
|
89
|
+
| `options` | - | `false` | `Record<string, any>` | Set options to loader class or function. |
|
|
90
|
+
| `reloadable` | `false` | `false` | `boolean` | If `false`, this strategy will not be called again when service or scheduler requests a reload. |
|
|
91
|
+
| `schedule` | - | `false` | `string` | Schedules the strategy reload (if reloadable) using cron syntax. This feature may not work correctly with serverless functions. |
|
|
92
|
+
| `scheduleTimezone` | - | `false` | `Timezone` | Set scheduler timezone. |
|
|
93
|
+
| `registerAs` | - | `false` | `string` | If set, all configuration will be set under the key passed on this variable. |
|
|
94
|
+
| `transformer` | - | `false` | `TransformerClass | TransformerMethod` | If you wish to transform the loaded configuration variables, you can pass a function to handle it. Accepts a function or a class with transform() method. |
|
|
95
|
+
| `validator` | - | `false` | `ValidatorClass | ValidatorMethod` | If you wish to transform the loaded configuration variables, you can pass a function to handle it. Accepts a function or a class with validate() method. |
|
|
92
96
|
|
|
93
97
|
#### Pre-made loaders
|
|
94
98
|
|
|
@@ -135,47 +139,44 @@ The examples below uses all availables options to help you to understand the mod
|
|
|
135
139
|
|
|
136
140
|
```typescript
|
|
137
141
|
import { Module } from '@nestjs/common';
|
|
138
|
-
import {
|
|
139
|
-
ExtendedConfigModule,
|
|
140
|
-
dotEnvLoader,
|
|
141
|
-
} from 'nestjs-config-extended';
|
|
142
|
+
import { ExtendedConfigModule, dotEnvLoader } from 'nestjs-config-extended';
|
|
142
143
|
|
|
143
144
|
@Module({
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
145
|
+
imports: [
|
|
146
|
+
ExtendedConfigModule.forRoot({
|
|
147
|
+
cache: true,
|
|
148
|
+
debug: false,
|
|
149
|
+
isGlobal: true,
|
|
150
|
+
preload: true,
|
|
151
|
+
strategies: [
|
|
152
|
+
{
|
|
153
|
+
identifier: 'DOT_ENV_STRATEGY',
|
|
154
|
+
loader: dotEnvLoader,
|
|
155
|
+
disable: false,
|
|
156
|
+
options: {
|
|
157
|
+
path: '.env',
|
|
158
|
+
encoding: 'UTF-8',
|
|
159
|
+
expandVariables: true,
|
|
160
|
+
},
|
|
161
|
+
reloadable: false,
|
|
162
|
+
schedule: '* * * * *', // Every minute, but will not schedule since strategy is not reloadable
|
|
163
|
+
scheduleTimezone: 'America/Sao_Paulo',
|
|
164
|
+
registerAs: 'anyKey',
|
|
165
|
+
transformer: (variables: Record<string, any>) => {
|
|
166
|
+
variables.number = Number(variables.number);
|
|
167
|
+
return variables;
|
|
168
|
+
},
|
|
169
|
+
validator: (variables: Record<string, any>) => {
|
|
170
|
+
if (variables.number > 0) {
|
|
171
|
+
return true;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
return false;
|
|
175
|
+
},
|
|
176
|
+
},
|
|
177
|
+
],
|
|
178
|
+
}),
|
|
179
|
+
],
|
|
179
180
|
})
|
|
180
181
|
export class AppModule {}
|
|
181
182
|
```
|
|
@@ -184,48 +185,45 @@ export class AppModule {}
|
|
|
184
185
|
|
|
185
186
|
```typescript
|
|
186
187
|
import { Module } from '@nestjs/common';
|
|
187
|
-
import {
|
|
188
|
-
ExtendedConfigModule,
|
|
189
|
-
dotEnvLoader,
|
|
190
|
-
} from 'nestjs-config-extended';
|
|
188
|
+
import { ExtendedConfigModule, dotEnvLoader } from 'nestjs-config-extended';
|
|
191
189
|
|
|
192
190
|
@Module({
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
191
|
+
imports: [
|
|
192
|
+
ExtendedConfigModule.forRootAsync({
|
|
193
|
+
isGlobal: true,
|
|
194
|
+
imports: [SampleStrategyModule],
|
|
197
195
|
inject: [SampleStrategyService],
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
196
|
+
useFactory: async (loaderClass: SampleStrategyService) => {
|
|
197
|
+
return {
|
|
198
|
+
cache: true,
|
|
199
|
+
debug: false,
|
|
200
|
+
strategies: [
|
|
201
|
+
{
|
|
202
|
+
identifier: 'INJECTED_CLASS_STRATEGY',
|
|
203
|
+
loader: loaderClass,
|
|
204
|
+
disable: false,
|
|
205
|
+
options: {},
|
|
206
|
+
reloadable: true,
|
|
207
|
+
schedule: '* * * * *', // Every minute
|
|
208
|
+
scheduleTimezone: 'America/Sao_Paulo',
|
|
209
|
+
registerAs: 'anyKey',
|
|
210
|
+
transformer: (variables: Record<string, any>) => {
|
|
211
|
+
variables.number = Number(variables.number);
|
|
212
|
+
return variables;
|
|
213
|
+
},
|
|
214
|
+
validator: (variables: Record<string, any>) => {
|
|
215
|
+
if (variables.number > 0) {
|
|
216
|
+
return true;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
return false;
|
|
220
|
+
},
|
|
221
|
+
},
|
|
222
|
+
],
|
|
223
|
+
};
|
|
224
|
+
},
|
|
225
|
+
}),
|
|
226
|
+
],
|
|
229
227
|
})
|
|
230
228
|
export class AppModule {}
|
|
231
229
|
```
|
|
@@ -238,4 +236,4 @@ export class AppModule {}
|
|
|
238
236
|
|
|
239
237
|
This module is [MIT licensed](LICENSE).
|
|
240
238
|
|
|
241
|
-
Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please [read more here](https://docs.nestjs.com/support).
|
|
239
|
+
Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please [read more here](https://docs.nestjs.com/support).
|
|
@@ -23,6 +23,8 @@ export declare class ExtendedConfigService<K = Record<string, any>> {
|
|
|
23
23
|
private set;
|
|
24
24
|
private get isCacheEnabled();
|
|
25
25
|
private loadVariables;
|
|
26
|
+
private configureSchedulerOnPreload;
|
|
27
|
+
private configureScheduler;
|
|
26
28
|
reload(strategyIdentifier?: string | Symbol): Promise<void>;
|
|
27
29
|
private loadVariablesByStrategy;
|
|
28
30
|
private retrieveVariables;
|
|
@@ -54,6 +54,7 @@ let ExtendedConfigService = class ExtendedConfigService {
|
|
|
54
54
|
this.set(key, preLoadedValues[key]);
|
|
55
55
|
}
|
|
56
56
|
this.isLoaded = true;
|
|
57
|
+
this.configureSchedulerOnPreload();
|
|
57
58
|
}
|
|
58
59
|
}
|
|
59
60
|
get isLoaded() {
|
|
@@ -144,21 +145,12 @@ let ExtendedConfigService = class ExtendedConfigService {
|
|
|
144
145
|
try {
|
|
145
146
|
for (var _b = __asyncValues(this.options.strategies || []), _c; _c = yield _b.next(), !_c.done;) {
|
|
146
147
|
const strategy = _c.value;
|
|
147
|
-
const { reloadable, identifier
|
|
148
|
+
const { reloadable, identifier } = strategy;
|
|
148
149
|
if (!onlyReloadable || reloadable) {
|
|
149
150
|
this.debug(config_constants_1.STARTING_STRATEGY, identifier);
|
|
150
151
|
yield this.loadVariablesByStrategy(strategy);
|
|
151
|
-
if (!
|
|
152
|
-
|
|
153
|
-
throw new exceptions_1.InvalidScheduleException(identifier);
|
|
154
|
-
}
|
|
155
|
-
this.scheduler.schedule(schedule, () => __awaiter(this, void 0, void 0, function* () {
|
|
156
|
-
this.debug(config_constants_1.RELOADING_VARIABLES_FROM_STRATEGY_BY_SCHEDULER, identifier);
|
|
157
|
-
yield this.loadVariablesByStrategy(strategy);
|
|
158
|
-
}), {
|
|
159
|
-
scheduled: true,
|
|
160
|
-
timezone: scheduleTimezone,
|
|
161
|
-
});
|
|
152
|
+
if (!onlyReloadable) {
|
|
153
|
+
this.configureScheduler(strategy);
|
|
162
154
|
}
|
|
163
155
|
}
|
|
164
156
|
}
|
|
@@ -172,6 +164,26 @@ let ExtendedConfigService = class ExtendedConfigService {
|
|
|
172
164
|
}
|
|
173
165
|
});
|
|
174
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
|
+
}
|
|
175
187
|
reload(strategyIdentifier) {
|
|
176
188
|
var _a, _b;
|
|
177
189
|
return __awaiter(this, void 0, void 0, function* () {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nestjs-config-extended",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "Extended Configuration Module for Nest to load environment variables package",
|
|
5
5
|
"author": "Pedro Sodré",
|
|
6
6
|
"license": "MIT",
|
|
@@ -20,6 +20,8 @@
|
|
|
20
20
|
"release": "npm publish --access=public"
|
|
21
21
|
},
|
|
22
22
|
"dependencies": {
|
|
23
|
+
"@nestjs/common": "^8.0.4",
|
|
24
|
+
"@nestjs/core": "^8.0.4",
|
|
23
25
|
"dotenv": "8.2.0",
|
|
24
26
|
"dotenv-expand": "5.1.0",
|
|
25
27
|
"lodash.get": "4.4.2",
|
|
@@ -27,13 +29,12 @@
|
|
|
27
29
|
"lodash.isfunction": "^3.0.9",
|
|
28
30
|
"lodash.set": "4.3.2",
|
|
29
31
|
"node-cron": "^2.0.3",
|
|
32
|
+
"rxjs": "^7.2.0",
|
|
30
33
|
"uuid": "8.3.2"
|
|
31
34
|
},
|
|
32
35
|
"devDependencies": {
|
|
33
|
-
"@nestjs/
|
|
34
|
-
"@nestjs/
|
|
35
|
-
"@nestjs/platform-express": "^7.6.5",
|
|
36
|
-
"@nestjs/testing": "^7.6.5",
|
|
36
|
+
"@nestjs/platform-express": "^8.0.4",
|
|
37
|
+
"@nestjs/testing": "^8.0.4",
|
|
37
38
|
"@types/jest": "^26.0.20",
|
|
38
39
|
"@types/lodash.get": "4.4.6",
|
|
39
40
|
"@types/lodash.has": "4.5.6",
|
|
@@ -48,15 +49,14 @@
|
|
|
48
49
|
"prettier": "2.2.1",
|
|
49
50
|
"reflect-metadata": "0.1.13",
|
|
50
51
|
"rimraf": "3.0.2",
|
|
51
|
-
"rxjs": "6.6.3",
|
|
52
52
|
"ts-jest": "26.4.4",
|
|
53
53
|
"tslint": "^6.1.3",
|
|
54
54
|
"typescript": "4.1.3"
|
|
55
55
|
},
|
|
56
56
|
"peerDependencies": {
|
|
57
|
-
"@nestjs/common": "^
|
|
57
|
+
"@nestjs/common": "^8.0.4",
|
|
58
58
|
"reflect-metadata": "^0.1.12",
|
|
59
|
-
"rxjs": "^
|
|
59
|
+
"rxjs": "^7.2.0"
|
|
60
60
|
},
|
|
61
61
|
"husky": {
|
|
62
62
|
"hooks": {
|