node-env-resolver 5.0.0 → 6.0.0
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 +403 -291
- package/dist/audit.d.cts +23 -0
- package/dist/audit.d.ts +23 -0
- package/dist/builder.cjs +1 -1
- package/dist/builder.js +1 -1
- package/dist/chunk-4QOKRJ3F.cjs +1 -0
- package/dist/chunk-6D5APQIG.cjs +1 -0
- package/dist/chunk-FH54ESWI.cjs +9 -0
- package/dist/chunk-HHOS5S5T.js +1 -0
- package/dist/chunk-LCW5BDAW.js +15 -0
- package/dist/chunk-PNUGGPRS.cjs +15 -0
- package/dist/chunk-WKIPEKF6.js +1 -0
- package/dist/chunk-XJLMVSPN.js +9 -0
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +72 -66
- package/dist/index.d.ts +72 -66
- package/dist/index.js +1 -1
- package/dist/resolvers.cjs +1 -1
- package/dist/resolvers.d.cts +50 -267
- package/dist/resolvers.d.ts +50 -267
- package/dist/resolvers.js +1 -1
- package/dist/types.cjs +1 -1
- package/dist/types.d.cts +33 -1
- package/dist/types.d.ts +33 -1
- package/dist/types.js +1 -1
- package/dist/utils.cjs +1 -1
- package/dist/utils.d.cts +61 -1
- package/dist/utils.d.ts +61 -1
- package/dist/utils.js +1 -1
- package/dist/valibot.cjs +1 -1
- package/dist/valibot.js +1 -1
- package/dist/validators.cjs +2 -2
- package/dist/validators.d.cts +771 -17
- package/dist/validators.d.ts +771 -17
- package/dist/validators.js +2 -2
- package/dist/web.cjs +4 -4
- package/dist/web.js +4 -4
- package/dist/zod.cjs +1 -1
- package/dist/zod.js +1 -1
- package/package.json +1 -1
- package/dist/chunk-6FXCXA4R.cjs +0 -11
- package/dist/chunk-BFLXBLZ3.cjs +0 -1
- package/dist/chunk-BJ2QNYTS.js +0 -11
- package/dist/chunk-P3TTYVEW.js +0 -16
- package/dist/chunk-UCLEPPLS.cjs +0 -16
- package/dist/chunk-ZMRT37K3.js +0 -1
package/README.md
CHANGED
|
@@ -16,7 +16,7 @@ npm install node-env-resolver
|
|
|
16
16
|
|
|
17
17
|
```ts
|
|
18
18
|
import { resolve } from 'node-env-resolver';
|
|
19
|
-
import { string } from 'node-env-resolver/
|
|
19
|
+
import { string } from 'node-env-resolver/validators';
|
|
20
20
|
|
|
21
21
|
const config = resolve({
|
|
22
22
|
PORT: 3000,
|
|
@@ -25,7 +25,15 @@ const config = resolve({
|
|
|
25
25
|
API_KEY: string({optional:true})
|
|
26
26
|
});
|
|
27
27
|
|
|
28
|
-
//
|
|
28
|
+
// Resulting type (fully inferred):
|
|
29
|
+
// type config = {
|
|
30
|
+
// PORT: number;
|
|
31
|
+
// NODE_ENV: 'development' | 'production' | 'test';
|
|
32
|
+
// DEBUG: boolean;
|
|
33
|
+
// API_KEY: string | undefined;
|
|
34
|
+
// }
|
|
35
|
+
|
|
36
|
+
// TypeScript knows the exact types:
|
|
29
37
|
config.PORT; // number
|
|
30
38
|
config.NODE_ENV; // 'development' | 'production' | 'test'
|
|
31
39
|
config.DEBUG; // boolean
|
|
@@ -35,40 +43,56 @@ config.API_KEY; // string | undefined
|
|
|
35
43
|
## Quick start (with custom resolvers)
|
|
36
44
|
|
|
37
45
|
```ts
|
|
38
|
-
import { resolve, resolveAsync
|
|
39
|
-
import {
|
|
46
|
+
import { resolve, resolveAsync } from 'node-env-resolver';
|
|
47
|
+
import { processEnv, dotenv } from 'node-env-resolver/resolvers';
|
|
48
|
+
import { string, url, postgres, redis } from 'node-env-resolver/validators';
|
|
40
49
|
import { awsSecrets, gcpSecrets, vaultSecrets } from 'node-env-resolver-aws';
|
|
41
50
|
|
|
42
|
-
// Synchronous with custom resolver (
|
|
43
|
-
const config = resolve(
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
]
|
|
51
|
+
// Synchronous with custom resolver (object syntax)
|
|
52
|
+
const config = resolve({
|
|
53
|
+
resolvers: [
|
|
54
|
+
[dotenv(), {
|
|
55
|
+
PORT: 3000,
|
|
56
|
+
NODE_ENV: ['development', 'production', 'test'] as const,
|
|
57
|
+
DEBUG: false,
|
|
58
|
+
API_KEY: string({optional:true})
|
|
59
|
+
}]
|
|
60
|
+
]
|
|
61
|
+
});
|
|
52
62
|
|
|
53
63
|
// Asynchronous with any resolvers (both sync and async work!)
|
|
54
|
-
const config = await resolveAsync(
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
64
|
+
const config = await resolveAsync({
|
|
65
|
+
resolvers: [
|
|
66
|
+
[dotenv(), {
|
|
67
|
+
PORT: 3000,
|
|
68
|
+
NODE_ENV: ['development', 'production', 'test'] as const,
|
|
69
|
+
DEBUG: false,
|
|
70
|
+
API_KEY: string({optional:true})
|
|
71
|
+
}],
|
|
72
|
+
[awsSecrets(), { DATABASE_URL: url() }]
|
|
73
|
+
]
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
// Resulting type (fully inferred):
|
|
77
|
+
// type config = {
|
|
78
|
+
// PORT: number;
|
|
79
|
+
// NODE_ENV: 'development' | 'production' | 'test';
|
|
80
|
+
// DEBUG: boolean;
|
|
81
|
+
// API_KEY: string | undefined;
|
|
82
|
+
// DATABASE_URL: string;
|
|
83
|
+
// }
|
|
84
|
+
|
|
85
|
+
// Multiple resolvers with options
|
|
86
|
+
const config = await resolveAsync({
|
|
87
|
+
resolvers: [
|
|
88
|
+
[processEnv(), { PORT: 3000 }],
|
|
89
|
+
[dotenv(), { DATABASE_URL: postgres() }],
|
|
90
|
+
[awsSecrets(), { API_KEY: string() }],
|
|
91
|
+
[gcpSecrets(), { JWT_SECRET: string() }],
|
|
92
|
+
[vaultSecrets(), { REDIS_URL: redis() }]
|
|
93
|
+
],
|
|
94
|
+
options: { priority: 'last' }
|
|
95
|
+
});
|
|
72
96
|
```
|
|
73
97
|
|
|
74
98
|
## Table of Contents
|
|
@@ -80,7 +104,7 @@ const config = await resolveAsync(
|
|
|
80
104
|
- [Required values](#required-values)
|
|
81
105
|
- [Default values](#default-values)
|
|
82
106
|
- [Optional values](#optional-values)
|
|
83
|
-
- [
|
|
107
|
+
- [OneOf](#oneOf)
|
|
84
108
|
- [Variable Naming Conventions](#variable-naming-conventions)
|
|
85
109
|
- [Performance & Bundle Size](#performance--bundle-size)
|
|
86
110
|
- [Custom validators](#custom-validators)
|
|
@@ -104,7 +128,7 @@ If an environment variable is missing and has no default, validation fails:
|
|
|
104
128
|
|
|
105
129
|
```ts
|
|
106
130
|
import { resolve } from 'node-env-resolver';
|
|
107
|
-
import { postgres, string } from 'node-env-resolver/
|
|
131
|
+
import { postgres, string } from 'node-env-resolver/validators';
|
|
108
132
|
|
|
109
133
|
const config = resolve({
|
|
110
134
|
DATABASE_URL: postgres(), // Required PostgreSQL URL
|
|
@@ -131,7 +155,7 @@ const config = resolve({
|
|
|
131
155
|
Use the `{optional: true}` option to make a value optional:
|
|
132
156
|
|
|
133
157
|
```ts
|
|
134
|
-
import { string, url, number } from 'node-env-resolver/
|
|
158
|
+
import { string, url, number } from 'node-env-resolver/validators';
|
|
135
159
|
|
|
136
160
|
const config = resolve({
|
|
137
161
|
API_KEY: string({optional: true}), // string | undefined
|
|
@@ -140,7 +164,7 @@ const config = resolve({
|
|
|
140
164
|
});
|
|
141
165
|
```
|
|
142
166
|
|
|
143
|
-
###
|
|
167
|
+
### OneOf
|
|
144
168
|
|
|
145
169
|
Use arrays for enum validation:
|
|
146
170
|
|
|
@@ -172,7 +196,7 @@ Variable names can use:
|
|
|
172
196
|
|
|
173
197
|
```ts
|
|
174
198
|
import { resolve } from 'node-env-resolver';
|
|
175
|
-
import { string } from 'node-env-resolver/
|
|
199
|
+
import { string } from 'node-env-resolver/validators';
|
|
176
200
|
|
|
177
201
|
const config = resolve({
|
|
178
202
|
PORT: 3000, // ✅ SCREAMING_SNAKE_CASE (traditional)
|
|
@@ -188,7 +212,7 @@ const config = resolve({
|
|
|
188
212
|
|
|
189
213
|
```ts
|
|
190
214
|
import { resolve } from 'node-env-resolver';
|
|
191
|
-
import { string } from 'node-env-resolver/
|
|
215
|
+
import { string } from 'node-env-resolver/validators';
|
|
192
216
|
|
|
193
217
|
const config = resolve({
|
|
194
218
|
'123PORT': 3000, // ❌ Starts with number
|
|
@@ -208,22 +232,19 @@ const config = resolve({
|
|
|
208
232
|
**Lightweight** The core library is **~4.3KB gzipped** with validation and resolver capabilities - **optimized for minimal bundle impact**.
|
|
209
233
|
|
|
210
234
|
```ts
|
|
211
|
-
import { resolveAsync
|
|
212
|
-
import { dotenv, json
|
|
213
|
-
import {
|
|
214
|
-
import {
|
|
215
|
-
|
|
216
|
-
const config = await resolveAsync(
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
[consul(), { SERVICE_DISCOVERY: json() }],
|
|
223
|
-
[etcd(), { CONFIG_VERSION: string() }],
|
|
224
|
-
[redis(), { CACHE_CONFIG: json() }]
|
|
235
|
+
import { resolveAsync } from 'node-env-resolver';
|
|
236
|
+
import { processEnv, dotenv, json } from 'node-env-resolver/resolvers';
|
|
237
|
+
import { postgres, string, redis } from 'node-env-resolver/validators';
|
|
238
|
+
import { awsSecrets } from 'node-env-resolver-aws';
|
|
239
|
+
|
|
240
|
+
const config = await resolveAsync({
|
|
241
|
+
resolvers: [
|
|
242
|
+
[processEnv(), { PORT: 3000 }],
|
|
243
|
+
[dotenv(), { DATABASE_URL: postgres() }],
|
|
244
|
+
[awsSecrets(), { API_KEY: string() }]
|
|
245
|
+
]
|
|
225
246
|
// Perfect for complex microservice architectures!
|
|
226
|
-
);
|
|
247
|
+
});
|
|
227
248
|
```
|
|
228
249
|
|
|
229
250
|
**Benefits:**
|
|
@@ -251,7 +272,7 @@ Advanced validators are lazy-loaded only when you use them, keeping the base bun
|
|
|
251
272
|
|
|
252
273
|
```ts
|
|
253
274
|
import { resolve, resolveAsync } from 'node-env-resolver';
|
|
254
|
-
import { postgres, url } from 'node-env-resolver/
|
|
275
|
+
import { postgres, url } from 'node-env-resolver/validators';
|
|
255
276
|
import { awsSecrets } from 'node-env-resolver-aws';
|
|
256
277
|
|
|
257
278
|
// Synchronous - works with all types
|
|
@@ -263,10 +284,11 @@ const config = resolve({
|
|
|
263
284
|
});
|
|
264
285
|
|
|
265
286
|
// Also works with async resolvers
|
|
266
|
-
const config = await resolveAsync(
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
]
|
|
287
|
+
const config = await resolveAsync({
|
|
288
|
+
resolvers: [
|
|
289
|
+
[awsSecrets(), { DATABASE_URL: postgres(), API_URL: url() }]
|
|
290
|
+
]
|
|
291
|
+
});
|
|
270
292
|
```
|
|
271
293
|
|
|
272
294
|
### What's in the 3.6KB core?
|
|
@@ -305,16 +327,16 @@ Import only what you need for optimal bundle size:
|
|
|
305
327
|
|
|
306
328
|
```ts
|
|
307
329
|
// Core (~3.6KB) - Main API
|
|
308
|
-
import { resolve, safeResolve
|
|
330
|
+
import { resolve, safeResolve } from 'node-env-resolver';
|
|
309
331
|
|
|
310
332
|
// Resolvers (separate chunk) - .env file parsing
|
|
311
|
-
import { dotenv } from 'node-env-resolver/resolvers';
|
|
333
|
+
import { processEnv, dotenv } from 'node-env-resolver/resolvers';
|
|
312
334
|
|
|
313
335
|
// Utils (separate chunk) - caching & retry logic
|
|
314
336
|
import { cached, retry, TTL, awsCache } from 'node-env-resolver/utils';
|
|
315
337
|
|
|
316
338
|
// Validators (separate chunk) - reusable validation functions
|
|
317
|
-
import {
|
|
339
|
+
import { url, email } from 'node-env-resolver/validators';
|
|
318
340
|
|
|
319
341
|
// Integrations (separate packages)
|
|
320
342
|
import { resolveZod } from 'node-env-resolver/zod';
|
|
@@ -358,11 +380,11 @@ For advanced use cases, you can import and compose the built-in validation funct
|
|
|
358
380
|
|
|
359
381
|
```ts
|
|
360
382
|
import { resolve } from 'node-env-resolver';
|
|
361
|
-
import {
|
|
383
|
+
import { url, email } from 'node-env-resolver/validators';
|
|
362
384
|
|
|
363
385
|
const validateMarketingUrl = (value: string): string => {
|
|
364
386
|
// Reuse built-in URL validator
|
|
365
|
-
const result =
|
|
387
|
+
const result = url(value);
|
|
366
388
|
if (!result.valid) {
|
|
367
389
|
throw new Error(result.error || 'Invalid URL');
|
|
368
390
|
}
|
|
@@ -380,47 +402,49 @@ const config = resolve({
|
|
|
380
402
|
});
|
|
381
403
|
```
|
|
382
404
|
|
|
383
|
-
Available validation functions: `validatePostgres`, `
|
|
405
|
+
Available validation functions: `validatePostgres`, `mysql`, `mongodb`, `redis`, `http`, `https`, `url`, `email`, `port`, `number`, `boolean`, `json`, `date`, `timestamp`.
|
|
384
406
|
|
|
385
407
|
## Multiple sources
|
|
386
408
|
|
|
387
409
|
Load configuration from multiple sources. By default, later sources override earlier ones:
|
|
388
410
|
```ts
|
|
389
|
-
import { resolve, resolveAsync
|
|
390
|
-
import { dotenv, json
|
|
391
|
-
import {
|
|
392
|
-
import {
|
|
411
|
+
import { resolve, resolveAsync } from 'node-env-resolver';
|
|
412
|
+
import { processEnv, dotenv, json } from 'node-env-resolver/resolvers';
|
|
413
|
+
import { postgres, string, redis } from 'node-env-resolver/validators';
|
|
414
|
+
import { awsSecrets } from 'node-env-resolver-aws';
|
|
393
415
|
|
|
394
416
|
// Async mode - supports both sync and async resolvers
|
|
395
|
-
const config = await resolveAsync(
|
|
396
|
-
[
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
417
|
+
const config = await resolveAsync({
|
|
418
|
+
resolvers: [
|
|
419
|
+
[processEnv(), {
|
|
420
|
+
PORT: 3000,
|
|
421
|
+
NODE_ENV: ['development', 'production'] as const,
|
|
422
|
+
}],
|
|
423
|
+
[dotenv(), {
|
|
424
|
+
DATABASE_URL: postgres(),
|
|
425
|
+
API_KEY: string(),
|
|
426
|
+
}]
|
|
427
|
+
]
|
|
428
|
+
});
|
|
405
429
|
|
|
406
430
|
// Sync mode - supports multiple SYNC resolvers
|
|
407
|
-
const config = resolve(
|
|
408
|
-
|
|
409
|
-
|
|
431
|
+
const config = resolve({
|
|
432
|
+
resolvers: [
|
|
433
|
+
[dotenv(), { PORT: 3000 }],
|
|
434
|
+
[json('config.json'), { DATABASE_URL: postgres() }]
|
|
435
|
+
]
|
|
410
436
|
// Both resolvers must have loadSync() method
|
|
411
|
-
);
|
|
437
|
+
});
|
|
412
438
|
|
|
413
439
|
// Support for ANY number of resolvers (3, 4, 5, 10+ resolvers!)
|
|
414
|
-
const config = await resolveAsync(
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
{ priority: 'last' }
|
|
423
|
-
);
|
|
440
|
+
const config = await resolveAsync({
|
|
441
|
+
resolvers: [
|
|
442
|
+
[processEnv(), { PORT: 3000 }],
|
|
443
|
+
[dotenv(), { DATABASE_URL: postgres() }],
|
|
444
|
+
[awsSecrets(), { API_KEY: string() }]
|
|
445
|
+
],
|
|
446
|
+
options: { priority: 'last' }
|
|
447
|
+
});
|
|
424
448
|
```
|
|
425
449
|
|
|
426
450
|
### Controlling merge behaviour
|
|
@@ -428,32 +452,39 @@ const config = await resolveAsync(
|
|
|
428
452
|
Use `priority` to control how resolvers merge values (works with both sync and async modes):
|
|
429
453
|
|
|
430
454
|
```ts
|
|
431
|
-
import { resolve, resolveAsync
|
|
432
|
-
import { dotenv, json
|
|
455
|
+
import { resolve, resolveAsync } from 'node-env-resolver';
|
|
456
|
+
import { processEnv, dotenv, json } from 'node-env-resolver/resolvers';
|
|
457
|
+
import { postgres } from 'node-env-resolver/validators';
|
|
433
458
|
import { awsSecrets } from 'node-env-resolver-aws';
|
|
434
459
|
|
|
435
460
|
// priority: 'last' (default) - later resolvers override earlier ones
|
|
436
|
-
const config = await resolveAsync(
|
|
437
|
-
|
|
438
|
-
|
|
461
|
+
const config = await resolveAsync({
|
|
462
|
+
resolvers: [
|
|
463
|
+
[processEnv(), { DATABASE_URL: postgres() }],
|
|
464
|
+
[awsSecrets(), { DATABASE_URL: postgres() }]
|
|
465
|
+
]
|
|
439
466
|
// AWS wins
|
|
440
|
-
);
|
|
467
|
+
});
|
|
441
468
|
|
|
442
469
|
// priority: 'first' - earlier resolvers take precedence
|
|
443
|
-
const config = await resolveAsync(
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
470
|
+
const config = await resolveAsync({
|
|
471
|
+
resolvers: [
|
|
472
|
+
[dotenv(), { DATABASE_URL: postgres() }],
|
|
473
|
+
[awsSecrets(), { DATABASE_URL: postgres() }]
|
|
474
|
+
],
|
|
475
|
+
options: { priority: 'first' }
|
|
447
476
|
// dotenv wins
|
|
448
|
-
);
|
|
477
|
+
});
|
|
449
478
|
|
|
450
|
-
// Also works with sync resolve()
|
|
451
|
-
const config = resolve(
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
479
|
+
// Also works with sync resolve()
|
|
480
|
+
const config = resolve({
|
|
481
|
+
resolvers: [
|
|
482
|
+
[dotenv(), { DATABASE_URL: postgres() }],
|
|
483
|
+
[json('config.json'), { DATABASE_URL: postgres() }]
|
|
484
|
+
],
|
|
485
|
+
options: { priority: 'first' }
|
|
455
486
|
// dotenv wins
|
|
456
|
-
);
|
|
487
|
+
});
|
|
457
488
|
```
|
|
458
489
|
|
|
459
490
|
This is useful for development workflows where local overrides should take precedence over cloud secrets.
|
|
@@ -468,16 +499,18 @@ When using `priority: 'first'`, resolvers are called sequentially, but execution
|
|
|
468
499
|
|
|
469
500
|
```ts
|
|
470
501
|
import { resolveAsync } from 'node-env-resolver';
|
|
471
|
-
import { dotenv
|
|
472
|
-
import {
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
502
|
+
import { dotenv } from 'node-env-resolver/resolvers';
|
|
503
|
+
import { postgres, string } from 'node-env-resolver/validators';
|
|
504
|
+
import { awsSecrets } from 'node-env-resolver-aws';
|
|
505
|
+
|
|
506
|
+
const config = await resolveAsync({
|
|
507
|
+
resolvers: [
|
|
508
|
+
[dotenv(), { DATABASE_URL: postgres(), API_KEY: string(), PORT: 3000 }],
|
|
509
|
+
[awsSecrets(), { DATABASE_URL: postgres(), API_KEY: string(), PORT: 3000 }]
|
|
510
|
+
],
|
|
511
|
+
options: { priority: 'first' }
|
|
512
|
+
});
|
|
513
|
+
// If dotenv() provides all required keys, awsSecrets() is never called!
|
|
481
514
|
```
|
|
482
515
|
|
|
483
516
|
This is particularly valuable in development where:
|
|
@@ -497,15 +530,16 @@ When using `priority: 'last'` (the default), all resolvers are called **in paral
|
|
|
497
530
|
|
|
498
531
|
```ts
|
|
499
532
|
import { resolveAsync } from 'node-env-resolver';
|
|
500
|
-
import { postgres, string } from 'node-env-resolver/
|
|
501
|
-
import { awsSecrets,
|
|
533
|
+
import { postgres, string } from 'node-env-resolver/validators';
|
|
534
|
+
import { awsSecrets, awsSsm } from 'node-env-resolver-aws';
|
|
502
535
|
|
|
503
|
-
const config = await resolveAsync(
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
536
|
+
const config = await resolveAsync({
|
|
537
|
+
resolvers: [
|
|
538
|
+
[awsSecrets(), { DATABASE_URL: postgres() }], // 100ms
|
|
539
|
+
[awsSsm(), { API_KEY: string() }], // 100ms
|
|
540
|
+
]
|
|
507
541
|
// Default: priority: 'last'
|
|
508
|
-
);
|
|
542
|
+
});
|
|
509
543
|
// Total time: ~100ms (parallel) instead of ~300ms (sequential)
|
|
510
544
|
```
|
|
511
545
|
|
|
@@ -522,14 +556,23 @@ Since `priority: 'last'` means "last write wins", the order doesn't matter for c
|
|
|
522
556
|
Like Zod's `safeParse()`, use `safeResolve()` to get a result object instead of throwing:
|
|
523
557
|
|
|
524
558
|
```ts
|
|
525
|
-
import { safeResolve } from 'node-env-resolver';
|
|
526
|
-
import { number, postgres } from 'node-env-resolver/
|
|
559
|
+
import { safeResolve, safeResolveAsync } from 'node-env-resolver';
|
|
560
|
+
import { number, postgres } from 'node-env-resolver/validators';
|
|
527
561
|
|
|
562
|
+
// Synchronous safe resolve
|
|
528
563
|
const result = safeResolve({
|
|
529
564
|
PORT: number(),
|
|
530
565
|
DATABASE_URL: postgres()
|
|
531
566
|
});
|
|
532
567
|
|
|
568
|
+
// Asynchronous safe resolve
|
|
569
|
+
const result = await safeResolveAsync({
|
|
570
|
+
resolvers: [
|
|
571
|
+
[processEnv(), { PORT: number() }],
|
|
572
|
+
[awsSecrets(), { DATABASE_URL: postgres() }]
|
|
573
|
+
]
|
|
574
|
+
});
|
|
575
|
+
|
|
533
576
|
if (result.success) {
|
|
534
577
|
// Use result.data with full type safety
|
|
535
578
|
console.log(result.data.PORT);
|
|
@@ -543,7 +586,7 @@ if (result.success) {
|
|
|
543
586
|
All functions have safe variants:
|
|
544
587
|
|
|
545
588
|
- `resolve()` → `safeResolve()` (both synchronous)
|
|
546
|
-
- `resolveAsync()` → `
|
|
589
|
+
- `resolveAsync()` → `safeResolveAsync()` (both async)
|
|
547
590
|
|
|
548
591
|
## Synchronous resolution
|
|
549
592
|
|
|
@@ -551,7 +594,7 @@ All functions have safe variants:
|
|
|
551
594
|
|
|
552
595
|
```ts
|
|
553
596
|
import { resolve } from 'node-env-resolver';
|
|
554
|
-
import { string, url, postgres } from 'node-env-resolver/
|
|
597
|
+
import { string, url, postgres } from 'node-env-resolver/validators';
|
|
555
598
|
|
|
556
599
|
// Synchronous - no await needed, works with ALL types
|
|
557
600
|
const config = resolve({
|
|
@@ -567,14 +610,17 @@ const config = resolve({
|
|
|
567
610
|
`resolveAsync()` is **async** when using custom resolvers:
|
|
568
611
|
|
|
569
612
|
```ts
|
|
570
|
-
import { resolveAsync
|
|
571
|
-
import {
|
|
613
|
+
import { resolveAsync } from 'node-env-resolver';
|
|
614
|
+
import { processEnv, dotenv } from 'node-env-resolver/resolvers';
|
|
615
|
+
import { postgres, url } from 'node-env-resolver/validators';
|
|
572
616
|
|
|
573
617
|
// Async - await required when using custom resolvers
|
|
574
|
-
const config = await resolveAsync(
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
)
|
|
618
|
+
const config = await resolveAsync({
|
|
619
|
+
resolvers: [
|
|
620
|
+
[processEnv(), { PORT: 3000 }],
|
|
621
|
+
[dotenv(), { DATABASE_URL: postgres(), API_URL: url() }]
|
|
622
|
+
]
|
|
623
|
+
});
|
|
578
624
|
```
|
|
579
625
|
|
|
580
626
|
## Advanced features
|
|
@@ -584,24 +630,27 @@ const config = await resolveAsync(
|
|
|
584
630
|
Parse command-line arguments as environment variables - perfect for CLI tools:
|
|
585
631
|
|
|
586
632
|
```ts
|
|
587
|
-
import { resolveAsync
|
|
588
|
-
import {
|
|
633
|
+
import { resolveAsync } from 'node-env-resolver';
|
|
634
|
+
import { processEnv } from 'node-env-resolver/resolvers';
|
|
635
|
+
import { postgres } from 'node-env-resolver/validators';
|
|
589
636
|
import { cliArgs } from 'node-env-resolver/cli';
|
|
590
637
|
|
|
591
638
|
// $ node app.js --port 8080 --database-url postgres://localhost --verbose
|
|
592
639
|
|
|
593
|
-
const config = await resolveAsync(
|
|
594
|
-
[
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
640
|
+
const config = await resolveAsync({
|
|
641
|
+
resolvers: [
|
|
642
|
+
[processEnv(), {
|
|
643
|
+
PORT: 3000,
|
|
644
|
+
DATABASE_URL: postgres(),
|
|
645
|
+
VERBOSE: false
|
|
646
|
+
}],
|
|
647
|
+
[cliArgs(), {
|
|
648
|
+
PORT: 3000,
|
|
649
|
+
DATABASE_URL: postgres(),
|
|
650
|
+
VERBOSE: false
|
|
651
|
+
}]
|
|
652
|
+
]
|
|
653
|
+
});
|
|
605
654
|
|
|
606
655
|
// config.PORT === 8080 (from CLI)
|
|
607
656
|
// config.DATABASE_URL === 'postgres://localhost' (from CLI)
|
|
@@ -787,8 +836,9 @@ if (result.success) {
|
|
|
787
836
|
Create resolvers to load configuration from any source:
|
|
788
837
|
|
|
789
838
|
```ts
|
|
790
|
-
import { resolveAsync,
|
|
791
|
-
import {
|
|
839
|
+
import { resolveAsync, type Resolver } from 'node-env-resolver';
|
|
840
|
+
import { processEnv } from 'node-env-resolver/resolvers';
|
|
841
|
+
import { string } from 'node-env-resolver/validators';
|
|
792
842
|
|
|
793
843
|
const databaseResolver: Resolver = {
|
|
794
844
|
name: 'database',
|
|
@@ -798,10 +848,12 @@ const databaseResolver: Resolver = {
|
|
|
798
848
|
}
|
|
799
849
|
};
|
|
800
850
|
|
|
801
|
-
const config = await resolveAsync(
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
)
|
|
851
|
+
const config = await resolveAsync({
|
|
852
|
+
resolvers: [
|
|
853
|
+
[processEnv(), { PORT: 3000 }],
|
|
854
|
+
[databaseResolver, { API_KEY: string() }]
|
|
855
|
+
]
|
|
856
|
+
});
|
|
805
857
|
```
|
|
806
858
|
|
|
807
859
|
### AWS Secrets Manager
|
|
@@ -816,7 +868,7 @@ Single source:
|
|
|
816
868
|
|
|
817
869
|
```ts
|
|
818
870
|
import { resolveSecrets } from 'node-env-resolver-aws';
|
|
819
|
-
import { postgres, string } from 'node-env-resolver/
|
|
871
|
+
import { postgres, string } from 'node-env-resolver/validators';
|
|
820
872
|
|
|
821
873
|
const config = await resolveSecrets({
|
|
822
874
|
secretId: 'myapp/secrets'
|
|
@@ -829,15 +881,18 @@ const config = await resolveSecrets({
|
|
|
829
881
|
Multiple sources:
|
|
830
882
|
|
|
831
883
|
```ts
|
|
832
|
-
import { resolveAsync
|
|
833
|
-
import {
|
|
884
|
+
import { resolveAsync } from 'node-env-resolver';
|
|
885
|
+
import { processEnv } from 'node-env-resolver/resolvers';
|
|
886
|
+
import { postgres, string } from 'node-env-resolver/validators';
|
|
834
887
|
import { awsSecrets, awsSsm } from 'node-env-resolver-aws';
|
|
835
888
|
|
|
836
|
-
const config = await resolveAsync(
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
)
|
|
889
|
+
const config = await resolveAsync({
|
|
890
|
+
resolvers: [
|
|
891
|
+
[processEnv(), { PORT: 3000 }],
|
|
892
|
+
[awsSecrets({ secretId: 'app/secrets' }), { DATABASE_URL: postgres() }],
|
|
893
|
+
[awsSsm({ path: '/app/config' }), { API_KEY: string() }]
|
|
894
|
+
]
|
|
895
|
+
});
|
|
841
896
|
```
|
|
842
897
|
|
|
843
898
|
See the [AWS package documentation](../node-env-resolver-aws/README.md) for details on credentials, caching, and best practices.
|
|
@@ -849,19 +904,21 @@ Cache expensive operations like AWS API calls:
|
|
|
849
904
|
```ts
|
|
850
905
|
import { resolveAsync } from 'node-env-resolver';
|
|
851
906
|
import { cached, TTL } from 'node-env-resolver/utils';
|
|
852
|
-
import { postgres, string } from 'node-env-resolver/
|
|
907
|
+
import { postgres, string } from 'node-env-resolver/validators';
|
|
853
908
|
import { awsSecrets } from 'node-env-resolver-aws';
|
|
854
909
|
|
|
855
910
|
export const getConfig = async () => {
|
|
856
|
-
return await resolveAsync(
|
|
857
|
-
[
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
911
|
+
return await resolveAsync({
|
|
912
|
+
resolvers: [
|
|
913
|
+
[cached(
|
|
914
|
+
awsSecrets({ secretId: 'app/secrets' }),
|
|
915
|
+
{ ttl: TTL.minutes5 }
|
|
916
|
+
), {
|
|
917
|
+
DATABASE_URL: postgres(),
|
|
918
|
+
API_KEY: string(),
|
|
919
|
+
}]
|
|
920
|
+
]
|
|
921
|
+
});
|
|
865
922
|
};
|
|
866
923
|
|
|
867
924
|
// Call getConfig() in your handlers
|
|
@@ -878,6 +935,25 @@ Performance:
|
|
|
878
935
|
|
|
879
936
|
**Important:** Call `resolve()` every time. The `cached()` wrapper handles the caching.
|
|
880
937
|
|
|
938
|
+
**Advanced caching options:**
|
|
939
|
+
|
|
940
|
+
```ts
|
|
941
|
+
import { cached, TTL } from 'node-env-resolver/utils';
|
|
942
|
+
|
|
943
|
+
// Stale-while-revalidate: serve stale data while refreshing in background
|
|
944
|
+
const resolver = cached(awsSecrets(), {
|
|
945
|
+
ttl: TTL.minutes5, // Fresh for 5 minutes
|
|
946
|
+
maxAge: TTL.hour, // Force refresh after 1 hour
|
|
947
|
+
staleWhileRevalidate: true // Serve stale data during refresh
|
|
948
|
+
});
|
|
949
|
+
|
|
950
|
+
// Custom cache key for debugging
|
|
951
|
+
const resolver = cached(awsSecrets(), {
|
|
952
|
+
ttl: TTL.minutes15,
|
|
953
|
+
key: 'prod-secrets-v2'
|
|
954
|
+
});
|
|
955
|
+
```
|
|
956
|
+
|
|
881
957
|
## API Reference
|
|
882
958
|
|
|
883
959
|
### Function variants
|
|
@@ -898,7 +974,7 @@ import {
|
|
|
898
974
|
string, number, boolean, url, email, port, json,
|
|
899
975
|
postgres, mysql, mongodb, redis,
|
|
900
976
|
http, https, date, timestamp, duration, file
|
|
901
|
-
} from 'node-env-resolver/
|
|
977
|
+
} from 'node-env-resolver/validators';
|
|
902
978
|
```
|
|
903
979
|
|
|
904
980
|
| Syntax | Type | Description |
|
|
@@ -942,18 +1018,20 @@ const config = resolve(schema, {
|
|
|
942
1018
|
enableAudit: true
|
|
943
1019
|
});
|
|
944
1020
|
|
|
945
|
-
// Multiple sources - options
|
|
946
|
-
const config = await resolveAsync(
|
|
947
|
-
[
|
|
948
|
-
|
|
949
|
-
|
|
1021
|
+
// Multiple sources - options in config object
|
|
1022
|
+
const config = await resolveAsync({
|
|
1023
|
+
resolvers: [
|
|
1024
|
+
[resolver1, schema],
|
|
1025
|
+
[resolver2, schema]
|
|
1026
|
+
],
|
|
1027
|
+
options: {
|
|
950
1028
|
interpolate: false,
|
|
951
1029
|
strict: true,
|
|
952
1030
|
policies: {...},
|
|
953
1031
|
enableAudit: true,
|
|
954
1032
|
priority: 'last'
|
|
955
1033
|
}
|
|
956
|
-
);
|
|
1034
|
+
});
|
|
957
1035
|
```
|
|
958
1036
|
|
|
959
1037
|
**Note:** The `resolvers` option has been removed. For single source resolution, use `resolve()` directly (defaults to `process.env`). For multiple sources, use `resolveAsync()` syntax.
|
|
@@ -970,7 +1048,7 @@ const config = await resolveAsync(
|
|
|
970
1048
|
|
|
971
1049
|
```ts
|
|
972
1050
|
import { resolve } from 'node-env-resolver';
|
|
973
|
-
import { url } from 'node-env-resolver/
|
|
1051
|
+
import { url } from 'node-env-resolver/validators';
|
|
974
1052
|
|
|
975
1053
|
// With interpolation
|
|
976
1054
|
process.env.BASE_URL = 'https://api.example.com';
|
|
@@ -1011,18 +1089,22 @@ const flakyResolver = {
|
|
|
1011
1089
|
};
|
|
1012
1090
|
|
|
1013
1091
|
// ❌ Throws immediately
|
|
1014
|
-
await resolveAsync(
|
|
1015
|
-
[
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1092
|
+
await resolveAsync({
|
|
1093
|
+
resolvers: [
|
|
1094
|
+
[flakyResolver, schema],
|
|
1095
|
+
[processEnv(), schema]
|
|
1096
|
+
],
|
|
1097
|
+
options: { strict: true } // default
|
|
1098
|
+
});
|
|
1019
1099
|
|
|
1020
1100
|
// ✅ Continues with processEnv()
|
|
1021
|
-
await resolveAsync(
|
|
1022
|
-
[
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1101
|
+
await resolveAsync({
|
|
1102
|
+
resolvers: [
|
|
1103
|
+
[flakyResolver, schema],
|
|
1104
|
+
[processEnv(), schema]
|
|
1105
|
+
],
|
|
1106
|
+
options: { strict: false } // graceful degradation
|
|
1107
|
+
});
|
|
1026
1108
|
```
|
|
1027
1109
|
|
|
1028
1110
|
**Note:** In sync mode with `strict: true`, resolvers without `loadSync()` method will throw errors.
|
|
@@ -1041,23 +1123,28 @@ await resolveAsync(
|
|
|
1041
1123
|
**Default:** `'last'`
|
|
1042
1124
|
|
|
1043
1125
|
```ts
|
|
1044
|
-
import { resolveAsync
|
|
1045
|
-
import {
|
|
1126
|
+
import { resolveAsync } from 'node-env-resolver';
|
|
1127
|
+
import { processEnv, dotenv } from 'node-env-resolver/resolvers';
|
|
1128
|
+
import { postgres } from 'node-env-resolver/validators';
|
|
1046
1129
|
import { awsSecrets } from 'node-env-resolver-aws';
|
|
1047
1130
|
|
|
1048
1131
|
// Production: AWS secrets override process.env
|
|
1049
|
-
await resolveAsync(
|
|
1050
|
-
|
|
1051
|
-
|
|
1132
|
+
await resolveAsync({
|
|
1133
|
+
resolvers: [
|
|
1134
|
+
[processEnv(), { DATABASE_URL: postgres() }],
|
|
1135
|
+
[awsSecrets(), { DATABASE_URL: postgres() }]
|
|
1136
|
+
]
|
|
1052
1137
|
// priority: 'last' (default) - AWS wins
|
|
1053
|
-
);
|
|
1138
|
+
});
|
|
1054
1139
|
|
|
1055
1140
|
// Development: Local .env overrides cloud
|
|
1056
|
-
await resolveAsync(
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1141
|
+
await resolveAsync({
|
|
1142
|
+
resolvers: [
|
|
1143
|
+
[dotenv(), { DATABASE_URL: postgres() }],
|
|
1144
|
+
[awsSecrets(), { DATABASE_URL: postgres() }]
|
|
1145
|
+
],
|
|
1146
|
+
options: { priority: 'first' } // dotenv wins
|
|
1147
|
+
});
|
|
1061
1148
|
```
|
|
1062
1149
|
|
|
1063
1150
|
**See:** "Controlling merge behaviour" section above for details.
|
|
@@ -1073,10 +1160,12 @@ await resolveAsync(
|
|
|
1073
1160
|
**Default:** `undefined` (no policies enforced)
|
|
1074
1161
|
|
|
1075
1162
|
```ts
|
|
1076
|
-
await resolveAsync(
|
|
1077
|
-
[
|
|
1078
|
-
|
|
1079
|
-
|
|
1163
|
+
await resolveAsync({
|
|
1164
|
+
resolvers: [
|
|
1165
|
+
[processEnv(), schema],
|
|
1166
|
+
[awsSecrets(), schema]
|
|
1167
|
+
],
|
|
1168
|
+
options: {
|
|
1080
1169
|
policies: {
|
|
1081
1170
|
// Block .env in production (default behaviour)
|
|
1082
1171
|
allowDotenvInProduction: false,
|
|
@@ -1088,7 +1177,7 @@ await resolveAsync(
|
|
|
1088
1177
|
}
|
|
1089
1178
|
}
|
|
1090
1179
|
}
|
|
1091
|
-
);
|
|
1180
|
+
});
|
|
1092
1181
|
```
|
|
1093
1182
|
|
|
1094
1183
|
**See:** "Security Policies" section below for complete documentation.
|
|
@@ -1108,11 +1197,13 @@ await resolveAsync(
|
|
|
1108
1197
|
**Default:** `false` (disabled in development), automatically `true` in production (`NODE_ENV === 'production'`)
|
|
1109
1198
|
|
|
1110
1199
|
```ts
|
|
1111
|
-
await resolveAsync(
|
|
1112
|
-
[
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1200
|
+
await resolveAsync({
|
|
1201
|
+
resolvers: [
|
|
1202
|
+
[processEnv(), schema],
|
|
1203
|
+
[awsSecrets(), schema]
|
|
1204
|
+
],
|
|
1205
|
+
options: { enableAudit: true } // Explicitly enable in development
|
|
1206
|
+
});
|
|
1116
1207
|
|
|
1117
1208
|
const logs = getAuditLog();
|
|
1118
1209
|
// [
|
|
@@ -1148,7 +1239,7 @@ interface PolicyOptions {
|
|
|
1148
1239
|
```ts
|
|
1149
1240
|
import express from 'express';
|
|
1150
1241
|
import { resolve } from 'node-env-resolver';
|
|
1151
|
-
import { postgres, string } from 'node-env-resolver/
|
|
1242
|
+
import { postgres, string } from 'node-env-resolver/validators';
|
|
1152
1243
|
|
|
1153
1244
|
const config = resolve({
|
|
1154
1245
|
PORT: 3000,
|
|
@@ -1165,7 +1256,7 @@ app.listen(config.PORT);
|
|
|
1165
1256
|
```ts
|
|
1166
1257
|
// env.mjs
|
|
1167
1258
|
import { resolve } from 'node-env-resolver/nextjs';
|
|
1168
|
-
import { postgres, string, url } from 'node-env-resolver/
|
|
1259
|
+
import { postgres, string, url } from 'node-env-resolver/validators';
|
|
1169
1260
|
|
|
1170
1261
|
export const env = resolve({
|
|
1171
1262
|
server: {
|
|
@@ -1183,18 +1274,20 @@ export const env = resolve({
|
|
|
1183
1274
|
```ts
|
|
1184
1275
|
import { resolveAsync } from 'node-env-resolver';
|
|
1185
1276
|
import { cached, TTL } from 'node-env-resolver/utils';
|
|
1186
|
-
import { postgres } from 'node-env-resolver/
|
|
1277
|
+
import { postgres } from 'node-env-resolver/validators';
|
|
1187
1278
|
import { awsSecrets } from 'node-env-resolver-aws';
|
|
1188
1279
|
|
|
1189
1280
|
const getConfig = async () => {
|
|
1190
|
-
return await resolveAsync(
|
|
1191
|
-
[
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1281
|
+
return await resolveAsync({
|
|
1282
|
+
resolvers: [
|
|
1283
|
+
[cached(
|
|
1284
|
+
awsSecrets({ secretId: 'lambda/config' }),
|
|
1285
|
+
{ ttl: TTL.minutes5 }
|
|
1286
|
+
), {
|
|
1287
|
+
DATABASE_URL: postgres(),
|
|
1288
|
+
}]
|
|
1289
|
+
]
|
|
1290
|
+
});
|
|
1198
1291
|
};
|
|
1199
1292
|
|
|
1200
1293
|
export const handler = async (event) => {
|
|
@@ -1215,14 +1308,17 @@ By default, `.env` files are **completely blocked in production** for security.
|
|
|
1215
1308
|
|
|
1216
1309
|
```ts
|
|
1217
1310
|
import { resolveAsync } from 'node-env-resolver';
|
|
1218
|
-
import { dotenv
|
|
1311
|
+
import { dotenv } from 'node-env-resolver/resolvers';
|
|
1312
|
+
import { postgres } from 'node-env-resolver/validators';
|
|
1219
1313
|
|
|
1220
1314
|
// In production (NODE_ENV=production)
|
|
1221
|
-
const config = await resolveAsync(
|
|
1222
|
-
[
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1315
|
+
const config = await resolveAsync({
|
|
1316
|
+
resolvers: [
|
|
1317
|
+
[dotenv(), {
|
|
1318
|
+
DATABASE_URL: postgres(),
|
|
1319
|
+
}]
|
|
1320
|
+
]
|
|
1321
|
+
});
|
|
1226
1322
|
// ❌ Throws: "DATABASE_URL cannot be sourced from .env files in production"
|
|
1227
1323
|
```
|
|
1228
1324
|
|
|
@@ -1230,38 +1326,44 @@ const config = await resolveAsync(
|
|
|
1230
1326
|
|
|
1231
1327
|
```ts
|
|
1232
1328
|
import { resolveAsync } from 'node-env-resolver';
|
|
1233
|
-
import { dotenv
|
|
1329
|
+
import { dotenv } from 'node-env-resolver/resolvers';
|
|
1330
|
+
import { postgres } from 'node-env-resolver/validators';
|
|
1234
1331
|
|
|
1235
|
-
const config = await resolveAsync(
|
|
1236
|
-
[
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1332
|
+
const config = await resolveAsync({
|
|
1333
|
+
resolvers: [
|
|
1334
|
+
[dotenv(), {
|
|
1335
|
+
DATABASE_URL: postgres(),
|
|
1336
|
+
}]
|
|
1337
|
+
],
|
|
1338
|
+
options: {
|
|
1240
1339
|
policies: {
|
|
1241
1340
|
allowDotenvInProduction: true // Allow all (risky!)
|
|
1242
1341
|
}
|
|
1243
1342
|
}
|
|
1244
|
-
);
|
|
1343
|
+
});
|
|
1245
1344
|
```
|
|
1246
1345
|
|
|
1247
1346
|
**Allow specific variables only (recommended if needed):**
|
|
1248
1347
|
|
|
1249
1348
|
```ts
|
|
1250
1349
|
import { resolveAsync } from 'node-env-resolver';
|
|
1251
|
-
import { dotenv
|
|
1350
|
+
import { dotenv } from 'node-env-resolver/resolvers';
|
|
1351
|
+
import { postgres } from 'node-env-resolver/validators';
|
|
1252
1352
|
|
|
1253
|
-
const config = await resolveAsync(
|
|
1254
|
-
[
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1353
|
+
const config = await resolveAsync({
|
|
1354
|
+
resolvers: [
|
|
1355
|
+
[dotenv(), {
|
|
1356
|
+
PORT: 3000,
|
|
1357
|
+
DATABASE_URL: postgres(),
|
|
1358
|
+
}]
|
|
1359
|
+
],
|
|
1360
|
+
options: {
|
|
1259
1361
|
policies: {
|
|
1260
1362
|
allowDotenvInProduction: ['PORT'] // Only PORT allowed from .env
|
|
1261
1363
|
// DATABASE_URL must come from process.env or cloud resolvers
|
|
1262
1364
|
}
|
|
1263
1365
|
}
|
|
1264
|
-
);
|
|
1366
|
+
});
|
|
1265
1367
|
```
|
|
1266
1368
|
|
|
1267
1369
|
### Policy: `enforceAllowedSources`
|
|
@@ -1269,19 +1371,22 @@ const config = await resolveAsync(
|
|
|
1269
1371
|
Restrict sensitive variables to specific resolvers (e.g., force secrets to come from AWS):
|
|
1270
1372
|
|
|
1271
1373
|
```ts
|
|
1272
|
-
import { resolveAsync
|
|
1273
|
-
import {
|
|
1374
|
+
import { resolveAsync } from 'node-env-resolver';
|
|
1375
|
+
import { processEnv } from 'node-env-resolver/resolvers';
|
|
1376
|
+
import { string } from 'node-env-resolver/validators';
|
|
1274
1377
|
import { awsSecrets } from 'node-env-resolver-aws';
|
|
1275
1378
|
|
|
1276
|
-
const config = await resolveAsync(
|
|
1277
|
-
[
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1379
|
+
const config = await resolveAsync({
|
|
1380
|
+
resolvers: [
|
|
1381
|
+
[processEnv(), {
|
|
1382
|
+
PORT: 3000,
|
|
1383
|
+
}],
|
|
1384
|
+
[awsSecrets({ secretId: 'prod/secrets' }), {
|
|
1385
|
+
DATABASE_PASSWORD: string(),
|
|
1386
|
+
API_KEY: string(),
|
|
1387
|
+
}]
|
|
1388
|
+
],
|
|
1389
|
+
options: {
|
|
1285
1390
|
policies: {
|
|
1286
1391
|
enforceAllowedSources: {
|
|
1287
1392
|
DATABASE_PASSWORD: ['aws-secrets'], // Must come from AWS
|
|
@@ -1290,32 +1395,34 @@ const config = await resolveAsync(
|
|
|
1290
1395
|
}
|
|
1291
1396
|
}
|
|
1292
1397
|
}
|
|
1293
|
-
);
|
|
1398
|
+
});
|
|
1294
1399
|
```
|
|
1295
1400
|
|
|
1296
1401
|
**Use case:** Ensure production secrets only come from AWS Secrets Manager, never from `.env` or `process.env`:
|
|
1297
1402
|
|
|
1298
1403
|
```ts
|
|
1299
|
-
import { resolveAsync
|
|
1300
|
-
import {
|
|
1301
|
-
import {
|
|
1404
|
+
import { resolveAsync } from 'node-env-resolver';
|
|
1405
|
+
import { processEnv } from 'node-env-resolver/resolvers';
|
|
1406
|
+
import { string } from 'node-env-resolver/validators';
|
|
1302
1407
|
import { awsSecrets } from 'node-env-resolver-aws';
|
|
1303
1408
|
|
|
1304
|
-
const config = await resolveAsync(
|
|
1305
|
-
[
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1409
|
+
const config = await resolveAsync({
|
|
1410
|
+
resolvers: [
|
|
1411
|
+
[processEnv(), {}],
|
|
1412
|
+
[awsSecrets({ secretId: 'prod/db' }), {
|
|
1413
|
+
DATABASE_PASSWORD: string(),
|
|
1414
|
+
STRIPE_SECRET: string(),
|
|
1415
|
+
}]
|
|
1416
|
+
],
|
|
1417
|
+
options: {
|
|
1311
1418
|
policies: {
|
|
1312
1419
|
enforceAllowedSources: {
|
|
1313
|
-
DATABASE_PASSWORD: ['
|
|
1314
|
-
STRIPE_SECRET: ['
|
|
1420
|
+
DATABASE_PASSWORD: ['aws-secrets'],
|
|
1421
|
+
STRIPE_SECRET: ['aws-secrets']
|
|
1315
1422
|
}
|
|
1316
1423
|
}
|
|
1317
1424
|
}
|
|
1318
|
-
);
|
|
1425
|
+
});
|
|
1319
1426
|
|
|
1320
1427
|
// ✅ If secrets come from AWS → Success
|
|
1321
1428
|
// ❌ If secrets come from process.env → Throws policy violation error
|
|
@@ -1355,7 +1462,7 @@ Track environment variable resolution for security and compliance monitoring.
|
|
|
1355
1462
|
|
|
1356
1463
|
```ts
|
|
1357
1464
|
import { resolve, getAuditLog, clearAuditLog } from 'node-env-resolver';
|
|
1358
|
-
import { postgres, string } from 'node-env-resolver/
|
|
1465
|
+
import { postgres, string } from 'node-env-resolver/validators';
|
|
1359
1466
|
|
|
1360
1467
|
// Option 1: Explicitly enable (works in any environment)
|
|
1361
1468
|
const config = resolve({
|
|
@@ -1378,7 +1485,7 @@ clearAuditLog();
|
|
|
1378
1485
|
|
|
1379
1486
|
```ts
|
|
1380
1487
|
import { resolve, getAuditLog } from 'node-env-resolver';
|
|
1381
|
-
import { postgres } from 'node-env-resolver/
|
|
1488
|
+
import { postgres } from 'node-env-resolver/validators';
|
|
1382
1489
|
|
|
1383
1490
|
// Option 2: Automatic in production
|
|
1384
1491
|
process.env.NODE_ENV = 'production';
|
|
@@ -1397,7 +1504,7 @@ When you have multiple `resolve()` calls, you can now get audit logs **specific
|
|
|
1397
1504
|
|
|
1398
1505
|
```ts
|
|
1399
1506
|
import { resolve, getAuditLog } from 'node-env-resolver';
|
|
1400
|
-
import { string, postgres } from 'node-env-resolver/
|
|
1507
|
+
import { string, postgres } from 'node-env-resolver/validators';
|
|
1401
1508
|
|
|
1402
1509
|
const appConfig = resolve({
|
|
1403
1510
|
PORT: 3000,
|
|
@@ -1453,16 +1560,18 @@ Audit logs include cache metadata to help monitor performance:
|
|
|
1453
1560
|
|
|
1454
1561
|
```ts
|
|
1455
1562
|
import { resolveAsync, getAuditLog } from 'node-env-resolver';
|
|
1456
|
-
import { cached } from 'node-env-resolver/utils';
|
|
1457
|
-
import { postgres } from 'node-env-resolver/
|
|
1563
|
+
import { cached, TTL } from 'node-env-resolver/utils';
|
|
1564
|
+
import { postgres } from 'node-env-resolver/validators';
|
|
1458
1565
|
import { awsSecrets } from 'node-env-resolver-aws';
|
|
1459
1566
|
|
|
1460
|
-
const config = await resolveAsync(
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1567
|
+
const config = await resolveAsync({
|
|
1568
|
+
resolvers: [
|
|
1569
|
+
[cached(awsSecrets({ secretId: 'prod/db' }), { ttl: TTL.minutes5 }), {
|
|
1570
|
+
DATABASE_URL: postgres(),
|
|
1571
|
+
}]
|
|
1572
|
+
],
|
|
1573
|
+
options: { enableAudit: true }
|
|
1574
|
+
});
|
|
1466
1575
|
|
|
1467
1576
|
const logs = getAuditLog();
|
|
1468
1577
|
logs.forEach(log => {
|
|
@@ -1481,18 +1590,21 @@ logs.forEach(log => {
|
|
|
1481
1590
|
Use audit logs for compliance and security monitoring:
|
|
1482
1591
|
|
|
1483
1592
|
```ts
|
|
1484
|
-
import { resolveAsync,
|
|
1485
|
-
import {
|
|
1593
|
+
import { resolveAsync, getAuditLog } from 'node-env-resolver';
|
|
1594
|
+
import { processEnv } from 'node-env-resolver/resolvers';
|
|
1595
|
+
import { string } from 'node-env-resolver/validators';
|
|
1486
1596
|
import { awsSecrets } from 'node-env-resolver-aws';
|
|
1487
1597
|
|
|
1488
1598
|
// In production
|
|
1489
|
-
const config = await resolveAsync(
|
|
1490
|
-
[
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1599
|
+
const config = await resolveAsync({
|
|
1600
|
+
resolvers: [
|
|
1601
|
+
[processEnv(), {}],
|
|
1602
|
+
[awsSecrets(), {
|
|
1603
|
+
DATABASE_PASSWORD: string(),
|
|
1604
|
+
API_KEY: string(),
|
|
1605
|
+
}]
|
|
1606
|
+
],
|
|
1607
|
+
options: {
|
|
1496
1608
|
policies: {
|
|
1497
1609
|
enforceAllowedSources: {
|
|
1498
1610
|
DATABASE_PASSWORD: ['aws-secrets'],
|
|
@@ -1501,7 +1613,7 @@ const config = await resolveAsync(
|
|
|
1501
1613
|
}
|
|
1502
1614
|
// enableAudit: true automatically in production
|
|
1503
1615
|
}
|
|
1504
|
-
);
|
|
1616
|
+
});
|
|
1505
1617
|
|
|
1506
1618
|
// Send audit logs to your monitoring system
|
|
1507
1619
|
const logs = getAuditLog();
|