node-env-resolver 4.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.
Files changed (111) hide show
  1. package/README.md +493 -376
  2. package/dist/audit.d.cts +29 -0
  3. package/dist/audit.d.ts +8 -7
  4. package/dist/builder.cjs +1 -1
  5. package/dist/builder.d.cts +149 -0
  6. package/dist/builder.d.ts +17 -14
  7. package/dist/builder.js +1 -1
  8. package/dist/chunk-4QOKRJ3F.cjs +1 -0
  9. package/dist/chunk-4YVXFL3I.cjs +9 -0
  10. package/dist/chunk-6D5APQIG.cjs +1 -0
  11. package/dist/chunk-DIXI6MUB.js +9 -0
  12. package/dist/chunk-FH54ESWI.cjs +9 -0
  13. package/dist/chunk-HHOS5S5T.js +1 -0
  14. package/dist/chunk-TAX63ALN.js +16 -0
  15. package/dist/chunk-WKIPEKF6.js +1 -0
  16. package/dist/chunk-XJLMVSPN.js +9 -0
  17. package/dist/chunk-XPL5XLTA.cjs +16 -0
  18. package/dist/cli.d.cts +52 -0
  19. package/dist/cli.d.ts +9 -6
  20. package/dist/index.cjs +1 -1
  21. package/dist/index.d.cts +148 -0
  22. package/dist/index.d.ts +99 -92
  23. package/dist/index.js +1 -1
  24. package/dist/resolver.cjs +1 -1
  25. package/dist/resolver.d.cts +20 -0
  26. package/dist/resolver.d.ts +8 -5
  27. package/dist/resolver.js +1 -1
  28. package/dist/resolvers.cjs +1 -1
  29. package/dist/resolvers.d.cts +412 -0
  30. package/dist/resolvers.d.ts +294 -22
  31. package/dist/resolvers.js +1 -1
  32. package/dist/types.cjs +1 -1
  33. package/dist/types.d.cts +106 -0
  34. package/dist/types.d.ts +90 -172
  35. package/dist/types.js +1 -1
  36. package/dist/utils.cjs +1 -1
  37. package/dist/utils.d.cts +213 -0
  38. package/dist/utils.d.ts +28 -26
  39. package/dist/utils.js +1 -1
  40. package/dist/valibot.cjs +1 -1
  41. package/dist/valibot.d.cts +156 -0
  42. package/dist/valibot.d.ts +11 -8
  43. package/dist/valibot.js +1 -1
  44. package/dist/validation-types.d.cts +29 -0
  45. package/dist/validation-types.d.ts +4 -3
  46. package/dist/validators.cjs +2 -1
  47. package/dist/validators.d.cts +116 -0
  48. package/dist/validators.d.ts +46 -18
  49. package/dist/validators.js +2 -1
  50. package/dist/web.cjs +3 -3
  51. package/dist/web.d.cts +69 -0
  52. package/dist/web.d.ts +28 -10
  53. package/dist/web.js +3 -3
  54. package/dist/zod.cjs +1 -1
  55. package/dist/zod.d.cts +135 -0
  56. package/dist/zod.d.ts +11 -8
  57. package/dist/zod.js +1 -1
  58. package/package.json +7 -7
  59. package/dist/advanced-features.test.d.ts +0 -2
  60. package/dist/advanced-features.test.d.ts.map +0 -1
  61. package/dist/audit.d.ts.map +0 -1
  62. package/dist/audit.test.d.ts +0 -2
  63. package/dist/audit.test.d.ts.map +0 -1
  64. package/dist/builder.d.ts.map +0 -1
  65. package/dist/chunk-54OHKWZJ.js +0 -1
  66. package/dist/chunk-FCFDGYTF.cjs +0 -15
  67. package/dist/chunk-HCVTYYLE.js +0 -9
  68. package/dist/chunk-IU5TG3HL.js +0 -15
  69. package/dist/chunk-J7ZHEMRP.js +0 -9
  70. package/dist/chunk-NKPXKLF7.js +0 -1
  71. package/dist/chunk-P3ZWKKKW.cjs +0 -1
  72. package/dist/chunk-THFGEKYL.cjs +0 -9
  73. package/dist/chunk-ULL4WKTV.cjs +0 -9
  74. package/dist/chunk-XPGSWVRT.cjs +0 -1
  75. package/dist/cli.d.ts.map +0 -1
  76. package/dist/cli.test.d.ts +0 -2
  77. package/dist/cli.test.d.ts.map +0 -1
  78. package/dist/composition.test.d.ts +0 -7
  79. package/dist/composition.test.d.ts.map +0 -1
  80. package/dist/computed.test.d.ts +0 -2
  81. package/dist/computed.test.d.ts.map +0 -1
  82. package/dist/index.d.ts.map +0 -1
  83. package/dist/index.test.d.ts +0 -2
  84. package/dist/index.test.d.ts.map +0 -1
  85. package/dist/nested.test.d.ts +0 -2
  86. package/dist/nested.test.d.ts.map +0 -1
  87. package/dist/performance.test.d.ts +0 -7
  88. package/dist/performance.test.d.ts.map +0 -1
  89. package/dist/priority.test.d.ts +0 -6
  90. package/dist/priority.test.d.ts.map +0 -1
  91. package/dist/providers.d.ts +0 -11
  92. package/dist/providers.d.ts.map +0 -1
  93. package/dist/resolver.d.ts.map +0 -1
  94. package/dist/resolvers.d.ts.map +0 -1
  95. package/dist/resolvers.test.d.ts +0 -2
  96. package/dist/resolvers.test.d.ts.map +0 -1
  97. package/dist/sync-resolvers.test.d.ts +0 -2
  98. package/dist/sync-resolvers.test.d.ts.map +0 -1
  99. package/dist/type-inference.test.d.ts +0 -2
  100. package/dist/type-inference.test.d.ts.map +0 -1
  101. package/dist/type-override.test.d.ts +0 -2
  102. package/dist/type-override.test.d.ts.map +0 -1
  103. package/dist/types.d.ts.map +0 -1
  104. package/dist/utils.d.ts.map +0 -1
  105. package/dist/utils.test.d.ts +0 -2
  106. package/dist/utils.test.d.ts.map +0 -1
  107. package/dist/valibot.d.ts.map +0 -1
  108. package/dist/validation-types.d.ts.map +0 -1
  109. package/dist/validators.d.ts.map +0 -1
  110. package/dist/web.d.ts.map +0 -1
  111. package/dist/zod.d.ts.map +0 -1
package/README.md CHANGED
@@ -4,7 +4,7 @@ Type-safe environment variable resolution with zero dependencies and ultra-small
4
4
 
