node-env-resolver 3.0.0 → 4.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 +349 -50
- package/dist/advanced-features.test.d.ts +2 -0
- package/dist/advanced-features.test.d.ts.map +1 -0
- package/dist/audit.cjs +1 -0
- package/dist/audit.d.ts +13 -1
- package/dist/audit.d.ts.map +1 -1
- package/dist/audit.js +1 -1
- package/dist/builder.cjs +1 -0
- package/dist/builder.js +1 -1
- package/dist/chunk-4ZOF2PI4.cjs +1 -0
- package/dist/chunk-54OHKWZJ.js +1 -0
- package/dist/chunk-5PELJRUQ.js +1 -0
- package/dist/chunk-FCFDGYTF.cjs +15 -0
- package/dist/chunk-HCVTYYLE.js +9 -0
- package/dist/chunk-IU5TG3HL.js +15 -0
- package/dist/chunk-J7ZHEMRP.js +9 -0
- package/dist/chunk-KIQXH265.cjs +1 -0
- package/dist/chunk-NKPXKLF7.js +1 -0
- package/dist/chunk-P3ZWKKKW.cjs +1 -0
- package/dist/chunk-THFGEKYL.cjs +9 -0
- package/dist/chunk-ULL4WKTV.cjs +9 -0
- package/dist/chunk-XPGSWVRT.cjs +1 -0
- package/dist/chunk-YOHHFBZN.js +1 -0
- package/dist/cli.cjs +1 -0
- package/dist/cli.d.ts +49 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +1 -0
- package/dist/cli.test.d.ts +2 -0
- package/dist/cli.test.d.ts.map +1 -0
- package/dist/computed.test.d.ts +2 -0
- package/dist/computed.test.d.ts.map +1 -0
- package/dist/index.cjs +1 -0
- package/dist/index.d.ts +93 -20
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/nested.test.d.ts +2 -0
- package/dist/nested.test.d.ts.map +1 -0
- package/dist/resolver.cjs +1 -0
- package/dist/resolver.d.ts.map +1 -1
- package/dist/resolver.js +1 -1
- package/dist/resolvers.cjs +1 -0
- package/dist/resolvers.d.ts +131 -2
- package/dist/resolvers.d.ts.map +1 -1
- package/dist/resolvers.js +1 -1
- package/dist/sync-resolvers.test.d.ts +2 -0
- package/dist/sync-resolvers.test.d.ts.map +1 -0
- package/dist/type-inference.test.d.ts +2 -0
- package/dist/type-inference.test.d.ts.map +1 -0
- package/dist/types.cjs +1 -0
- package/dist/types.d.ts +82 -6
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +1 -0
- package/dist/utils.cjs +1 -0
- package/dist/utils.d.ts +170 -0
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +1 -1
- package/dist/valibot.cjs +1 -0
- package/dist/valibot.js +1 -1
- package/dist/validation-types.cjs +1 -0
- package/dist/validators.cjs +1 -0
- package/dist/validators.d.ts +17 -0
- package/dist/validators.d.ts.map +1 -1
- package/dist/validators.js +1 -1
- package/dist/web.cjs +6 -0
- package/dist/web.js +4 -4
- package/dist/zod.cjs +1 -0
- package/dist/zod.js +1 -1
- package/package.json +30 -14
- package/dist/chunk-5SFFBKLW.js +0 -9
- package/dist/chunk-CC4LJOXE.js +0 -3
- package/dist/chunk-DAGPFV3V.js +0 -1
- package/dist/chunk-ICRS72Z4.js +0 -1
- package/dist/chunk-WOT6VMZA.js +0 -1
- package/dist/chunk-XP3U6MKF.js +0 -5
package/README.md
CHANGED
|
@@ -31,25 +31,60 @@ config.DEBUG; // boolean
|
|
|
31
31
|
config.API_KEY; // string | undefined
|
|
32
32
|
```
|
|
33
33
|
|
|
34
|
-
## Quick start (
|
|
34
|
+
## Quick start (with custom resolvers)
|
|
35
35
|
|
|
36
36
|
```ts
|
|
37
37
|
import { resolve, processEnv } from 'node-env-resolver';
|
|
38
|
+
import { dotenv } from 'node-env-resolver/resolvers';
|
|
38
39
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
40
|
+
// Synchronous with custom resolver (array syntax)
|
|
41
|
+
const config = resolve([
|
|
42
|
+
dotenv(), // Resolver must support loadSync()
|
|
43
|
+
{
|
|
44
|
+
PORT: 3000,
|
|
45
|
+
NODE_ENV: ['development', 'production', 'test'] as const,
|
|
46
|
+
DEBUG: false,
|
|
47
|
+
API_KEY: 'string?'
|
|
48
|
+
}
|
|
49
|
+
]);
|
|
45
50
|
|
|
46
|
-
//
|
|
47
|
-
config.
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
+
// Asynchronous with any resolvers (both sync and async work!)
|
|
52
|
+
const config = await resolve.async([
|
|
53
|
+
dotenv(), {
|
|
54
|
+
PORT: 3000,
|
|
55
|
+
NODE_ENV: ['development', 'production', 'test'] as const,
|
|
56
|
+
DEBUG: false,
|
|
57
|
+
API_KEY: 'string?'
|
|
58
|
+
}],
|
|
59
|
+
[awsSecrets(), { DATABASE_URL: 'url' }]);
|
|
51
60
|
```
|
|
52
61
|
|
|
62
|
+
## Table of Contents
|
|
63
|
+
|
|
64
|
+
- [Install](#install)
|
|
65
|
+
- [Quick start](#quick-start-uses-processenv)
|
|
66
|
+
- [Quick start (with custom resolvers)](#quick-start-with-custom-resolvers)
|
|
67
|
+
- [Basic usage](#basic-usage)
|
|
68
|
+
- [Required values](#required-values)
|
|
69
|
+
- [Default values](#default-values)
|
|
70
|
+
- [Optional values](#optional-values)
|
|
71
|
+
- [Enums](#enums)
|
|
72
|
+
- [Variable Naming Conventions](#variable-naming-conventions)
|
|
73
|
+
- [Performance & Bundle Size](#performance--bundle-size)
|
|
74
|
+
- [Built-in validators](#built-in-validators)
|
|
75
|
+
- [Custom validators](#custom-validators)
|
|
76
|
+
- [Multiple sources](#multiple-sources)
|
|
77
|
+
- [Safe error handling](#safe-error-handling)
|
|
78
|
+
- [Synchronous resolution](#synchronous-resolution)
|
|
79
|
+
- [Advanced features](#advanced-features)
|
|
80
|
+
- [API Reference](#api-reference)
|
|
81
|
+
- [Configuration Options](#configuration-options)
|
|
82
|
+
- [Framework examples](#framework-examples)
|
|
83
|
+
- [Security Policies](#security-policies)
|
|
84
|
+
- [Audit Logging](#audit-logging)
|
|
85
|
+
- [Error messages](#error-messages)
|
|
86
|
+
- [Licence](#licence)
|
|
87
|
+
|
|
53
88
|
## Basic usage
|
|
54
89
|
|
|
55
90
|
### Required values
|
|
@@ -102,9 +137,49 @@ config.NODE_ENV; // 'development' | 'production' | 'test'
|
|
|
102
137
|
config.LOG_LEVEL; // 'debug' | 'info' | 'warn' | 'error'
|
|
103
138
|
```
|
|
104
139
|
|
|
140
|
+
## Variable Naming Conventions
|
|
141
|
+
|
|
142
|
+
The library validates environment variable names to prevent shell errors and ensure cross-platform compatibility.
|
|
143
|
+
|
|
144
|
+
### Supported Name Formats
|
|
145
|
+
|
|
146
|
+
Variable names can use:
|
|
147
|
+
- **Letters** (both uppercase and lowercase): `A-Z`, `a-z`
|
|
148
|
+
- **Numbers** (not as first character): `0-9`
|
|
149
|
+
- **Underscores**: `_`
|
|
150
|
+
|
|
151
|
+
### Valid Examples
|
|
152
|
+
|
|
153
|
+
```ts
|
|
154
|
+
const config = resolve({
|
|
155
|
+
PORT: 3000, // ✅ SCREAMING_SNAKE_CASE (traditional)
|
|
156
|
+
port: 3000, // ✅ lowercase
|
|
157
|
+
myApiKey: 'string', // ✅ camelCase
|
|
158
|
+
my_api_key: 'string', // ✅ snake_case
|
|
159
|
+
API_KEY_V2: 'string', // ✅ with numbers (not first char)
|
|
160
|
+
_PRIVATE: 'string' // ✅ starting with underscore
|
|
161
|
+
});
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
### Invalid Examples
|
|
165
|
+
|
|
166
|
+
```ts
|
|
167
|
+
const config = resolve({
|
|
168
|
+
'123PORT': 3000, // ❌ Starts with number
|
|
169
|
+
'API-KEY': 'string', // ❌ Contains hyphen
|
|
170
|
+
'API.KEY': 'string', // ❌ Contains dot
|
|
171
|
+
'API KEY': 'string', // ❌ Contains space
|
|
172
|
+
'API@KEY': 'string' // ❌ Special characters
|
|
173
|
+
});
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
**Recommendation:** While all formats are supported, **SCREAMING_SNAKE_CASE** (e.g., `DATABASE_URL`, `API_KEY`) is the most common convention for environment variables and ensures maximum compatibility with legacy systems.
|
|
177
|
+
|
|
178
|
+
**Validation pattern:** `/^[A-Za-z_][A-Za-z0-9_]*$/`
|
|
179
|
+
|
|
105
180
|
## Performance & Bundle Size
|
|
106
181
|
|
|
107
|
-
**Lightweight
|
|
182
|
+
**Lightweight** The core library is **~3.6KB gzipped** with validation and resolver capabilities.
|
|
108
183
|
|
|
109
184
|
### Efficient validation architecture
|
|
110
185
|
|
|
@@ -133,7 +208,7 @@ const config = resolve({
|
|
|
133
208
|
});
|
|
134
209
|
|
|
135
210
|
// Also works with async resolvers
|
|
136
|
-
const config = await resolve.
|
|
211
|
+
const config = await resolve.async([
|
|
137
212
|
awsSecrets(),
|
|
138
213
|
{ DATABASE_URL: 'postgres', API_URL: 'url' }
|
|
139
214
|
]);
|
|
@@ -231,6 +306,62 @@ Advanced validators provide specialized validation and parsing:
|
|
|
231
306
|
- `'port'` - Port number (returns number, 1-65535)
|
|
232
307
|
- `'date'` - ISO 8601 date string (returns string)
|
|
233
308
|
- `'timestamp'` - Unix timestamp (returns number)
|
|
309
|
+
- `'duration'` - Time duration like `'5s'`, `'2m'`, `'1h'` (returns milliseconds as number)
|
|
310
|
+
- `'file'` - Read content from file path (returns trimmed file content as string)
|
|
311
|
+
|
|
312
|
+
**Array types:**
|
|
313
|
+
|
|
314
|
+
- `'string[]'` - Array of strings from comma-separated values (returns `string[]`)
|
|
315
|
+
- `'number[]'` - Array of numbers from comma-separated values (returns `number[]`)
|
|
316
|
+
- `'url[]'` - Array of validated URLs from comma-separated values (returns `string[]`)
|
|
317
|
+
|
|
318
|
+
```ts
|
|
319
|
+
// Array examples
|
|
320
|
+
process.env.TAGS = 'frontend,backend,mobile';
|
|
321
|
+
process.env.PORTS = '3000,8080,9000';
|
|
322
|
+
process.env.ALLOWED_ORIGINS = 'https://app.com,https://api.com';
|
|
323
|
+
|
|
324
|
+
const config = resolve({
|
|
325
|
+
TAGS: 'string[]', // ['frontend', 'backend', 'mobile']
|
|
326
|
+
PORTS: 'number[]', // [3000, 8080, 9000]
|
|
327
|
+
ALLOWED_ORIGINS: 'url[]' // ['https://app.com', 'https://api.com']
|
|
328
|
+
});
|
|
329
|
+
|
|
330
|
+
// Custom separator
|
|
331
|
+
const config = resolve({
|
|
332
|
+
TAGS: { type: 'string[]', separator: '|' } // Use pipe instead of comma
|
|
333
|
+
});
|
|
334
|
+
|
|
335
|
+
// Duration parsing
|
|
336
|
+
process.env.TIMEOUT = '30s';
|
|
337
|
+
process.env.CACHE_TTL = '5m';
|
|
338
|
+
process.env.SESSION_DURATION = '24h';
|
|
339
|
+
|
|
340
|
+
const config = await resolve.async([processEnv(), {
|
|
341
|
+
TIMEOUT: 'duration', // 30000 (milliseconds)
|
|
342
|
+
CACHE_TTL: 'duration', // 300000
|
|
343
|
+
SESSION_DURATION: 'duration' // 86400000
|
|
344
|
+
}]);
|
|
345
|
+
|
|
346
|
+
// File reading (useful for Docker/Kubernetes secrets)
|
|
347
|
+
// Method 1: Explicit path in env var
|
|
348
|
+
process.env.DB_PASSWORD_FILE = '/run/secrets/db_password';
|
|
349
|
+
const config = await resolve.async([processEnv(), {
|
|
350
|
+
DB_PASSWORD_FILE: 'file' // 'my-secret-password' (file content, trimmed)
|
|
351
|
+
}]);
|
|
352
|
+
|
|
353
|
+
// Method 2: Using secretsDir (cleaner for Docker/K8s)
|
|
354
|
+
// Reads from /run/secrets/db_password automatically (key name lowercased)
|
|
355
|
+
const config = await resolve.async([processEnv(), {
|
|
356
|
+
DB_PASSWORD: 'file' // Reads /run/secrets/db_password
|
|
357
|
+
}], { secretsDir: '/run/secrets' });
|
|
358
|
+
|
|
359
|
+
// Per-field secretsDir (overrides global)
|
|
360
|
+
const config = await resolve.async([processEnv(), {
|
|
361
|
+
DB_PASSWORD: { type: 'file', secretsDir: '/custom/secrets' },
|
|
362
|
+
API_KEY: 'file' // Uses global secretsDir
|
|
363
|
+
}], { secretsDir: '/run/secrets' });
|
|
364
|
+
```
|
|
234
365
|
|
|
235
366
|
All validators automatically handle type coercion from environment variable strings.
|
|
236
367
|
|
|
@@ -296,9 +427,10 @@ Load configuration from multiple sources. By default, later sources override ear
|
|
|
296
427
|
|
|
297
428
|
```ts
|
|
298
429
|
import { resolve, processEnv } from 'node-env-resolver';
|
|
299
|
-
import { dotenv } from 'node-env-resolver/resolvers';
|
|
430
|
+
import { dotenv, json } from 'node-env-resolver/resolvers';
|
|
300
431
|
|
|
301
|
-
|
|
432
|
+
// Async mode - supports both sync and async resolvers
|
|
433
|
+
const config = await resolve.async(
|
|
302
434
|
[processEnv(), {
|
|
303
435
|
PORT: 3000,
|
|
304
436
|
NODE_ENV: ['development', 'production'] as const,
|
|
@@ -308,27 +440,42 @@ const config = await resolve.with(
|
|
|
308
440
|
API_KEY: 'string',
|
|
309
441
|
}]
|
|
310
442
|
);
|
|
443
|
+
|
|
444
|
+
// Sync mode - supports multiple SYNC resolvers (NEW!)
|
|
445
|
+
const config = resolve(
|
|
446
|
+
[dotenv(), { PORT: 3000 }],
|
|
447
|
+
[json('config.json'), { DATABASE_URL: 'postgres' }]
|
|
448
|
+
// Both resolvers must have loadSync() method
|
|
449
|
+
);
|
|
311
450
|
```
|
|
312
451
|
|
|
313
452
|
### Controlling merge behaviour
|
|
314
453
|
|
|
315
|
-
Use `priority` to control how resolvers merge values:
|
|
454
|
+
Use `priority` to control how resolvers merge values (works with both sync and async modes):
|
|
316
455
|
|
|
317
456
|
```ts
|
|
318
457
|
// priority: 'last' (default) - later resolvers override earlier ones
|
|
319
|
-
const config = await resolve.
|
|
458
|
+
const config = await resolve.async(
|
|
320
459
|
[processEnv(), { DATABASE_URL: 'postgres' }],
|
|
321
460
|
[awsSecrets(), { DATABASE_URL: 'postgres' }]
|
|
322
461
|
// AWS wins
|
|
323
462
|
);
|
|
324
463
|
|
|
325
464
|
// priority: 'first' - earlier resolvers take precedence
|
|
326
|
-
const config = await resolve.
|
|
465
|
+
const config = await resolve.async(
|
|
327
466
|
[dotenv(), { DATABASE_URL: 'postgres' }],
|
|
328
467
|
[awsSecrets(), { DATABASE_URL: 'postgres' }],
|
|
329
468
|
{ priority: 'first' }
|
|
330
469
|
// dotenv wins
|
|
331
470
|
);
|
|
471
|
+
|
|
472
|
+
// Also works with sync resolve() (NEW!)
|
|
473
|
+
const config = resolve(
|
|
474
|
+
[dotenv(), { DATABASE_URL: 'postgres' }],
|
|
475
|
+
[json('config.json'), { DATABASE_URL: 'postgres' }],
|
|
476
|
+
{ priority: 'first' }
|
|
477
|
+
// dotenv wins
|
|
478
|
+
);
|
|
332
479
|
```
|
|
333
480
|
|
|
334
481
|
This is useful for development workflows where local overrides should take precedence over cloud secrets.
|
|
@@ -342,7 +489,7 @@ The library includes two automatic performance optimizations:
|
|
|
342
489
|
When using `priority: 'first'`, resolvers are called sequentially, but execution **stops early** once all required environment variables are satisfied:
|
|
343
490
|
|
|
344
491
|
```ts
|
|
345
|
-
const config = await resolve.
|
|
492
|
+
const config = await resolve.async(
|
|
346
493
|
[dotenv(), { DATABASE_URL: 'postgres', API_KEY: 'string', PORT: 3000 }],
|
|
347
494
|
[awsSecrets(), { DATABASE_URL: 'postgres', API_KEY: 'string', PORT: 3000 }],
|
|
348
495
|
[gcpSecrets(), { DATABASE_URL: 'postgres', API_KEY: 'string', PORT: 3000 }],
|
|
@@ -367,7 +514,7 @@ This is particularly valuable in development where:
|
|
|
367
514
|
When using `priority: 'last'` (the default), all resolvers are called **in parallel** for maximum performance:
|
|
368
515
|
|
|
369
516
|
```ts
|
|
370
|
-
const config = await resolve.
|
|
517
|
+
const config = await resolve.async(
|
|
371
518
|
[awsSecrets(), { DATABASE_URL: 'postgres' }], // 100ms
|
|
372
519
|
[awsParameterStore(), { API_KEY: 'string' }], // 100ms
|
|
373
520
|
[gcpSecrets(), { JWT_SECRET: 'string' }] // 100ms
|
|
@@ -409,7 +556,7 @@ if (result.success) {
|
|
|
409
556
|
All functions have safe variants:
|
|
410
557
|
|
|
411
558
|
- `resolve()` → `safeResolve()` (both synchronous)
|
|
412
|
-
- `resolve.
|
|
559
|
+
- `resolve.async()` → `safeResolve.async()` (both async)
|
|
413
560
|
|
|
414
561
|
## Synchronous resolution
|
|
415
562
|
|
|
@@ -429,11 +576,11 @@ const config = resolve({
|
|
|
429
576
|
});
|
|
430
577
|
```
|
|
431
578
|
|
|
432
|
-
`resolve.
|
|
579
|
+
`resolve.async()` is **async** when using custom resolvers:
|
|
433
580
|
|
|
434
581
|
```ts
|
|
435
582
|
// Async - await required when using custom resolvers
|
|
436
|
-
const config = await resolve.
|
|
583
|
+
const config = await resolve.async(
|
|
437
584
|
[processEnv(), { PORT: 3000 }],
|
|
438
585
|
[dotenv(), { DATABASE_URL: 'postgres', API_URL: 'url' }]
|
|
439
586
|
);
|
|
@@ -441,6 +588,107 @@ const config = await resolve.with(
|
|
|
441
588
|
|
|
442
589
|
## Advanced features
|
|
443
590
|
|
|
591
|
+
### CLI arguments
|
|
592
|
+
|
|
593
|
+
Parse command-line arguments as environment variables - perfect for CLI tools:
|
|
594
|
+
|
|
595
|
+
```ts
|
|
596
|
+
import { resolve } from 'node-env-resolver';
|
|
597
|
+
import { cliArgs } from 'node-env-resolver/cli';
|
|
598
|
+
|
|
599
|
+
// $ node app.js --port 8080 --database-url postgres://localhost --verbose
|
|
600
|
+
|
|
601
|
+
const config = await resolve.async(
|
|
602
|
+
[processEnv(), {
|
|
603
|
+
PORT: 3000,
|
|
604
|
+
DATABASE_URL: 'postgres',
|
|
605
|
+
VERBOSE: false
|
|
606
|
+
}],
|
|
607
|
+
[cliArgs(), {
|
|
608
|
+
PORT: 3000,
|
|
609
|
+
DATABASE_URL: 'postgres',
|
|
610
|
+
VERBOSE: false
|
|
611
|
+
}]
|
|
612
|
+
);
|
|
613
|
+
|
|
614
|
+
// config.PORT === 8080 (from CLI)
|
|
615
|
+
// config.DATABASE_URL === 'postgres://localhost' (from CLI)
|
|
616
|
+
// config.VERBOSE === true (from CLI flag)
|
|
617
|
+
```
|
|
618
|
+
|
|
619
|
+
**Supported formats:**
|
|
620
|
+
- `--key value` → `KEY=value`
|
|
621
|
+
- `--key=value` → `KEY=value`
|
|
622
|
+
- `--flag` → `FLAG=true` (boolean flags)
|
|
623
|
+
- `--kebab-case` → `KEBAB_CASE` (auto-normalization)
|
|
624
|
+
|
|
625
|
+
**Bundle size:** ~500 bytes (lazy-loaded)
|
|
626
|
+
|
|
627
|
+
### Computed fields
|
|
628
|
+
|
|
629
|
+
Derive properties from resolved configuration:
|
|
630
|
+
|
|
631
|
+
```ts
|
|
632
|
+
import { resolve } from 'node-env-resolver';
|
|
633
|
+
import { withComputed } from 'node-env-resolver/utils';
|
|
634
|
+
|
|
635
|
+
const config = resolve({
|
|
636
|
+
HOST: 'localhost',
|
|
637
|
+
PORT: 3000,
|
|
638
|
+
SSL_ENABLED: false,
|
|
639
|
+
NODE_ENV: ['development', 'production'] as const
|
|
640
|
+
});
|
|
641
|
+
|
|
642
|
+
// Add computed properties
|
|
643
|
+
const appConfig = withComputed(config, {
|
|
644
|
+
// Build URLs from components
|
|
645
|
+
url: (c) => `${c.SSL_ENABLED ? 'https' : 'http'}://${c.HOST}:${c.PORT}`,
|
|
646
|
+
|
|
647
|
+
// Environment checks
|
|
648
|
+
isProd: (c) => c.NODE_ENV === 'production',
|
|
649
|
+
isDev: (c) => c.NODE_ENV === 'development',
|
|
650
|
+
|
|
651
|
+
// Complex derived config
|
|
652
|
+
serverOptions: (c) => ({
|
|
653
|
+
host: c.HOST,
|
|
654
|
+
port: c.PORT,
|
|
655
|
+
cors: c.NODE_ENV !== 'production',
|
|
656
|
+
compression: c.NODE_ENV === 'production'
|
|
657
|
+
})
|
|
658
|
+
});
|
|
659
|
+
|
|
660
|
+
console.log(appConfig.url); // 'http://localhost:3000'
|
|
661
|
+
console.log(appConfig.isProd); // false
|
|
662
|
+
console.log(appConfig.serverOptions); // { host: 'localhost', ... }
|
|
663
|
+
```
|
|
664
|
+
|
|
665
|
+
**Common patterns:**
|
|
666
|
+
```ts
|
|
667
|
+
// Build database connection URLs
|
|
668
|
+
withComputed(config, {
|
|
669
|
+
databaseUrl: (c) =>
|
|
670
|
+
`postgres://${c.DB_USER}:${c.DB_PASSWORD}@${c.DB_HOST}:${c.DB_PORT}/${c.DB_NAME}`
|
|
671
|
+
});
|
|
672
|
+
|
|
673
|
+
// Derive API endpoints
|
|
674
|
+
withComputed(config, {
|
|
675
|
+
endpoints: (c) => ({
|
|
676
|
+
users: `${c.API_BASE_URL}/${c.API_VERSION}/users`,
|
|
677
|
+
posts: `${c.API_BASE_URL}/${c.API_VERSION}/posts`
|
|
678
|
+
})
|
|
679
|
+
});
|
|
680
|
+
|
|
681
|
+
// Feature flags based on environment
|
|
682
|
+
withComputed(config, {
|
|
683
|
+
features: (c) => ({
|
|
684
|
+
analytics: c.ENABLE_ANALYTICS && c.NODE_ENV === 'production',
|
|
685
|
+
debug: c.NODE_ENV === 'development'
|
|
686
|
+
})
|
|
687
|
+
});
|
|
688
|
+
```
|
|
689
|
+
|
|
690
|
+
**Bundle size:** ~200 bytes (included in utils)
|
|
691
|
+
|
|
444
692
|
### Zod integration
|
|
445
693
|
|
|
446
694
|
Use Zod schemas for powerful validation:
|
|
@@ -557,7 +805,7 @@ const databaseResolver: Resolver = {
|
|
|
557
805
|
}
|
|
558
806
|
};
|
|
559
807
|
|
|
560
|
-
const config = await resolve.
|
|
808
|
+
const config = await resolve.async(
|
|
561
809
|
[processEnv(), { PORT: 3000 }],
|
|
562
810
|
[databaseResolver, { API_KEY: 'string' }]
|
|
563
811
|
);
|
|
@@ -590,7 +838,7 @@ Multiple sources:
|
|
|
590
838
|
import { resolve } from 'node-env-resolver';
|
|
591
839
|
import { awsSecrets, awsSsm } from 'node-env-resolver-aws';
|
|
592
840
|
|
|
593
|
-
const config = await resolve.
|
|
841
|
+
const config = await resolve.async(
|
|
594
842
|
[processEnv(), { PORT: 3000 }],
|
|
595
843
|
[awsSecrets({ secretId: 'app/secrets' }), { DATABASE_URL: 'postgres' }],
|
|
596
844
|
[awsSsm({ path: '/app/config' }), { API_KEY: 'string' }]
|
|
@@ -609,7 +857,7 @@ import { cached, TTL } from 'node-env-resolver/utils';
|
|
|
609
857
|
import { awsSecrets } from 'node-env-resolver-aws';
|
|
610
858
|
|
|
611
859
|
export const getConfig = async () => {
|
|
612
|
-
return await resolve.
|
|
860
|
+
return await resolve.async(
|
|
613
861
|
[cached(
|
|
614
862
|
awsSecrets({ secretId: 'app/secrets' }),
|
|
615
863
|
{ ttl: TTL.minutes5 }
|
|
@@ -642,19 +890,22 @@ Performance:
|
|
|
642
890
|
|----------|-----------|----------|
|
|
643
891
|
| `resolve()` | Sync, throws on error | Most applications (reading from process.env) |
|
|
644
892
|
| `safeResolve()` | Sync, returns result object | Graceful error handling |
|
|
645
|
-
| `resolve.
|
|
646
|
-
| `safeResolve.
|
|
893
|
+
| `resolve.async()` | Async, throws on error | Multiple sources (dotenv, AWS, etc.) |
|
|
894
|
+
| `safeResolve.async()` | Async, returns result object | Multiple sources with error handling |
|
|
647
895
|
|
|
648
896
|
### Shorthand syntax
|
|
649
897
|
|
|
650
898
|
| Syntax | Type | Description |
|
|
651
899
|
|--------|------|-------------|
|
|
652
|
-
| `'string'` | `string` | Required string |
|
|
900
|
+
| `'string'` | `string` | Required string (empty strings rejected by default) |
|
|
653
901
|
| `'string?'` | `string \| undefined` | Optional string |
|
|
654
902
|
| `'number'` | `number` | Required number (coerced from string) |
|
|
655
903
|
| `'number?'` | `number \| undefined` | Optional number |
|
|
656
904
|
| `'boolean'` | `boolean` | Required boolean (coerced from string) |
|
|
657
905
|
| `'boolean?'` | `boolean \| undefined` | Optional boolean |
|
|
906
|
+
| `'string[]'` | `string[]` | Array of strings (comma-separated) |
|
|
907
|
+
| `'number[]'` | `number[]` | Array of numbers (comma-separated) |
|
|
908
|
+
| `'url[]'` | `string[]` | Array of validated URLs |
|
|
658
909
|
| `'url'` | `string` | Validated URL (returns string) |
|
|
659
910
|
| `'email'` | `string` | Validated email address |
|
|
660
911
|
| `'port'` | `number` | Validated port number (1-65535) |
|
|
@@ -662,6 +913,8 @@ Performance:
|
|
|
662
913
|
| `'postgres'` | `string` | Validated PostgreSQL URL |
|
|
663
914
|
| `'date'` | `string` | Validated ISO 8601 date |
|
|
664
915
|
| `'timestamp'` | `number` | Validated Unix timestamp |
|
|
916
|
+
| `'duration'` | `number` | Time duration (`5s`, `2m`, `1h` → milliseconds) |
|
|
917
|
+
| `'file'` | `string` | Read content from file path |
|
|
665
918
|
| `3000` | `number` | Number with default |
|
|
666
919
|
| `false` | `boolean` | Boolean with default |
|
|
667
920
|
| `['a', 'b']` | `'a' \| 'b'` | Enum (requires `as const`) |
|
|
@@ -679,8 +932,8 @@ const config = resolve(schema, {
|
|
|
679
932
|
enableAudit: true
|
|
680
933
|
});
|
|
681
934
|
|
|
682
|
-
// Multiple sources - options as last parameter to resolve.
|
|
683
|
-
const config = await resolve.
|
|
935
|
+
// Multiple sources - options as last parameter to resolve.async()
|
|
936
|
+
const config = await resolve.async(
|
|
684
937
|
[resolver1, schema],
|
|
685
938
|
[resolver2, schema],
|
|
686
939
|
{
|
|
@@ -693,7 +946,7 @@ const config = await resolve.with(
|
|
|
693
946
|
);
|
|
694
947
|
```
|
|
695
948
|
|
|
696
|
-
**Note:** The `resolvers` option has been removed. For single source resolution, use `resolve()` directly (defaults to `process.env`). For multiple sources, use `resolve.
|
|
949
|
+
**Note:** The `resolvers` option has been removed. For single source resolution, use `resolve()` directly (defaults to `process.env`). For multiple sources, use `resolve.async()` syntax.
|
|
697
950
|
|
|
698
951
|
### `interpolate`
|
|
699
952
|
|
|
@@ -703,7 +956,7 @@ const config = await resolve.with(
|
|
|
703
956
|
|
|
704
957
|
**Why:** Keeps configuration DRY and maintainable.
|
|
705
958
|
|
|
706
|
-
**Default:** `true` in `resolve.
|
|
959
|
+
**Default:** `true` in `resolve.async()`, `false` in `resolve()`
|
|
707
960
|
|
|
708
961
|
```ts
|
|
709
962
|
// With interpolation
|
|
@@ -745,14 +998,14 @@ const flakyResolver = {
|
|
|
745
998
|
};
|
|
746
999
|
|
|
747
1000
|
// ❌ Throws immediately
|
|
748
|
-
await resolve.
|
|
1001
|
+
await resolve.async(
|
|
749
1002
|
[flakyResolver, schema],
|
|
750
1003
|
[processEnv(), schema],
|
|
751
1004
|
{ strict: true } // default
|
|
752
1005
|
);
|
|
753
1006
|
|
|
754
1007
|
// ✅ Continues with processEnv()
|
|
755
|
-
await resolve.
|
|
1008
|
+
await resolve.async(
|
|
756
1009
|
[flakyResolver, schema],
|
|
757
1010
|
[processEnv(), schema],
|
|
758
1011
|
{ strict: false } // graceful degradation
|
|
@@ -776,14 +1029,14 @@ await resolve.with(
|
|
|
776
1029
|
|
|
777
1030
|
```ts
|
|
778
1031
|
// Production: AWS secrets override process.env
|
|
779
|
-
await resolve.
|
|
1032
|
+
await resolve.async(
|
|
780
1033
|
[processEnv(), { DATABASE_URL: 'postgres' }],
|
|
781
1034
|
[awsSecrets(), { DATABASE_URL: 'postgres' }]
|
|
782
1035
|
// priority: 'last' (default) - AWS wins
|
|
783
1036
|
);
|
|
784
1037
|
|
|
785
1038
|
// Development: Local .env overrides cloud
|
|
786
|
-
await resolve.
|
|
1039
|
+
await resolve.async(
|
|
787
1040
|
[dotenv(), { DATABASE_URL: 'postgres' }],
|
|
788
1041
|
[awsSecrets(), { DATABASE_URL: 'postgres' }],
|
|
789
1042
|
{ priority: 'first' } // dotenv wins
|
|
@@ -803,7 +1056,7 @@ await resolve.with(
|
|
|
803
1056
|
**Default:** `undefined` (no policies enforced)
|
|
804
1057
|
|
|
805
1058
|
```ts
|
|
806
|
-
await resolve.
|
|
1059
|
+
await resolve.async(
|
|
807
1060
|
[processEnv(), schema],
|
|
808
1061
|
[awsSecrets(), schema],
|
|
809
1062
|
{
|
|
@@ -838,7 +1091,7 @@ await resolve.with(
|
|
|
838
1091
|
**Default:** `false` (disabled in development), automatically `true` in production (`NODE_ENV === 'production'`)
|
|
839
1092
|
|
|
840
1093
|
```ts
|
|
841
|
-
await resolve.
|
|
1094
|
+
await resolve.async(
|
|
842
1095
|
[processEnv(), schema],
|
|
843
1096
|
[awsSecrets(), schema],
|
|
844
1097
|
{ enableAudit: true } // Explicitly enable in development
|
|
@@ -857,11 +1110,12 @@ const logs = getAuditLog();
|
|
|
857
1110
|
|
|
858
1111
|
```ts
|
|
859
1112
|
interface ResolveOptions {
|
|
860
|
-
interpolate?: boolean; // Variable interpolation (default: true in .
|
|
1113
|
+
interpolate?: boolean; // Variable interpolation (default: true in .async())
|
|
861
1114
|
strict?: boolean; // Fail-fast behaviour (default: true)
|
|
862
1115
|
priority?: 'first' | 'last'; // Merge strategy (default: 'last')
|
|
863
1116
|
policies?: PolicyOptions; // Security policies (default: undefined)
|
|
864
1117
|
enableAudit?: boolean; // Audit logging (default: auto in production)
|
|
1118
|
+
secretsDir?: string; // Base directory for file secrets (Docker/K8s)
|
|
865
1119
|
}
|
|
866
1120
|
|
|
867
1121
|
interface PolicyOptions {
|
|
@@ -913,7 +1167,7 @@ import { cached, TTL } from 'node-env-resolver/utils';
|
|
|
913
1167
|
import { awsSecrets } from 'node-env-resolver-aws';
|
|
914
1168
|
|
|
915
1169
|
const getConfig = async () => {
|
|
916
|
-
return await resolve.
|
|
1170
|
+
return await resolve.async(
|
|
917
1171
|
[cached(
|
|
918
1172
|
awsSecrets({ secretId: 'lambda/config' }),
|
|
919
1173
|
{ ttl: TTL.minutes5 }
|
|
@@ -941,7 +1195,7 @@ By default, `.env` files are **completely blocked in production** for security.
|
|
|
941
1195
|
|
|
942
1196
|
```ts
|
|
943
1197
|
// In production (NODE_ENV=production)
|
|
944
|
-
const config = await resolve.
|
|
1198
|
+
const config = await resolve.async(
|
|
945
1199
|
[dotenv(), {
|
|
946
1200
|
DATABASE_URL: 'postgres',
|
|
947
1201
|
}]
|
|
@@ -952,7 +1206,7 @@ const config = await resolve.with(
|
|
|
952
1206
|
**Allow all .env variables (NOT recommended):**
|
|
953
1207
|
|
|
954
1208
|
```ts
|
|
955
|
-
const config = await resolve.
|
|
1209
|
+
const config = await resolve.async(
|
|
956
1210
|
[dotenv(), {
|
|
957
1211
|
DATABASE_URL: 'postgres',
|
|
958
1212
|
}],
|
|
@@ -967,7 +1221,7 @@ const config = await resolve.with(
|
|
|
967
1221
|
**Allow specific variables only (recommended if needed):**
|
|
968
1222
|
|
|
969
1223
|
```ts
|
|
970
|
-
const config = await resolve.
|
|
1224
|
+
const config = await resolve.async(
|
|
971
1225
|
[dotenv(), {
|
|
972
1226
|
PORT: 3000,
|
|
973
1227
|
DATABASE_URL: 'postgres',
|
|
@@ -989,7 +1243,7 @@ Restrict sensitive variables to specific resolvers (e.g., force secrets to come
|
|
|
989
1243
|
import { resolve, processEnv } from 'node-env-resolver';
|
|
990
1244
|
import { awsSecrets } from 'node-env-resolver-aws';
|
|
991
1245
|
|
|
992
|
-
const config = await resolve.
|
|
1246
|
+
const config = await resolve.async(
|
|
993
1247
|
[processEnv(), {
|
|
994
1248
|
PORT: 3000,
|
|
995
1249
|
}],
|
|
@@ -1016,7 +1270,7 @@ import { resolve, processEnv } from 'node-env-resolver';
|
|
|
1016
1270
|
import { cached, TTL } from 'node-env-resolver/utils';
|
|
1017
1271
|
import { awsSecrets } from 'node-env-resolver-aws';
|
|
1018
1272
|
|
|
1019
|
-
const config = await resolve.
|
|
1273
|
+
const config = await resolve.async(
|
|
1020
1274
|
[processEnv(), {}],
|
|
1021
1275
|
[cached(awsSecrets({ secretId: 'prod/db' }), { ttl: TTL.minutes5 }), {
|
|
1022
1276
|
DATABASE_PASSWORD: 'string',
|
|
@@ -1082,7 +1336,7 @@ const config = resolve({
|
|
|
1082
1336
|
console.log(getAuditLog());
|
|
1083
1337
|
// [
|
|
1084
1338
|
// { type: 'env_loaded', key: 'DATABASE_URL', source: 'process.env', timestamp: ... },
|
|
1085
|
-
// { type: 'env_loaded', key: 'API_KEY', source: '
|
|
1339
|
+
// { type: 'env_loaded', key: 'API_KEY', source: 'process.env', timestamp: ... },
|
|
1086
1340
|
// { type: 'validation_success', metadata: { variableCount: 2 }, timestamp: ... }
|
|
1087
1341
|
// ]
|
|
1088
1342
|
|
|
@@ -1102,6 +1356,51 @@ const config = resolve({
|
|
|
1102
1356
|
console.log(getAuditLog());
|
|
1103
1357
|
```
|
|
1104
1358
|
|
|
1359
|
+
### Per-Config Audit Tracking
|
|
1360
|
+
|
|
1361
|
+
When you have multiple `resolve()` calls, you can now get audit logs **specific to each config object**:
|
|
1362
|
+
|
|
1363
|
+
```ts
|
|
1364
|
+
import { resolve, getAuditLog } from 'node-env-resolver';
|
|
1365
|
+
|
|
1366
|
+
const appConfig = resolve({
|
|
1367
|
+
PORT: 3000,
|
|
1368
|
+
API_KEY: 'string'
|
|
1369
|
+
}, { enableAudit: true });
|
|
1370
|
+
|
|
1371
|
+
const dbConfig = resolve({
|
|
1372
|
+
DATABASE_URL: 'postgres',
|
|
1373
|
+
DB_POOL_SIZE: 10
|
|
1374
|
+
}, { enableAudit: true });
|
|
1375
|
+
|
|
1376
|
+
// Get audit logs for specific config (NEW!)
|
|
1377
|
+
const appAudit = getAuditLog(appConfig); // Only PORT and API_KEY events
|
|
1378
|
+
const dbAudit = getAuditLog(dbConfig); // Only DATABASE_URL and DB_POOL_SIZE events
|
|
1379
|
+
|
|
1380
|
+
// Still works: get ALL audit events (backward compatible)
|
|
1381
|
+
const allAudit = getAuditLog(); // All events from both configs
|
|
1382
|
+
|
|
1383
|
+
console.log('App config audit:', appAudit);
|
|
1384
|
+
// [
|
|
1385
|
+
// { type: 'env_loaded', key: 'PORT', source: 'process.env', ... },
|
|
1386
|
+
// { type: 'env_loaded', key: 'API_KEY', source: 'process.env', ... },
|
|
1387
|
+
// { type: 'validation_success', ... }
|
|
1388
|
+
// ]
|
|
1389
|
+
|
|
1390
|
+
console.log('DB config audit:', dbAudit);
|
|
1391
|
+
// [
|
|
1392
|
+
// { type: 'env_loaded', key: 'DATABASE_URL', source: 'process.env', ... },
|
|
1393
|
+
// { type: 'env_loaded', key: 'DB_POOL_SIZE', source: 'process.env', ... },
|
|
1394
|
+
// { type: 'validation_success', ... }
|
|
1395
|
+
// ]
|
|
1396
|
+
```
|
|
1397
|
+
|
|
1398
|
+
**Use cases:**
|
|
1399
|
+
- Multi-tenant applications with separate configs per tenant
|
|
1400
|
+
- Microservices with different config sources
|
|
1401
|
+
- Testing/debugging specific configuration loads
|
|
1402
|
+
- Isolating audit trails in complex applications
|
|
1403
|
+
|
|
1105
1404
|
### Audit Event Types
|
|
1106
1405
|
|
|
1107
1406
|
| Event Type | When It Occurs |
|
|
@@ -1121,7 +1420,7 @@ import { resolve, getAuditLog } from 'node-env-resolver';
|
|
|
1121
1420
|
import { cached } from 'node-env-resolver/utils';
|
|
1122
1421
|
import { awsSecrets } from 'node-env-resolver-aws';
|
|
1123
1422
|
|
|
1124
|
-
const config = await resolve.
|
|
1423
|
+
const config = await resolve.async(
|
|
1125
1424
|
[cached(awsSecrets({ secretId: 'prod/db' }), { ttl: 300000 }), {
|
|
1126
1425
|
DATABASE_URL: 'postgres',
|
|
1127
1426
|
}],
|
|
@@ -1149,7 +1448,7 @@ import { resolve, processEnv, getAuditLog } from 'node-env-resolver';
|
|
|
1149
1448
|
import { awsSecrets } from 'node-env-resolver-aws';
|
|
1150
1449
|
|
|
1151
1450
|
// In production
|
|
1152
|
-
const config = await resolve.
|
|
1451
|
+
const config = await resolve.async(
|
|
1153
1452
|
[processEnv(), {}],
|
|
1154
1453
|
[awsSecrets(), {
|
|
1155
1454
|
DATABASE_PASSWORD: 'string',
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"advanced-features.test.d.ts","sourceRoot":"","sources":["../src/advanced-features.test.ts"],"names":[],"mappings":""}
|
package/dist/audit.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
'use strict';var chunk4ZOF2PI4_cjs=require('./chunk-4ZOF2PI4.cjs');require('./chunk-KIQXH265.cjs');chunk4ZOF2PI4_cjs.f();Object.defineProperty(exports,"attachAuditSession",{enumerable:true,get:function(){return chunk4ZOF2PI4_cjs.a}});Object.defineProperty(exports,"clearAuditLog",{enumerable:true,get:function(){return chunk4ZOF2PI4_cjs.d}});Object.defineProperty(exports,"getAuditLog",{enumerable:true,get:function(){return chunk4ZOF2PI4_cjs.c}});Object.defineProperty(exports,"logAuditEvent",{enumerable:true,get:function(){return chunk4ZOF2PI4_cjs.b}});
|