node-env-resolver 4.0.0 → 5.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 +289 -251
- package/dist/audit.d.cts +29 -0
- package/dist/audit.d.ts +8 -7
- package/dist/builder.cjs +1 -1
- package/dist/builder.d.cts +149 -0
- package/dist/builder.d.ts +17 -14
- package/dist/builder.js +1 -1
- package/dist/chunk-4YVXFL3I.cjs +9 -0
- package/dist/chunk-6FXCXA4R.cjs +11 -0
- package/dist/chunk-BFLXBLZ3.cjs +1 -0
- package/dist/chunk-BJ2QNYTS.js +11 -0
- package/dist/chunk-DIXI6MUB.js +9 -0
- package/dist/chunk-P3TTYVEW.js +16 -0
- package/dist/chunk-UCLEPPLS.cjs +16 -0
- package/dist/chunk-ZMRT37K3.js +1 -0
- package/dist/cli.d.cts +52 -0
- package/dist/cli.d.ts +9 -6
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +150 -0
- package/dist/index.d.ts +86 -77
- package/dist/index.js +1 -1
- package/dist/resolver.cjs +1 -1
- package/dist/resolver.d.cts +20 -0
- package/dist/resolver.d.ts +8 -5
- package/dist/resolver.js +1 -1
- package/dist/resolvers.cjs +1 -1
- package/dist/resolvers.d.cts +407 -0
- package/dist/resolvers.d.ts +289 -22
- package/dist/resolvers.js +1 -1
- package/dist/types.cjs +1 -1
- package/dist/types.d.cts +74 -0
- package/dist/types.d.ts +58 -172
- package/dist/types.js +1 -1
- package/dist/utils.cjs +1 -1
- package/dist/utils.d.cts +213 -0
- package/dist/utils.d.ts +28 -26
- package/dist/utils.js +1 -1
- package/dist/valibot.cjs +1 -1
- package/dist/valibot.d.cts +156 -0
- package/dist/valibot.d.ts +11 -8
- package/dist/valibot.js +1 -1
- package/dist/validation-types.d.cts +29 -0
- package/dist/validation-types.d.ts +4 -3
- package/dist/validators.cjs +2 -1
- package/dist/validators.d.cts +116 -0
- package/dist/validators.d.ts +46 -18
- package/dist/validators.js +2 -1
- package/dist/web.cjs +4 -4
- package/dist/web.d.cts +69 -0
- package/dist/web.d.ts +28 -10
- package/dist/web.js +4 -4
- package/dist/zod.cjs +1 -1
- package/dist/zod.d.cts +135 -0
- package/dist/zod.d.ts +11 -8
- package/dist/zod.js +1 -1
- package/package.json +7 -7
- package/dist/advanced-features.test.d.ts +0 -2
- package/dist/advanced-features.test.d.ts.map +0 -1
- package/dist/audit.d.ts.map +0 -1
- package/dist/audit.test.d.ts +0 -2
- package/dist/audit.test.d.ts.map +0 -1
- package/dist/builder.d.ts.map +0 -1
- package/dist/chunk-54OHKWZJ.js +0 -1
- package/dist/chunk-FCFDGYTF.cjs +0 -15
- package/dist/chunk-HCVTYYLE.js +0 -9
- package/dist/chunk-IU5TG3HL.js +0 -15
- package/dist/chunk-J7ZHEMRP.js +0 -9
- package/dist/chunk-NKPXKLF7.js +0 -1
- package/dist/chunk-P3ZWKKKW.cjs +0 -1
- package/dist/chunk-THFGEKYL.cjs +0 -9
- package/dist/chunk-ULL4WKTV.cjs +0 -9
- package/dist/chunk-XPGSWVRT.cjs +0 -1
- package/dist/cli.d.ts.map +0 -1
- package/dist/cli.test.d.ts +0 -2
- package/dist/cli.test.d.ts.map +0 -1
- package/dist/composition.test.d.ts +0 -7
- package/dist/composition.test.d.ts.map +0 -1
- package/dist/computed.test.d.ts +0 -2
- package/dist/computed.test.d.ts.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.test.d.ts +0 -2
- package/dist/index.test.d.ts.map +0 -1
- package/dist/nested.test.d.ts +0 -2
- package/dist/nested.test.d.ts.map +0 -1
- package/dist/performance.test.d.ts +0 -7
- package/dist/performance.test.d.ts.map +0 -1
- package/dist/priority.test.d.ts +0 -6
- package/dist/priority.test.d.ts.map +0 -1
- package/dist/providers.d.ts +0 -11
- package/dist/providers.d.ts.map +0 -1
- package/dist/resolver.d.ts.map +0 -1
- package/dist/resolvers.d.ts.map +0 -1
- package/dist/resolvers.test.d.ts +0 -2
- package/dist/resolvers.test.d.ts.map +0 -1
- package/dist/sync-resolvers.test.d.ts +0 -2
- package/dist/sync-resolvers.test.d.ts.map +0 -1
- package/dist/type-inference.test.d.ts +0 -2
- package/dist/type-inference.test.d.ts.map +0 -1
- package/dist/type-override.test.d.ts +0 -2
- package/dist/type-override.test.d.ts.map +0 -1
- package/dist/types.d.ts.map +0 -1
- package/dist/utils.d.ts.map +0 -1
- package/dist/utils.test.d.ts +0 -2
- package/dist/utils.test.d.ts.map +0 -1
- package/dist/valibot.d.ts.map +0 -1
- package/dist/validation-types.d.ts.map +0 -1
- package/dist/validators.d.ts.map +0 -1
- package/dist/web.d.ts.map +0 -1
- 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
|
[](https://www.npmjs.com/package/node-env-resolver)
|
|
6
6
|
|
|
7
|
-
**Bundle Size:** ~
|
|
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:
|
|
25
|
+
API_KEY: string({optional:true})
|
|
25
26
|
});
|
|
26
27
|
|
|
27
28
|
// config is fully typed
|
|
@@ -34,8 +35,9 @@ 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
42
|
// Synchronous with custom resolver (array syntax)
|
|
41
43
|
const config = resolve([
|
|
@@ -44,19 +46,29 @@ const config = resolve([
|
|
|
44
46
|
PORT: 3000,
|
|
45
47
|
NODE_ENV: ['development', 'production', 'test'] as const,
|
|
46
48
|
DEBUG: false,
|
|
47
|
-
API_KEY:
|
|
49
|
+
API_KEY: string({optional:true})
|
|
48
50
|
}
|
|
49
51
|
]);
|
|
50
52
|
|
|
51
53
|
// Asynchronous with any resolvers (both sync and async work!)
|
|
52
|
-
const config = await
|
|
53
|
-
dotenv(),
|
|
54
|
+
const config = await resolveAsync([
|
|
55
|
+
dotenv(), {
|
|
54
56
|
PORT: 3000,
|
|
55
57
|
NODE_ENV: ['development', 'production', 'test'] as const,
|
|
56
58
|
DEBUG: false,
|
|
57
|
-
API_KEY:
|
|
59
|
+
API_KEY: string({optional:true})
|
|
58
60
|
}],
|
|
59
|
-
[awsSecrets(), { DATABASE_URL:
|
|
61
|
+
[awsSecrets(), { DATABASE_URL: url() }]);
|
|
62
|
+
|
|
63
|
+
const config = await resolveAsync(
|
|
64
|
+
[processEnv(), { PORT: 3000 }],
|
|
65
|
+
[dotenv(), { DATABASE_URL: postgres() }],
|
|
66
|
+
[awsSecrets(), { API_KEY: string() }],
|
|
67
|
+
[gcpSecrets(), { JWT_SECRET: string() }],
|
|
68
|
+
[vaultSecrets(), { REDIS_URL: redis() }]
|
|
69
|
+
// Options as last parameter
|
|
70
|
+
{ priority: 'last' }
|
|
71
|
+
);
|
|
60
72
|
```
|
|
61
73
|
|
|
62
74
|
## Table of Contents
|
|
@@ -71,7 +83,6 @@ const config = await resolve.async([
|
|
|
71
83
|
- [Enums](#enums)
|
|
72
84
|
- [Variable Naming Conventions](#variable-naming-conventions)
|
|
73
85
|
- [Performance & Bundle Size](#performance--bundle-size)
|
|
74
|
-
- [Built-in validators](#built-in-validators)
|
|
75
86
|
- [Custom validators](#custom-validators)
|
|
76
87
|
- [Multiple sources](#multiple-sources)
|
|
77
88
|
- [Safe error handling](#safe-error-handling)
|
|
@@ -92,9 +103,12 @@ const config = await resolve.async([
|
|
|
92
103
|
If an environment variable is missing and has no default, validation fails:
|
|
93
104
|
|
|
94
105
|
```ts
|
|
106
|
+
import { resolve } from 'node-env-resolver';
|
|
107
|
+
import { postgres, string } from 'node-env-resolver/resolvers';
|
|
108
|
+
|
|
95
109
|
const config = resolve({
|
|
96
|
-
DATABASE_URL:
|
|
97
|
-
API_KEY:
|
|
110
|
+
DATABASE_URL: postgres(), // Required PostgreSQL URL
|
|
111
|
+
API_KEY: string() // Required
|
|
98
112
|
});
|
|
99
113
|
```
|
|
100
114
|
|
|
@@ -103,6 +117,8 @@ const config = resolve({
|
|
|
103
117
|
Provide a default value to use when the environment variable is not set:
|
|
104
118
|
|
|
105
119
|
```ts
|
|
120
|
+
import { resolve } from 'node-env-resolver';
|
|
121
|
+
|
|
106
122
|
const config = resolve({
|
|
107
123
|
PORT: 3000, // Defaults to 3000
|
|
108
124
|
DEBUG: false, // Defaults to false
|
|
@@ -112,13 +128,15 @@ const config = resolve({
|
|
|
112
128
|
|
|
113
129
|
### Optional values
|
|
114
130
|
|
|
115
|
-
|
|
131
|
+
Use the `{optional: true}` option to make a value optional:
|
|
116
132
|
|
|
117
133
|
```ts
|
|
134
|
+
import { string, url, number } from 'node-env-resolver/resolvers';
|
|
135
|
+
|
|
118
136
|
const config = resolve({
|
|
119
|
-
API_KEY:
|
|
120
|
-
REDIS_URL:
|
|
121
|
-
MAX_RETRIES:
|
|
137
|
+
API_KEY: string({optional: true}), // string | undefined
|
|
138
|
+
REDIS_URL: url({optional: true}), // string | undefined
|
|
139
|
+
MAX_RETRIES: number({optional: true}) // number | undefined
|
|
122
140
|
});
|
|
123
141
|
```
|
|
124
142
|
|
|
@@ -127,6 +145,8 @@ const config = resolve({
|
|
|
127
145
|
Use arrays for enum validation:
|
|
128
146
|
|
|
129
147
|
```ts
|
|
148
|
+
import { resolve } from 'node-env-resolver';
|
|
149
|
+
|
|
130
150
|
const config = resolve({
|
|
131
151
|
NODE_ENV: ['development', 'production', 'test'] as const,
|
|
132
152
|
LOG_LEVEL: ['debug', 'info', 'warn', 'error'] as const
|
|
@@ -151,25 +171,31 @@ Variable names can use:
|
|
|
151
171
|
### Valid Examples
|
|
152
172
|
|
|
153
173
|
```ts
|
|
174
|
+
import { resolve } from 'node-env-resolver';
|
|
175
|
+
import { string } from 'node-env-resolver/resolvers';
|
|
176
|
+
|
|
154
177
|
const config = resolve({
|
|
155
178
|
PORT: 3000, // ✅ SCREAMING_SNAKE_CASE (traditional)
|
|
156
179
|
port: 3000, // ✅ lowercase
|
|
157
|
-
myApiKey:
|
|
158
|
-
my_api_key:
|
|
159
|
-
API_KEY_V2:
|
|
160
|
-
_PRIVATE:
|
|
180
|
+
myApiKey: string(), // ✅ camelCase
|
|
181
|
+
my_api_key: string(), // ✅ snake_case
|
|
182
|
+
API_KEY_V2: string(), // ✅ with numbers (not first char)
|
|
183
|
+
_PRIVATE: string() // ✅ starting with underscore
|
|
161
184
|
});
|
|
162
185
|
```
|
|
163
186
|
|
|
164
187
|
### Invalid Examples
|
|
165
188
|
|
|
166
189
|
```ts
|
|
190
|
+
import { resolve } from 'node-env-resolver';
|
|
191
|
+
import { string } from 'node-env-resolver/resolvers';
|
|
192
|
+
|
|
167
193
|
const config = resolve({
|
|
168
194
|
'123PORT': 3000, // ❌ Starts with number
|
|
169
|
-
'API-KEY':
|
|
170
|
-
'API.KEY':
|
|
171
|
-
'API KEY':
|
|
172
|
-
'API@KEY':
|
|
195
|
+
'API-KEY': string(), // ❌ Contains hyphen
|
|
196
|
+
'API.KEY': string(), // ❌ Contains dot
|
|
197
|
+
'API KEY': string(), // ❌ Contains space
|
|
198
|
+
'API@KEY': string() // ❌ Special characters
|
|
173
199
|
});
|
|
174
200
|
```
|
|
175
201
|
|
|
@@ -179,7 +205,32 @@ const config = resolve({
|
|
|
179
205
|
|
|
180
206
|
## Performance & Bundle Size
|
|
181
207
|
|
|
182
|
-
**Lightweight** The core library is **~
|
|
208
|
+
**Lightweight** The core library is **~4.3KB gzipped** with validation and resolver capabilities - **optimized for minimal bundle impact**.
|
|
209
|
+
|
|
210
|
+
```ts
|
|
211
|
+
import { resolveAsync, processEnv } from 'node-env-resolver';
|
|
212
|
+
import { dotenv, json, postgres, string, redis as redisValidator } from 'node-env-resolver/resolvers';
|
|
213
|
+
import { awsSecrets, gcpSecrets, vaultSecrets } from 'node-env-resolver-aws';
|
|
214
|
+
import { consul, etcd, redis } from 'node-env-resolver-integrations';
|
|
215
|
+
|
|
216
|
+
const config = await resolveAsync(
|
|
217
|
+
[processEnv(), { PORT: 3000 }],
|
|
218
|
+
[dotenv(), { DATABASE_URL: postgres() }],
|
|
219
|
+
[awsSecrets(), { API_KEY: string() }],
|
|
220
|
+
[gcpSecrets(), { JWT_SECRET: string() }],
|
|
221
|
+
[vaultSecrets(), { REDIS_URL: redisValidator() }],
|
|
222
|
+
[consul(), { SERVICE_DISCOVERY: json() }],
|
|
223
|
+
[etcd(), { CONFIG_VERSION: string() }],
|
|
224
|
+
[redis(), { CACHE_CONFIG: json() }]
|
|
225
|
+
// Perfect for complex microservice architectures!
|
|
226
|
+
);
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
**Benefits:**
|
|
230
|
+
- **No artificial limits** - use as many resolvers as needed
|
|
231
|
+
- **Better type inference** - proper schema merging with full TypeScript support
|
|
232
|
+
- **Cleaner code** - single implementation instead of multiple overloads
|
|
233
|
+
- **Future-proof** - automatically supports new resolver types
|
|
183
234
|
|
|
184
235
|
### Efficient validation architecture
|
|
185
236
|
|
|
@@ -199,18 +250,22 @@ The library uses a two-tier validation strategy with lazy-loaded validators:
|
|
|
199
250
|
Advanced validators are lazy-loaded only when you use them, keeping the base bundle minimal. All types work both synchronously and asynchronously:
|
|
200
251
|
|
|
201
252
|
```ts
|
|
253
|
+
import { resolve, resolveAsync } from 'node-env-resolver';
|
|
254
|
+
import { postgres, url } from 'node-env-resolver/resolvers';
|
|
255
|
+
import { awsSecrets } from 'node-env-resolver-aws';
|
|
256
|
+
|
|
202
257
|
// Synchronous - works with all types
|
|
203
258
|
const config = resolve({
|
|
204
259
|
PORT: 3000,
|
|
205
|
-
DATABASE_URL:
|
|
206
|
-
API_URL:
|
|
260
|
+
DATABASE_URL: postgres(),
|
|
261
|
+
API_URL: url(),
|
|
207
262
|
NODE_ENV: ['development', 'production'] as const
|
|
208
263
|
});
|
|
209
264
|
|
|
210
265
|
// Also works with async resolvers
|
|
211
|
-
const config = await
|
|
266
|
+
const config = await resolveAsync([
|
|
212
267
|
awsSecrets(),
|
|
213
|
-
{ DATABASE_URL:
|
|
268
|
+
{ DATABASE_URL: postgres(), API_URL: url() }
|
|
214
269
|
]);
|
|
215
270
|
```
|
|
216
271
|
|
|
@@ -235,6 +290,8 @@ Only the code you actually use gets loaded!
|
|
|
235
290
|
Audit logging is lazy-loaded only when enabled, keeping the base bundle minimal:
|
|
236
291
|
|
|
237
292
|
```ts
|
|
293
|
+
import { resolve } from 'node-env-resolver';
|
|
294
|
+
|
|
238
295
|
// Base bundle
|
|
239
296
|
const config = resolve({ PORT: 3000 });
|
|
240
297
|
|
|
@@ -267,109 +324,13 @@ import { awsSecrets } from 'node-env-resolver-aws';
|
|
|
267
324
|
|
|
268
325
|
**Most apps only need the core (~3.6KB)!**
|
|
269
326
|
|
|
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
327
|
## Custom validators
|
|
369
328
|
|
|
370
329
|
Write your own validation functions:
|
|
371
330
|
|
|
372
331
|
```ts
|
|
332
|
+
import { resolve } from 'node-env-resolver';
|
|
333
|
+
|
|
373
334
|
const isValidPort = (value: string): number => {
|
|
374
335
|
const port = parseInt(value, 10);
|
|
375
336
|
if (isNaN(port) || port < 1 || port > 65535) {
|
|
@@ -424,29 +385,42 @@ Available validation functions: `validatePostgres`, `validateMysql`, `validateMo
|
|
|
424
385
|
## Multiple sources
|
|
425
386
|
|
|
426
387
|
Load configuration from multiple sources. By default, later sources override earlier ones:
|
|
427
|
-
|
|
428
388
|
```ts
|
|
429
|
-
import { resolve, processEnv } from 'node-env-resolver';
|
|
430
|
-
import { dotenv, json } from 'node-env-resolver/resolvers';
|
|
389
|
+
import { resolve, resolveAsync, processEnv } from 'node-env-resolver';
|
|
390
|
+
import { dotenv, json, postgres, string, redis } from 'node-env-resolver/resolvers';
|
|
391
|
+
import { awsSecrets, gcpSecrets, vaultSecrets } from 'node-env-resolver-aws';
|
|
392
|
+
import { consul } from 'node-env-resolver-integrations';
|
|
431
393
|
|
|
432
394
|
// Async mode - supports both sync and async resolvers
|
|
433
|
-
const config = await
|
|
395
|
+
const config = await resolveAsync(
|
|
434
396
|
[processEnv(), {
|
|
435
397
|
PORT: 3000,
|
|
436
398
|
NODE_ENV: ['development', 'production'] as const,
|
|
437
399
|
}],
|
|
438
400
|
[dotenv(), {
|
|
439
|
-
DATABASE_URL:
|
|
440
|
-
API_KEY:
|
|
401
|
+
DATABASE_URL: postgres(),
|
|
402
|
+
API_KEY: string(),
|
|
441
403
|
}]
|
|
442
404
|
);
|
|
443
405
|
|
|
444
|
-
// Sync mode - supports multiple SYNC resolvers
|
|
406
|
+
// Sync mode - supports multiple SYNC resolvers
|
|
445
407
|
const config = resolve(
|
|
446
408
|
[dotenv(), { PORT: 3000 }],
|
|
447
|
-
[json('config.json'), { DATABASE_URL:
|
|
409
|
+
[json('config.json'), { DATABASE_URL: postgres() }]
|
|
448
410
|
// Both resolvers must have loadSync() method
|
|
449
411
|
);
|
|
412
|
+
|
|
413
|
+
// Support for ANY number of resolvers (3, 4, 5, 10+ resolvers!)
|
|
414
|
+
const config = await resolveAsync(
|
|
415
|
+
[processEnv(), { PORT: 3000 }],
|
|
416
|
+
[dotenv(), { DATABASE_URL: postgres() }],
|
|
417
|
+
[awsSecrets(), { API_KEY: string() }],
|
|
418
|
+
[gcpSecrets(), { JWT_SECRET: string() }],
|
|
419
|
+
[vaultSecrets(), { REDIS_URL: redis() }],
|
|
420
|
+
[consul(), { SERVICE_DISCOVERY: json() }]
|
|
421
|
+
// Options as last parameter
|
|
422
|
+
{ priority: 'last' }
|
|
423
|
+
);
|
|
450
424
|
```
|
|
451
425
|
|
|
452
426
|
### Controlling merge behaviour
|
|
@@ -454,25 +428,29 @@ const config = resolve(
|
|
|
454
428
|
Use `priority` to control how resolvers merge values (works with both sync and async modes):
|
|
455
429
|
|
|
456
430
|
```ts
|
|
431
|
+
import { resolve, resolveAsync, processEnv } from 'node-env-resolver';
|
|
432
|
+
import { dotenv, json, postgres } from 'node-env-resolver/resolvers';
|
|
433
|
+
import { awsSecrets } from 'node-env-resolver-aws';
|
|
434
|
+
|
|
457
435
|
// priority: 'last' (default) - later resolvers override earlier ones
|
|
458
|
-
const config = await
|
|
459
|
-
[processEnv(), { DATABASE_URL:
|
|
460
|
-
[awsSecrets(), { DATABASE_URL:
|
|
436
|
+
const config = await resolveAsync(
|
|
437
|
+
[processEnv(), { DATABASE_URL: postgres() }],
|
|
438
|
+
[awsSecrets(), { DATABASE_URL: postgres() }]
|
|
461
439
|
// AWS wins
|
|
462
440
|
);
|
|
463
441
|
|
|
464
442
|
// priority: 'first' - earlier resolvers take precedence
|
|
465
|
-
const config = await
|
|
466
|
-
[dotenv(), { DATABASE_URL:
|
|
467
|
-
[awsSecrets(), { DATABASE_URL:
|
|
443
|
+
const config = await resolveAsync(
|
|
444
|
+
[dotenv(), { DATABASE_URL: postgres() }],
|
|
445
|
+
[awsSecrets(), { DATABASE_URL: postgres() }],
|
|
468
446
|
{ priority: 'first' }
|
|
469
447
|
// dotenv wins
|
|
470
448
|
);
|
|
471
449
|
|
|
472
450
|
// Also works with sync resolve() (NEW!)
|
|
473
451
|
const config = resolve(
|
|
474
|
-
[dotenv(), { DATABASE_URL:
|
|
475
|
-
[json('config.json'), { DATABASE_URL:
|
|
452
|
+
[dotenv(), { DATABASE_URL: postgres() }],
|
|
453
|
+
[json('config.json'), { DATABASE_URL: postgres() }],
|
|
476
454
|
{ priority: 'first' }
|
|
477
455
|
// dotenv wins
|
|
478
456
|
);
|
|
@@ -489,10 +467,14 @@ The library includes two automatic performance optimizations:
|
|
|
489
467
|
When using `priority: 'first'`, resolvers are called sequentially, but execution **stops early** once all required environment variables are satisfied:
|
|
490
468
|
|
|
491
469
|
```ts
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
470
|
+
import { resolveAsync } from 'node-env-resolver';
|
|
471
|
+
import { dotenv, postgres, string } from 'node-env-resolver/resolvers';
|
|
472
|
+
import { awsSecrets, gcpSecrets } from 'node-env-resolver-aws';
|
|
473
|
+
|
|
474
|
+
const config = await resolveAsync(
|
|
475
|
+
[dotenv(), { DATABASE_URL: postgres(), API_KEY: string(), PORT: 3000 }],
|
|
476
|
+
[awsSecrets(), { DATABASE_URL: postgres(), API_KEY: string(), PORT: 3000 }],
|
|
477
|
+
[gcpSecrets(), { DATABASE_URL: postgres(), API_KEY: string(), PORT: 3000 }],
|
|
496
478
|
{ priority: 'first' }
|
|
497
479
|
);
|
|
498
480
|
// If dotenv() provides all required keys, awsSecrets() and gcpSecrets() are never called!
|
|
@@ -514,10 +496,14 @@ This is particularly valuable in development where:
|
|
|
514
496
|
When using `priority: 'last'` (the default), all resolvers are called **in parallel** for maximum performance:
|
|
515
497
|
|
|
516
498
|
```ts
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
499
|
+
import { resolveAsync } from 'node-env-resolver';
|
|
500
|
+
import { postgres, string } from 'node-env-resolver/resolvers';
|
|
501
|
+
import { awsSecrets, awsParameterStore, gcpSecrets } from 'node-env-resolver-aws';
|
|
502
|
+
|
|
503
|
+
const config = await resolveAsync(
|
|
504
|
+
[awsSecrets(), { DATABASE_URL: postgres() }], // 100ms
|
|
505
|
+
[awsParameterStore(), { API_KEY: string() }], // 100ms
|
|
506
|
+
[gcpSecrets(), { JWT_SECRET: string() }] // 100ms
|
|
521
507
|
// Default: priority: 'last'
|
|
522
508
|
);
|
|
523
509
|
// Total time: ~100ms (parallel) instead of ~300ms (sequential)
|
|
@@ -537,10 +523,11 @@ Like Zod's `safeParse()`, use `safeResolve()` to get a result object instead of
|
|
|
537
523
|
|
|
538
524
|
```ts
|
|
539
525
|
import { safeResolve } from 'node-env-resolver';
|
|
526
|
+
import { number, postgres } from 'node-env-resolver/resolvers';
|
|
540
527
|
|
|
541
528
|
const result = safeResolve({
|
|
542
|
-
PORT:
|
|
543
|
-
DATABASE_URL:
|
|
529
|
+
PORT: number(),
|
|
530
|
+
DATABASE_URL: postgres()
|
|
544
531
|
});
|
|
545
532
|
|
|
546
533
|
if (result.success) {
|
|
@@ -556,7 +543,7 @@ if (result.success) {
|
|
|
556
543
|
All functions have safe variants:
|
|
557
544
|
|
|
558
545
|
- `resolve()` → `safeResolve()` (both synchronous)
|
|
559
|
-
- `
|
|
546
|
+
- `resolveAsync()` → `saferesolveAsync()` (both async)
|
|
560
547
|
|
|
561
548
|
## Synchronous resolution
|
|
562
549
|
|
|
@@ -564,25 +551,29 @@ All functions have safe variants:
|
|
|
564
551
|
|
|
565
552
|
```ts
|
|
566
553
|
import { resolve } from 'node-env-resolver';
|
|
554
|
+
import { string, url, postgres } from 'node-env-resolver/resolvers';
|
|
567
555
|
|
|
568
556
|
// Synchronous - no await needed, works with ALL types
|
|
569
557
|
const config = resolve({
|
|
570
558
|
PORT: 3000,
|
|
571
559
|
NODE_ENV: ['development', 'production'] as const,
|
|
572
|
-
API_KEY:
|
|
573
|
-
API_URL:
|
|
574
|
-
DATABASE_URL:
|
|
560
|
+
API_KEY: string(),
|
|
561
|
+
API_URL: url(), // Advanced types work synchronously!
|
|
562
|
+
DATABASE_URL: postgres(),
|
|
575
563
|
DEBUG: false
|
|
576
564
|
});
|
|
577
565
|
```
|
|
578
566
|
|
|
579
|
-
`
|
|
567
|
+
`resolveAsync()` is **async** when using custom resolvers:
|
|
580
568
|
|
|
581
569
|
```ts
|
|
570
|
+
import { resolveAsync, processEnv } from 'node-env-resolver';
|
|
571
|
+
import { dotenv, postgres, url } from 'node-env-resolver/resolvers';
|
|
572
|
+
|
|
582
573
|
// Async - await required when using custom resolvers
|
|
583
|
-
const config = await
|
|
574
|
+
const config = await resolveAsync(
|
|
584
575
|
[processEnv(), { PORT: 3000 }],
|
|
585
|
-
[dotenv(), { DATABASE_URL:
|
|
576
|
+
[dotenv(), { DATABASE_URL: postgres(), API_URL: url() }]
|
|
586
577
|
);
|
|
587
578
|
```
|
|
588
579
|
|
|
@@ -593,20 +584,21 @@ const config = await resolve.async(
|
|
|
593
584
|
Parse command-line arguments as environment variables - perfect for CLI tools:
|
|
594
585
|
|
|
595
586
|
```ts
|
|
596
|
-
import {
|
|
587
|
+
import { resolveAsync, processEnv } from 'node-env-resolver';
|
|
588
|
+
import { postgres } from 'node-env-resolver/resolvers';
|
|
597
589
|
import { cliArgs } from 'node-env-resolver/cli';
|
|
598
590
|
|
|
599
591
|
// $ node app.js --port 8080 --database-url postgres://localhost --verbose
|
|
600
592
|
|
|
601
|
-
const config = await
|
|
593
|
+
const config = await resolveAsync(
|
|
602
594
|
[processEnv(), {
|
|
603
595
|
PORT: 3000,
|
|
604
|
-
DATABASE_URL:
|
|
596
|
+
DATABASE_URL: postgres(),
|
|
605
597
|
VERBOSE: false
|
|
606
598
|
}],
|
|
607
599
|
[cliArgs(), {
|
|
608
600
|
PORT: 3000,
|
|
609
|
-
DATABASE_URL:
|
|
601
|
+
DATABASE_URL: postgres(),
|
|
610
602
|
VERBOSE: false
|
|
611
603
|
}]
|
|
612
604
|
);
|
|
@@ -795,7 +787,8 @@ if (result.success) {
|
|
|
795
787
|
Create resolvers to load configuration from any source:
|
|
796
788
|
|
|
797
789
|
```ts
|
|
798
|
-
import {
|
|
790
|
+
import { resolveAsync, processEnv, type Resolver } from 'node-env-resolver';
|
|
791
|
+
import { string } from 'node-env-resolver/resolvers';
|
|
799
792
|
|
|
800
793
|
const databaseResolver: Resolver = {
|
|
801
794
|
name: 'database',
|
|
@@ -805,9 +798,9 @@ const databaseResolver: Resolver = {
|
|
|
805
798
|
}
|
|
806
799
|
};
|
|
807
800
|
|
|
808
|
-
const config = await
|
|
801
|
+
const config = await resolveAsync(
|
|
809
802
|
[processEnv(), { PORT: 3000 }],
|
|
810
|
-
[databaseResolver, { API_KEY:
|
|
803
|
+
[databaseResolver, { API_KEY: string() }]
|
|
811
804
|
);
|
|
812
805
|
```
|
|
813
806
|
|
|
@@ -823,25 +816,27 @@ Single source:
|
|
|
823
816
|
|
|
824
817
|
```ts
|
|
825
818
|
import { resolveSecrets } from 'node-env-resolver-aws';
|
|
819
|
+
import { postgres, string } from 'node-env-resolver/resolvers';
|
|
826
820
|
|
|
827
821
|
const config = await resolveSecrets({
|
|
828
822
|
secretId: 'myapp/secrets'
|
|
829
823
|
}, {
|
|
830
|
-
DATABASE_URL:
|
|
831
|
-
API_KEY:
|
|
824
|
+
DATABASE_URL: postgres(),
|
|
825
|
+
API_KEY: string()
|
|
832
826
|
});
|
|
833
827
|
```
|
|
834
828
|
|
|
835
829
|
Multiple sources:
|
|
836
830
|
|
|
837
831
|
```ts
|
|
838
|
-
import {
|
|
832
|
+
import { resolveAsync, processEnv } from 'node-env-resolver';
|
|
833
|
+
import { postgres, string } from 'node-env-resolver/resolvers';
|
|
839
834
|
import { awsSecrets, awsSsm } from 'node-env-resolver-aws';
|
|
840
835
|
|
|
841
|
-
const config = await
|
|
836
|
+
const config = await resolveAsync(
|
|
842
837
|
[processEnv(), { PORT: 3000 }],
|
|
843
|
-
[awsSecrets({ secretId: 'app/secrets' }), { DATABASE_URL:
|
|
844
|
-
[awsSsm({ path: '/app/config' }), { API_KEY:
|
|
838
|
+
[awsSecrets({ secretId: 'app/secrets' }), { DATABASE_URL: postgres() }],
|
|
839
|
+
[awsSsm({ path: '/app/config' }), { API_KEY: string() }]
|
|
845
840
|
);
|
|
846
841
|
```
|
|
847
842
|
|
|
@@ -852,18 +847,19 @@ See the [AWS package documentation](../node-env-resolver-aws/README.md) for deta
|
|
|
852
847
|
Cache expensive operations like AWS API calls:
|
|
853
848
|
|
|
854
849
|
```ts
|
|
855
|
-
import {
|
|
850
|
+
import { resolveAsync } from 'node-env-resolver';
|
|
856
851
|
import { cached, TTL } from 'node-env-resolver/utils';
|
|
852
|
+
import { postgres, string } from 'node-env-resolver/resolvers';
|
|
857
853
|
import { awsSecrets } from 'node-env-resolver-aws';
|
|
858
854
|
|
|
859
855
|
export const getConfig = async () => {
|
|
860
|
-
return await
|
|
856
|
+
return await resolveAsync(
|
|
861
857
|
[cached(
|
|
862
858
|
awsSecrets({ secretId: 'app/secrets' }),
|
|
863
859
|
{ ttl: TTL.minutes5 }
|
|
864
860
|
), {
|
|
865
|
-
DATABASE_URL:
|
|
866
|
-
API_KEY:
|
|
861
|
+
DATABASE_URL: postgres(),
|
|
862
|
+
API_KEY: string(),
|
|
867
863
|
}]
|
|
868
864
|
);
|
|
869
865
|
};
|
|
@@ -890,34 +886,48 @@ Performance:
|
|
|
890
886
|
|----------|-----------|----------|
|
|
891
887
|
| `resolve()` | Sync, throws on error | Most applications (reading from process.env) |
|
|
892
888
|
| `safeResolve()` | Sync, returns result object | Graceful error handling |
|
|
893
|
-
| `
|
|
894
|
-
| `
|
|
889
|
+
| `resolveAsync()` | Async, throws on error | Multiple sources (dotenv, AWS, etc.) |
|
|
890
|
+
| `safeResolveAsync()` | Async, returns result object | Multiple sources with error handling |
|
|
891
|
+
|
|
892
|
+
### Resolver API Reference
|
|
895
893
|
|
|
896
|
-
|
|
894
|
+
Import resolver functions from `'node-env-resolver/resolvers'`:
|
|
895
|
+
|
|
896
|
+
```ts
|
|
897
|
+
import {
|
|
898
|
+
string, number, boolean, url, email, port, json,
|
|
899
|
+
postgres, mysql, mongodb, redis,
|
|
900
|
+
http, https, date, timestamp, duration, file
|
|
901
|
+
} from 'node-env-resolver/resolvers';
|
|
902
|
+
```
|
|
897
903
|
|
|
898
904
|
| Syntax | Type | Description |
|
|
899
905
|
|--------|------|-------------|
|
|
900
|
-
| `
|
|
901
|
-
| `
|
|
902
|
-
| `
|
|
903
|
-
| `
|
|
904
|
-
| `
|
|
905
|
-
| `
|
|
906
|
-
| `
|
|
907
|
-
| `
|
|
908
|
-
| `
|
|
909
|
-
| `
|
|
910
|
-
| `
|
|
911
|
-
| `
|
|
912
|
-
| `
|
|
913
|
-
| `
|
|
914
|
-
| `
|
|
915
|
-
| `
|
|
916
|
-
| `
|
|
917
|
-
| `
|
|
918
|
-
| `
|
|
919
|
-
| `
|
|
920
|
-
| `
|
|
906
|
+
| `string()` | `string` | Required string (empty strings rejected by default) |
|
|
907
|
+
| `string({optional: true})` | `string \| undefined` | Optional string |
|
|
908
|
+
| `number()` | `number` | Required number (coerced from string) |
|
|
909
|
+
| `number({optional: true})` | `number \| undefined` | Optional number |
|
|
910
|
+
| `boolean()` | `boolean` | Required boolean (coerced from string) |
|
|
911
|
+
| `boolean({optional: true})` | `boolean \| undefined` | Optional boolean |
|
|
912
|
+
| `url()` | `string` | Validated URL (returns string) |
|
|
913
|
+
| `url({optional: true})` | `string \| undefined` | Optional URL |
|
|
914
|
+
| `email()` | `string` | Validated email address |
|
|
915
|
+
| `port()` | `number` | Validated port number (1-65535) |
|
|
916
|
+
| `json()` | `unknown` | Parsed JSON (returns object/array) |
|
|
917
|
+
| `postgres()` or `postgresql()` | `string` | Validated PostgreSQL URL |
|
|
918
|
+
| `mysql()` | `string` | Validated MySQL connection string |
|
|
919
|
+
| `mongodb()` | `string` | Validated MongoDB connection string |
|
|
920
|
+
| `redis()` | `string` | Validated Redis connection string |
|
|
921
|
+
| `http()` | `string` | HTTP or HTTPS URL |
|
|
922
|
+
| `https()` | `string` | HTTPS-only URL |
|
|
923
|
+
| `date()` | `string` | Validated ISO 8601 date |
|
|
924
|
+
| `timestamp()` | `number` | Validated Unix timestamp |
|
|
925
|
+
| `duration()` | `number` | Time duration (`5s`, `2m`, `1h` → milliseconds) |
|
|
926
|
+
| `file()` | `string` | Read content from file path |
|
|
927
|
+
| `3000` | `number` | Number with default value |
|
|
928
|
+
| `false` | `boolean` | Boolean with default value |
|
|
929
|
+
| `'defaultValue'` | `string` | String with default value |
|
|
930
|
+
| `['a', 'b'] as const` | `'a' \| 'b'` | Enum (requires `as const`) |
|
|
921
931
|
|
|
922
932
|
## Configuration Options
|
|
923
933
|
|
|
@@ -932,8 +942,8 @@ const config = resolve(schema, {
|
|
|
932
942
|
enableAudit: true
|
|
933
943
|
});
|
|
934
944
|
|
|
935
|
-
// Multiple sources - options as last parameter to
|
|
936
|
-
const config = await
|
|
945
|
+
// Multiple sources - options as last parameter to resolveAsync()
|
|
946
|
+
const config = await resolveAsync(
|
|
937
947
|
[resolver1, schema],
|
|
938
948
|
[resolver2, schema],
|
|
939
949
|
{
|
|
@@ -946,7 +956,7 @@ const config = await resolve.async(
|
|
|
946
956
|
);
|
|
947
957
|
```
|
|
948
958
|
|
|
949
|
-
**Note:** The `resolvers` option has been removed. For single source resolution, use `resolve()` directly (defaults to `process.env`). For multiple sources, use `
|
|
959
|
+
**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
960
|
|
|
951
961
|
### `interpolate`
|
|
952
962
|
|
|
@@ -956,16 +966,19 @@ const config = await resolve.async(
|
|
|
956
966
|
|
|
957
967
|
**Why:** Keeps configuration DRY and maintainable.
|
|
958
968
|
|
|
959
|
-
**Default:** `true` in `
|
|
969
|
+
**Default:** `true` in `resolveAsync()`, `false` in `resolve()`
|
|
960
970
|
|
|
961
971
|
```ts
|
|
972
|
+
import { resolve } from 'node-env-resolver';
|
|
973
|
+
import { url } from 'node-env-resolver/resolvers';
|
|
974
|
+
|
|
962
975
|
// With interpolation
|
|
963
976
|
process.env.BASE_URL = 'https://api.example.com';
|
|
964
977
|
process.env.API_ENDPOINT = '${BASE_URL}/v1';
|
|
965
978
|
|
|
966
979
|
const config = resolve({
|
|
967
|
-
BASE_URL:
|
|
968
|
-
API_ENDPOINT:
|
|
980
|
+
BASE_URL: url(),
|
|
981
|
+
API_ENDPOINT: url()
|
|
969
982
|
}, {
|
|
970
983
|
interpolate: true
|
|
971
984
|
});
|
|
@@ -998,14 +1011,14 @@ const flakyResolver = {
|
|
|
998
1011
|
};
|
|
999
1012
|
|
|
1000
1013
|
// ❌ Throws immediately
|
|
1001
|
-
await
|
|
1014
|
+
await resolveAsync(
|
|
1002
1015
|
[flakyResolver, schema],
|
|
1003
1016
|
[processEnv(), schema],
|
|
1004
1017
|
{ strict: true } // default
|
|
1005
1018
|
);
|
|
1006
1019
|
|
|
1007
1020
|
// ✅ Continues with processEnv()
|
|
1008
|
-
await
|
|
1021
|
+
await resolveAsync(
|
|
1009
1022
|
[flakyResolver, schema],
|
|
1010
1023
|
[processEnv(), schema],
|
|
1011
1024
|
{ strict: false } // graceful degradation
|
|
@@ -1028,17 +1041,21 @@ await resolve.async(
|
|
|
1028
1041
|
**Default:** `'last'`
|
|
1029
1042
|
|
|
1030
1043
|
```ts
|
|
1044
|
+
import { resolveAsync, processEnv } from 'node-env-resolver';
|
|
1045
|
+
import { dotenv, postgres } from 'node-env-resolver/resolvers';
|
|
1046
|
+
import { awsSecrets } from 'node-env-resolver-aws';
|
|
1047
|
+
|
|
1031
1048
|
// Production: AWS secrets override process.env
|
|
1032
|
-
await
|
|
1033
|
-
[processEnv(), { DATABASE_URL:
|
|
1034
|
-
[awsSecrets(), { DATABASE_URL:
|
|
1049
|
+
await resolveAsync(
|
|
1050
|
+
[processEnv(), { DATABASE_URL: postgres() }],
|
|
1051
|
+
[awsSecrets(), { DATABASE_URL: postgres() }]
|
|
1035
1052
|
// priority: 'last' (default) - AWS wins
|
|
1036
1053
|
);
|
|
1037
1054
|
|
|
1038
1055
|
// Development: Local .env overrides cloud
|
|
1039
|
-
await
|
|
1040
|
-
[dotenv(), { DATABASE_URL:
|
|
1041
|
-
[awsSecrets(), { DATABASE_URL:
|
|
1056
|
+
await resolveAsync(
|
|
1057
|
+
[dotenv(), { DATABASE_URL: postgres() }],
|
|
1058
|
+
[awsSecrets(), { DATABASE_URL: postgres() }],
|
|
1042
1059
|
{ priority: 'first' } // dotenv wins
|
|
1043
1060
|
);
|
|
1044
1061
|
```
|
|
@@ -1056,7 +1073,7 @@ await resolve.async(
|
|
|
1056
1073
|
**Default:** `undefined` (no policies enforced)
|
|
1057
1074
|
|
|
1058
1075
|
```ts
|
|
1059
|
-
await
|
|
1076
|
+
await resolveAsync(
|
|
1060
1077
|
[processEnv(), schema],
|
|
1061
1078
|
[awsSecrets(), schema],
|
|
1062
1079
|
{
|
|
@@ -1091,7 +1108,7 @@ await resolve.async(
|
|
|
1091
1108
|
**Default:** `false` (disabled in development), automatically `true` in production (`NODE_ENV === 'production'`)
|
|
1092
1109
|
|
|
1093
1110
|
```ts
|
|
1094
|
-
await
|
|
1111
|
+
await resolveAsync(
|
|
1095
1112
|
[processEnv(), schema],
|
|
1096
1113
|
[awsSecrets(), schema],
|
|
1097
1114
|
{ enableAudit: true } // Explicitly enable in development
|
|
@@ -1131,11 +1148,12 @@ interface PolicyOptions {
|
|
|
1131
1148
|
```ts
|
|
1132
1149
|
import express from 'express';
|
|
1133
1150
|
import { resolve } from 'node-env-resolver';
|
|
1151
|
+
import { postgres, string } from 'node-env-resolver/resolvers';
|
|
1134
1152
|
|
|
1135
1153
|
const config = resolve({
|
|
1136
1154
|
PORT: 3000,
|
|
1137
|
-
DATABASE_URL:
|
|
1138
|
-
SESSION_SECRET:
|
|
1155
|
+
DATABASE_URL: postgres(),
|
|
1156
|
+
SESSION_SECRET: string()
|
|
1139
1157
|
});
|
|
1140
1158
|
|
|
1141
1159
|
const app = express();
|
|
@@ -1147,14 +1165,15 @@ app.listen(config.PORT);
|
|
|
1147
1165
|
```ts
|
|
1148
1166
|
// env.mjs
|
|
1149
1167
|
import { resolve } from 'node-env-resolver/nextjs';
|
|
1168
|
+
import { postgres, string, url } from 'node-env-resolver/resolvers';
|
|
1150
1169
|
|
|
1151
1170
|
export const env = resolve({
|
|
1152
1171
|
server: {
|
|
1153
|
-
DATABASE_URL:
|
|
1154
|
-
API_SECRET:
|
|
1172
|
+
DATABASE_URL: postgres(),
|
|
1173
|
+
API_SECRET: string(),
|
|
1155
1174
|
},
|
|
1156
1175
|
client: {
|
|
1157
|
-
NEXT_PUBLIC_APP_URL:
|
|
1176
|
+
NEXT_PUBLIC_APP_URL: url(),
|
|
1158
1177
|
}
|
|
1159
1178
|
});
|
|
1160
1179
|
```
|
|
@@ -1162,17 +1181,18 @@ export const env = resolve({
|
|
|
1162
1181
|
### AWS Lambda
|
|
1163
1182
|
|
|
1164
1183
|
```ts
|
|
1165
|
-
import {
|
|
1184
|
+
import { resolveAsync } from 'node-env-resolver';
|
|
1166
1185
|
import { cached, TTL } from 'node-env-resolver/utils';
|
|
1186
|
+
import { postgres } from 'node-env-resolver/resolvers';
|
|
1167
1187
|
import { awsSecrets } from 'node-env-resolver-aws';
|
|
1168
1188
|
|
|
1169
1189
|
const getConfig = async () => {
|
|
1170
|
-
return await
|
|
1190
|
+
return await resolveAsync(
|
|
1171
1191
|
[cached(
|
|
1172
1192
|
awsSecrets({ secretId: 'lambda/config' }),
|
|
1173
1193
|
{ ttl: TTL.minutes5 }
|
|
1174
1194
|
), {
|
|
1175
|
-
DATABASE_URL:
|
|
1195
|
+
DATABASE_URL: postgres(),
|
|
1176
1196
|
}]
|
|
1177
1197
|
);
|
|
1178
1198
|
};
|
|
@@ -1194,10 +1214,13 @@ By default, `.env` files are **completely blocked in production** for security.
|
|
|
1194
1214
|
**Default behavior (secure):**
|
|
1195
1215
|
|
|
1196
1216
|
```ts
|
|
1217
|
+
import { resolveAsync } from 'node-env-resolver';
|
|
1218
|
+
import { dotenv, postgres } from 'node-env-resolver/resolvers';
|
|
1219
|
+
|
|
1197
1220
|
// In production (NODE_ENV=production)
|
|
1198
|
-
const config = await
|
|
1221
|
+
const config = await resolveAsync(
|
|
1199
1222
|
[dotenv(), {
|
|
1200
|
-
DATABASE_URL:
|
|
1223
|
+
DATABASE_URL: postgres(),
|
|
1201
1224
|
}]
|
|
1202
1225
|
);
|
|
1203
1226
|
// ❌ Throws: "DATABASE_URL cannot be sourced from .env files in production"
|
|
@@ -1206,9 +1229,12 @@ const config = await resolve.async(
|
|
|
1206
1229
|
**Allow all .env variables (NOT recommended):**
|
|
1207
1230
|
|
|
1208
1231
|
```ts
|
|
1209
|
-
|
|
1232
|
+
import { resolveAsync } from 'node-env-resolver';
|
|
1233
|
+
import { dotenv, postgres } from 'node-env-resolver/resolvers';
|
|
1234
|
+
|
|
1235
|
+
const config = await resolveAsync(
|
|
1210
1236
|
[dotenv(), {
|
|
1211
|
-
DATABASE_URL:
|
|
1237
|
+
DATABASE_URL: postgres(),
|
|
1212
1238
|
}],
|
|
1213
1239
|
{
|
|
1214
1240
|
policies: {
|
|
@@ -1221,10 +1247,13 @@ const config = await resolve.async(
|
|
|
1221
1247
|
**Allow specific variables only (recommended if needed):**
|
|
1222
1248
|
|
|
1223
1249
|
```ts
|
|
1224
|
-
|
|
1250
|
+
import { resolveAsync } from 'node-env-resolver';
|
|
1251
|
+
import { dotenv, postgres } from 'node-env-resolver/resolvers';
|
|
1252
|
+
|
|
1253
|
+
const config = await resolveAsync(
|
|
1225
1254
|
[dotenv(), {
|
|
1226
1255
|
PORT: 3000,
|
|
1227
|
-
DATABASE_URL:
|
|
1256
|
+
DATABASE_URL: postgres(),
|
|
1228
1257
|
}],
|
|
1229
1258
|
{
|
|
1230
1259
|
policies: {
|
|
@@ -1240,16 +1269,17 @@ const config = await resolve.async(
|
|
|
1240
1269
|
Restrict sensitive variables to specific resolvers (e.g., force secrets to come from AWS):
|
|
1241
1270
|
|
|
1242
1271
|
```ts
|
|
1243
|
-
import {
|
|
1272
|
+
import { resolveAsync, processEnv } from 'node-env-resolver';
|
|
1273
|
+
import { string } from 'node-env-resolver/resolvers';
|
|
1244
1274
|
import { awsSecrets } from 'node-env-resolver-aws';
|
|
1245
1275
|
|
|
1246
|
-
const config = await
|
|
1276
|
+
const config = await resolveAsync(
|
|
1247
1277
|
[processEnv(), {
|
|
1248
1278
|
PORT: 3000,
|
|
1249
1279
|
}],
|
|
1250
1280
|
[awsSecrets({ secretId: 'prod/secrets' }), {
|
|
1251
|
-
DATABASE_PASSWORD:
|
|
1252
|
-
API_KEY:
|
|
1281
|
+
DATABASE_PASSWORD: string(),
|
|
1282
|
+
API_KEY: string(),
|
|
1253
1283
|
}],
|
|
1254
1284
|
{
|
|
1255
1285
|
policies: {
|
|
@@ -1266,15 +1296,16 @@ const config = await resolve.async(
|
|
|
1266
1296
|
**Use case:** Ensure production secrets only come from AWS Secrets Manager, never from `.env` or `process.env`:
|
|
1267
1297
|
|
|
1268
1298
|
```ts
|
|
1269
|
-
import {
|
|
1299
|
+
import { resolveAsync, processEnv } from 'node-env-resolver';
|
|
1300
|
+
import { string } from 'node-env-resolver/resolvers';
|
|
1270
1301
|
import { cached, TTL } from 'node-env-resolver/utils';
|
|
1271
1302
|
import { awsSecrets } from 'node-env-resolver-aws';
|
|
1272
1303
|
|
|
1273
|
-
const config = await
|
|
1304
|
+
const config = await resolveAsync(
|
|
1274
1305
|
[processEnv(), {}],
|
|
1275
1306
|
[cached(awsSecrets({ secretId: 'prod/db' }), { ttl: TTL.minutes5 }), {
|
|
1276
|
-
DATABASE_PASSWORD:
|
|
1277
|
-
STRIPE_SECRET:
|
|
1307
|
+
DATABASE_PASSWORD: string(),
|
|
1308
|
+
STRIPE_SECRET: string(),
|
|
1278
1309
|
}],
|
|
1279
1310
|
{
|
|
1280
1311
|
policies: {
|
|
@@ -1324,11 +1355,12 @@ Track environment variable resolution for security and compliance monitoring.
|
|
|
1324
1355
|
|
|
1325
1356
|
```ts
|
|
1326
1357
|
import { resolve, getAuditLog, clearAuditLog } from 'node-env-resolver';
|
|
1358
|
+
import { postgres, string } from 'node-env-resolver/resolvers';
|
|
1327
1359
|
|
|
1328
1360
|
// Option 1: Explicitly enable (works in any environment)
|
|
1329
1361
|
const config = resolve({
|
|
1330
|
-
DATABASE_URL:
|
|
1331
|
-
API_KEY:
|
|
1362
|
+
DATABASE_URL: postgres(),
|
|
1363
|
+
API_KEY: string(),
|
|
1332
1364
|
}, {
|
|
1333
1365
|
enableAudit: true // ← Enable audit logging
|
|
1334
1366
|
});
|
|
@@ -1345,11 +1377,14 @@ clearAuditLog();
|
|
|
1345
1377
|
```
|
|
1346
1378
|
|
|
1347
1379
|
```ts
|
|
1380
|
+
import { resolve, getAuditLog } from 'node-env-resolver';
|
|
1381
|
+
import { postgres } from 'node-env-resolver/resolvers';
|
|
1382
|
+
|
|
1348
1383
|
// Option 2: Automatic in production
|
|
1349
1384
|
process.env.NODE_ENV = 'production';
|
|
1350
1385
|
|
|
1351
1386
|
const config = resolve({
|
|
1352
|
-
DATABASE_URL:
|
|
1387
|
+
DATABASE_URL: postgres(),
|
|
1353
1388
|
});
|
|
1354
1389
|
|
|
1355
1390
|
// Audit automatically enabled in production
|
|
@@ -1362,14 +1397,15 @@ When you have multiple `resolve()` calls, you can now get audit logs **specific
|
|
|
1362
1397
|
|
|
1363
1398
|
```ts
|
|
1364
1399
|
import { resolve, getAuditLog } from 'node-env-resolver';
|
|
1400
|
+
import { string, postgres } from 'node-env-resolver/resolvers';
|
|
1365
1401
|
|
|
1366
1402
|
const appConfig = resolve({
|
|
1367
1403
|
PORT: 3000,
|
|
1368
|
-
API_KEY:
|
|
1404
|
+
API_KEY: string()
|
|
1369
1405
|
}, { enableAudit: true });
|
|
1370
1406
|
|
|
1371
1407
|
const dbConfig = resolve({
|
|
1372
|
-
DATABASE_URL:
|
|
1408
|
+
DATABASE_URL: postgres(),
|
|
1373
1409
|
DB_POOL_SIZE: 10
|
|
1374
1410
|
}, { enableAudit: true });
|
|
1375
1411
|
|
|
@@ -1416,13 +1452,14 @@ console.log('DB config audit:', dbAudit);
|
|
|
1416
1452
|
Audit logs include cache metadata to help monitor performance:
|
|
1417
1453
|
|
|
1418
1454
|
```ts
|
|
1419
|
-
import {
|
|
1455
|
+
import { resolveAsync, getAuditLog } from 'node-env-resolver';
|
|
1420
1456
|
import { cached } from 'node-env-resolver/utils';
|
|
1457
|
+
import { postgres } from 'node-env-resolver/resolvers';
|
|
1421
1458
|
import { awsSecrets } from 'node-env-resolver-aws';
|
|
1422
1459
|
|
|
1423
|
-
const config = await
|
|
1460
|
+
const config = await resolveAsync(
|
|
1424
1461
|
[cached(awsSecrets({ secretId: 'prod/db' }), { ttl: 300000 }), {
|
|
1425
|
-
DATABASE_URL:
|
|
1462
|
+
DATABASE_URL: postgres(),
|
|
1426
1463
|
}],
|
|
1427
1464
|
{ enableAudit: true }
|
|
1428
1465
|
);
|
|
@@ -1444,15 +1481,16 @@ logs.forEach(log => {
|
|
|
1444
1481
|
Use audit logs for compliance and security monitoring:
|
|
1445
1482
|
|
|
1446
1483
|
```ts
|
|
1447
|
-
import {
|
|
1484
|
+
import { resolveAsync, processEnv, getAuditLog } from 'node-env-resolver';
|
|
1485
|
+
import { string } from 'node-env-resolver/resolvers';
|
|
1448
1486
|
import { awsSecrets } from 'node-env-resolver-aws';
|
|
1449
1487
|
|
|
1450
1488
|
// In production
|
|
1451
|
-
const config = await
|
|
1489
|
+
const config = await resolveAsync(
|
|
1452
1490
|
[processEnv(), {}],
|
|
1453
1491
|
[awsSecrets(), {
|
|
1454
|
-
DATABASE_PASSWORD:
|
|
1455
|
-
API_KEY:
|
|
1492
|
+
DATABASE_PASSWORD: string(),
|
|
1493
|
+
API_KEY: string(),
|
|
1456
1494
|
}],
|
|
1457
1495
|
{
|
|
1458
1496
|
policies: {
|