5
5
  [![npm version](https://img.shields.io/npm/v/node-env-resolver)](https://www.npmjs.com/package/node-env-resolver)
6
6
 
7
- **Bundle Size:** ~3.6KB gzipped
7
+ **Bundle Size:** ~4.3KB gzipped
8
8
 
9
9
  ## Install
10
10
 
@@ -16,12 +16,13 @@ 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/resolvers';
19
20
 
20
21
  const config = resolve({
21
22
  PORT: 3000,
22
23
  NODE_ENV: ['development', 'production', 'test'] as const,
23
24
  DEBUG: false,
24
- API_KEY: 'string?'
25
+ API_KEY: string({optional:true})
25
26
  });
26
27
 
27
28
  // config is fully typed
@@ -34,29 +35,46 @@ config.API_KEY; // string | undefined
34
35
  ## Quick start (with custom resolvers)
35
36
 
36
37
  ```ts
37
- import { resolve, processEnv } from 'node-env-resolver';
38
- import { dotenv } from 'node-env-resolver/resolvers';
38
+ import { resolve, resolveAsync, processEnv } from 'node-env-resolver';
39
+ import { dotenv, string, url, postgres, redis } from 'node-env-resolver/resolvers';
40
+ import { awsSecrets, gcpSecrets, vaultSecrets } from 'node-env-resolver-aws';
39
41
 
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
- ]);
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
+ });
50
53
 
51
54
  // 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' }]);
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
+ });
60
78
  ```
61
79
 
62
80
  ## Table of Contents
@@ -71,7 +89,6 @@ const config = await resolve.async([
71
89
  - [Enums](#enums)
72
90
  - [Variable Naming Conventions](#variable-naming-conventions)
73
91
  - [Performance & Bundle Size](#performance--bundle-size)
74
- - [Built-in validators](#built-in-validators)
75
92
  - [Custom validators](#custom-validators)
76
93
  - [Multiple sources](#multiple-sources)
77
94
  - [Safe error handling](#safe-error-handling)
@@ -92,9 +109,12 @@ const config = await resolve.async([
92
109
  If an environment variable is missing and has no default, validation fails:
93
110
 
94
111
  ```ts
112
+ import { resolve } from 'node-env-resolver';
113
+ import { postgres, string } from 'node-env-resolver/resolvers';
114
+
95
115
  const config = resolve({
96
- DATABASE_URL: 'postgres', // Required PostgreSQL URL
97
- API_KEY: 'string' // Required
116
+ DATABASE_URL: postgres(), // Required PostgreSQL URL
117
+ API_KEY: string() // Required
98
118
  });
99
119
  ```
100
120
 
@@ -103,6 +123,8 @@ const config = resolve({
103
123
  Provide a default value to use when the environment variable is not set:
104
124
 
105
125
  ```ts
126
+ import { resolve } from 'node-env-resolver';
127
+
106
128
  const config = resolve({
107
129
  PORT: 3000, // Defaults to 3000
108
130
  DEBUG: false, // Defaults to false
@@ -112,13 +134,15 @@ const config = resolve({
112
134
 
113
135
  ### Optional values
114
136
 
115
- Add `?` to make a value optional:
137
+ Use the `{optional: true}` option to make a value optional:
116
138
 
117
139
  ```ts
140
+ import { string, url, number } from 'node-env-resolver/resolvers';
141
+
118
142
  const config = resolve({
119
- API_KEY: 'string?', // string | undefined
120
- REDIS_URL: 'url?', // string | undefined
121
- MAX_RETRIES: 'number?' // number | undefined
143
+ API_KEY: string({optional: true}), // string | undefined
144
+ REDIS_URL: url({optional: true}), // string | undefined
145
+ MAX_RETRIES: number({optional: true}) // number | undefined
122
146
  });
123
147
  ```
124
148
 
@@ -127,6 +151,8 @@ const config = resolve({
127
151
  Use arrays for enum validation:
128
152
 
129
153
  ```ts
154
+ import { resolve } from 'node-env-resolver';
155
+
130
156
  const config = resolve({
131
157
  NODE_ENV: ['development', 'production', 'test'] as const,
132
158
  LOG_LEVEL: ['debug', 'info', 'warn', 'error'] as const
@@ -151,25 +177,31 @@ Variable names can use:
151
177
  ### Valid Examples
152
178
 
153
179
  ```ts
180
+ import { resolve } from 'node-env-resolver';
181
+ import { string } from 'node-env-resolver/resolvers';
182
+
154
183
  const config = resolve({
155
184
  PORT: 3000, // ✅ SCREAMING_SNAKE_CASE (traditional)
156
185
  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
186
+ myApiKey: string(), // ✅ camelCase
187
+ my_api_key: string(), // ✅ snake_case
188
+ API_KEY_V2: string(), // ✅ with numbers (not first char)
189
+ _PRIVATE: string() // ✅ starting with underscore
161
190
  });
162
191
  ```
163
192
 
164
193
  ### Invalid Examples
165
194
 
166
195
  ```ts
196
+ import { resolve } from 'node-env-resolver';
197
+ import { string } from 'node-env-resolver/resolvers';
198
+
167
199
  const config = resolve({
168
200
  '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
201
+ 'API-KEY': string(), // ❌ Contains hyphen
202
+ 'API.KEY': string(), // ❌ Contains dot
203
+ 'API KEY': string(), // ❌ Contains space
204
+ 'API@KEY': string() // ❌ Special characters
173
205
  });
174
206
  ```
175
207
 
@@ -179,7 +211,28 @@ const config = resolve({
179
211
 
180
212
  ## Performance & Bundle Size
181
213
 
182
- **Lightweight** The core library is **~3.6KB gzipped** with validation and resolver capabilities.
214
+ **Lightweight** The core library is **~4.3KB gzipped** with validation and resolver capabilities - **optimized for minimal bundle impact**.
215
+
216
+ ```ts
217
+ import { resolveAsync, processEnv } from 'node-env-resolver';
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
+ ]
227
+ // Perfect for complex microservice architectures!
228
+ });
229
+ ```
230
+
231
+ **Benefits:**
232
+ - **No artificial limits** - use as many resolvers as needed
233
+ - **Better type inference** - proper schema merging with full TypeScript support
234
+ - **Cleaner code** - single implementation instead of multiple overloads
235
+ - **Future-proof** - automatically supports new resolver types
183
236
 
184
237
  ### Efficient validation architecture
185
238
 
@@ -199,19 +252,24 @@ The library uses a two-tier validation strategy with lazy-loaded validators:
199
252
  Advanced validators are lazy-loaded only when you use them, keeping the base bundle minimal. All types work both synchronously and asynchronously:
200
253
 
201
254
  ```ts
255
+ import { resolve, resolveAsync } from 'node-env-resolver';
256
+ import { postgres, url } from 'node-env-resolver/resolvers';
257
+ import { awsSecrets } from 'node-env-resolver-aws';
258
+
202
259
  // Synchronous - works with all types
203
260
  const config = resolve({
204
261
  PORT: 3000,
205
- DATABASE_URL: 'postgres',
206
- API_URL: 'url',
262
+ DATABASE_URL: postgres(),
263
+ API_URL: url(),
207
264
  NODE_ENV: ['development', 'production'] as const
208
265
  });
209
266
 
210
267
  // Also works with async resolvers
211
- const config = await resolve.async([
212
- awsSecrets(),
213
- { DATABASE_URL: 'postgres', API_URL: 'url' }
214
- ]);
268
+ const config = await resolveAsync({
269
+ resolvers: [
270
+ [awsSecrets(), { DATABASE_URL: postgres(), API_URL: url() }]
271
+ ]
272
+ });
215
273
  ```
216
274
 
217
275
  ### What's in the 3.6KB core?
@@ -235,6 +293,8 @@ Only the code you actually use gets loaded!
235
293
  Audit logging is lazy-loaded only when enabled, keeping the base bundle minimal:
236
294
 
237
295
  ```ts
296
+ import { resolve } from 'node-env-resolver';
297
+
238
298
  // Base bundle
239
299
  const config = resolve({ PORT: 3000 });
240
300
 
@@ -267,109 +327,13 @@ import { awsSecrets } from 'node-env-resolver-aws';
267
327
 
268
328
  **Most apps only need the core (~3.6KB)!**
269
329
 
270
- ## Built-in validators
271
-
272
- All validator types work synchronously and asynchronously.
273
-
274
- ### Basic types
275
-
276
- These types use inline validation:
277
-
278
- - `'string'` - Any string value (with optional `min`/`max` length)
279
- - `'number'` - Numeric value (coerced from string, with optional `min`/`max`)
280
- - `'boolean'` - Boolean value (`'true'`/`'false'` coerced to boolean)
281
- - `'enum'` - Array of allowed values
282
- - `'pattern'` - Regex pattern validation
283
- - `'custom'` - Custom validator function
284
-
285
- ### Advanced types
286
-
287
- Advanced validators provide specialized validation and parsing:
288
-
289
- **Network types:**
290
-
291
- - `'url'` - Valid URL (returns string)
292
- - `'http'` - HTTP or HTTPS URL (returns string)
293
- - `'https'` - HTTPS-only URL (returns string)
294
- - `'email'` - Email address (returns string)
295
-
296
- **Database connection strings:**
297
-
298
- - `'postgres'` or `'postgresql'` - PostgreSQL connection string (returns string)
299
- - `'mysql'` - MySQL connection string (returns string)
300
- - `'mongodb'` - MongoDB connection string (returns string)
301
- - `'redis'` - Redis connection string (returns string)
302
-
303
- **Format types:**
304
-
305
- - `'json'` - JSON value (returns parsed object/array)
306
- - `'port'` - Port number (returns number, 1-65535)
307
- - `'date'` - ISO 8601 date string (returns string)
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
- ```
365
-
366
- All validators automatically handle type coercion from environment variable strings.
367
-
368
330
  ## Custom validators
369
331
 
370
332
  Write your own validation functions:
371
333
 
372
334
  ```ts
335
+ import { resolve } from 'node-env-resolver';
336
+
373
337
  const isValidPort = (value: string): number => {
374
338
  const port = parseInt(value, 10);
375
339
  if (isNaN(port) || port < 1 || port > 65535) {
@@ -424,29 +388,43 @@ Available validation functions: `validatePostgres`, `validateMysql`, `validateMo
424
388
  ## Multiple sources
425
389
 
426
390
  Load configuration from multiple sources. By default, later sources override earlier ones:
427
-
428
391
  ```ts
429
- import { resolve, processEnv } from 'node-env-resolver';
430
- import { dotenv, json } from 'node-env-resolver/resolvers';
392
+ import { resolve, resolveAsync, processEnv } from 'node-env-resolver';
393
+ import { dotenv, json, postgres, string, redis } from 'node-env-resolver/resolvers';
394
+ import { awsSecrets } from 'node-env-resolver-aws';
431
395
 
432
396
  // Async mode - supports both sync and async resolvers
433
- const config = await resolve.async(
434
- [processEnv(), {
435
- PORT: 3000,
436
- NODE_ENV: ['development', 'production'] as const,
437
- }],
438
- [dotenv(), {
439
- DATABASE_URL: 'postgres',
440
- API_KEY: 'string',
441
- }]
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' }]
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
+ });
409
+
410
+ // Sync mode - supports multiple SYNC resolvers
411
+ const config = resolve({
412
+ resolvers: [
413
+ [dotenv(), { PORT: 3000 }],
414
+ [json('config.json'), { DATABASE_URL: postgres() }]
415
+ ]
448
416
  // Both resolvers must have loadSync() method
449
- );
417
+ });
418
+
419
+ // Support for ANY number of resolvers (3, 4, 5, 10+ resolvers!)
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
+ });
450
428
  ```
451
429
 
452
430
  ### Controlling merge behaviour
@@ -454,28 +432,38 @@ const config = resolve(
454
432
  Use `priority` to control how resolvers merge values (works with both sync and async modes):
455
433
 
456
434
  ```ts
435
+ import { resolve, resolveAsync, processEnv } from 'node-env-resolver';
436
+ import { dotenv, json, postgres } from 'node-env-resolver/resolvers';
437
+ import { awsSecrets } from 'node-env-resolver-aws';
438
+
457
439
  // priority: 'last' (default) - later resolvers override earlier ones
458
- const config = await resolve.async(
459
- [processEnv(), { DATABASE_URL: 'postgres' }],
460
- [awsSecrets(), { DATABASE_URL: 'postgres' }]
440
+ const config = await resolveAsync({
441
+ resolvers: [
442
+ [processEnv(), { DATABASE_URL: postgres() }],
443
+ [awsSecrets(), { DATABASE_URL: postgres() }]
444
+ ]
461
445
  // AWS wins
462
- );
446
+ });
463
447
 
464
448
  // priority: 'first' - earlier resolvers take precedence
465
- const config = await resolve.async(
466
- [dotenv(), { DATABASE_URL: 'postgres' }],
467
- [awsSecrets(), { DATABASE_URL: 'postgres' }],
468
- { priority: 'first' }
449
+ const config = await resolveAsync({
450
+ resolvers: [
451
+ [dotenv(), { DATABASE_URL: postgres() }],
452
+ [awsSecrets(), { DATABASE_URL: postgres() }]
453
+ ],
454
+ options: { priority: 'first' }
469
455
  // dotenv wins
470
- );
456
+ });
471
457
 
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' }
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' }
477
465
  // dotenv wins
478
- );
466
+ });
479
467
  ```
480
468
 
481
469
  This is useful for development workflows where local overrides should take precedence over cloud secrets.
@@ -489,13 +477,18 @@ The library includes two automatic performance optimizations:
489
477
  When using `priority: 'first'`, resolvers are called sequentially, but execution **stops early** once all required environment variables are satisfied:
490
478
 
491
479
  ```ts
492
- const config = await resolve.async(
493
- [dotenv(), { DATABASE_URL: 'postgres', API_KEY: 'string', PORT: 3000 }],
494
- [awsSecrets(), { DATABASE_URL: 'postgres', API_KEY: 'string', PORT: 3000 }],
495
- [gcpSecrets(), { DATABASE_URL: 'postgres', API_KEY: 'string', PORT: 3000 }],
496
- { priority: 'first' }
497
- );
498
- // If dotenv() provides all required keys, awsSecrets() and gcpSecrets() are never called!
480
+ import { resolveAsync } from 'node-env-resolver';
481
+ import { dotenv, postgres, string } from 'node-env-resolver/resolvers';
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!
499
492
  ```
500
493
 
501
494
  This is particularly valuable in development where:
@@ -514,12 +507,17 @@ This is particularly valuable in development where:
514
507
  When using `priority: 'last'` (the default), all resolvers are called **in parallel** for maximum performance:
515
508
 
516
509
  ```ts
517
- const config = await resolve.async(
518
- [awsSecrets(), { DATABASE_URL: 'postgres' }], // 100ms
519
- [awsParameterStore(), { API_KEY: 'string' }], // 100ms
520
- [gcpSecrets(), { JWT_SECRET: 'string' }] // 100ms
510
+ import { resolveAsync } from 'node-env-resolver';
511
+ import { postgres, string } from 'node-env-resolver/resolvers';
512
+ import { awsSecrets, awsSsm } from 'node-env-resolver-aws';
513
+
514
+ const config = await resolveAsync({
515
+ resolvers: [
516
+ [awsSecrets(), { DATABASE_URL: postgres() }], // 100ms
517
+ [awsSsm(), { API_KEY: string() }], // 100ms
518
+ ]
521
519
  // Default: priority: 'last'
522
- );
520
+ });
523
521
  // Total time: ~100ms (parallel) instead of ~300ms (sequential)
524
522
  ```
525
523
 
@@ -536,11 +534,21 @@ Since `priority: 'last'` means "last write wins", the order doesn't matter for c
536
534
  Like Zod's `safeParse()`, use `safeResolve()` to get a result object instead of throwing:
537
535
 
538
536
  ```ts
539
- import { safeResolve } from 'node-env-resolver';
537
+ import { safeResolve, safeResolveAsync } from 'node-env-resolver';
538
+ import { number, postgres } from 'node-env-resolver/resolvers';
540
539
 
540
+ // Synchronous safe resolve
541
541
  const result = safeResolve({
542
- PORT: 'number',
543
- DATABASE_URL: 'postgres'
542
+ PORT: number(),
543
+ DATABASE_URL: postgres()
544
+ });
545
+
546
+ // Asynchronous safe resolve
547
+ const result = await safeResolveAsync({
548
+ resolvers: [
549
+ [processEnv(), { PORT: number() }],
550
+ [awsSecrets(), { DATABASE_URL: postgres() }]
551
+ ]
544
552
  });
545
553
 
546
554
  if (result.success) {
@@ -556,7 +564,7 @@ if (result.success) {
556
564
  All functions have safe variants:
557
565
 
558
566
  - `resolve()` → `safeResolve()` (both synchronous)
559
- - `resolve.async()` → `safeResolve.async()` (both async)
567
+ - `resolveAsync()` → `safeResolveAsync()` (both async)
560
568
 
561
569
  ## Synchronous resolution
562
570
 
@@ -564,26 +572,32 @@ All functions have safe variants:
564
572
 
565
573
  ```ts
566
574
  import { resolve } from 'node-env-resolver';
575
+ import { string, url, postgres } from 'node-env-resolver/resolvers';
567
576
 
568
577
  // Synchronous - no await needed, works with ALL types
569
578
  const config = resolve({
570
579
  PORT: 3000,
571
580
  NODE_ENV: ['development', 'production'] as const,
572
- API_KEY: 'string',
573
- API_URL: 'url', // Advanced types work synchronously!
574
- DATABASE_URL: 'postgres',
581
+ API_KEY: string(),
582
+ API_URL: url(), // Advanced types work synchronously!
583
+ DATABASE_URL: postgres(),
575
584
  DEBUG: false
576
585
  });
577
586
  ```
578
587
 
579
- `resolve.async()` is **async** when using custom resolvers:
588
+ `resolveAsync()` is **async** when using custom resolvers:
580
589
 
581
590
  ```ts
591
+ import { resolveAsync, processEnv } from 'node-env-resolver';
592
+ import { dotenv, postgres, url } from 'node-env-resolver/resolvers';
593
+
582
594
  // Async - await required when using custom resolvers
583
- const config = await resolve.async(
584
- [processEnv(), { PORT: 3000 }],
585
- [dotenv(), { DATABASE_URL: 'postgres', API_URL: 'url' }]
586
- );
595
+ const config = await resolveAsync({
596
+ resolvers: [
597
+ [processEnv(), { PORT: 3000 }],
598
+ [dotenv(), { DATABASE_URL: postgres(), API_URL: url() }]
599
+ ]
600
+ });
587
601
  ```
588
602
 
589
603
  ## Advanced features
@@ -593,23 +607,26 @@ const config = await resolve.async(
593
607
  Parse command-line arguments as environment variables - perfect for CLI tools:
594
608
 
595
609
  ```ts
596
- import { resolve } from 'node-env-resolver';
610
+ import { resolveAsync, processEnv } from 'node-env-resolver';
611
+ import { postgres } from 'node-env-resolver/resolvers';
597
612
  import { cliArgs } from 'node-env-resolver/cli';
598
613
 
599
614
  // $ node app.js --port 8080 --database-url postgres://localhost --verbose
600
615
 
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
- );
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
+ });
613
630
 
614
631
  // config.PORT === 8080 (from CLI)
615
632
  // config.DATABASE_URL === 'postgres://localhost' (from CLI)
@@ -795,7 +812,8 @@ if (result.success) {
795
812
  Create resolvers to load configuration from any source:
796
813
 
797
814
  ```ts
798
- import { resolve, type Resolver } from 'node-env-resolver';
815
+ import { resolveAsync, processEnv, type Resolver } from 'node-env-resolver';
816
+ import { string } from 'node-env-resolver/resolvers';
799
817
 
800
818
  const databaseResolver: Resolver = {
801
819
  name: 'database',
@@ -805,10 +823,12 @@ const databaseResolver: Resolver = {
805
823
  }
806
824
  };
807
825
 
808
- const config = await resolve.async(
809
- [processEnv(), { PORT: 3000 }],
810
- [databaseResolver, { API_KEY: 'string' }]
811
- );
826
+ const config = await resolveAsync({
827
+ resolvers: [
828
+ [processEnv(), { PORT: 3000 }],
829
+ [databaseResolver, { API_KEY: string() }]
830
+ ]
831
+ });
812
832
  ```
813
833
 
814
834
  ### AWS Secrets Manager
@@ -823,26 +843,30 @@ Single source:
823
843
 
824
844
  ```ts
825
845
  import { resolveSecrets } from 'node-env-resolver-aws';
846
+ import { postgres, string } from 'node-env-resolver/resolvers';
826
847
 
827
848
  const config = await resolveSecrets({
828
849
  secretId: 'myapp/secrets'
829
850
  }, {
830
- DATABASE_URL: 'postgres',
831
- API_KEY: 'string'
851
+ DATABASE_URL: postgres(),
852
+ API_KEY: string()
832
853
  });
833
854
  ```
834
855
 
835
856
  Multiple sources:
836
857
 
837
858
  ```ts
838
- import { resolve } from 'node-env-resolver';
859
+ import { resolveAsync, processEnv } from 'node-env-resolver';
860
+ import { postgres, string } from 'node-env-resolver/resolvers';
839
861
  import { awsSecrets, awsSsm } from 'node-env-resolver-aws';
840
862
 
841
- const config = await resolve.async(
842
- [processEnv(), { PORT: 3000 }],
843
- [awsSecrets({ secretId: 'app/secrets' }), { DATABASE_URL: 'postgres' }],
844
- [awsSsm({ path: '/app/config' }), { API_KEY: 'string' }]
845
- );
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
+ });
846
870
  ```
847
871
 
848
872
  See the [AWS package documentation](../node-env-resolver-aws/README.md) for details on credentials, caching, and best practices.
@@ -852,20 +876,23 @@ See the [AWS package documentation](../node-env-resolver-aws/README.md) for deta
852
876
  Cache expensive operations like AWS API calls:
853
877
 
854
878
  ```ts
855
- import { resolve } from 'node-env-resolver';
879
+ import { resolveAsync } from 'node-env-resolver';
856
880
  import { cached, TTL } from 'node-env-resolver/utils';
881
+ import { postgres, string } from 'node-env-resolver/resolvers';
857
882
  import { awsSecrets } from 'node-env-resolver-aws';
858
883
 
859
884
  export const getConfig = async () => {
860
- return await resolve.async(
861
- [cached(
862
- awsSecrets({ secretId: 'app/secrets' }),
863
- { ttl: TTL.minutes5 }
864
- ), {
865
- DATABASE_URL: 'postgres',
866
- API_KEY: 'string',
867
- }]
868
- );
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
+ });
869
896
  };
870
897
 
871
898
  // Call getConfig() in your handlers
@@ -882,6 +909,25 @@ Performance:
882
909
 
883
910
  **Important:** Call `resolve()` every time. The `cached()` wrapper handles the caching.
884
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
+
885
931
  ## API Reference
886
932
 
887
933
  ### Function variants
@@ -890,34 +936,48 @@ Performance:
890
936
  |----------|-----------|----------|
891
937
  | `resolve()` | Sync, throws on error | Most applications (reading from process.env) |
892
938
  | `safeResolve()` | Sync, returns result object | Graceful error handling |
893
- | `resolve.async()` | Async, throws on error | Multiple sources (dotenv, AWS, etc.) |
894
- | `safeResolve.async()` | Async, returns result object | Multiple sources with error handling |
939
+ | `resolveAsync()` | Async, throws on error | Multiple sources (dotenv, AWS, etc.) |
940
+ | `safeResolveAsync()` | Async, returns result object | Multiple sources with error handling |
941
+
942
+ ### Resolver API Reference
895
943
 
896
- ### Shorthand syntax
944
+ Import resolver functions from `'node-env-resolver/resolvers'`:
945
+
946
+ ```ts
947
+ import {
948
+ string, number, boolean, url, email, port, json,
949
+ postgres, mysql, mongodb, redis,
950
+ http, https, date, timestamp, duration, file
951
+ } from 'node-env-resolver/resolvers';
952
+ ```
897
953
 
898
954
  | Syntax | Type | Description |
899
955
  |--------|------|-------------|
900
- | `'string'` | `string` | Required string (empty strings rejected by default) |
901
- | `'string?'` | `string \| undefined` | Optional string |
902
- | `'number'` | `number` | Required number (coerced from string) |
903
- | `'number?'` | `number \| undefined` | Optional number |
904
- | `'boolean'` | `boolean` | Required boolean (coerced from string) |
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 |
909
- | `'url'` | `string` | Validated URL (returns string) |
910
- | `'email'` | `string` | Validated email address |
911
- | `'port'` | `number` | Validated port number (1-65535) |
912
- | `'json'` | `unknown` | Parsed JSON (returns object/array) |
913
- | `'postgres'` | `string` | Validated PostgreSQL URL |
914
- | `'date'` | `string` | Validated ISO 8601 date |
915
- | `'timestamp'` | `number` | Validated Unix timestamp |
916
- | `'duration'` | `number` | Time duration (`5s`, `2m`, `1h` → milliseconds) |
917
- | `'file'` | `string` | Read content from file path |
918
- | `3000` | `number` | Number with default |
919
- | `false` | `boolean` | Boolean with default |
920
- | `['a', 'b']` | `'a' \| 'b'` | Enum (requires `as const`) |
956
+ | `string()` | `string` | Required string (empty strings rejected by default) |
957
+ | `string({optional: true})` | `string \| undefined` | Optional string |
958
+ | `number()` | `number` | Required number (coerced from string) |
959
+ | `number({optional: true})` | `number \| undefined` | Optional number |
960
+ | `boolean()` | `boolean` | Required boolean (coerced from string) |
961
+ | `boolean({optional: true})` | `boolean \| undefined` | Optional boolean |
962
+ | `url()` | `string` | Validated URL (returns string) |
963
+ | `url({optional: true})` | `string \| undefined` | Optional URL |
964
+ | `email()` | `string` | Validated email address |
965
+ | `port()` | `number` | Validated port number (1-65535) |
966
+ | `json()` | `unknown` | Parsed JSON (returns object/array) |
967
+ | `postgres()` or `postgresql()` | `string` | Validated PostgreSQL URL |
968
+ | `mysql()` | `string` | Validated MySQL connection string |
969
+ | `mongodb()` | `string` | Validated MongoDB connection string |
970
+ | `redis()` | `string` | Validated Redis connection string |
971
+ | `http()` | `string` | HTTP or HTTPS URL |
972
+ | `https()` | `string` | HTTPS-only URL |
973
+ | `date()` | `string` | Validated ISO 8601 date |
974
+ | `timestamp()` | `number` | Validated Unix timestamp |
975
+ | `duration()` | `number` | Time duration (`5s`, `2m`, `1h` → milliseconds) |
976
+ | `file()` | `string` | Read content from file path |
977
+ | `3000` | `number` | Number with default value |
978
+ | `false` | `boolean` | Boolean with default value |
979
+ | `'defaultValue'` | `string` | String with default value |
980
+ | `['a', 'b'] as const` | `'a' \| 'b'` | Enum (requires `as const`) |
921
981
 
922
982
  ## Configuration Options
923
983
 
@@ -932,21 +992,23 @@ const config = resolve(schema, {
932
992
  enableAudit: true
933
993
  });
934
994
 
935
- // Multiple sources - options as last parameter to resolve.async()
936
- const config = await resolve.async(
937
- [resolver1, schema],
938
- [resolver2, schema],
939
- {
995
+ // Multiple sources - options in config object
996
+ const config = await resolveAsync({
997
+ resolvers: [
998
+ [resolver1, schema],
999
+ [resolver2, schema]
1000
+ ],
1001
+ options: {
940
1002
  interpolate: false,
941
1003
  strict: true,
942
1004
  policies: {...},
943
1005
  enableAudit: true,
944
1006
  priority: 'last'
945
1007
  }
946
- );
1008
+ });
947
1009
  ```
948
1010
 
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.
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.
950
1012
 
951
1013
  ### `interpolate`
952
1014
 
@@ -956,16 +1018,19 @@ const config = await resolve.async(
956
1018
 
957
1019
  **Why:** Keeps configuration DRY and maintainable.
958
1020
 
959
- **Default:** `true` in `resolve.async()`, `false` in `resolve()`
1021
+ **Default:** `true` in `resolveAsync()`, `false` in `resolve()`
960
1022
 
961
1023
  ```ts
1024
+ import { resolve } from 'node-env-resolver';
1025
+ import { url } from 'node-env-resolver/resolvers';
1026
+
962
1027
  // With interpolation
963
1028
  process.env.BASE_URL = 'https://api.example.com';
964
1029
  process.env.API_ENDPOINT = '${BASE_URL}/v1';
965
1030
 
966
1031
  const config = resolve({
967
- BASE_URL: 'url',
968
- API_ENDPOINT: 'url'
1032
+ BASE_URL: url(),
1033
+ API_ENDPOINT: url()
969
1034
  }, {
970
1035
  interpolate: true
971
1036
  });
@@ -998,18 +1063,22 @@ const flakyResolver = {
998
1063
  };
999
1064
 
1000
1065
  // ❌ Throws immediately
1001
- await resolve.async(
1002
- [flakyResolver, schema],
1003
- [processEnv(), schema],
1004
- { strict: true } // default
1005
- );
1066
+ await resolveAsync({
1067
+ resolvers: [
1068
+ [flakyResolver, schema],
1069
+ [processEnv(), schema]
1070
+ ],
1071
+ options: { strict: true } // default
1072
+ });
1006
1073
 
1007
1074
  // ✅ Continues with processEnv()
1008
- await resolve.async(
1009
- [flakyResolver, schema],
1010
- [processEnv(), schema],
1011
- { strict: false } // graceful degradation
1012
- );
1075
+ await resolveAsync({
1076
+ resolvers: [
1077
+ [flakyResolver, schema],
1078
+ [processEnv(), schema]
1079
+ ],
1080
+ options: { strict: false } // graceful degradation
1081
+ });
1013
1082
  ```
1014
1083
 
1015
1084
  **Note:** In sync mode with `strict: true`, resolvers without `loadSync()` method will throw errors.
@@ -1028,19 +1097,27 @@ await resolve.async(
1028
1097
  **Default:** `'last'`
1029
1098
 
1030
1099
  ```ts
1100
+ import { resolveAsync, processEnv } from 'node-env-resolver';
1101
+ import { dotenv, postgres } from 'node-env-resolver/resolvers';
1102
+ import { awsSecrets } from 'node-env-resolver-aws';
1103
+
1031
1104
  // Production: AWS secrets override process.env
1032
- await resolve.async(
1033
- [processEnv(), { DATABASE_URL: 'postgres' }],
1034
- [awsSecrets(), { DATABASE_URL: 'postgres' }]
1105
+ await resolveAsync({
1106
+ resolvers: [
1107
+ [processEnv(), { DATABASE_URL: postgres() }],
1108
+ [awsSecrets(), { DATABASE_URL: postgres() }]
1109
+ ]
1035
1110
  // priority: 'last' (default) - AWS wins
1036
- );
1111
+ });
1037
1112
 
1038
1113
  // Development: Local .env overrides cloud
1039
- await resolve.async(
1040
- [dotenv(), { DATABASE_URL: 'postgres' }],
1041
- [awsSecrets(), { DATABASE_URL: 'postgres' }],
1042
- { priority: 'first' } // dotenv wins
1043
- );
1114
+ await resolveAsync({
1115
+ resolvers: [
1116
+ [dotenv(), { DATABASE_URL: postgres() }],
1117
+ [awsSecrets(), { DATABASE_URL: postgres() }]
1118
+ ],
1119
+ options: { priority: 'first' } // dotenv wins
1120
+ });
1044
1121
  ```
1045
1122
 
1046
1123
  **See:** "Controlling merge behaviour" section above for details.
@@ -1056,10 +1133,12 @@ await resolve.async(
1056
1133
  **Default:** `undefined` (no policies enforced)
1057
1134
 
1058
1135
  ```ts
1059
- await resolve.async(
1060
- [processEnv(), schema],
1061
- [awsSecrets(), schema],
1062
- {
1136
+ await resolveAsync({
1137
+ resolvers: [
1138
+ [processEnv(), schema],
1139
+ [awsSecrets(), schema]
1140
+ ],
1141
+ options: {
1063
1142
  policies: {
1064
1143
  // Block .env in production (default behaviour)
1065
1144
  allowDotenvInProduction: false,
@@ -1071,7 +1150,7 @@ await resolve.async(
1071
1150
  }
1072
1151
  }
1073
1152
  }
1074
- );
1153
+ });
1075
1154
  ```
1076
1155
 
1077
1156
  **See:** "Security Policies" section below for complete documentation.
@@ -1091,11 +1170,13 @@ await resolve.async(
1091
1170
  **Default:** `false` (disabled in development), automatically `true` in production (`NODE_ENV === 'production'`)
1092
1171
 
1093
1172
  ```ts
1094
- await resolve.async(
1095
- [processEnv(), schema],
1096
- [awsSecrets(), schema],
1097
- { enableAudit: true } // Explicitly enable in development
1098
- );
1173
+ await resolveAsync({
1174
+ resolvers: [
1175
+ [processEnv(), schema],
1176
+ [awsSecrets(), schema]
1177
+ ],
1178
+ options: { enableAudit: true } // Explicitly enable in development
1179
+ });
1099
1180
 
1100
1181
  const logs = getAuditLog();
1101
1182
  // [
@@ -1131,11 +1212,12 @@ interface PolicyOptions {
1131
1212
  ```ts
1132
1213
  import express from 'express';
1133
1214
  import { resolve } from 'node-env-resolver';
1215
+ import { postgres, string } from 'node-env-resolver/resolvers';
1134
1216
 
1135
1217
  const config = resolve({
1136
1218
  PORT: 3000,
1137
- DATABASE_URL: 'postgres',
1138
- SESSION_SECRET: 'string'
1219
+ DATABASE_URL: postgres(),
1220
+ SESSION_SECRET: string()
1139
1221
  });
1140
1222
 
1141
1223
  const app = express();
@@ -1147,14 +1229,15 @@ app.listen(config.PORT);
1147
1229
  ```ts
1148
1230
  // env.mjs
1149
1231
  import { resolve } from 'node-env-resolver/nextjs';
1232
+ import { postgres, string, url } from 'node-env-resolver/resolvers';
1150
1233
 
1151
1234
  export const env = resolve({
1152
1235
  server: {
1153
- DATABASE_URL: 'postgres',
1154
- API_SECRET: 'string',
1236
+ DATABASE_URL: postgres(),
1237
+ API_SECRET: string(),
1155
1238
  },
1156
1239
  client: {
1157
- NEXT_PUBLIC_APP_URL: 'url',
1240
+ NEXT_PUBLIC_APP_URL: url(),
1158
1241
  }
1159
1242
  });
1160
1243
  ```
@@ -1162,19 +1245,22 @@ export const env = resolve({
1162
1245
  ### AWS Lambda
1163
1246
 
1164
1247
  ```ts
1165
- import { resolve } from 'node-env-resolver';
1248
+ import { resolveAsync } from 'node-env-resolver';
1166
1249
  import { cached, TTL } from 'node-env-resolver/utils';
1250
+ import { postgres } from 'node-env-resolver/resolvers';
1167
1251
  import { awsSecrets } from 'node-env-resolver-aws';
1168
1252
 
1169
1253
  const getConfig = async () => {
1170
- return await resolve.async(
1171
- [cached(
1172
- awsSecrets({ secretId: 'lambda/config' }),
1173
- { ttl: TTL.minutes5 }
1174
- ), {
1175
- DATABASE_URL: 'postgres',
1176
- }]
1177
- );
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
+ });
1178
1264
  };
1179
1265
 
1180
1266
  export const handler = async (event) => {
@@ -1194,45 +1280,60 @@ By default, `.env` files are **completely blocked in production** for security.
1194
1280
  **Default behavior (secure):**
1195
1281
 
1196
1282
  ```ts
1283
+ import { resolveAsync } from 'node-env-resolver';
1284
+ import { dotenv, postgres } from 'node-env-resolver/resolvers';
1285
+
1197
1286
  // In production (NODE_ENV=production)
1198
- const config = await resolve.async(
1199
- [dotenv(), {
1200
- DATABASE_URL: 'postgres',
1201
- }]
1202
- );
1287
+ const config = await resolveAsync({
1288
+ resolvers: [
1289
+ [dotenv(), {
1290
+ DATABASE_URL: postgres(),
1291
+ }]
1292
+ ]
1293
+ });
1203
1294
  // ❌ Throws: "DATABASE_URL cannot be sourced from .env files in production"
1204
1295
  ```
1205
1296
 
1206
1297
  **Allow all .env variables (NOT recommended):**
1207
1298
 
1208
1299
  ```ts
1209
- const config = await resolve.async(
1210
- [dotenv(), {
1211
- DATABASE_URL: 'postgres',
1212
- }],
1213
- {
1300
+ import { resolveAsync } from 'node-env-resolver';
1301
+ import { dotenv, postgres } from 'node-env-resolver/resolvers';
1302
+
1303
+ const config = await resolveAsync({
1304
+ resolvers: [
1305
+ [dotenv(), {
1306
+ DATABASE_URL: postgres(),
1307
+ }]
1308
+ ],
1309
+ options: {
1214
1310
  policies: {
1215
1311
  allowDotenvInProduction: true // Allow all (risky!)
1216
1312
  }
1217
1313
  }
1218
- );
1314
+ });
1219
1315
  ```
1220
1316
 
1221
1317
  **Allow specific variables only (recommended if needed):**
1222
1318
 
1223
1319
  ```ts
1224
- const config = await resolve.async(
1225
- [dotenv(), {
1226
- PORT: 3000,
1227
- DATABASE_URL: 'postgres',
1228
- }],
1229
- {
1320
+ import { resolveAsync } from 'node-env-resolver';
1321
+ import { dotenv, postgres } from 'node-env-resolver/resolvers';
1322
+
1323
+ const config = await resolveAsync({
1324
+ resolvers: [
1325
+ [dotenv(), {
1326
+ PORT: 3000,
1327
+ DATABASE_URL: postgres(),
1328
+ }]
1329
+ ],
1330
+ options: {
1230
1331
  policies: {
1231
1332
  allowDotenvInProduction: ['PORT'] // Only PORT allowed from .env
1232
1333
  // DATABASE_URL must come from process.env or cloud resolvers
1233
1334
  }
1234
1335
  }
1235
- );
1336
+ });
1236
1337
  ```
1237
1338
 
1238
1339
  ### Policy: `enforceAllowedSources`
@@ -1240,18 +1341,21 @@ const config = await resolve.async(
1240
1341
  Restrict sensitive variables to specific resolvers (e.g., force secrets to come from AWS):
1241
1342
 
1242
1343
  ```ts
1243
- import { resolve, processEnv } from 'node-env-resolver';
1344
+ import { resolveAsync, processEnv } from 'node-env-resolver';
1345
+ import { string } from 'node-env-resolver/resolvers';
1244
1346
  import { awsSecrets } from 'node-env-resolver-aws';
1245
1347
 
1246
- const config = await resolve.async(
1247
- [processEnv(), {
1248
- PORT: 3000,
1249
- }],
1250
- [awsSecrets({ secretId: 'prod/secrets' }), {
1251
- DATABASE_PASSWORD: 'string',
1252
- API_KEY: 'string',
1253
- }],
1254
- {
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: {
1255
1359
  policies: {
1256
1360
  enforceAllowedSources: {
1257
1361
  DATABASE_PASSWORD: ['aws-secrets'], // Must come from AWS
@@ -1260,31 +1364,33 @@ const config = await resolve.async(
1260
1364
  }
1261
1365
  }
1262
1366
  }
1263
- );
1367
+ });
1264
1368
  ```
1265
1369
 
1266
1370
  **Use case:** Ensure production secrets only come from AWS Secrets Manager, never from `.env` or `process.env`:
1267
1371
 
1268
1372
  ```ts
1269
- import { resolve, processEnv } from 'node-env-resolver';
1270
- import { cached, TTL } from 'node-env-resolver/utils';
1373
+ import { resolveAsync, processEnv } from 'node-env-resolver';
1374
+ import { string } from 'node-env-resolver/resolvers';
1271
1375
  import { awsSecrets } from 'node-env-resolver-aws';
1272
1376
 
1273
- const config = await resolve.async(
1274
- [processEnv(), {}],
1275
- [cached(awsSecrets({ secretId: 'prod/db' }), { ttl: TTL.minutes5 }), {
1276
- DATABASE_PASSWORD: 'string',
1277
- STRIPE_SECRET: 'string',
1278
- }],
1279
- {
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: {
1280
1386
  policies: {
1281
1387
  enforceAllowedSources: {
1282
- DATABASE_PASSWORD: ['cached(aws-secrets)'],
1283
- STRIPE_SECRET: ['cached(aws-secrets)']
1388
+ DATABASE_PASSWORD: ['aws-secrets'],
1389
+ STRIPE_SECRET: ['aws-secrets']
1284
1390
  }
1285
1391
  }
1286
1392
  }
1287
- );
1393
+ });
1288
1394
 
1289
1395
  // ✅ If secrets come from AWS → Success
1290
1396
  // ❌ If secrets come from process.env → Throws policy violation error
@@ -1324,11 +1430,12 @@ Track environment variable resolution for security and compliance monitoring.
1324
1430
 
1325
1431
  ```ts
1326
1432
  import { resolve, getAuditLog, clearAuditLog } from 'node-env-resolver';
1433
+ import { postgres, string } from 'node-env-resolver/resolvers';
1327
1434
 
1328
1435
  // Option 1: Explicitly enable (works in any environment)
1329
1436
  const config = resolve({
1330
- DATABASE_URL: 'postgres',
1331
- API_KEY: 'string',
1437
+ DATABASE_URL: postgres(),
1438
+ API_KEY: string(),
1332
1439
  }, {
1333
1440
  enableAudit: true // ← Enable audit logging
1334
1441
  });
@@ -1345,11 +1452,14 @@ clearAuditLog();
1345
1452
  ```
1346
1453
 
1347
1454
  ```ts
1455
+ import { resolve, getAuditLog } from 'node-env-resolver';
1456
+ import { postgres } from 'node-env-resolver/resolvers';
1457
+
1348
1458
  // Option 2: Automatic in production
1349
1459
  process.env.NODE_ENV = 'production';
1350
1460
 
1351
1461
  const config = resolve({
1352
- DATABASE_URL: 'postgres',
1462
+ DATABASE_URL: postgres(),
1353
1463
  });
1354
1464
 
1355
1465
  // Audit automatically enabled in production
@@ -1362,14 +1472,15 @@ When you have multiple `resolve()` calls, you can now get audit logs **specific
1362
1472
 
1363
1473
  ```ts
1364
1474
  import { resolve, getAuditLog } from 'node-env-resolver';
1475
+ import { string, postgres } from 'node-env-resolver/resolvers';
1365
1476
 
1366
1477
  const appConfig = resolve({
1367
1478
  PORT: 3000,
1368
- API_KEY: 'string'
1479
+ API_KEY: string()
1369
1480
  }, { enableAudit: true });
1370
1481
 
1371
1482
  const dbConfig = resolve({
1372
- DATABASE_URL: 'postgres',
1483
+ DATABASE_URL: postgres(),
1373
1484
  DB_POOL_SIZE: 10
1374
1485
  }, { enableAudit: true });
1375
1486
 
@@ -1416,16 +1527,19 @@ console.log('DB config audit:', dbAudit);
1416
1527
  Audit logs include cache metadata to help monitor performance:
1417
1528
 
1418
1529
  ```ts
1419
- import { resolve, getAuditLog } from 'node-env-resolver';
1420
- import { cached } from 'node-env-resolver/utils';
1530
+ import { resolveAsync, getAuditLog } from 'node-env-resolver';
1531
+ import { cached, TTL } from 'node-env-resolver/utils';
1532
+ import { postgres } from 'node-env-resolver/resolvers';
1421
1533
  import { awsSecrets } from 'node-env-resolver-aws';
1422
1534
 
1423
- const config = await resolve.async(
1424
- [cached(awsSecrets({ secretId: 'prod/db' }), { ttl: 300000 }), {
1425
- DATABASE_URL: 'postgres',
1426
- }],
1427
- { enableAudit: true }
1428
- );
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
+ });
1429
1543
 
1430
1544
  const logs = getAuditLog();
1431
1545
  logs.forEach(log => {
@@ -1444,17 +1558,20 @@ logs.forEach(log => {
1444
1558
  Use audit logs for compliance and security monitoring:
1445
1559
 
1446
1560
  ```ts
1447
- import { resolve, processEnv, getAuditLog } from 'node-env-resolver';
1561
+ import { resolveAsync, processEnv, getAuditLog } from 'node-env-resolver';
1562
+ import { string } from 'node-env-resolver/resolvers';
1448
1563
  import { awsSecrets } from 'node-env-resolver-aws';
1449
1564
 
1450
1565
  // In production
1451
- const config = await resolve.async(
1452
- [processEnv(), {}],
1453
- [awsSecrets(), {
1454
- DATABASE_PASSWORD: 'string',
1455
- API_KEY: 'string',
1456
- }],
1457
- {
1566
+ const config = await resolveAsync({
1567
+ resolvers: [
1568
+ [processEnv(), {}],
1569
+ [awsSecrets(), {
1570
+ DATABASE_PASSWORD: string(),
1571
+ API_KEY: string(),
1572
+ }]
1573
+ ],
1574
+ options: {
1458
1575
  policies: {
1459
1576
  enforceAllowedSources: {
1460
1577
  DATABASE_PASSWORD: ['aws-secrets'],
@@ -1463,7 +1580,7 @@ const config = await resolve.async(
1463
1580
  }
1464
1581
  // enableAudit: true automatically in production
1465
1582
  }
1466
- );
1583
+ });
1467
1584
 
1468
1585
  // Send audit logs to your monitoring system
1469
1586
  const logs = getAuditLog();