node-env-resolver 2.0.0 → 3.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 +345 -69
- package/dist/audit.js +1 -1
- package/dist/builder.d.ts +1 -1
- package/dist/builder.d.ts.map +1 -1
- package/dist/builder.js +1 -1
- package/dist/chunk-5SFFBKLW.js +9 -0
- package/dist/chunk-CC4LJOXE.js +3 -0
- package/dist/chunk-DAGPFV3V.js +1 -0
- package/dist/chunk-ICRS72Z4.js +1 -0
- package/dist/chunk-WOT6VMZA.js +1 -0
- package/dist/chunk-XP3U6MKF.js +5 -0
- package/dist/index.d.ts +2 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -5
- package/dist/performance.test.d.ts +7 -0
- package/dist/performance.test.d.ts.map +1 -0
- package/dist/resolver.d.ts.map +1 -1
- package/dist/resolver.js +1 -9
- package/dist/resolvers.js +1 -3
- package/dist/types.d.ts +25 -2
- package/dist/types.d.ts.map +1 -1
- package/dist/utils.js +1 -1
- package/dist/valibot.d.ts +153 -0
- package/dist/valibot.d.ts.map +1 -0
- package/dist/valibot.js +1 -0
- package/dist/validation-types.d.ts +28 -0
- package/dist/validation-types.d.ts.map +1 -0
- package/dist/validation-types.js +0 -0
- package/dist/validators.d.ts +5 -2
- package/dist/validators.d.ts.map +1 -1
- package/dist/validators.js +1 -1
- package/dist/web.d.ts +1 -1
- package/dist/web.d.ts.map +1 -1
- package/dist/web.js +4 -4
- package/dist/zod.d.ts +18 -65
- package/dist/zod.d.ts.map +1 -1
- package/dist/zod.js +1 -1
- package/package.json +17 -16
- package/dist/standard-schema.d.ts +0 -32
- package/dist/standard-schema.d.ts.map +0 -1
- package/dist/standard-schema.js +0 -3
package/README.md
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
# node-env-resolver
|
|
2
2
|
|
|
3
|
-
Type-safe environment variable resolution with zero dependencies.
|
|
3
|
+
Type-safe environment variable resolution with zero dependencies and ultra-small bundle size.
|
|
4
4
|
|
|
5
5
|
[](https://www.npmjs.com/package/node-env-resolver)
|
|
6
6
|
|
|
7
|
+
**Bundle Size:** ~3.6KB gzipped
|
|
8
|
+
|
|
7
9
|
## Install
|
|
8
10
|
|
|
9
11
|
```bash
|
|
@@ -17,14 +19,14 @@ import { resolve } from 'node-env-resolver';
|
|
|
17
19
|
|
|
18
20
|
const config = resolve({
|
|
19
21
|
PORT: 3000,
|
|
20
|
-
|
|
22
|
+
NODE_ENV: ['development', 'production', 'test'] as const,
|
|
21
23
|
DEBUG: false,
|
|
22
24
|
API_KEY: 'string?'
|
|
23
25
|
});
|
|
24
26
|
|
|
25
27
|
// config is fully typed
|
|
26
28
|
config.PORT; // number
|
|
27
|
-
config.
|
|
29
|
+
config.NODE_ENV; // 'development' | 'production' | 'test'
|
|
28
30
|
config.DEBUG; // boolean
|
|
29
31
|
config.API_KEY; // string | undefined
|
|
30
32
|
```
|
|
@@ -32,18 +34,18 @@ config.API_KEY; // string | undefined
|
|
|
32
34
|
## Quick start (async)
|
|
33
35
|
|
|
34
36
|
```ts
|
|
35
|
-
import { resolve } from 'node-env-resolver';
|
|
37
|
+
import { resolve, processEnv } from 'node-env-resolver';
|
|
36
38
|
|
|
37
|
-
const config = await resolve.with([
|
|
39
|
+
const config = await resolve.with([processEnv(), {
|
|
38
40
|
PORT: 3000,
|
|
39
|
-
|
|
41
|
+
NODE_ENV: ['development', 'production', 'test'] as const,
|
|
40
42
|
DEBUG: false,
|
|
41
43
|
API_KEY: 'string?'
|
|
42
44
|
}]);
|
|
43
45
|
|
|
44
46
|
// config is fully typed
|
|
45
47
|
config.PORT; // number
|
|
46
|
-
config.
|
|
48
|
+
config.NODE_ENV; // 'development' | 'production' | 'test'
|
|
47
49
|
config.DEBUG; // boolean
|
|
48
50
|
config.API_KEY; // string | undefined
|
|
49
51
|
```
|
|
@@ -56,8 +58,8 @@ If an environment variable is missing and has no default, validation fails:
|
|
|
56
58
|
|
|
57
59
|
```ts
|
|
58
60
|
const config = resolve({
|
|
59
|
-
DATABASE_URL: '
|
|
60
|
-
API_KEY: 'string'
|
|
61
|
+
DATABASE_URL: 'postgres', // Required PostgreSQL URL
|
|
62
|
+
API_KEY: 'string' // Required
|
|
61
63
|
});
|
|
62
64
|
```
|
|
63
65
|
|
|
@@ -100,29 +102,135 @@ config.NODE_ENV; // 'development' | 'production' | 'test'
|
|
|
100
102
|
config.LOG_LEVEL; // 'debug' | 'info' | 'warn' | 'error'
|
|
101
103
|
```
|
|
102
104
|
|
|
105
|
+
## Performance & Bundle Size
|
|
106
|
+
|
|
107
|
+
**Lightweight with powerful features.** The core library is **~3.6KB gzipped** with comprehensive validation and resolver capabilities - **38% smaller** than previous versions while maintaining all features.
|
|
108
|
+
|
|
109
|
+
### Efficient validation architecture
|
|
110
|
+
|
|
111
|
+
The library uses a two-tier validation strategy with lazy-loaded validators:
|
|
112
|
+
|
|
113
|
+
**Basic types** (~3.6KB core - inline validation):
|
|
114
|
+
- `string`, `number`, `boolean` (with min/max validation)
|
|
115
|
+
- `enum` (array validation)
|
|
116
|
+
- `pattern` (regex validation)
|
|
117
|
+
- `custom` (validator functions)
|
|
118
|
+
|
|
119
|
+
**Advanced types** (+1KB when used - lazy-loaded):
|
|
120
|
+
- Database URLs: `postgres`, `mysql`, `mongodb`, `redis`
|
|
121
|
+
- Web types: `http`, `https`, `url`, `email`
|
|
122
|
+
- Format types: `json`, `date`, `timestamp`, `port`
|
|
123
|
+
|
|
124
|
+
Advanced validators are lazy-loaded only when you use them, keeping the base bundle minimal. All types work both synchronously and asynchronously:
|
|
125
|
+
|
|
126
|
+
```ts
|
|
127
|
+
// Synchronous - works with all types
|
|
128
|
+
const config = resolve({
|
|
129
|
+
PORT: 3000,
|
|
130
|
+
DATABASE_URL: 'postgres',
|
|
131
|
+
API_URL: 'url',
|
|
132
|
+
NODE_ENV: ['development', 'production'] as const
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
// Also works with async resolvers
|
|
136
|
+
const config = await resolve.with([
|
|
137
|
+
awsSecrets(),
|
|
138
|
+
{ DATABASE_URL: 'postgres', API_URL: 'url' }
|
|
139
|
+
]);
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
### What's in the 3.6KB core?
|
|
143
|
+
- Core resolver logic (async/sync resolution)
|
|
144
|
+
- Schema normalization & type coercion
|
|
145
|
+
- Basic validation (string, number, boolean, enum, pattern, custom)
|
|
146
|
+
- Interpolation & policy checking
|
|
147
|
+
- Provenance tracking & error handling
|
|
148
|
+
|
|
149
|
+
### Code Splitting Architecture
|
|
150
|
+
The library uses intelligent code splitting to keep the core minimal:
|
|
151
|
+
- **Advanced validators** (~1KB): Lazy-loaded when using types like `url`, `email`, `postgres`
|
|
152
|
+
- **Audit logging** (~150 bytes): Lazy-loaded when `enableAudit: true`
|
|
153
|
+
- **dotenv parser** (~1.6KB): Separate import from `'node-env-resolver/resolvers'`
|
|
154
|
+
- **Utility functions** (~1KB): Separate import from `'node-env-resolver/utils'`
|
|
155
|
+
|
|
156
|
+
Only the code you actually use gets loaded!
|
|
157
|
+
|
|
158
|
+
### Audit Logging (Lazy Loaded)
|
|
159
|
+
|
|
160
|
+
Audit logging is lazy-loaded only when enabled, keeping the base bundle minimal:
|
|
161
|
+
|
|
162
|
+
```ts
|
|
163
|
+
// Base bundle
|
|
164
|
+
const config = resolve({ PORT: 3000 });
|
|
165
|
+
|
|
166
|
+
// Audit module loaded when enabled
|
|
167
|
+
const config = resolve({ PORT: 3000 }, { enableAudit: true });
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
### Optimized Module Structure
|
|
171
|
+
|
|
172
|
+
Import only what you need for optimal bundle size:
|
|
173
|
+
|
|
174
|
+
```ts
|
|
175
|
+
// Core (~3.6KB) - Main API
|
|
176
|
+
import { resolve, safeResolve, processEnv } from 'node-env-resolver';
|
|
177
|
+
|
|
178
|
+
// Resolvers (separate chunk) - .env file parsing
|
|
179
|
+
import { dotenv } from 'node-env-resolver/resolvers';
|
|
180
|
+
|
|
181
|
+
// Utils (separate chunk) - caching & retry logic
|
|
182
|
+
import { cached, retry, TTL, awsCache } from 'node-env-resolver/utils';
|
|
183
|
+
|
|
184
|
+
// Validators (separate chunk) - reusable validation functions
|
|
185
|
+
import { validateUrl, validateEmail } from 'node-env-resolver/validators';
|
|
186
|
+
|
|
187
|
+
// Integrations (separate packages)
|
|
188
|
+
import { resolveZod } from 'node-env-resolver/zod';
|
|
189
|
+
import { resolveValibot } from 'node-env-resolver/valibot';
|
|
190
|
+
import { awsSecrets } from 'node-env-resolver-aws';
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
**Most apps only need the core (~3.6KB)!**
|
|
194
|
+
|
|
103
195
|
## Built-in validators
|
|
104
196
|
|
|
197
|
+
All validator types work synchronously and asynchronously.
|
|
198
|
+
|
|
105
199
|
### Basic types
|
|
106
200
|
|
|
107
|
-
|
|
108
|
-
|
|
201
|
+
These types use inline validation:
|
|
202
|
+
|
|
203
|
+
- `'string'` - Any string value (with optional `min`/`max` length)
|
|
204
|
+
- `'number'` - Numeric value (coerced from string, with optional `min`/`max`)
|
|
109
205
|
- `'boolean'` - Boolean value (`'true'`/`'false'` coerced to boolean)
|
|
110
|
-
- `'
|
|
111
|
-
- `'
|
|
206
|
+
- `'enum'` - Array of allowed values
|
|
207
|
+
- `'pattern'` - Regex pattern validation
|
|
208
|
+
- `'custom'` - Custom validator function
|
|
209
|
+
|
|
210
|
+
### Advanced types
|
|
112
211
|
|
|
113
|
-
|
|
212
|
+
Advanced validators provide specialized validation and parsing:
|
|
114
213
|
|
|
115
|
-
|
|
116
|
-
- `'http'` - HTTP URL
|
|
117
|
-
- `'https'` - HTTPS URL
|
|
118
|
-
- `'email'` - Email address
|
|
214
|
+
**Network types:**
|
|
119
215
|
|
|
120
|
-
|
|
216
|
+
- `'url'` - Valid URL (returns string)
|
|
217
|
+
- `'http'` - HTTP or HTTPS URL (returns string)
|
|
218
|
+
- `'https'` - HTTPS-only URL (returns string)
|
|
219
|
+
- `'email'` - Email address (returns string)
|
|
121
220
|
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
- `'
|
|
125
|
-
- `'
|
|
221
|
+
**Database connection strings:**
|
|
222
|
+
|
|
223
|
+
- `'postgres'` or `'postgresql'` - PostgreSQL connection string (returns string)
|
|
224
|
+
- `'mysql'` - MySQL connection string (returns string)
|
|
225
|
+
- `'mongodb'` - MongoDB connection string (returns string)
|
|
226
|
+
- `'redis'` - Redis connection string (returns string)
|
|
227
|
+
|
|
228
|
+
**Format types:**
|
|
229
|
+
|
|
230
|
+
- `'json'` - JSON value (returns parsed object/array)
|
|
231
|
+
- `'port'` - Port number (returns number, 1-65535)
|
|
232
|
+
- `'date'` - ISO 8601 date string (returns string)
|
|
233
|
+
- `'timestamp'` - Unix timestamp (returns number)
|
|
126
234
|
|
|
127
235
|
All validators automatically handle type coercion from environment variable strings.
|
|
128
236
|
|
|
@@ -152,12 +260,43 @@ const config = resolve({
|
|
|
152
260
|
});
|
|
153
261
|
```
|
|
154
262
|
|
|
263
|
+
### Reusing built-in validators
|
|
264
|
+
|
|
265
|
+
For advanced use cases, you can import and compose the built-in validation functions:
|
|
266
|
+
|
|
267
|
+
```ts
|
|
268
|
+
import { resolve } from 'node-env-resolver';
|
|
269
|
+
import { validateUrl, validateEmail } from 'node-env-resolver/validators';
|
|
270
|
+
|
|
271
|
+
const validateMarketingUrl = (value: string): string => {
|
|
272
|
+
// Reuse built-in URL validator
|
|
273
|
+
const result = validateUrl(value);
|
|
274
|
+
if (!result.valid) {
|
|
275
|
+
throw new Error(result.error || 'Invalid URL');
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
// Add custom business logic
|
|
279
|
+
if (!value.includes('marketing.example.com')) {
|
|
280
|
+
throw new Error('Must be a marketing domain URL');
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
return value;
|
|
284
|
+
};
|
|
285
|
+
|
|
286
|
+
const config = resolve({
|
|
287
|
+
MARKETING_URL: validateMarketingUrl
|
|
288
|
+
});
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
Available validation functions: `validatePostgres`, `validateMysql`, `validateMongodb`, `validateRedis`, `validateHttp`, `validateHttps`, `validateUrl`, `validateEmail`, `validatePort`, `validateNumber`, `validateBoolean`, `validateJson`, `validateDate`, `validateTimestamp`.
|
|
292
|
+
|
|
155
293
|
## Multiple sources
|
|
156
294
|
|
|
157
295
|
Load configuration from multiple sources. By default, later sources override earlier ones:
|
|
158
296
|
|
|
159
297
|
```ts
|
|
160
|
-
import { resolve, processEnv
|
|
298
|
+
import { resolve, processEnv } from 'node-env-resolver';
|
|
299
|
+
import { dotenv } from 'node-env-resolver/resolvers';
|
|
161
300
|
|
|
162
301
|
const config = await resolve.with(
|
|
163
302
|
[processEnv(), {
|
|
@@ -165,7 +304,7 @@ const config = await resolve.with(
|
|
|
165
304
|
NODE_ENV: ['development', 'production'] as const,
|
|
166
305
|
}],
|
|
167
306
|
[dotenv(), {
|
|
168
|
-
DATABASE_URL: '
|
|
307
|
+
DATABASE_URL: 'postgres',
|
|
169
308
|
API_KEY: 'string',
|
|
170
309
|
}]
|
|
171
310
|
);
|
|
@@ -178,15 +317,15 @@ Use `priority` to control how resolvers merge values:
|
|
|
178
317
|
```ts
|
|
179
318
|
// priority: 'last' (default) - later resolvers override earlier ones
|
|
180
319
|
const config = await resolve.with(
|
|
181
|
-
[processEnv(), { DATABASE_URL: '
|
|
182
|
-
[awsSecrets(), { DATABASE_URL: '
|
|
320
|
+
[processEnv(), { DATABASE_URL: 'postgres' }],
|
|
321
|
+
[awsSecrets(), { DATABASE_URL: 'postgres' }]
|
|
183
322
|
// AWS wins
|
|
184
323
|
);
|
|
185
324
|
|
|
186
325
|
// priority: 'first' - earlier resolvers take precedence
|
|
187
326
|
const config = await resolve.with(
|
|
188
|
-
[dotenv(), { DATABASE_URL: '
|
|
189
|
-
[awsSecrets(), { DATABASE_URL: '
|
|
327
|
+
[dotenv(), { DATABASE_URL: 'postgres' }],
|
|
328
|
+
[awsSecrets(), { DATABASE_URL: 'postgres' }],
|
|
190
329
|
{ priority: 'first' }
|
|
191
330
|
// dotenv wins
|
|
192
331
|
);
|
|
@@ -194,6 +333,57 @@ const config = await resolve.with(
|
|
|
194
333
|
|
|
195
334
|
This is useful for development workflows where local overrides should take precedence over cloud secrets.
|
|
196
335
|
|
|
336
|
+
### Performance optimizations
|
|
337
|
+
|
|
338
|
+
The library includes two automatic performance optimizations:
|
|
339
|
+
|
|
340
|
+
#### 1. Early termination with `priority: 'first'`
|
|
341
|
+
|
|
342
|
+
When using `priority: 'first'`, resolvers are called sequentially, but execution **stops early** once all required environment variables are satisfied:
|
|
343
|
+
|
|
344
|
+
```ts
|
|
345
|
+
const config = await resolve.with(
|
|
346
|
+
[dotenv(), { DATABASE_URL: 'postgres', API_KEY: 'string', PORT: 3000 }],
|
|
347
|
+
[awsSecrets(), { DATABASE_URL: 'postgres', API_KEY: 'string', PORT: 3000 }],
|
|
348
|
+
[gcpSecrets(), { DATABASE_URL: 'postgres', API_KEY: 'string', PORT: 3000 }],
|
|
349
|
+
{ priority: 'first' }
|
|
350
|
+
);
|
|
351
|
+
// If dotenv() provides all required keys, awsSecrets() and gcpSecrets() are never called!
|
|
352
|
+
```
|
|
353
|
+
|
|
354
|
+
This is particularly valuable in development where:
|
|
355
|
+
|
|
356
|
+
- Local `.env` file contains all needed variables
|
|
357
|
+
- Expensive remote resolver calls (AWS Secrets Manager, Parameter Store, GCP Secret Manager) are skipped
|
|
358
|
+
- Startup time is significantly reduced
|
|
359
|
+
|
|
360
|
+
**What counts as "satisfied"?**
|
|
361
|
+
|
|
362
|
+
- Only **required** keys (no `optional: true`, no `default` value) trigger early termination
|
|
363
|
+
- Optional variables and variables with defaults do **not** prevent calling remaining resolvers
|
|
364
|
+
|
|
365
|
+
#### 2. Parallel execution with `priority: 'last'`
|
|
366
|
+
|
|
367
|
+
When using `priority: 'last'` (the default), all resolvers are called **in parallel** for maximum performance:
|
|
368
|
+
|
|
369
|
+
```ts
|
|
370
|
+
const config = await resolve.with(
|
|
371
|
+
[awsSecrets(), { DATABASE_URL: 'postgres' }], // 100ms
|
|
372
|
+
[awsParameterStore(), { API_KEY: 'string' }], // 100ms
|
|
373
|
+
[gcpSecrets(), { JWT_SECRET: 'string' }] // 100ms
|
|
374
|
+
// Default: priority: 'last'
|
|
375
|
+
);
|
|
376
|
+
// Total time: ~100ms (parallel) instead of ~300ms (sequential)
|
|
377
|
+
```
|
|
378
|
+
|
|
379
|
+
Since `priority: 'last'` means "last write wins", the order doesn't matter for conflict resolution, so all resolvers can run concurrently.
|
|
380
|
+
|
|
381
|
+
**Use cases:**
|
|
382
|
+
|
|
383
|
+
- Production environments loading from multiple remote secret stores
|
|
384
|
+
- Fetching different variables from different sources
|
|
385
|
+
- Significant speedup when resolvers have network latency
|
|
386
|
+
|
|
197
387
|
## Safe error handling
|
|
198
388
|
|
|
199
389
|
Like Zod's `safeParse()`, use `safeResolve()` to get a result object instead of throwing:
|
|
@@ -203,7 +393,7 @@ import { safeResolve } from 'node-env-resolver';
|
|
|
203
393
|
|
|
204
394
|
const result = safeResolve({
|
|
205
395
|
PORT: 'number',
|
|
206
|
-
DATABASE_URL: '
|
|
396
|
+
DATABASE_URL: 'postgres'
|
|
207
397
|
});
|
|
208
398
|
|
|
209
399
|
if (result.success) {
|
|
@@ -228,34 +418,36 @@ All functions have safe variants:
|
|
|
228
418
|
```ts
|
|
229
419
|
import { resolve } from 'node-env-resolver';
|
|
230
420
|
|
|
231
|
-
// Synchronous - no await needed
|
|
421
|
+
// Synchronous - no await needed, works with ALL types
|
|
232
422
|
const config = resolve({
|
|
233
423
|
PORT: 3000,
|
|
234
|
-
NODE_ENV: ['development', 'production'] as const
|
|
424
|
+
NODE_ENV: ['development', 'production'] as const,
|
|
425
|
+
API_KEY: 'string',
|
|
426
|
+
API_URL: 'url', // Advanced types work synchronously!
|
|
427
|
+
DATABASE_URL: 'postgres',
|
|
428
|
+
DEBUG: false
|
|
235
429
|
});
|
|
236
430
|
```
|
|
237
431
|
|
|
238
|
-
|
|
432
|
+
`resolve.with()` is **async** when using custom resolvers:
|
|
239
433
|
|
|
240
434
|
```ts
|
|
241
|
-
// Async - await required
|
|
435
|
+
// Async - await required when using custom resolvers
|
|
242
436
|
const config = await resolve.with(
|
|
243
437
|
[processEnv(), { PORT: 3000 }],
|
|
244
|
-
[dotenv(), { DATABASE_URL: 'url' }]
|
|
438
|
+
[dotenv(), { DATABASE_URL: 'postgres', API_URL: 'url' }]
|
|
245
439
|
);
|
|
246
440
|
```
|
|
247
441
|
|
|
248
|
-
Synchronous resolution works with shorthand syntax and Zod schemas (see Zod integration below).
|
|
249
|
-
|
|
250
442
|
## Advanced features
|
|
251
443
|
|
|
252
444
|
### Zod integration
|
|
253
445
|
|
|
254
|
-
Use Zod schemas
|
|
446
|
+
Use Zod schemas for powerful validation:
|
|
255
447
|
|
|
256
448
|
```ts
|
|
257
449
|
import { resolveZod } from 'node-env-resolver/zod';
|
|
258
|
-
import
|
|
450
|
+
import * as z from 'zod'; // ✅ Recommended import pattern (better tree-shaking)
|
|
259
451
|
|
|
260
452
|
const schema = z.object({
|
|
261
453
|
PORT: z.coerce.number().default(3000),
|
|
@@ -265,12 +457,90 @@ const schema = z.object({
|
|
|
265
457
|
});
|
|
266
458
|
|
|
267
459
|
const config = await resolveZod(schema);
|
|
460
|
+
// config.PORT is number, config.DATABASE_URL is string, etc.
|
|
461
|
+
```
|
|
462
|
+
|
|
463
|
+
**Safe error handling** with field-level errors:
|
|
464
|
+
|
|
465
|
+
```ts
|
|
466
|
+
import { safeResolveZod } from 'node-env-resolver/zod';
|
|
467
|
+
import * as z from 'zod';
|
|
468
|
+
|
|
469
|
+
const schema = z.object({
|
|
470
|
+
PORT: z.coerce.number(),
|
|
471
|
+
DATABASE_URL: z.string().url()
|
|
472
|
+
});
|
|
473
|
+
|
|
474
|
+
const result = await safeResolveZod(schema);
|
|
475
|
+
|
|
476
|
+
if (result.success) {
|
|
477
|
+
console.log(result.data.PORT);
|
|
478
|
+
} else {
|
|
479
|
+
console.error(result.error); // Summary message
|
|
480
|
+
result.issues.forEach(issue => {
|
|
481
|
+
// Field-level details: path, message, code
|
|
482
|
+
console.log(`${issue.path.join('.')}: ${issue.message}`);
|
|
483
|
+
});
|
|
484
|
+
}
|
|
268
485
|
```
|
|
269
486
|
|
|
270
|
-
Available functions
|
|
487
|
+
**Available functions:**
|
|
271
488
|
|
|
272
489
|
- `resolveZod()` - Async, throws on error
|
|
273
|
-
- `safeResolveZod()` - Async, returns
|
|
490
|
+
- `safeResolveZod()` - Async, returns `{ success, data/error, issues }`
|
|
491
|
+
- `resolveSyncZod()` - Sync, throws on error
|
|
492
|
+
- `safeResolveSyncZod()` - Sync, returns `{ success, data/error, issues }`
|
|
493
|
+
|
|
494
|
+
### Valibot integration
|
|
495
|
+
|
|
496
|
+
Use Valibot for lightweight, modular validation:
|
|
497
|
+
|
|
498
|
+
```ts
|
|
499
|
+
import { resolveValibot } from 'node-env-resolver/valibot';
|
|
500
|
+
import * as v from 'valibot'; // User imports valibot separately
|
|
501
|
+
|
|
502
|
+
const schema = v.object({
|
|
503
|
+
PORT: v.pipe(v.string(), v.transform(Number)),
|
|
504
|
+
DATABASE_URL: v.pipe(v.string(), v.url()),
|
|
505
|
+
NODE_ENV: v.picklist(['development', 'production', 'test']),
|
|
506
|
+
DEBUG: v.optional(v.pipe(v.string(), v.transform(Boolean))),
|
|
507
|
+
});
|
|
508
|
+
|
|
509
|
+
const config = await resolveValibot(schema);
|
|
510
|
+
```
|
|
511
|
+
|
|
512
|
+
**Safe error handling** with unified error format:
|
|
513
|
+
|
|
514
|
+
```ts
|
|
515
|
+
import { safeResolveValibot } from 'node-env-resolver/valibot';
|
|
516
|
+
import * as v from 'valibot';
|
|
517
|
+
|
|
518
|
+
const schema = v.object({
|
|
519
|
+
PORT: v.pipe(v.string(), v.transform(Number)),
|
|
520
|
+
DATABASE_URL: v.pipe(v.string(), v.url())
|
|
521
|
+
});
|
|
522
|
+
|
|
523
|
+
const result = await safeResolveValibot(schema);
|
|
524
|
+
|
|
525
|
+
if (result.success) {
|
|
526
|
+
console.log(result.data.PORT);
|
|
527
|
+
} else {
|
|
528
|
+
console.error(result.error); // Summary message
|
|
529
|
+
result.issues.forEach(issue => {
|
|
530
|
+
// Same format as Zod - consistent across validators!
|
|
531
|
+
console.log(`${issue.path.join('.')}: ${issue.message}`);
|
|
532
|
+
});
|
|
533
|
+
}
|
|
534
|
+
```
|
|
535
|
+
|
|
536
|
+
**Available functions:**
|
|
537
|
+
|
|
538
|
+
- `resolveValibot()` - Async, throws on error
|
|
539
|
+
- `safeResolveValibot()` - Async, returns `{ success, data/error, issues }`
|
|
540
|
+
- `resolveSyncValibot()` - Sync, throws on error
|
|
541
|
+
- `safeResolveSyncValibot()` - Sync, returns `{ success, data/error, issues }`
|
|
542
|
+
|
|
543
|
+
**Why unified errors?** Switch between Zod and Valibot without changing error handling code!
|
|
274
544
|
|
|
275
545
|
### Custom resolvers
|
|
276
546
|
|
|
@@ -289,7 +559,7 @@ const databaseResolver: Resolver = {
|
|
|
289
559
|
|
|
290
560
|
const config = await resolve.with(
|
|
291
561
|
[processEnv(), { PORT: 3000 }],
|
|
292
|
-
[databaseResolver, {
|
|
562
|
+
[databaseResolver, { API_KEY: 'string' }]
|
|
293
563
|
);
|
|
294
564
|
```
|
|
295
565
|
|
|
@@ -309,7 +579,7 @@ import { resolveSecrets } from 'node-env-resolver-aws';
|
|
|
309
579
|
const config = await resolveSecrets({
|
|
310
580
|
secretId: 'myapp/secrets'
|
|
311
581
|
}, {
|
|
312
|
-
DATABASE_URL: '
|
|
582
|
+
DATABASE_URL: 'postgres',
|
|
313
583
|
API_KEY: 'string'
|
|
314
584
|
});
|
|
315
585
|
```
|
|
@@ -322,8 +592,8 @@ import { awsSecrets, awsSsm } from 'node-env-resolver-aws';
|
|
|
322
592
|
|
|
323
593
|
const config = await resolve.with(
|
|
324
594
|
[processEnv(), { PORT: 3000 }],
|
|
325
|
-
[awsSecrets({ secretId: 'app/secrets' }), { DATABASE_URL: '
|
|
326
|
-
[awsSsm({ path: '/app/config' }), {
|
|
595
|
+
[awsSecrets({ secretId: 'app/secrets' }), { DATABASE_URL: 'postgres' }],
|
|
596
|
+
[awsSsm({ path: '/app/config' }), { API_KEY: 'string' }]
|
|
327
597
|
);
|
|
328
598
|
```
|
|
329
599
|
|
|
@@ -334,7 +604,8 @@ See the [AWS package documentation](../node-env-resolver-aws/README.md) for deta
|
|
|
334
604
|
Cache expensive operations like AWS API calls:
|
|
335
605
|
|
|
336
606
|
```ts
|
|
337
|
-
import { resolve
|
|
607
|
+
import { resolve } from 'node-env-resolver';
|
|
608
|
+
import { cached, TTL } from 'node-env-resolver/utils';
|
|
338
609
|
import { awsSecrets } from 'node-env-resolver-aws';
|
|
339
610
|
|
|
340
611
|
export const getConfig = async () => {
|
|
@@ -343,7 +614,7 @@ export const getConfig = async () => {
|
|
|
343
614
|
awsSecrets({ secretId: 'app/secrets' }),
|
|
344
615
|
{ ttl: TTL.minutes5 }
|
|
345
616
|
), {
|
|
346
|
-
DATABASE_URL: '
|
|
617
|
+
DATABASE_URL: 'postgres',
|
|
347
618
|
API_KEY: 'string',
|
|
348
619
|
}]
|
|
349
620
|
);
|
|
@@ -380,15 +651,17 @@ Performance:
|
|
|
380
651
|
|--------|------|-------------|
|
|
381
652
|
| `'string'` | `string` | Required string |
|
|
382
653
|
| `'string?'` | `string \| undefined` | Optional string |
|
|
383
|
-
| `'number'` | `number` | Required number |
|
|
654
|
+
| `'number'` | `number` | Required number (coerced from string) |
|
|
384
655
|
| `'number?'` | `number \| undefined` | Optional number |
|
|
385
|
-
| `'boolean'` | `boolean` | Required boolean |
|
|
656
|
+
| `'boolean'` | `boolean` | Required boolean (coerced from string) |
|
|
386
657
|
| `'boolean?'` | `boolean \| undefined` | Optional boolean |
|
|
387
|
-
| `'url'` | `string` |
|
|
388
|
-
| `'email'` | `string` |
|
|
389
|
-
| `'port'` | `number` |
|
|
390
|
-
| `'json'` | `unknown` | Parsed JSON |
|
|
391
|
-
| `'postgres'` | `string` | PostgreSQL
|
|
658
|
+
| `'url'` | `string` | Validated URL (returns string) |
|
|
659
|
+
| `'email'` | `string` | Validated email address |
|
|
660
|
+
| `'port'` | `number` | Validated port number (1-65535) |
|
|
661
|
+
| `'json'` | `unknown` | Parsed JSON (returns object/array) |
|
|
662
|
+
| `'postgres'` | `string` | Validated PostgreSQL URL |
|
|
663
|
+
| `'date'` | `string` | Validated ISO 8601 date |
|
|
664
|
+
| `'timestamp'` | `number` | Validated Unix timestamp |
|
|
392
665
|
| `3000` | `number` | Number with default |
|
|
393
666
|
| `false` | `boolean` | Boolean with default |
|
|
394
667
|
| `['a', 'b']` | `'a' \| 'b'` | Enum (requires `as const`) |
|
|
@@ -504,15 +777,15 @@ await resolve.with(
|
|
|
504
777
|
```ts
|
|
505
778
|
// Production: AWS secrets override process.env
|
|
506
779
|
await resolve.with(
|
|
507
|
-
[processEnv(), { DATABASE_URL: '
|
|
508
|
-
[awsSecrets(), { DATABASE_URL: '
|
|
780
|
+
[processEnv(), { DATABASE_URL: 'postgres' }],
|
|
781
|
+
[awsSecrets(), { DATABASE_URL: 'postgres' }]
|
|
509
782
|
// priority: 'last' (default) - AWS wins
|
|
510
783
|
);
|
|
511
784
|
|
|
512
785
|
// Development: Local .env overrides cloud
|
|
513
786
|
await resolve.with(
|
|
514
|
-
[dotenv(), { DATABASE_URL: '
|
|
515
|
-
[awsSecrets(), { DATABASE_URL: '
|
|
787
|
+
[dotenv(), { DATABASE_URL: 'postgres' }],
|
|
788
|
+
[awsSecrets(), { DATABASE_URL: 'postgres' }],
|
|
516
789
|
{ priority: 'first' } // dotenv wins
|
|
517
790
|
);
|
|
518
791
|
```
|
|
@@ -623,7 +896,7 @@ import { resolve } from 'node-env-resolver/nextjs';
|
|
|
623
896
|
|
|
624
897
|
export const env = resolve({
|
|
625
898
|
server: {
|
|
626
|
-
DATABASE_URL: '
|
|
899
|
+
DATABASE_URL: 'postgres',
|
|
627
900
|
API_SECRET: 'string',
|
|
628
901
|
},
|
|
629
902
|
client: {
|
|
@@ -635,7 +908,8 @@ export const env = resolve({
|
|
|
635
908
|
### AWS Lambda
|
|
636
909
|
|
|
637
910
|
```ts
|
|
638
|
-
import { resolve
|
|
911
|
+
import { resolve } from 'node-env-resolver';
|
|
912
|
+
import { cached, TTL } from 'node-env-resolver/utils';
|
|
639
913
|
import { awsSecrets } from 'node-env-resolver-aws';
|
|
640
914
|
|
|
641
915
|
const getConfig = async () => {
|
|
@@ -644,7 +918,7 @@ const getConfig = async () => {
|
|
|
644
918
|
awsSecrets({ secretId: 'lambda/config' }),
|
|
645
919
|
{ ttl: TTL.minutes5 }
|
|
646
920
|
), {
|
|
647
|
-
DATABASE_URL: '
|
|
921
|
+
DATABASE_URL: 'postgres',
|
|
648
922
|
}]
|
|
649
923
|
);
|
|
650
924
|
};
|
|
@@ -669,7 +943,7 @@ By default, `.env` files are **completely blocked in production** for security.
|
|
|
669
943
|
// In production (NODE_ENV=production)
|
|
670
944
|
const config = await resolve.with(
|
|
671
945
|
[dotenv(), {
|
|
672
|
-
DATABASE_URL: '
|
|
946
|
+
DATABASE_URL: 'postgres',
|
|
673
947
|
}]
|
|
674
948
|
);
|
|
675
949
|
// ❌ Throws: "DATABASE_URL cannot be sourced from .env files in production"
|
|
@@ -680,7 +954,7 @@ const config = await resolve.with(
|
|
|
680
954
|
```ts
|
|
681
955
|
const config = await resolve.with(
|
|
682
956
|
[dotenv(), {
|
|
683
|
-
DATABASE_URL: '
|
|
957
|
+
DATABASE_URL: 'postgres',
|
|
684
958
|
}],
|
|
685
959
|
{
|
|
686
960
|
policies: {
|
|
@@ -696,7 +970,7 @@ const config = await resolve.with(
|
|
|
696
970
|
const config = await resolve.with(
|
|
697
971
|
[dotenv(), {
|
|
698
972
|
PORT: 3000,
|
|
699
|
-
DATABASE_URL: '
|
|
973
|
+
DATABASE_URL: 'postgres',
|
|
700
974
|
}],
|
|
701
975
|
{
|
|
702
976
|
policies: {
|
|
@@ -738,7 +1012,8 @@ const config = await resolve.with(
|
|
|
738
1012
|
**Use case:** Ensure production secrets only come from AWS Secrets Manager, never from `.env` or `process.env`:
|
|
739
1013
|
|
|
740
1014
|
```ts
|
|
741
|
-
import { resolve, processEnv
|
|
1015
|
+
import { resolve, processEnv } from 'node-env-resolver';
|
|
1016
|
+
import { cached, TTL } from 'node-env-resolver/utils';
|
|
742
1017
|
import { awsSecrets } from 'node-env-resolver-aws';
|
|
743
1018
|
|
|
744
1019
|
const config = await resolve.with(
|
|
@@ -798,7 +1073,7 @@ import { resolve, getAuditLog, clearAuditLog } from 'node-env-resolver';
|
|
|
798
1073
|
|
|
799
1074
|
// Option 1: Explicitly enable (works in any environment)
|
|
800
1075
|
const config = resolve({
|
|
801
|
-
DATABASE_URL: '
|
|
1076
|
+
DATABASE_URL: 'postgres',
|
|
802
1077
|
API_KEY: 'string',
|
|
803
1078
|
}, {
|
|
804
1079
|
enableAudit: true // ← Enable audit logging
|
|
@@ -820,7 +1095,7 @@ clearAuditLog();
|
|
|
820
1095
|
process.env.NODE_ENV = 'production';
|
|
821
1096
|
|
|
822
1097
|
const config = resolve({
|
|
823
|
-
DATABASE_URL: '
|
|
1098
|
+
DATABASE_URL: 'postgres',
|
|
824
1099
|
});
|
|
825
1100
|
|
|
826
1101
|
// Audit automatically enabled in production
|
|
@@ -842,12 +1117,13 @@ console.log(getAuditLog());
|
|
|
842
1117
|
Audit logs include cache metadata to help monitor performance:
|
|
843
1118
|
|
|
844
1119
|
```ts
|
|
845
|
-
import { resolve,
|
|
1120
|
+
import { resolve, getAuditLog } from 'node-env-resolver';
|
|
1121
|
+
import { cached } from 'node-env-resolver/utils';
|
|
846
1122
|
import { awsSecrets } from 'node-env-resolver-aws';
|
|
847
1123
|
|
|
848
1124
|
const config = await resolve.with(
|
|
849
1125
|
[cached(awsSecrets({ secretId: 'prod/db' }), { ttl: 300000 }), {
|
|
850
|
-
DATABASE_URL: '
|
|
1126
|
+
DATABASE_URL: 'postgres',
|
|
851
1127
|
}],
|
|
852
1128
|
{ enableAudit: true }
|
|
853
1129
|
);
|
package/dist/audit.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
export{c as clearAuditLog,b as getAuditLog,a as logAuditEvent}from'./chunk-DAGPFV3V.js';import'./chunk-WOT6VMZA.js';
|
package/dist/builder.d.ts
CHANGED
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
* .resolve();
|
|
14
14
|
* ```
|
|
15
15
|
*/
|
|
16
|
-
import type { Resolver, SimpleEnvSchema, InferSimpleSchema, ResolveOptions } from './types
|
|
16
|
+
import type { Resolver, SimpleEnvSchema, InferSimpleSchema, ResolveOptions } from './types';
|
|
17
17
|
export type MergeSchemas<T, U> = Omit<T, keyof U> & U;
|
|
18
18
|
/**
|
|
19
19
|
* EnvBuilder class - accumulates resolvers and schemas, then resolves
|