node-env-resolver 5.0.0 → 5.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 +314 -235
- 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-P3TTYVEW.js → chunk-TAX63ALN.js} +2 -2
- package/dist/chunk-WKIPEKF6.js +1 -0
- package/dist/chunk-XJLMVSPN.js +9 -0
- package/dist/{chunk-UCLEPPLS.cjs → chunk-XPL5XLTA.cjs} +2 -2
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +61 -63
- package/dist/index.d.ts +61 -63
- package/dist/index.js +1 -1
- package/dist/resolvers.cjs +1 -1
- package/dist/resolvers.d.cts +7 -2
- package/dist/resolvers.d.ts +7 -2
- 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 +1 -1
- package/dist/utils.d.ts +1 -1
- package/dist/utils.js +1 -1
- package/dist/valibot.cjs +1 -1
- package/dist/valibot.js +1 -1
- 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-ZMRT37K3.js +0 -1
package/README.md
CHANGED
|
@@ -39,36 +39,42 @@ import { resolve, resolveAsync, processEnv } from 'node-env-resolver';
|
|
|
39
39
|
import { dotenv, string, url, postgres, redis } from 'node-env-resolver/resolvers';
|
|
40
40
|
import { awsSecrets, gcpSecrets, vaultSecrets } from 'node-env-resolver-aws';
|
|
41
41
|
|
|
42
|
-
// Synchronous with custom resolver (
|
|
43
|
-
const config = resolve(
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
]
|
|
42
|
+
// Synchronous with custom resolver (object syntax)
|
|
43
|
+
const config = resolve({
|
|
44
|
+
resolvers: [
|
|
45
|
+
[dotenv(), {
|
|
46
|
+
PORT: 3000,
|
|
47
|
+
NODE_ENV: ['development', 'production', 'test'] as const,
|
|
48
|
+
DEBUG: false,
|
|
49
|
+
API_KEY: string({optional:true})
|
|
50
|
+
}]
|
|
51
|
+
]
|
|
52
|
+
});
|
|
52
53
|
|
|
53
54
|
// 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
|
-
)
|
|
55
|
+
const config = await resolveAsync({
|
|
56
|
+
resolvers: [
|
|
57
|
+
[dotenv(), {
|
|
58
|
+
PORT: 3000,
|
|
59
|
+
NODE_ENV: ['development', 'production', 'test'] as const,
|
|
60
|
+
DEBUG: false,
|
|
61
|
+
API_KEY: string({optional:true})
|
|
62
|
+
}],
|
|
63
|
+
[awsSecrets(), { DATABASE_URL: url() }]
|
|
64
|
+
]
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
// Multiple resolvers with options
|
|
68
|
+
const config = await resolveAsync({
|
|
69
|
+
resolvers: [
|
|
70
|
+
[processEnv(), { PORT: 3000 }],
|
|
71
|
+
[dotenv(), { DATABASE_URL: postgres() }],
|
|
72
|
+
[awsSecrets(), { API_KEY: string() }],
|
|
73
|
+
[gcpSecrets(), { JWT_SECRET: string() }],
|
|
74
|
+
[vaultSecrets(), { REDIS_URL: redis() }]
|
|
75
|
+
],
|
|
76
|
+
options: { priority: 'last' }
|
|
77
|
+
});
|
|
72
78
|
```
|
|
73
79
|
|
|
74
80
|
## Table of Contents
|
|
@@ -209,21 +215,17 @@ const config = resolve({
|
|
|
209
215
|
|
|
210
216
|
```ts
|
|
211
217
|
import { resolveAsync, processEnv } from 'node-env-resolver';
|
|
212
|
-
import { dotenv, json, postgres, string, redis
|
|
213
|
-
import { awsSecrets
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
[vaultSecrets(), { REDIS_URL: redisValidator() }],
|
|
222
|
-
[consul(), { SERVICE_DISCOVERY: json() }],
|
|
223
|
-
[etcd(), { CONFIG_VERSION: string() }],
|
|
224
|
-
[redis(), { CACHE_CONFIG: json() }]
|
|
218
|
+
import { dotenv, json, postgres, string, redis } from 'node-env-resolver/resolvers';
|
|
219
|
+
import { awsSecrets } from 'node-env-resolver-aws';
|
|
220
|
+
|
|
221
|
+
const config = await resolveAsync({
|
|
222
|
+
resolvers: [
|
|
223
|
+
[processEnv(), { PORT: 3000 }],
|
|
224
|
+
[dotenv(), { DATABASE_URL: postgres() }],
|
|
225
|
+
[awsSecrets(), { API_KEY: string() }]
|
|
226
|
+
]
|
|
225
227
|
// Perfect for complex microservice architectures!
|
|
226
|
-
);
|
|
228
|
+
});
|
|
227
229
|
```
|
|
228
230
|
|
|
229
231
|
**Benefits:**
|
|
@@ -263,10 +265,11 @@ const config = resolve({
|
|
|
263
265
|
});
|
|
264
266
|
|
|
265
267
|
// Also works with async resolvers
|
|
266
|
-
const config = await resolveAsync(
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
]
|
|
268
|
+
const config = await resolveAsync({
|
|
269
|
+
resolvers: [
|
|
270
|
+
[awsSecrets(), { DATABASE_URL: postgres(), API_URL: url() }]
|
|
271
|
+
]
|
|
272
|
+
});
|
|
270
273
|
```
|
|
271
274
|
|
|
272
275
|
### What's in the 3.6KB core?
|
|
@@ -388,39 +391,40 @@ Load configuration from multiple sources. By default, later sources override ear
|
|
|
388
391
|
```ts
|
|
389
392
|
import { resolve, resolveAsync, processEnv } from 'node-env-resolver';
|
|
390
393
|
import { dotenv, json, postgres, string, redis } from 'node-env-resolver/resolvers';
|
|
391
|
-
import { awsSecrets
|
|
392
|
-
import { consul } from 'node-env-resolver-integrations';
|
|
394
|
+
import { awsSecrets } from 'node-env-resolver-aws';
|
|
393
395
|
|
|
394
396
|
// Async mode - supports both sync and async resolvers
|
|
395
|
-
const config = await resolveAsync(
|
|
396
|
-
[
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
397
|
+
const config = await resolveAsync({
|
|
398
|
+
resolvers: [
|
|
399
|
+
[processEnv(), {
|
|
400
|
+
PORT: 3000,
|
|
401
|
+
NODE_ENV: ['development', 'production'] as const,
|
|
402
|
+
}],
|
|
403
|
+
[dotenv(), {
|
|
404
|
+
DATABASE_URL: postgres(),
|
|
405
|
+
API_KEY: string(),
|
|
406
|
+
}]
|
|
407
|
+
]
|
|
408
|
+
});
|
|
405
409
|
|
|
406
410
|
// Sync mode - supports multiple SYNC resolvers
|
|
407
|
-
const config = resolve(
|
|
408
|
-
|
|
409
|
-
|
|
411
|
+
const config = resolve({
|
|
412
|
+
resolvers: [
|
|
413
|
+
[dotenv(), { PORT: 3000 }],
|
|
414
|
+
[json('config.json'), { DATABASE_URL: postgres() }]
|
|
415
|
+
]
|
|
410
416
|
// Both resolvers must have loadSync() method
|
|
411
|
-
);
|
|
417
|
+
});
|
|
412
418
|
|
|
413
419
|
// 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
|
-
);
|
|
420
|
+
const config = await resolveAsync({
|
|
421
|
+
resolvers: [
|
|
422
|
+
[processEnv(), { PORT: 3000 }],
|
|
423
|
+
[dotenv(), { DATABASE_URL: postgres() }],
|
|
424
|
+
[awsSecrets(), { API_KEY: string() }]
|
|
425
|
+
],
|
|
426
|
+
options: { priority: 'last' }
|
|
427
|
+
});
|
|
424
428
|
```
|
|
425
429
|
|
|
426
430
|
### Controlling merge behaviour
|
|
@@ -433,27 +437,33 @@ import { dotenv, json, postgres } from 'node-env-resolver/resolvers';
|
|
|
433
437
|
import { awsSecrets } from 'node-env-resolver-aws';
|
|
434
438
|
|
|
435
439
|
// priority: 'last' (default) - later resolvers override earlier ones
|
|
436
|
-
const config = await resolveAsync(
|
|
437
|
-
|
|
438
|
-
|
|
440
|
+
const config = await resolveAsync({
|
|
441
|
+
resolvers: [
|
|
442
|
+
[processEnv(), { DATABASE_URL: postgres() }],
|
|
443
|
+
[awsSecrets(), { DATABASE_URL: postgres() }]
|
|
444
|
+
]
|
|
439
445
|
// AWS wins
|
|
440
|
-
);
|
|
446
|
+
});
|
|
441
447
|
|
|
442
448
|
// priority: 'first' - earlier resolvers take precedence
|
|
443
|
-
const config = await resolveAsync(
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
449
|
+
const config = await resolveAsync({
|
|
450
|
+
resolvers: [
|
|
451
|
+
[dotenv(), { DATABASE_URL: postgres() }],
|
|
452
|
+
[awsSecrets(), { DATABASE_URL: postgres() }]
|
|
453
|
+
],
|
|
454
|
+
options: { priority: 'first' }
|
|
447
455
|
// dotenv wins
|
|
448
|
-
);
|
|
456
|
+
});
|
|
449
457
|
|
|
450
|
-
// Also works with sync resolve()
|
|
451
|
-
const config = resolve(
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
458
|
+
// Also works with sync resolve()
|
|
459
|
+
const config = resolve({
|
|
460
|
+
resolvers: [
|
|
461
|
+
[dotenv(), { DATABASE_URL: postgres() }],
|
|
462
|
+
[json('config.json'), { DATABASE_URL: postgres() }]
|
|
463
|
+
],
|
|
464
|
+
options: { priority: 'first' }
|
|
455
465
|
// dotenv wins
|
|
456
|
-
);
|
|
466
|
+
});
|
|
457
467
|
```
|
|
458
468
|
|
|
459
469
|
This is useful for development workflows where local overrides should take precedence over cloud secrets.
|
|
@@ -469,15 +479,16 @@ When using `priority: 'first'`, resolvers are called sequentially, but execution
|
|
|
469
479
|
```ts
|
|
470
480
|
import { resolveAsync } from 'node-env-resolver';
|
|
471
481
|
import { dotenv, postgres, string } from 'node-env-resolver/resolvers';
|
|
472
|
-
import { awsSecrets
|
|
473
|
-
|
|
474
|
-
const config = await resolveAsync(
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
482
|
+
import { awsSecrets } from 'node-env-resolver-aws';
|
|
483
|
+
|
|
484
|
+
const config = await resolveAsync({
|
|
485
|
+
resolvers: [
|
|
486
|
+
[dotenv(), { DATABASE_URL: postgres(), API_KEY: string(), PORT: 3000 }],
|
|
487
|
+
[awsSecrets(), { DATABASE_URL: postgres(), API_KEY: string(), PORT: 3000 }]
|
|
488
|
+
],
|
|
489
|
+
options: { priority: 'first' }
|
|
490
|
+
});
|
|
491
|
+
// If dotenv() provides all required keys, awsSecrets() is never called!
|
|
481
492
|
```
|
|
482
493
|
|
|
483
494
|
This is particularly valuable in development where:
|
|
@@ -498,14 +509,15 @@ When using `priority: 'last'` (the default), all resolvers are called **in paral
|
|
|
498
509
|
```ts
|
|
499
510
|
import { resolveAsync } from 'node-env-resolver';
|
|
500
511
|
import { postgres, string } from 'node-env-resolver/resolvers';
|
|
501
|
-
import { awsSecrets,
|
|
512
|
+
import { awsSecrets, awsSsm } from 'node-env-resolver-aws';
|
|
502
513
|
|
|
503
|
-
const config = await resolveAsync(
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
514
|
+
const config = await resolveAsync({
|
|
515
|
+
resolvers: [
|
|
516
|
+
[awsSecrets(), { DATABASE_URL: postgres() }], // 100ms
|
|
517
|
+
[awsSsm(), { API_KEY: string() }], // 100ms
|
|
518
|
+
]
|
|
507
519
|
// Default: priority: 'last'
|
|
508
|
-
);
|
|
520
|
+
});
|
|
509
521
|
// Total time: ~100ms (parallel) instead of ~300ms (sequential)
|
|
510
522
|
```
|
|
511
523
|
|
|
@@ -522,14 +534,23 @@ Since `priority: 'last'` means "last write wins", the order doesn't matter for c
|
|
|
522
534
|
Like Zod's `safeParse()`, use `safeResolve()` to get a result object instead of throwing:
|
|
523
535
|
|
|
524
536
|
```ts
|
|
525
|
-
import { safeResolve } from 'node-env-resolver';
|
|
537
|
+
import { safeResolve, safeResolveAsync } from 'node-env-resolver';
|
|
526
538
|
import { number, postgres } from 'node-env-resolver/resolvers';
|
|
527
539
|
|
|
540
|
+
// Synchronous safe resolve
|
|
528
541
|
const result = safeResolve({
|
|
529
542
|
PORT: number(),
|
|
530
543
|
DATABASE_URL: postgres()
|
|
531
544
|
});
|
|
532
545
|
|
|
546
|
+
// Asynchronous safe resolve
|
|
547
|
+
const result = await safeResolveAsync({
|
|
548
|
+
resolvers: [
|
|
549
|
+
[processEnv(), { PORT: number() }],
|
|
550
|
+
[awsSecrets(), { DATABASE_URL: postgres() }]
|
|
551
|
+
]
|
|
552
|
+
});
|
|
553
|
+
|
|
533
554
|
if (result.success) {
|
|
534
555
|
// Use result.data with full type safety
|
|
535
556
|
console.log(result.data.PORT);
|
|
@@ -543,7 +564,7 @@ if (result.success) {
|
|
|
543
564
|
All functions have safe variants:
|
|
544
565
|
|
|
545
566
|
- `resolve()` → `safeResolve()` (both synchronous)
|
|
546
|
-
- `resolveAsync()` → `
|
|
567
|
+
- `resolveAsync()` → `safeResolveAsync()` (both async)
|
|
547
568
|
|
|
548
569
|
## Synchronous resolution
|
|
549
570
|
|
|
@@ -571,10 +592,12 @@ import { resolveAsync, processEnv } from 'node-env-resolver';
|
|
|
571
592
|
import { dotenv, postgres, url } from 'node-env-resolver/resolvers';
|
|
572
593
|
|
|
573
594
|
// Async - await required when using custom resolvers
|
|
574
|
-
const config = await resolveAsync(
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
)
|
|
595
|
+
const config = await resolveAsync({
|
|
596
|
+
resolvers: [
|
|
597
|
+
[processEnv(), { PORT: 3000 }],
|
|
598
|
+
[dotenv(), { DATABASE_URL: postgres(), API_URL: url() }]
|
|
599
|
+
]
|
|
600
|
+
});
|
|
578
601
|
```
|
|
579
602
|
|
|
580
603
|
## Advanced features
|
|
@@ -590,18 +613,20 @@ import { cliArgs } from 'node-env-resolver/cli';
|
|
|
590
613
|
|
|
591
614
|
// $ node app.js --port 8080 --database-url postgres://localhost --verbose
|
|
592
615
|
|
|
593
|
-
const config = await resolveAsync(
|
|
594
|
-
[
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
616
|
+
const config = await resolveAsync({
|
|
617
|
+
resolvers: [
|
|
618
|
+
[processEnv(), {
|
|
619
|
+
PORT: 3000,
|
|
620
|
+
DATABASE_URL: postgres(),
|
|
621
|
+
VERBOSE: false
|
|
622
|
+
}],
|
|
623
|
+
[cliArgs(), {
|
|
624
|
+
PORT: 3000,
|
|
625
|
+
DATABASE_URL: postgres(),
|
|
626
|
+
VERBOSE: false
|
|
627
|
+
}]
|
|
628
|
+
]
|
|
629
|
+
});
|
|
605
630
|
|
|
606
631
|
// config.PORT === 8080 (from CLI)
|
|
607
632
|
// config.DATABASE_URL === 'postgres://localhost' (from CLI)
|
|
@@ -798,10 +823,12 @@ const databaseResolver: Resolver = {
|
|
|
798
823
|
}
|
|
799
824
|
};
|
|
800
825
|
|
|
801
|
-
const config = await resolveAsync(
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
)
|
|
826
|
+
const config = await resolveAsync({
|
|
827
|
+
resolvers: [
|
|
828
|
+
[processEnv(), { PORT: 3000 }],
|
|
829
|
+
[databaseResolver, { API_KEY: string() }]
|
|
830
|
+
]
|
|
831
|
+
});
|
|
805
832
|
```
|
|
806
833
|
|
|
807
834
|
### AWS Secrets Manager
|
|
@@ -833,11 +860,13 @@ import { resolveAsync, processEnv } from 'node-env-resolver';
|
|
|
833
860
|
import { postgres, string } from 'node-env-resolver/resolvers';
|
|
834
861
|
import { awsSecrets, awsSsm } from 'node-env-resolver-aws';
|
|
835
862
|
|
|
836
|
-
const config = await resolveAsync(
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
)
|
|
863
|
+
const config = await resolveAsync({
|
|
864
|
+
resolvers: [
|
|
865
|
+
[processEnv(), { PORT: 3000 }],
|
|
866
|
+
[awsSecrets({ secretId: 'app/secrets' }), { DATABASE_URL: postgres() }],
|
|
867
|
+
[awsSsm({ path: '/app/config' }), { API_KEY: string() }]
|
|
868
|
+
]
|
|
869
|
+
});
|
|
841
870
|
```
|
|
842
871
|
|
|
843
872
|
See the [AWS package documentation](../node-env-resolver-aws/README.md) for details on credentials, caching, and best practices.
|
|
@@ -853,15 +882,17 @@ import { postgres, string } from 'node-env-resolver/resolvers';
|
|
|
853
882
|
import { awsSecrets } from 'node-env-resolver-aws';
|
|
854
883
|
|
|
855
884
|
export const getConfig = async () => {
|
|
856
|
-
return await resolveAsync(
|
|
857
|
-
[
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
885
|
+
return await resolveAsync({
|
|
886
|
+
resolvers: [
|
|
887
|
+
[cached(
|
|
888
|
+
awsSecrets({ secretId: 'app/secrets' }),
|
|
889
|
+
{ ttl: TTL.minutes5 }
|
|
890
|
+
), {
|
|
891
|
+
DATABASE_URL: postgres(),
|
|
892
|
+
API_KEY: string(),
|
|
893
|
+
}]
|
|
894
|
+
]
|
|
895
|
+
});
|
|
865
896
|
};
|
|
866
897
|
|
|
867
898
|
// Call getConfig() in your handlers
|
|
@@ -878,6 +909,25 @@ Performance:
|
|
|
878
909
|
|
|
879
910
|
**Important:** Call `resolve()` every time. The `cached()` wrapper handles the caching.
|
|
880
911
|
|
|
912
|
+
**Advanced caching options:**
|
|
913
|
+
|
|
914
|
+
```ts
|
|
915
|
+
import { cached, TTL } from 'node-env-resolver/utils';
|
|
916
|
+
|
|
917
|
+
// Stale-while-revalidate: serve stale data while refreshing in background
|
|
918
|
+
const resolver = cached(awsSecrets(), {
|
|
919
|
+
ttl: TTL.minutes5, // Fresh for 5 minutes
|
|
920
|
+
maxAge: TTL.hour, // Force refresh after 1 hour
|
|
921
|
+
staleWhileRevalidate: true // Serve stale data during refresh
|
|
922
|
+
});
|
|
923
|
+
|
|
924
|
+
// Custom cache key for debugging
|
|
925
|
+
const resolver = cached(awsSecrets(), {
|
|
926
|
+
ttl: TTL.minutes15,
|
|
927
|
+
key: 'prod-secrets-v2'
|
|
928
|
+
});
|
|
929
|
+
```
|
|
930
|
+
|
|
881
931
|
## API Reference
|
|
882
932
|
|
|
883
933
|
### Function variants
|
|
@@ -942,18 +992,20 @@ const config = resolve(schema, {
|
|
|
942
992
|
enableAudit: true
|
|
943
993
|
});
|
|
944
994
|
|
|
945
|
-
// Multiple sources - options
|
|
946
|
-
const config = await resolveAsync(
|
|
947
|
-
[
|
|
948
|
-
|
|
949
|
-
|
|
995
|
+
// Multiple sources - options in config object
|
|
996
|
+
const config = await resolveAsync({
|
|
997
|
+
resolvers: [
|
|
998
|
+
[resolver1, schema],
|
|
999
|
+
[resolver2, schema]
|
|
1000
|
+
],
|
|
1001
|
+
options: {
|
|
950
1002
|
interpolate: false,
|
|
951
1003
|
strict: true,
|
|
952
1004
|
policies: {...},
|
|
953
1005
|
enableAudit: true,
|
|
954
1006
|
priority: 'last'
|
|
955
1007
|
}
|
|
956
|
-
);
|
|
1008
|
+
});
|
|
957
1009
|
```
|
|
958
1010
|
|
|
959
1011
|
**Note:** The `resolvers` option has been removed. For single source resolution, use `resolve()` directly (defaults to `process.env`). For multiple sources, use `resolveAsync()` syntax.
|
|
@@ -1011,18 +1063,22 @@ const flakyResolver = {
|
|
|
1011
1063
|
};
|
|
1012
1064
|
|
|
1013
1065
|
// ❌ Throws immediately
|
|
1014
|
-
await resolveAsync(
|
|
1015
|
-
[
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1066
|
+
await resolveAsync({
|
|
1067
|
+
resolvers: [
|
|
1068
|
+
[flakyResolver, schema],
|
|
1069
|
+
[processEnv(), schema]
|
|
1070
|
+
],
|
|
1071
|
+
options: { strict: true } // default
|
|
1072
|
+
});
|
|
1019
1073
|
|
|
1020
1074
|
// ✅ Continues with processEnv()
|
|
1021
|
-
await resolveAsync(
|
|
1022
|
-
[
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1075
|
+
await resolveAsync({
|
|
1076
|
+
resolvers: [
|
|
1077
|
+
[flakyResolver, schema],
|
|
1078
|
+
[processEnv(), schema]
|
|
1079
|
+
],
|
|
1080
|
+
options: { strict: false } // graceful degradation
|
|
1081
|
+
});
|
|
1026
1082
|
```
|
|
1027
1083
|
|
|
1028
1084
|
**Note:** In sync mode with `strict: true`, resolvers without `loadSync()` method will throw errors.
|
|
@@ -1046,18 +1102,22 @@ import { dotenv, postgres } from 'node-env-resolver/resolvers';
|
|
|
1046
1102
|
import { awsSecrets } from 'node-env-resolver-aws';
|
|
1047
1103
|
|
|
1048
1104
|
// Production: AWS secrets override process.env
|
|
1049
|
-
await resolveAsync(
|
|
1050
|
-
|
|
1051
|
-
|
|
1105
|
+
await resolveAsync({
|
|
1106
|
+
resolvers: [
|
|
1107
|
+
[processEnv(), { DATABASE_URL: postgres() }],
|
|
1108
|
+
[awsSecrets(), { DATABASE_URL: postgres() }]
|
|
1109
|
+
]
|
|
1052
1110
|
// priority: 'last' (default) - AWS wins
|
|
1053
|
-
);
|
|
1111
|
+
});
|
|
1054
1112
|
|
|
1055
1113
|
// Development: Local .env overrides cloud
|
|
1056
|
-
await resolveAsync(
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1114
|
+
await resolveAsync({
|
|
1115
|
+
resolvers: [
|
|
1116
|
+
[dotenv(), { DATABASE_URL: postgres() }],
|
|
1117
|
+
[awsSecrets(), { DATABASE_URL: postgres() }]
|
|
1118
|
+
],
|
|
1119
|
+
options: { priority: 'first' } // dotenv wins
|
|
1120
|
+
});
|
|
1061
1121
|
```
|
|
1062
1122
|
|
|
1063
1123
|
**See:** "Controlling merge behaviour" section above for details.
|
|
@@ -1073,10 +1133,12 @@ await resolveAsync(
|
|
|
1073
1133
|
**Default:** `undefined` (no policies enforced)
|
|
1074
1134
|
|
|
1075
1135
|
```ts
|
|
1076
|
-
await resolveAsync(
|
|
1077
|
-
[
|
|
1078
|
-
|
|
1079
|
-
|
|
1136
|
+
await resolveAsync({
|
|
1137
|
+
resolvers: [
|
|
1138
|
+
[processEnv(), schema],
|
|
1139
|
+
[awsSecrets(), schema]
|
|
1140
|
+
],
|
|
1141
|
+
options: {
|
|
1080
1142
|
policies: {
|
|
1081
1143
|
// Block .env in production (default behaviour)
|
|
1082
1144
|
allowDotenvInProduction: false,
|
|
@@ -1088,7 +1150,7 @@ await resolveAsync(
|
|
|
1088
1150
|
}
|
|
1089
1151
|
}
|
|
1090
1152
|
}
|
|
1091
|
-
);
|
|
1153
|
+
});
|
|
1092
1154
|
```
|
|
1093
1155
|
|
|
1094
1156
|
**See:** "Security Policies" section below for complete documentation.
|
|
@@ -1108,11 +1170,13 @@ await resolveAsync(
|
|
|
1108
1170
|
**Default:** `false` (disabled in development), automatically `true` in production (`NODE_ENV === 'production'`)
|
|
1109
1171
|
|
|
1110
1172
|
```ts
|
|
1111
|
-
await resolveAsync(
|
|
1112
|
-
[
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1173
|
+
await resolveAsync({
|
|
1174
|
+
resolvers: [
|
|
1175
|
+
[processEnv(), schema],
|
|
1176
|
+
[awsSecrets(), schema]
|
|
1177
|
+
],
|
|
1178
|
+
options: { enableAudit: true } // Explicitly enable in development
|
|
1179
|
+
});
|
|
1116
1180
|
|
|
1117
1181
|
const logs = getAuditLog();
|
|
1118
1182
|
// [
|
|
@@ -1187,14 +1251,16 @@ import { postgres } from 'node-env-resolver/resolvers';
|
|
|
1187
1251
|
import { awsSecrets } from 'node-env-resolver-aws';
|
|
1188
1252
|
|
|
1189
1253
|
const getConfig = async () => {
|
|
1190
|
-
return await resolveAsync(
|
|
1191
|
-
[
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1254
|
+
return await resolveAsync({
|
|
1255
|
+
resolvers: [
|
|
1256
|
+
[cached(
|
|
1257
|
+
awsSecrets({ secretId: 'lambda/config' }),
|
|
1258
|
+
{ ttl: TTL.minutes5 }
|
|
1259
|
+
), {
|
|
1260
|
+
DATABASE_URL: postgres(),
|
|
1261
|
+
}]
|
|
1262
|
+
]
|
|
1263
|
+
});
|
|
1198
1264
|
};
|
|
1199
1265
|
|
|
1200
1266
|
export const handler = async (event) => {
|
|
@@ -1218,11 +1284,13 @@ import { resolveAsync } from 'node-env-resolver';
|
|
|
1218
1284
|
import { dotenv, postgres } from 'node-env-resolver/resolvers';
|
|
1219
1285
|
|
|
1220
1286
|
// In production (NODE_ENV=production)
|
|
1221
|
-
const config = await resolveAsync(
|
|
1222
|
-
[
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1287
|
+
const config = await resolveAsync({
|
|
1288
|
+
resolvers: [
|
|
1289
|
+
[dotenv(), {
|
|
1290
|
+
DATABASE_URL: postgres(),
|
|
1291
|
+
}]
|
|
1292
|
+
]
|
|
1293
|
+
});
|
|
1226
1294
|
// ❌ Throws: "DATABASE_URL cannot be sourced from .env files in production"
|
|
1227
1295
|
```
|
|
1228
1296
|
|
|
@@ -1232,16 +1300,18 @@ const config = await resolveAsync(
|
|
|
1232
1300
|
import { resolveAsync } from 'node-env-resolver';
|
|
1233
1301
|
import { dotenv, postgres } from 'node-env-resolver/resolvers';
|
|
1234
1302
|
|
|
1235
|
-
const config = await resolveAsync(
|
|
1236
|
-
[
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1303
|
+
const config = await resolveAsync({
|
|
1304
|
+
resolvers: [
|
|
1305
|
+
[dotenv(), {
|
|
1306
|
+
DATABASE_URL: postgres(),
|
|
1307
|
+
}]
|
|
1308
|
+
],
|
|
1309
|
+
options: {
|
|
1240
1310
|
policies: {
|
|
1241
1311
|
allowDotenvInProduction: true // Allow all (risky!)
|
|
1242
1312
|
}
|
|
1243
1313
|
}
|
|
1244
|
-
);
|
|
1314
|
+
});
|
|
1245
1315
|
```
|
|
1246
1316
|
|
|
1247
1317
|
**Allow specific variables only (recommended if needed):**
|
|
@@ -1250,18 +1320,20 @@ const config = await resolveAsync(
|
|
|
1250
1320
|
import { resolveAsync } from 'node-env-resolver';
|
|
1251
1321
|
import { dotenv, postgres } from 'node-env-resolver/resolvers';
|
|
1252
1322
|
|
|
1253
|
-
const config = await resolveAsync(
|
|
1254
|
-
[
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1323
|
+
const config = await resolveAsync({
|
|
1324
|
+
resolvers: [
|
|
1325
|
+
[dotenv(), {
|
|
1326
|
+
PORT: 3000,
|
|
1327
|
+
DATABASE_URL: postgres(),
|
|
1328
|
+
}]
|
|
1329
|
+
],
|
|
1330
|
+
options: {
|
|
1259
1331
|
policies: {
|
|
1260
1332
|
allowDotenvInProduction: ['PORT'] // Only PORT allowed from .env
|
|
1261
1333
|
// DATABASE_URL must come from process.env or cloud resolvers
|
|
1262
1334
|
}
|
|
1263
1335
|
}
|
|
1264
|
-
);
|
|
1336
|
+
});
|
|
1265
1337
|
```
|
|
1266
1338
|
|
|
1267
1339
|
### Policy: `enforceAllowedSources`
|
|
@@ -1273,15 +1345,17 @@ import { resolveAsync, processEnv } from 'node-env-resolver';
|
|
|
1273
1345
|
import { string } from 'node-env-resolver/resolvers';
|
|
1274
1346
|
import { awsSecrets } from 'node-env-resolver-aws';
|
|
1275
1347
|
|
|
1276
|
-
const config = await resolveAsync(
|
|
1277
|
-
[
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1348
|
+
const config = await resolveAsync({
|
|
1349
|
+
resolvers: [
|
|
1350
|
+
[processEnv(), {
|
|
1351
|
+
PORT: 3000,
|
|
1352
|
+
}],
|
|
1353
|
+
[awsSecrets({ secretId: 'prod/secrets' }), {
|
|
1354
|
+
DATABASE_PASSWORD: string(),
|
|
1355
|
+
API_KEY: string(),
|
|
1356
|
+
}]
|
|
1357
|
+
],
|
|
1358
|
+
options: {
|
|
1285
1359
|
policies: {
|
|
1286
1360
|
enforceAllowedSources: {
|
|
1287
1361
|
DATABASE_PASSWORD: ['aws-secrets'], // Must come from AWS
|
|
@@ -1290,7 +1364,7 @@ const config = await resolveAsync(
|
|
|
1290
1364
|
}
|
|
1291
1365
|
}
|
|
1292
1366
|
}
|
|
1293
|
-
);
|
|
1367
|
+
});
|
|
1294
1368
|
```
|
|
1295
1369
|
|
|
1296
1370
|
**Use case:** Ensure production secrets only come from AWS Secrets Manager, never from `.env` or `process.env`:
|
|
@@ -1298,24 +1372,25 @@ const config = await resolveAsync(
|
|
|
1298
1372
|
```ts
|
|
1299
1373
|
import { resolveAsync, processEnv } from 'node-env-resolver';
|
|
1300
1374
|
import { string } from 'node-env-resolver/resolvers';
|
|
1301
|
-
import { cached, TTL } from 'node-env-resolver/utils';
|
|
1302
1375
|
import { awsSecrets } from 'node-env-resolver-aws';
|
|
1303
1376
|
|
|
1304
|
-
const config = await resolveAsync(
|
|
1305
|
-
[
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1377
|
+
const config = await resolveAsync({
|
|
1378
|
+
resolvers: [
|
|
1379
|
+
[processEnv(), {}],
|
|
1380
|
+
[awsSecrets({ secretId: 'prod/db' }), {
|
|
1381
|
+
DATABASE_PASSWORD: string(),
|
|
1382
|
+
STRIPE_SECRET: string(),
|
|
1383
|
+
}]
|
|
1384
|
+
],
|
|
1385
|
+
options: {
|
|
1311
1386
|
policies: {
|
|
1312
1387
|
enforceAllowedSources: {
|
|
1313
|
-
DATABASE_PASSWORD: ['
|
|
1314
|
-
STRIPE_SECRET: ['
|
|
1388
|
+
DATABASE_PASSWORD: ['aws-secrets'],
|
|
1389
|
+
STRIPE_SECRET: ['aws-secrets']
|
|
1315
1390
|
}
|
|
1316
1391
|
}
|
|
1317
1392
|
}
|
|
1318
|
-
);
|
|
1393
|
+
});
|
|
1319
1394
|
|
|
1320
1395
|
// ✅ If secrets come from AWS → Success
|
|
1321
1396
|
// ❌ If secrets come from process.env → Throws policy violation error
|
|
@@ -1453,16 +1528,18 @@ Audit logs include cache metadata to help monitor performance:
|
|
|
1453
1528
|
|
|
1454
1529
|
```ts
|
|
1455
1530
|
import { resolveAsync, getAuditLog } from 'node-env-resolver';
|
|
1456
|
-
import { cached } from 'node-env-resolver/utils';
|
|
1531
|
+
import { cached, TTL } from 'node-env-resolver/utils';
|
|
1457
1532
|
import { postgres } from 'node-env-resolver/resolvers';
|
|
1458
1533
|
import { awsSecrets } from 'node-env-resolver-aws';
|
|
1459
1534
|
|
|
1460
|
-
const config = await resolveAsync(
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1535
|
+
const config = await resolveAsync({
|
|
1536
|
+
resolvers: [
|
|
1537
|
+
[cached(awsSecrets({ secretId: 'prod/db' }), { ttl: TTL.minutes5 }), {
|
|
1538
|
+
DATABASE_URL: postgres(),
|
|
1539
|
+
}]
|
|
1540
|
+
],
|
|
1541
|
+
options: { enableAudit: true }
|
|
1542
|
+
});
|
|
1466
1543
|
|
|
1467
1544
|
const logs = getAuditLog();
|
|
1468
1545
|
logs.forEach(log => {
|
|
@@ -1486,13 +1563,15 @@ import { string } from 'node-env-resolver/resolvers';
|
|
|
1486
1563
|
import { awsSecrets } from 'node-env-resolver-aws';
|
|
1487
1564
|
|
|
1488
1565
|
// In production
|
|
1489
|
-
const config = await resolveAsync(
|
|
1490
|
-
[
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1566
|
+
const config = await resolveAsync({
|
|
1567
|
+
resolvers: [
|
|
1568
|
+
[processEnv(), {}],
|
|
1569
|
+
[awsSecrets(), {
|
|
1570
|
+
DATABASE_PASSWORD: string(),
|
|
1571
|
+
API_KEY: string(),
|
|
1572
|
+
}]
|
|
1573
|
+
],
|
|
1574
|
+
options: {
|
|
1496
1575
|
policies: {
|
|
1497
1576
|
enforceAllowedSources: {
|
|
1498
1577
|
DATABASE_PASSWORD: ['aws-secrets'],
|
|
@@ -1501,7 +1580,7 @@ const config = await resolveAsync(
|
|
|
1501
1580
|
}
|
|
1502
1581
|
// enableAudit: true automatically in production
|
|
1503
1582
|
}
|
|
1504
|
-
);
|
|
1583
|
+
});
|
|
1505
1584
|
|
|
1506
1585
|
// Send audit logs to your monitoring system
|
|
1507
1586
|
const logs = getAuditLog();
|