node-env-resolver 2.0.1 → 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 +230 -90
- 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/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
|
@@ -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:** ~6KB gzipped
|
|
7
|
+
**Bundle Size:** ~3.6KB gzipped
|
|
8
8
|
|
|
9
9
|
## Install
|
|
10
10
|
|
|
@@ -19,14 +19,14 @@ import { resolve } from 'node-env-resolver';
|
|
|
19
19
|
|
|
20
20
|
const config = resolve({
|
|
21
21
|
PORT: 3000,
|
|
22
|
-
|
|
22
|
+
NODE_ENV: ['development', 'production', 'test'] as const,
|
|
23
23
|
DEBUG: false,
|
|
24
24
|
API_KEY: 'string?'
|
|
25
25
|
});
|
|
26
26
|
|
|
27
27
|
// config is fully typed
|
|
28
28
|
config.PORT; // number
|
|
29
|
-
config.
|
|
29
|
+
config.NODE_ENV; // 'development' | 'production' | 'test'
|
|
30
30
|
config.DEBUG; // boolean
|
|
31
31
|
config.API_KEY; // string | undefined
|
|
32
32
|
```
|
|
@@ -34,18 +34,18 @@ config.API_KEY; // string | undefined
|
|
|
34
34
|
## Quick start (async)
|
|
35
35
|
|
|
36
36
|
```ts
|
|
37
|
-
import { resolve } from 'node-env-resolver';
|
|
37
|
+
import { resolve, processEnv } from 'node-env-resolver';
|
|
38
38
|
|
|
39
|
-
const config = await resolve.with([
|
|
39
|
+
const config = await resolve.with([processEnv(), {
|
|
40
40
|
PORT: 3000,
|
|
41
|
-
|
|
41
|
+
NODE_ENV: ['development', 'production', 'test'] as const,
|
|
42
42
|
DEBUG: false,
|
|
43
43
|
API_KEY: 'string?'
|
|
44
44
|
}]);
|
|
45
45
|
|
|
46
46
|
// config is fully typed
|
|
47
47
|
config.PORT; // number
|
|
48
|
-
config.
|
|
48
|
+
config.NODE_ENV; // 'development' | 'production' | 'test'
|
|
49
49
|
config.DEBUG; // boolean
|
|
50
50
|
config.API_KEY; // string | undefined
|
|
51
51
|
```
|
|
@@ -58,8 +58,8 @@ If an environment variable is missing and has no default, validation fails:
|
|
|
58
58
|
|
|
59
59
|
```ts
|
|
60
60
|
const config = resolve({
|
|
61
|
-
DATABASE_URL: '
|
|
62
|
-
API_KEY: 'string'
|
|
61
|
+
DATABASE_URL: 'postgres', // Required PostgreSQL URL
|
|
62
|
+
API_KEY: 'string' // Required
|
|
63
63
|
});
|
|
64
64
|
```
|
|
65
65
|
|
|
@@ -104,36 +104,32 @@ config.LOG_LEVEL; // 'debug' | 'info' | 'warn' | 'error'
|
|
|
104
104
|
|
|
105
105
|
## Performance & Bundle Size
|
|
106
106
|
|
|
107
|
-
**
|
|
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
108
|
|
|
109
|
-
###
|
|
109
|
+
### Efficient validation architecture
|
|
110
110
|
|
|
111
|
-
|
|
111
|
+
The library uses a two-tier validation strategy with lazy-loaded validators:
|
|
112
|
+
|
|
113
|
+
**Basic types** (~3.6KB core - inline validation):
|
|
112
114
|
- `string`, `number`, `boolean` (with min/max validation)
|
|
113
115
|
- `enum` (array validation)
|
|
114
116
|
- `pattern` (regex validation)
|
|
115
117
|
- `custom` (validator functions)
|
|
116
118
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
DEBUG: false,
|
|
122
|
-
NODE_ENV: ['development', 'production'] as const,
|
|
123
|
-
API_KEY: 'string'
|
|
124
|
-
});
|
|
125
|
-
```
|
|
126
|
-
|
|
127
|
-
### All types work synchronously
|
|
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`
|
|
128
123
|
|
|
129
|
-
|
|
124
|
+
Advanced validators are lazy-loaded only when you use them, keeping the base bundle minimal. All types work both synchronously and asynchronously:
|
|
130
125
|
|
|
131
126
|
```ts
|
|
132
127
|
// Synchronous - works with all types
|
|
133
128
|
const config = resolve({
|
|
129
|
+
PORT: 3000,
|
|
134
130
|
DATABASE_URL: 'postgres',
|
|
135
131
|
API_URL: 'url',
|
|
136
|
-
|
|
132
|
+
NODE_ENV: ['development', 'production'] as const
|
|
137
133
|
});
|
|
138
134
|
|
|
139
135
|
// Also works with async resolvers
|
|
@@ -143,37 +139,66 @@ const config = await resolve.with([
|
|
|
143
139
|
]);
|
|
144
140
|
```
|
|
145
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
|
+
|
|
146
158
|
### Audit Logging (Lazy Loaded)
|
|
147
159
|
|
|
148
|
-
Audit logging is
|
|
160
|
+
Audit logging is lazy-loaded only when enabled, keeping the base bundle minimal:
|
|
149
161
|
|
|
150
162
|
```ts
|
|
151
|
-
//
|
|
163
|
+
// Base bundle
|
|
152
164
|
const config = resolve({ PORT: 3000 });
|
|
153
165
|
|
|
154
|
-
// Audit loaded when enabled
|
|
166
|
+
// Audit module loaded when enabled
|
|
155
167
|
const config = resolve({ PORT: 3000 }, { enableAudit: true });
|
|
156
168
|
```
|
|
157
169
|
|
|
158
|
-
###
|
|
170
|
+
### Optimized Module Structure
|
|
159
171
|
|
|
160
|
-
|
|
172
|
+
Import only what you need for optimal bundle size:
|
|
161
173
|
|
|
162
174
|
```ts
|
|
163
|
-
//
|
|
164
|
-
import { resolve } from 'node-env-resolver';
|
|
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';
|
|
165
180
|
|
|
166
|
-
|
|
167
|
-
import {
|
|
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';
|
|
168
191
|
```
|
|
169
192
|
|
|
193
|
+
**Most apps only need the core (~3.6KB)!**
|
|
194
|
+
|
|
170
195
|
## Built-in validators
|
|
171
196
|
|
|
172
197
|
All validator types work synchronously and asynchronously.
|
|
173
198
|
|
|
174
199
|
### Basic types
|
|
175
200
|
|
|
176
|
-
These types use inline validation
|
|
201
|
+
These types use inline validation:
|
|
177
202
|
|
|
178
203
|
- `'string'` - Any string value (with optional `min`/`max` length)
|
|
179
204
|
- `'number'` - Numeric value (coerced from string, with optional `min`/`max`)
|
|
@@ -188,24 +213,24 @@ Advanced validators provide specialized validation and parsing:
|
|
|
188
213
|
|
|
189
214
|
**Network types:**
|
|
190
215
|
|
|
191
|
-
- `'url'` - Valid URL (returns
|
|
192
|
-
- `'http'` - HTTP or HTTPS URL
|
|
193
|
-
- `'https'` - HTTPS-only URL (
|
|
194
|
-
- `'email'` - Email address
|
|
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)
|
|
195
220
|
|
|
196
221
|
**Database connection strings:**
|
|
197
222
|
|
|
198
|
-
- `'postgres'` or `'postgresql'` - PostgreSQL connection string
|
|
199
|
-
- `'mysql'` - MySQL connection string
|
|
200
|
-
- `'mongodb'` - MongoDB connection string
|
|
201
|
-
- `'redis'` - Redis connection string
|
|
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)
|
|
202
227
|
|
|
203
228
|
**Format types:**
|
|
204
229
|
|
|
205
|
-
- `'json'` - JSON value (parsed
|
|
206
|
-
- `'port'` - Port number (1-65535)
|
|
207
|
-
- `'date'` - ISO 8601 date string
|
|
208
|
-
- `'timestamp'` - Unix timestamp
|
|
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)
|
|
209
234
|
|
|
210
235
|
All validators automatically handle type coercion from environment variable strings.
|
|
211
236
|
|
|
@@ -235,12 +260,43 @@ const config = resolve({
|
|
|
235
260
|
});
|
|
236
261
|
```
|
|
237
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
|
+
|
|
238
293
|
## Multiple sources
|
|
239
294
|
|
|
240
295
|
Load configuration from multiple sources. By default, later sources override earlier ones:
|
|
241
296
|
|
|
242
297
|
```ts
|
|
243
|
-
import { resolve, processEnv
|
|
298
|
+
import { resolve, processEnv } from 'node-env-resolver';
|
|
299
|
+
import { dotenv } from 'node-env-resolver/resolvers';
|
|
244
300
|
|
|
245
301
|
const config = await resolve.with(
|
|
246
302
|
[processEnv(), {
|
|
@@ -248,7 +304,7 @@ const config = await resolve.with(
|
|
|
248
304
|
NODE_ENV: ['development', 'production'] as const,
|
|
249
305
|
}],
|
|
250
306
|
[dotenv(), {
|
|
251
|
-
DATABASE_URL: '
|
|
307
|
+
DATABASE_URL: 'postgres',
|
|
252
308
|
API_KEY: 'string',
|
|
253
309
|
}]
|
|
254
310
|
);
|
|
@@ -261,15 +317,15 @@ Use `priority` to control how resolvers merge values:
|
|
|
261
317
|
```ts
|
|
262
318
|
// priority: 'last' (default) - later resolvers override earlier ones
|
|
263
319
|
const config = await resolve.with(
|
|
264
|
-
[processEnv(), { DATABASE_URL: '
|
|
265
|
-
[awsSecrets(), { DATABASE_URL: '
|
|
320
|
+
[processEnv(), { DATABASE_URL: 'postgres' }],
|
|
321
|
+
[awsSecrets(), { DATABASE_URL: 'postgres' }]
|
|
266
322
|
// AWS wins
|
|
267
323
|
);
|
|
268
324
|
|
|
269
325
|
// priority: 'first' - earlier resolvers take precedence
|
|
270
326
|
const config = await resolve.with(
|
|
271
|
-
[dotenv(), { DATABASE_URL: '
|
|
272
|
-
[awsSecrets(), { DATABASE_URL: '
|
|
327
|
+
[dotenv(), { DATABASE_URL: 'postgres' }],
|
|
328
|
+
[awsSecrets(), { DATABASE_URL: 'postgres' }],
|
|
273
329
|
{ priority: 'first' }
|
|
274
330
|
// dotenv wins
|
|
275
331
|
);
|
|
@@ -287,9 +343,9 @@ When using `priority: 'first'`, resolvers are called sequentially, but execution
|
|
|
287
343
|
|
|
288
344
|
```ts
|
|
289
345
|
const config = await resolve.with(
|
|
290
|
-
[dotenv(), { DATABASE_URL: '
|
|
291
|
-
[awsSecrets(), { DATABASE_URL: '
|
|
292
|
-
[gcpSecrets(), { DATABASE_URL: '
|
|
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 }],
|
|
293
349
|
{ priority: 'first' }
|
|
294
350
|
);
|
|
295
351
|
// If dotenv() provides all required keys, awsSecrets() and gcpSecrets() are never called!
|
|
@@ -312,7 +368,7 @@ When using `priority: 'last'` (the default), all resolvers are called **in paral
|
|
|
312
368
|
|
|
313
369
|
```ts
|
|
314
370
|
const config = await resolve.with(
|
|
315
|
-
[awsSecrets(), { DATABASE_URL: '
|
|
371
|
+
[awsSecrets(), { DATABASE_URL: 'postgres' }], // 100ms
|
|
316
372
|
[awsParameterStore(), { API_KEY: 'string' }], // 100ms
|
|
317
373
|
[gcpSecrets(), { JWT_SECRET: 'string' }] // 100ms
|
|
318
374
|
// Default: priority: 'last'
|
|
@@ -337,7 +393,7 @@ import { safeResolve } from 'node-env-resolver';
|
|
|
337
393
|
|
|
338
394
|
const result = safeResolve({
|
|
339
395
|
PORT: 'number',
|
|
340
|
-
DATABASE_URL: '
|
|
396
|
+
DATABASE_URL: 'postgres'
|
|
341
397
|
});
|
|
342
398
|
|
|
343
399
|
if (result.success) {
|
|
@@ -387,11 +443,11 @@ const config = await resolve.with(
|
|
|
387
443
|
|
|
388
444
|
### Zod integration
|
|
389
445
|
|
|
390
|
-
Use Zod schemas
|
|
446
|
+
Use Zod schemas for powerful validation:
|
|
391
447
|
|
|
392
448
|
```ts
|
|
393
449
|
import { resolveZod } from 'node-env-resolver/zod';
|
|
394
|
-
import
|
|
450
|
+
import * as z from 'zod'; // ✅ Recommended import pattern (better tree-shaking)
|
|
395
451
|
|
|
396
452
|
const schema = z.object({
|
|
397
453
|
PORT: z.coerce.number().default(3000),
|
|
@@ -401,12 +457,90 @@ const schema = z.object({
|
|
|
401
457
|
});
|
|
402
458
|
|
|
403
459
|
const config = await resolveZod(schema);
|
|
460
|
+
// config.PORT is number, config.DATABASE_URL is string, etc.
|
|
404
461
|
```
|
|
405
462
|
|
|
406
|
-
|
|
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
|
+
}
|
|
485
|
+
```
|
|
486
|
+
|
|
487
|
+
**Available functions:**
|
|
407
488
|
|
|
408
489
|
- `resolveZod()` - Async, throws on error
|
|
409
|
-
- `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!
|
|
410
544
|
|
|
411
545
|
### Custom resolvers
|
|
412
546
|
|
|
@@ -425,7 +559,7 @@ const databaseResolver: Resolver = {
|
|
|
425
559
|
|
|
426
560
|
const config = await resolve.with(
|
|
427
561
|
[processEnv(), { PORT: 3000 }],
|
|
428
|
-
[databaseResolver, {
|
|
562
|
+
[databaseResolver, { API_KEY: 'string' }]
|
|
429
563
|
);
|
|
430
564
|
```
|
|
431
565
|
|
|
@@ -445,7 +579,7 @@ import { resolveSecrets } from 'node-env-resolver-aws';
|
|
|
445
579
|
const config = await resolveSecrets({
|
|
446
580
|
secretId: 'myapp/secrets'
|
|
447
581
|
}, {
|
|
448
|
-
DATABASE_URL: '
|
|
582
|
+
DATABASE_URL: 'postgres',
|
|
449
583
|
API_KEY: 'string'
|
|
450
584
|
});
|
|
451
585
|
```
|
|
@@ -458,8 +592,8 @@ import { awsSecrets, awsSsm } from 'node-env-resolver-aws';
|
|
|
458
592
|
|
|
459
593
|
const config = await resolve.with(
|
|
460
594
|
[processEnv(), { PORT: 3000 }],
|
|
461
|
-
[awsSecrets({ secretId: 'app/secrets' }), { DATABASE_URL: '
|
|
462
|
-
[awsSsm({ path: '/app/config' }), {
|
|
595
|
+
[awsSecrets({ secretId: 'app/secrets' }), { DATABASE_URL: 'postgres' }],
|
|
596
|
+
[awsSsm({ path: '/app/config' }), { API_KEY: 'string' }]
|
|
463
597
|
);
|
|
464
598
|
```
|
|
465
599
|
|
|
@@ -470,7 +604,8 @@ See the [AWS package documentation](../node-env-resolver-aws/README.md) for deta
|
|
|
470
604
|
Cache expensive operations like AWS API calls:
|
|
471
605
|
|
|
472
606
|
```ts
|
|
473
|
-
import { resolve
|
|
607
|
+
import { resolve } from 'node-env-resolver';
|
|
608
|
+
import { cached, TTL } from 'node-env-resolver/utils';
|
|
474
609
|
import { awsSecrets } from 'node-env-resolver-aws';
|
|
475
610
|
|
|
476
611
|
export const getConfig = async () => {
|
|
@@ -479,7 +614,7 @@ export const getConfig = async () => {
|
|
|
479
614
|
awsSecrets({ secretId: 'app/secrets' }),
|
|
480
615
|
{ ttl: TTL.minutes5 }
|
|
481
616
|
), {
|
|
482
|
-
DATABASE_URL: '
|
|
617
|
+
DATABASE_URL: 'postgres',
|
|
483
618
|
API_KEY: 'string',
|
|
484
619
|
}]
|
|
485
620
|
);
|
|
@@ -516,15 +651,17 @@ Performance:
|
|
|
516
651
|
|--------|------|-------------|
|
|
517
652
|
| `'string'` | `string` | Required string |
|
|
518
653
|
| `'string?'` | `string \| undefined` | Optional string |
|
|
519
|
-
| `'number'` | `number` | Required number |
|
|
654
|
+
| `'number'` | `number` | Required number (coerced from string) |
|
|
520
655
|
| `'number?'` | `number \| undefined` | Optional number |
|
|
521
|
-
| `'boolean'` | `boolean` | Required boolean |
|
|
656
|
+
| `'boolean'` | `boolean` | Required boolean (coerced from string) |
|
|
522
657
|
| `'boolean?'` | `boolean \| undefined` | Optional boolean |
|
|
523
|
-
| `'url'` | `string` |
|
|
524
|
-
| `'email'` | `string` |
|
|
525
|
-
| `'port'` | `number` |
|
|
526
|
-
| `'json'` | `unknown` | Parsed JSON |
|
|
527
|
-
| `'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 |
|
|
528
665
|
| `3000` | `number` | Number with default |
|
|
529
666
|
| `false` | `boolean` | Boolean with default |
|
|
530
667
|
| `['a', 'b']` | `'a' \| 'b'` | Enum (requires `as const`) |
|
|
@@ -640,15 +777,15 @@ await resolve.with(
|
|
|
640
777
|
```ts
|
|
641
778
|
// Production: AWS secrets override process.env
|
|
642
779
|
await resolve.with(
|
|
643
|
-
[processEnv(), { DATABASE_URL: '
|
|
644
|
-
[awsSecrets(), { DATABASE_URL: '
|
|
780
|
+
[processEnv(), { DATABASE_URL: 'postgres' }],
|
|
781
|
+
[awsSecrets(), { DATABASE_URL: 'postgres' }]
|
|
645
782
|
// priority: 'last' (default) - AWS wins
|
|
646
783
|
);
|
|
647
784
|
|
|
648
785
|
// Development: Local .env overrides cloud
|
|
649
786
|
await resolve.with(
|
|
650
|
-
[dotenv(), { DATABASE_URL: '
|
|
651
|
-
[awsSecrets(), { DATABASE_URL: '
|
|
787
|
+
[dotenv(), { DATABASE_URL: 'postgres' }],
|
|
788
|
+
[awsSecrets(), { DATABASE_URL: 'postgres' }],
|
|
652
789
|
{ priority: 'first' } // dotenv wins
|
|
653
790
|
);
|
|
654
791
|
```
|
|
@@ -759,7 +896,7 @@ import { resolve } from 'node-env-resolver/nextjs';
|
|
|
759
896
|
|
|
760
897
|
export const env = resolve({
|
|
761
898
|
server: {
|
|
762
|
-
DATABASE_URL: '
|
|
899
|
+
DATABASE_URL: 'postgres',
|
|
763
900
|
API_SECRET: 'string',
|
|
764
901
|
},
|
|
765
902
|
client: {
|
|
@@ -771,7 +908,8 @@ export const env = resolve({
|
|
|
771
908
|
### AWS Lambda
|
|
772
909
|
|
|
773
910
|
```ts
|
|
774
|
-
import { resolve
|
|
911
|
+
import { resolve } from 'node-env-resolver';
|
|
912
|
+
import { cached, TTL } from 'node-env-resolver/utils';
|
|
775
913
|
import { awsSecrets } from 'node-env-resolver-aws';
|
|
776
914
|
|
|
777
915
|
const getConfig = async () => {
|
|
@@ -780,7 +918,7 @@ const getConfig = async () => {
|
|
|
780
918
|
awsSecrets({ secretId: 'lambda/config' }),
|
|
781
919
|
{ ttl: TTL.minutes5 }
|
|
782
920
|
), {
|
|
783
|
-
DATABASE_URL: '
|
|
921
|
+
DATABASE_URL: 'postgres',
|
|
784
922
|
}]
|
|
785
923
|
);
|
|
786
924
|
};
|
|
@@ -805,7 +943,7 @@ By default, `.env` files are **completely blocked in production** for security.
|
|
|
805
943
|
// In production (NODE_ENV=production)
|
|
806
944
|
const config = await resolve.with(
|
|
807
945
|
[dotenv(), {
|
|
808
|
-
DATABASE_URL: '
|
|
946
|
+
DATABASE_URL: 'postgres',
|
|
809
947
|
}]
|
|
810
948
|
);
|
|
811
949
|
// ❌ Throws: "DATABASE_URL cannot be sourced from .env files in production"
|
|
@@ -816,7 +954,7 @@ const config = await resolve.with(
|
|
|
816
954
|
```ts
|
|
817
955
|
const config = await resolve.with(
|
|
818
956
|
[dotenv(), {
|
|
819
|
-
DATABASE_URL: '
|
|
957
|
+
DATABASE_URL: 'postgres',
|
|
820
958
|
}],
|
|
821
959
|
{
|
|
822
960
|
policies: {
|
|
@@ -832,7 +970,7 @@ const config = await resolve.with(
|
|
|
832
970
|
const config = await resolve.with(
|
|
833
971
|
[dotenv(), {
|
|
834
972
|
PORT: 3000,
|
|
835
|
-
DATABASE_URL: '
|
|
973
|
+
DATABASE_URL: 'postgres',
|
|
836
974
|
}],
|
|
837
975
|
{
|
|
838
976
|
policies: {
|
|
@@ -874,7 +1012,8 @@ const config = await resolve.with(
|
|
|
874
1012
|
**Use case:** Ensure production secrets only come from AWS Secrets Manager, never from `.env` or `process.env`:
|
|
875
1013
|
|
|
876
1014
|
```ts
|
|
877
|
-
import { resolve, processEnv
|
|
1015
|
+
import { resolve, processEnv } from 'node-env-resolver';
|
|
1016
|
+
import { cached, TTL } from 'node-env-resolver/utils';
|
|
878
1017
|
import { awsSecrets } from 'node-env-resolver-aws';
|
|
879
1018
|
|
|
880
1019
|
const config = await resolve.with(
|
|
@@ -934,7 +1073,7 @@ import { resolve, getAuditLog, clearAuditLog } from 'node-env-resolver';
|
|
|
934
1073
|
|
|
935
1074
|
// Option 1: Explicitly enable (works in any environment)
|
|
936
1075
|
const config = resolve({
|
|
937
|
-
DATABASE_URL: '
|
|
1076
|
+
DATABASE_URL: 'postgres',
|
|
938
1077
|
API_KEY: 'string',
|
|
939
1078
|
}, {
|
|
940
1079
|
enableAudit: true // ← Enable audit logging
|
|
@@ -956,7 +1095,7 @@ clearAuditLog();
|
|
|
956
1095
|
process.env.NODE_ENV = 'production';
|
|
957
1096
|
|
|
958
1097
|
const config = resolve({
|
|
959
|
-
DATABASE_URL: '
|
|
1098
|
+
DATABASE_URL: 'postgres',
|
|
960
1099
|
});
|
|
961
1100
|
|
|
962
1101
|
// Audit automatically enabled in production
|
|
@@ -978,12 +1117,13 @@ console.log(getAuditLog());
|
|
|
978
1117
|
Audit logs include cache metadata to help monitor performance:
|
|
979
1118
|
|
|
980
1119
|
```ts
|
|
981
|
-
import { resolve,
|
|
1120
|
+
import { resolve, getAuditLog } from 'node-env-resolver';
|
|
1121
|
+
import { cached } from 'node-env-resolver/utils';
|
|
982
1122
|
import { awsSecrets } from 'node-env-resolver-aws';
|
|
983
1123
|
|
|
984
1124
|
const config = await resolve.with(
|
|
985
1125
|
[cached(awsSecrets({ secretId: 'prod/db' }), { ttl: 300000 }), {
|
|
986
|
-
DATABASE_URL: '
|
|
1126
|
+
DATABASE_URL: 'postgres',
|
|
987
1127
|
}],
|
|
988
1128
|
{ enableAudit: true }
|
|
989
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
|
package/dist/builder.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"builder.d.ts","sourceRoot":"","sources":["../src/builder.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,KAAK,EACV,QAAQ,EACR,eAAe,EAEf,iBAAiB,EACjB,cAAc,EACf,MAAM,
|
|
1
|
+
{"version":3,"file":"builder.d.ts","sourceRoot":"","sources":["../src/builder.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,KAAK,EACV,QAAQ,EACR,eAAe,EAEf,iBAAiB,EACjB,cAAc,EACf,MAAM,SAAS,CAAC;AAIjB,MAAM,MAAM,YAAY,CAAC,CAAC,EAAE,CAAC,IAAI,IAAI,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC,GAAG,CAAC,CAAC;AAUtD;;GAEG;AACH,qBAAa,UAAU,CAAC,YAAY,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;IAIhE,OAAO,CAAC,WAAW;IACnB,OAAO,CAAC,OAAO;IAJjB,OAAO,CAAC,MAAM,CAAwC;gBAG5C,WAAW,EAAE,eAAe,EAC5B,OAAO,GAAE,OAAO,CAAC,cAAc,CAAM;IAG/C;;;OAGG;IACH,IAAI,CAAC,IAAI,SAAS,eAAe,EAC/B,QAAQ,EAAE,QAAQ,EAClB,MAAM,EAAE,IAAI,GACX,UAAU,CAAC,YAAY,CAAC,YAAY,EAAE,iBAAiB,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I,CAAC,IAAI,SAAS,eAAe,EAC/B,QAAQ,EAAE,QAAQ,EAClB,MAAM,EAAE,IAAI,GACX,qBAAqB,CAAC,YAAY,CAAC,YAAY,EAAE,iBAAiB,CAAC,IAAI,CAAC,CAAC,CAAC;IAW7E;;;OAGG;IACH,OAAO,IAAI,YAAY;IASvB;;OAEG;IACH,OAAO,IAAI,YAAY;IAIvB;;OAEG;IACH,MAAM,IAAI,YAAY;CAGvB"}
|
package/dist/builder.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {a as a$1,b as b$1}from'./chunk-CC4LJOXE.js';import {a,c}from'./chunk-5SFFBKLW.js';import'./chunk-ICRS72Z4.js';import'./chunk-WOT6VMZA.js';var i=class{constructor(e,s={}){this.localSchema=e;this.options=s;}layers=[];from(e,s){return this.layers.push({resolver:e,schema:s}),this}resolve(){let e=a(this.localSchema);for(let l of this.layers){let m=a(l.schema);Object.assign(e,m);}let s=this.buildDefaultResolvers(),r=this.layers.map(l=>l.resolver),a$1=this.options.extend||[],c$1=[...s,...r,...a$1],u=this.options.policies??{};return c(e,c$1,{...this.options,policies:u,interpolate:this.options.interpolate??true,strict:this.options.strict??true})}buildDefaultResolvers(){let e=process.env.NODE_ENV==="production",s=[];return e||s.push(a$1({expand:true})),s.push(b$1()),s}},n=class{constructor(e,s={}){this.localSchema=e;this.options=s;}layers=[];from(e,s){return this.layers.push({resolver:e,schema:s}),this}resolveSync(){let e=a(this.localSchema);for(let l of this.layers){let m=a(l.schema);Object.assign(e,m);}let s=this.buildDefaultResolvers(),r=this.layers.map(l=>l.resolver),a$1=this.options.extend||[],c$1=[...s,...r,...a$1],u=this.options.policies??{};return c(e,c$1,{...this.options,policies:u,interpolate:this.options.interpolate??true,strict:this.options.strict??true})}buildDefaultResolvers(){let e=process.env.NODE_ENV==="production",s=[];return e||s.push(a$1({expand:true})),s.push(b$1()),s}};function y(t,e){return new i(t,e)}function b(t,e){return new n(t,e)}var d=class{builder;simpleResolve=null;constructor(e,s={}){this.builder=new i(e,s);}from(e,s){return this.builder.from(e,s),this}async compose(){return this.builder.resolve()}then(e,s){return this.simpleResolve||(this.simpleResolve=Promise.resolve(this.builder.resolve())),this.simpleResolve.then(e,s)}catch(e){return this.simpleResolve||(this.simpleResolve=Promise.resolve(this.builder.resolve())),this.simpleResolve.catch(e)}finally(e){return this.simpleResolve||(this.simpleResolve=Promise.resolve(this.builder.resolve())),this.simpleResolve.finally(e)}},S=class{builder;_resolved=null;_hasComposition=false;constructor(e,s={}){Object.defineProperty(this,"builder",{value:new n(e,s),enumerable:false,writable:true}),Object.defineProperty(this,"_hasComposition",{value:false,enumerable:false,writable:true});let r=this.builder.resolveSync();Object.defineProperty(this,"_resolved",{value:r,enumerable:false,writable:true}),Object.assign(this,r);}from(e,s){return Object.defineProperty(this,"_hasComposition",{value:true,enumerable:false,writable:true}),this.builder.from(e,s),this}compose(){return this._hasComposition&&(this._resolved=this.builder.resolveSync(),Object.assign(this,this._resolved)),this._resolved}valueOf(){return this._resolved}toJSON(){return this._resolved}};export{i as EnvBuilder,n as EnvBuilderSync,d as ResolvableBuilder,S as ResolvableBuilderSync,y as env,b as envSync};
|