node-env-resolver 3.0.1 → 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 +396 -246
- package/dist/audit.cjs +1 -0
- package/dist/audit.d.cts +29 -0
- package/dist/audit.d.ts +19 -6
- package/dist/audit.js +1 -1
- package/dist/builder.cjs +1 -0
- 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-4ZOF2PI4.cjs +1 -0
- package/dist/chunk-5PELJRUQ.js +1 -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-KIQXH265.cjs +1 -0
- package/dist/chunk-P3TTYVEW.js +16 -0
- package/dist/chunk-UCLEPPLS.cjs +16 -0
- package/dist/chunk-YOHHFBZN.js +1 -0
- package/dist/chunk-ZMRT37K3.js +1 -0
- package/dist/cli.cjs +1 -0
- package/dist/cli.d.cts +52 -0
- package/dist/cli.d.ts +9 -6
- package/dist/cli.js +1 -1
- package/dist/index.cjs +1 -0
- package/dist/index.d.cts +150 -0
- package/dist/index.d.ts +122 -40
- package/dist/index.js +1 -1
- package/dist/resolver.cjs +1 -0
- 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 -0
- 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 -0
- package/dist/types.d.cts +74 -0
- package/dist/types.d.ts +58 -164
- package/dist/types.js +1 -0
- package/dist/utils.cjs +1 -0
- package/dist/utils.d.cts +213 -0
- package/dist/utils.d.ts +37 -33
- package/dist/utils.js +1 -1
- package/dist/valibot.cjs +1 -0
- package/dist/valibot.d.cts +156 -0
- package/dist/valibot.d.ts +11 -8
- package/dist/valibot.js +1 -1
- package/dist/validation-types.cjs +1 -0
- package/dist/validation-types.d.cts +29 -0
- package/dist/validation-types.d.ts +4 -3
- package/dist/validators.cjs +2 -0
- package/dist/validators.d.cts +116 -0
- package/dist/validators.d.ts +46 -18
- package/dist/validators.js +2 -1
- package/dist/web.cjs +6 -0
- 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 -0
- package/dist/zod.d.cts +135 -0
- package/dist/zod.d.ts +11 -8
- package/dist/zod.js +1 -1
- package/package.json +31 -19
- 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-476ZUILE.js +0 -1
- package/dist/chunk-DAGPFV3V.js +0 -1
- package/dist/chunk-MGTYH57K.js +0 -9
- package/dist/chunk-MWG7FWHN.js +0 -5
- package/dist/chunk-QWDK3BUB.js +0 -15
- package/dist/chunk-VJZZNSRI.js +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/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
|
|
@@ -31,36 +32,57 @@ config.DEBUG; // boolean
|
|
|
31
32
|
config.API_KEY; // string | undefined
|
|
32
33
|
```
|
|
33
34
|
|
|
34
|
-
## Quick start (
|
|
35
|
+
## Quick start (with custom resolvers)
|
|
35
36
|
|
|
36
37
|
```ts
|
|
37
|
-
import { resolve, processEnv } from 'node-env-resolver';
|
|
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';
|
|
38
41
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
42
|
+
// Synchronous with custom resolver (array syntax)
|
|
43
|
+
const config = resolve([
|
|
44
|
+
dotenv(), // Resolver must support loadSync()
|
|
45
|
+
{
|
|
46
|
+
PORT: 3000,
|
|
47
|
+
NODE_ENV: ['development', 'production', 'test'] as const,
|
|
48
|
+
DEBUG: false,
|
|
49
|
+
API_KEY: string({optional:true})
|
|
50
|
+
}
|
|
51
|
+
]);
|
|
45
52
|
|
|
46
|
-
//
|
|
47
|
-
config
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
53
|
+
// Asynchronous with any resolvers (both sync and async work!)
|
|
54
|
+
const config = await resolveAsync([
|
|
55
|
+
dotenv(), {
|
|
56
|
+
PORT: 3000,
|
|
57
|
+
NODE_ENV: ['development', 'production', 'test'] as const,
|
|
58
|
+
DEBUG: false,
|
|
59
|
+
API_KEY: string({optional:true})
|
|
60
|
+
}],
|
|
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
|
+
);
|
|
51
72
|
```
|
|
52
73
|
|
|
53
74
|
## Table of Contents
|
|
54
75
|
|
|
55
76
|
- [Install](#install)
|
|
56
77
|
- [Quick start](#quick-start-uses-processenv)
|
|
78
|
+
- [Quick start (with custom resolvers)](#quick-start-with-custom-resolvers)
|
|
57
79
|
- [Basic usage](#basic-usage)
|
|
58
80
|
- [Required values](#required-values)
|
|
59
81
|
- [Default values](#default-values)
|
|
60
82
|
- [Optional values](#optional-values)
|
|
61
83
|
- [Enums](#enums)
|
|
84
|
+
- [Variable Naming Conventions](#variable-naming-conventions)
|
|
62
85
|
- [Performance & Bundle Size](#performance--bundle-size)
|
|
63
|
-
- [Built-in validators](#built-in-validators)
|
|
64
86
|
- [Custom validators](#custom-validators)
|
|
65
87
|
- [Multiple sources](#multiple-sources)
|
|
66
88
|
- [Safe error handling](#safe-error-handling)
|
|
@@ -81,9 +103,12 @@ config.API_KEY; // string | undefined
|
|
|
81
103
|
If an environment variable is missing and has no default, validation fails:
|
|
82
104
|
|
|
83
105
|
```ts
|
|
106
|
+
import { resolve } from 'node-env-resolver';
|
|
107
|
+
import { postgres, string } from 'node-env-resolver/resolvers';
|
|
108
|
+
|
|
84
109
|
const config = resolve({
|
|
85
|
-
DATABASE_URL:
|
|
86
|
-
API_KEY:
|
|
110
|
+
DATABASE_URL: postgres(), // Required PostgreSQL URL
|
|
111
|
+
API_KEY: string() // Required
|
|
87
112
|
});
|
|
88
113
|
```
|
|
89
114
|
|
|
@@ -92,6 +117,8 @@ const config = resolve({
|
|
|
92
117
|
Provide a default value to use when the environment variable is not set:
|
|
93
118
|
|
|
94
119
|
```ts
|
|
120
|
+
import { resolve } from 'node-env-resolver';
|
|
121
|
+
|
|
95
122
|
const config = resolve({
|
|
96
123
|
PORT: 3000, // Defaults to 3000
|
|
97
124
|
DEBUG: false, // Defaults to false
|
|
@@ -101,13 +128,15 @@ const config = resolve({
|
|
|
101
128
|
|
|
102
129
|
### Optional values
|
|
103
130
|
|
|
104
|
-
|
|
131
|
+
Use the `{optional: true}` option to make a value optional:
|
|
105
132
|
|
|
106
133
|
```ts
|
|
134
|
+
import { string, url, number } from 'node-env-resolver/resolvers';
|
|
135
|
+
|
|
107
136
|
const config = resolve({
|
|
108
|
-
API_KEY:
|
|
109
|
-
REDIS_URL:
|
|
110
|
-
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
|
|
111
140
|
});
|
|
112
141
|
```
|
|
113
142
|
|
|
@@ -116,6 +145,8 @@ const config = resolve({
|
|
|
116
145
|
Use arrays for enum validation:
|
|
117
146
|
|
|
118
147
|
```ts
|
|
148
|
+
import { resolve } from 'node-env-resolver';
|
|
149
|
+
|
|
119
150
|
const config = resolve({
|
|
120
151
|
NODE_ENV: ['development', 'production', 'test'] as const,
|
|
121
152
|
LOG_LEVEL: ['debug', 'info', 'warn', 'error'] as const
|
|
@@ -126,9 +157,80 @@ config.NODE_ENV; // 'development' | 'production' | 'test'
|
|
|
126
157
|
config.LOG_LEVEL; // 'debug' | 'info' | 'warn' | 'error'
|
|
127
158
|
```
|
|
128
159
|
|
|
160
|
+
## Variable Naming Conventions
|
|
161
|
+
|
|
162
|
+
The library validates environment variable names to prevent shell errors and ensure cross-platform compatibility.
|
|
163
|
+
|
|
164
|
+
### Supported Name Formats
|
|
165
|
+
|
|
166
|
+
Variable names can use:
|
|
167
|
+
- **Letters** (both uppercase and lowercase): `A-Z`, `a-z`
|
|
168
|
+
- **Numbers** (not as first character): `0-9`
|
|
169
|
+
- **Underscores**: `_`
|
|
170
|
+
|
|
171
|
+
### Valid Examples
|
|
172
|
+
|
|
173
|
+
```ts
|
|
174
|
+
import { resolve } from 'node-env-resolver';
|
|
175
|
+
import { string } from 'node-env-resolver/resolvers';
|
|
176
|
+
|
|
177
|
+
const config = resolve({
|
|
178
|
+
PORT: 3000, // ✅ SCREAMING_SNAKE_CASE (traditional)
|
|
179
|
+
port: 3000, // ✅ lowercase
|
|
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
|
|
184
|
+
});
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
### Invalid Examples
|
|
188
|
+
|
|
189
|
+
```ts
|
|
190
|
+
import { resolve } from 'node-env-resolver';
|
|
191
|
+
import { string } from 'node-env-resolver/resolvers';
|
|
192
|
+
|
|
193
|
+
const config = resolve({
|
|
194
|
+
'123PORT': 3000, // ❌ Starts with number
|
|
195
|
+
'API-KEY': string(), // ❌ Contains hyphen
|
|
196
|
+
'API.KEY': string(), // ❌ Contains dot
|
|
197
|
+
'API KEY': string(), // ❌ Contains space
|
|
198
|
+
'API@KEY': string() // ❌ Special characters
|
|
199
|
+
});
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
**Recommendation:** While all formats are supported, **SCREAMING_SNAKE_CASE** (e.g., `DATABASE_URL`, `API_KEY`) is the most common convention for environment variables and ensures maximum compatibility with legacy systems.
|
|
203
|
+
|
|
204
|
+
**Validation pattern:** `/^[A-Za-z_][A-Za-z0-9_]*$/`
|
|
205
|
+
|
|
129
206
|
## Performance & Bundle Size
|
|
130
207
|
|
|
131
|
-
**Lightweight
|
|
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
|
|
132
234
|
|
|
133
235
|
### Efficient validation architecture
|
|
134
236
|
|
|
@@ -148,18 +250,22 @@ The library uses a two-tier validation strategy with lazy-loaded validators:
|
|
|
148
250
|
Advanced validators are lazy-loaded only when you use them, keeping the base bundle minimal. All types work both synchronously and asynchronously:
|
|
149
251
|
|
|
150
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
|
+
|
|
151
257
|
// Synchronous - works with all types
|
|
152
258
|
const config = resolve({
|
|
153
259
|
PORT: 3000,
|
|
154
|
-
DATABASE_URL:
|
|
155
|
-
API_URL:
|
|
260
|
+
DATABASE_URL: postgres(),
|
|
261
|
+
API_URL: url(),
|
|
156
262
|
NODE_ENV: ['development', 'production'] as const
|
|
157
263
|
});
|
|
158
264
|
|
|
159
265
|
// Also works with async resolvers
|
|
160
|
-
const config = await
|
|
266
|
+
const config = await resolveAsync([
|
|
161
267
|
awsSecrets(),
|
|
162
|
-
{ DATABASE_URL:
|
|
268
|
+
{ DATABASE_URL: postgres(), API_URL: url() }
|
|
163
269
|
]);
|
|
164
270
|
```
|
|
165
271
|
|
|
@@ -184,6 +290,8 @@ Only the code you actually use gets loaded!
|
|
|
184
290
|
Audit logging is lazy-loaded only when enabled, keeping the base bundle minimal:
|
|
185
291
|
|
|
186
292
|
```ts
|
|
293
|
+
import { resolve } from 'node-env-resolver';
|
|
294
|
+
|
|
187
295
|
// Base bundle
|
|
188
296
|
const config = resolve({ PORT: 3000 });
|
|
189
297
|
|
|
@@ -216,109 +324,13 @@ import { awsSecrets } from 'node-env-resolver-aws';
|
|
|
216
324
|
|
|
217
325
|
**Most apps only need the core (~3.6KB)!**
|
|
218
326
|
|
|
219
|
-
## Built-in validators
|
|
220
|
-
|
|
221
|
-
All validator types work synchronously and asynchronously.
|
|
222
|
-
|
|
223
|
-
### Basic types
|
|
224
|
-
|
|
225
|
-
These types use inline validation:
|
|
226
|
-
|
|
227
|
-
- `'string'` - Any string value (with optional `min`/`max` length)
|
|
228
|
-
- `'number'` - Numeric value (coerced from string, with optional `min`/`max`)
|
|
229
|
-
- `'boolean'` - Boolean value (`'true'`/`'false'` coerced to boolean)
|
|
230
|
-
- `'enum'` - Array of allowed values
|
|
231
|
-
- `'pattern'` - Regex pattern validation
|
|
232
|
-
- `'custom'` - Custom validator function
|
|
233
|
-
|
|
234
|
-
### Advanced types
|
|
235
|
-
|
|
236
|
-
Advanced validators provide specialized validation and parsing:
|
|
237
|
-
|
|
238
|
-
**Network types:**
|
|
239
|
-
|
|
240
|
-
- `'url'` - Valid URL (returns string)
|
|
241
|
-
- `'http'` - HTTP or HTTPS URL (returns string)
|
|
242
|
-
- `'https'` - HTTPS-only URL (returns string)
|
|
243
|
-
- `'email'` - Email address (returns string)
|
|
244
|
-
|
|
245
|
-
**Database connection strings:**
|
|
246
|
-
|
|
247
|
-
- `'postgres'` or `'postgresql'` - PostgreSQL connection string (returns string)
|
|
248
|
-
- `'mysql'` - MySQL connection string (returns string)
|
|
249
|
-
- `'mongodb'` - MongoDB connection string (returns string)
|
|
250
|
-
- `'redis'` - Redis connection string (returns string)
|
|
251
|
-
|
|
252
|
-
**Format types:**
|
|
253
|
-
|
|
254
|
-
- `'json'` - JSON value (returns parsed object/array)
|
|
255
|
-
- `'port'` - Port number (returns number, 1-65535)
|
|
256
|
-
- `'date'` - ISO 8601 date string (returns string)
|
|
257
|
-
- `'timestamp'` - Unix timestamp (returns number)
|
|
258
|
-
- `'duration'` - Time duration like `'5s'`, `'2m'`, `'1h'` (returns milliseconds as number)
|
|
259
|
-
- `'file'` - Read content from file path (returns trimmed file content as string)
|
|
260
|
-
|
|
261
|
-
**Array types:**
|
|
262
|
-
|
|
263
|
-
- `'string[]'` - Array of strings from comma-separated values (returns `string[]`)
|
|
264
|
-
- `'number[]'` - Array of numbers from comma-separated values (returns `number[]`)
|
|
265
|
-
- `'url[]'` - Array of validated URLs from comma-separated values (returns `string[]`)
|
|
266
|
-
|
|
267
|
-
```ts
|
|
268
|
-
// Array examples
|
|
269
|
-
process.env.TAGS = 'frontend,backend,mobile';
|
|
270
|
-
process.env.PORTS = '3000,8080,9000';
|
|
271
|
-
process.env.ALLOWED_ORIGINS = 'https://app.com,https://api.com';
|
|
272
|
-
|
|
273
|
-
const config = resolve({
|
|
274
|
-
TAGS: 'string[]', // ['frontend', 'backend', 'mobile']
|
|
275
|
-
PORTS: 'number[]', // [3000, 8080, 9000]
|
|
276
|
-
ALLOWED_ORIGINS: 'url[]' // ['https://app.com', 'https://api.com']
|
|
277
|
-
});
|
|
278
|
-
|
|
279
|
-
// Custom separator
|
|
280
|
-
const config = resolve({
|
|
281
|
-
TAGS: { type: 'string[]', separator: '|' } // Use pipe instead of comma
|
|
282
|
-
});
|
|
283
|
-
|
|
284
|
-
// Duration parsing
|
|
285
|
-
process.env.TIMEOUT = '30s';
|
|
286
|
-
process.env.CACHE_TTL = '5m';
|
|
287
|
-
process.env.SESSION_DURATION = '24h';
|
|
288
|
-
|
|
289
|
-
const config = await resolve.with([processEnv(), {
|
|
290
|
-
TIMEOUT: 'duration', // 30000 (milliseconds)
|
|
291
|
-
CACHE_TTL: 'duration', // 300000
|
|
292
|
-
SESSION_DURATION: 'duration' // 86400000
|
|
293
|
-
}]);
|
|
294
|
-
|
|
295
|
-
// File reading (useful for Docker/Kubernetes secrets)
|
|
296
|
-
// Method 1: Explicit path in env var
|
|
297
|
-
process.env.DB_PASSWORD_FILE = '/run/secrets/db_password';
|
|
298
|
-
const config = await resolve.with([processEnv(), {
|
|
299
|
-
DB_PASSWORD_FILE: 'file' // 'my-secret-password' (file content, trimmed)
|
|
300
|
-
}]);
|
|
301
|
-
|
|
302
|
-
// Method 2: Using secretsDir (cleaner for Docker/K8s)
|
|
303
|
-
// Reads from /run/secrets/db_password automatically (key name lowercased)
|
|
304
|
-
const config = await resolve.with([processEnv(), {
|
|
305
|
-
DB_PASSWORD: 'file' // Reads /run/secrets/db_password
|
|
306
|
-
}], { secretsDir: '/run/secrets' });
|
|
307
|
-
|
|
308
|
-
// Per-field secretsDir (overrides global)
|
|
309
|
-
const config = await resolve.with([processEnv(), {
|
|
310
|
-
DB_PASSWORD: { type: 'file', secretsDir: '/custom/secrets' },
|
|
311
|
-
API_KEY: 'file' // Uses global secretsDir
|
|
312
|
-
}], { secretsDir: '/run/secrets' });
|
|
313
|
-
```
|
|
314
|
-
|
|
315
|
-
All validators automatically handle type coercion from environment variable strings.
|
|
316
|
-
|
|
317
327
|
## Custom validators
|
|
318
328
|
|
|
319
329
|
Write your own validation functions:
|
|
320
330
|
|
|
321
331
|
```ts
|
|
332
|
+
import { resolve } from 'node-env-resolver';
|
|
333
|
+
|
|
322
334
|
const isValidPort = (value: string): number => {
|
|
323
335
|
const port = parseInt(value, 10);
|
|
324
336
|
if (isNaN(port) || port < 1 || port > 65535) {
|
|
@@ -373,39 +385,72 @@ Available validation functions: `validatePostgres`, `validateMysql`, `validateMo
|
|
|
373
385
|
## Multiple sources
|
|
374
386
|
|
|
375
387
|
Load configuration from multiple sources. By default, later sources override earlier ones:
|
|
376
|
-
|
|
377
388
|
```ts
|
|
378
|
-
import { resolve, processEnv } from 'node-env-resolver';
|
|
379
|
-
import { dotenv } 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';
|
|
380
393
|
|
|
381
|
-
|
|
394
|
+
// Async mode - supports both sync and async resolvers
|
|
395
|
+
const config = await resolveAsync(
|
|
382
396
|
[processEnv(), {
|
|
383
397
|
PORT: 3000,
|
|
384
398
|
NODE_ENV: ['development', 'production'] as const,
|
|
385
399
|
}],
|
|
386
400
|
[dotenv(), {
|
|
387
|
-
DATABASE_URL:
|
|
388
|
-
API_KEY:
|
|
401
|
+
DATABASE_URL: postgres(),
|
|
402
|
+
API_KEY: string(),
|
|
389
403
|
}]
|
|
390
404
|
);
|
|
405
|
+
|
|
406
|
+
// Sync mode - supports multiple SYNC resolvers
|
|
407
|
+
const config = resolve(
|
|
408
|
+
[dotenv(), { PORT: 3000 }],
|
|
409
|
+
[json('config.json'), { DATABASE_URL: postgres() }]
|
|
410
|
+
// Both resolvers must have loadSync() method
|
|
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
|
+
);
|
|
391
424
|
```
|
|
392
425
|
|
|
393
426
|
### Controlling merge behaviour
|
|
394
427
|
|
|
395
|
-
Use `priority` to control how resolvers merge values:
|
|
428
|
+
Use `priority` to control how resolvers merge values (works with both sync and async modes):
|
|
396
429
|
|
|
397
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
|
+
|
|
398
435
|
// priority: 'last' (default) - later resolvers override earlier ones
|
|
399
|
-
const config = await
|
|
400
|
-
[processEnv(), { DATABASE_URL:
|
|
401
|
-
[awsSecrets(), { DATABASE_URL:
|
|
436
|
+
const config = await resolveAsync(
|
|
437
|
+
[processEnv(), { DATABASE_URL: postgres() }],
|
|
438
|
+
[awsSecrets(), { DATABASE_URL: postgres() }]
|
|
402
439
|
// AWS wins
|
|
403
440
|
);
|
|
404
441
|
|
|
405
442
|
// priority: 'first' - earlier resolvers take precedence
|
|
406
|
-
const config = await
|
|
407
|
-
[dotenv(), { DATABASE_URL:
|
|
408
|
-
[awsSecrets(), { DATABASE_URL:
|
|
443
|
+
const config = await resolveAsync(
|
|
444
|
+
[dotenv(), { DATABASE_URL: postgres() }],
|
|
445
|
+
[awsSecrets(), { DATABASE_URL: postgres() }],
|
|
446
|
+
{ priority: 'first' }
|
|
447
|
+
// dotenv wins
|
|
448
|
+
);
|
|
449
|
+
|
|
450
|
+
// Also works with sync resolve() (NEW!)
|
|
451
|
+
const config = resolve(
|
|
452
|
+
[dotenv(), { DATABASE_URL: postgres() }],
|
|
453
|
+
[json('config.json'), { DATABASE_URL: postgres() }],
|
|
409
454
|
{ priority: 'first' }
|
|
410
455
|
// dotenv wins
|
|
411
456
|
);
|
|
@@ -422,10 +467,14 @@ The library includes two automatic performance optimizations:
|
|
|
422
467
|
When using `priority: 'first'`, resolvers are called sequentially, but execution **stops early** once all required environment variables are satisfied:
|
|
423
468
|
|
|
424
469
|
```ts
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
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 }],
|
|
429
478
|
{ priority: 'first' }
|
|
430
479
|
);
|
|
431
480
|
// If dotenv() provides all required keys, awsSecrets() and gcpSecrets() are never called!
|
|
@@ -447,10 +496,14 @@ This is particularly valuable in development where:
|
|
|
447
496
|
When using `priority: 'last'` (the default), all resolvers are called **in parallel** for maximum performance:
|
|
448
497
|
|
|
449
498
|
```ts
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
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
|
|
454
507
|
// Default: priority: 'last'
|
|
455
508
|
);
|
|
456
509
|
// Total time: ~100ms (parallel) instead of ~300ms (sequential)
|
|
@@ -470,10 +523,11 @@ Like Zod's `safeParse()`, use `safeResolve()` to get a result object instead of
|
|
|
470
523
|
|
|
471
524
|
```ts
|
|
472
525
|
import { safeResolve } from 'node-env-resolver';
|
|
526
|
+
import { number, postgres } from 'node-env-resolver/resolvers';
|
|
473
527
|
|
|
474
528
|
const result = safeResolve({
|
|
475
|
-
PORT:
|
|
476
|
-
DATABASE_URL:
|
|
529
|
+
PORT: number(),
|
|
530
|
+
DATABASE_URL: postgres()
|
|
477
531
|
});
|
|
478
532
|
|
|
479
533
|
if (result.success) {
|
|
@@ -489,7 +543,7 @@ if (result.success) {
|
|
|
489
543
|
All functions have safe variants:
|
|
490
544
|
|
|
491
545
|
- `resolve()` → `safeResolve()` (both synchronous)
|
|
492
|
-
- `
|
|
546
|
+
- `resolveAsync()` → `saferesolveAsync()` (both async)
|
|
493
547
|
|
|
494
548
|
## Synchronous resolution
|
|
495
549
|
|
|
@@ -497,25 +551,29 @@ All functions have safe variants:
|
|
|
497
551
|
|
|
498
552
|
```ts
|
|
499
553
|
import { resolve } from 'node-env-resolver';
|
|
554
|
+
import { string, url, postgres } from 'node-env-resolver/resolvers';
|
|
500
555
|
|
|
501
556
|
// Synchronous - no await needed, works with ALL types
|
|
502
557
|
const config = resolve({
|
|
503
558
|
PORT: 3000,
|
|
504
559
|
NODE_ENV: ['development', 'production'] as const,
|
|
505
|
-
API_KEY:
|
|
506
|
-
API_URL:
|
|
507
|
-
DATABASE_URL:
|
|
560
|
+
API_KEY: string(),
|
|
561
|
+
API_URL: url(), // Advanced types work synchronously!
|
|
562
|
+
DATABASE_URL: postgres(),
|
|
508
563
|
DEBUG: false
|
|
509
564
|
});
|
|
510
565
|
```
|
|
511
566
|
|
|
512
|
-
`
|
|
567
|
+
`resolveAsync()` is **async** when using custom resolvers:
|
|
513
568
|
|
|
514
569
|
```ts
|
|
570
|
+
import { resolveAsync, processEnv } from 'node-env-resolver';
|
|
571
|
+
import { dotenv, postgres, url } from 'node-env-resolver/resolvers';
|
|
572
|
+
|
|
515
573
|
// Async - await required when using custom resolvers
|
|
516
|
-
const config = await
|
|
574
|
+
const config = await resolveAsync(
|
|
517
575
|
[processEnv(), { PORT: 3000 }],
|
|
518
|
-
[dotenv(), { DATABASE_URL:
|
|
576
|
+
[dotenv(), { DATABASE_URL: postgres(), API_URL: url() }]
|
|
519
577
|
);
|
|
520
578
|
```
|
|
521
579
|
|
|
@@ -526,20 +584,21 @@ const config = await resolve.with(
|
|
|
526
584
|
Parse command-line arguments as environment variables - perfect for CLI tools:
|
|
527
585
|
|
|
528
586
|
```ts
|
|
529
|
-
import {
|
|
587
|
+
import { resolveAsync, processEnv } from 'node-env-resolver';
|
|
588
|
+
import { postgres } from 'node-env-resolver/resolvers';
|
|
530
589
|
import { cliArgs } from 'node-env-resolver/cli';
|
|
531
590
|
|
|
532
591
|
// $ node app.js --port 8080 --database-url postgres://localhost --verbose
|
|
533
592
|
|
|
534
|
-
const config = await
|
|
593
|
+
const config = await resolveAsync(
|
|
535
594
|
[processEnv(), {
|
|
536
595
|
PORT: 3000,
|
|
537
|
-
DATABASE_URL:
|
|
596
|
+
DATABASE_URL: postgres(),
|
|
538
597
|
VERBOSE: false
|
|
539
598
|
}],
|
|
540
599
|
[cliArgs(), {
|
|
541
600
|
PORT: 3000,
|
|
542
|
-
DATABASE_URL:
|
|
601
|
+
DATABASE_URL: postgres(),
|
|
543
602
|
VERBOSE: false
|
|
544
603
|
}]
|
|
545
604
|
);
|
|
@@ -728,7 +787,8 @@ if (result.success) {
|
|
|
728
787
|
Create resolvers to load configuration from any source:
|
|
729
788
|
|
|
730
789
|
```ts
|
|
731
|
-
import {
|
|
790
|
+
import { resolveAsync, processEnv, type Resolver } from 'node-env-resolver';
|
|
791
|
+
import { string } from 'node-env-resolver/resolvers';
|
|
732
792
|
|
|
733
793
|
const databaseResolver: Resolver = {
|
|
734
794
|
name: 'database',
|
|
@@ -738,9 +798,9 @@ const databaseResolver: Resolver = {
|
|
|
738
798
|
}
|
|
739
799
|
};
|
|
740
800
|
|
|
741
|
-
const config = await
|
|
801
|
+
const config = await resolveAsync(
|
|
742
802
|
[processEnv(), { PORT: 3000 }],
|
|
743
|
-
[databaseResolver, { API_KEY:
|
|
803
|
+
[databaseResolver, { API_KEY: string() }]
|
|
744
804
|
);
|
|
745
805
|
```
|
|
746
806
|
|
|
@@ -756,25 +816,27 @@ Single source:
|
|
|
756
816
|
|
|
757
817
|
```ts
|
|
758
818
|
import { resolveSecrets } from 'node-env-resolver-aws';
|
|
819
|
+
import { postgres, string } from 'node-env-resolver/resolvers';
|
|
759
820
|
|
|
760
821
|
const config = await resolveSecrets({
|
|
761
822
|
secretId: 'myapp/secrets'
|
|
762
823
|
}, {
|
|
763
|
-
DATABASE_URL:
|
|
764
|
-
API_KEY:
|
|
824
|
+
DATABASE_URL: postgres(),
|
|
825
|
+
API_KEY: string()
|
|
765
826
|
});
|
|
766
827
|
```
|
|
767
828
|
|
|
768
829
|
Multiple sources:
|
|
769
830
|
|
|
770
831
|
```ts
|
|
771
|
-
import {
|
|
832
|
+
import { resolveAsync, processEnv } from 'node-env-resolver';
|
|
833
|
+
import { postgres, string } from 'node-env-resolver/resolvers';
|
|
772
834
|
import { awsSecrets, awsSsm } from 'node-env-resolver-aws';
|
|
773
835
|
|
|
774
|
-
const config = await
|
|
836
|
+
const config = await resolveAsync(
|
|
775
837
|
[processEnv(), { PORT: 3000 }],
|
|
776
|
-
[awsSecrets({ secretId: 'app/secrets' }), { DATABASE_URL:
|
|
777
|
-
[awsSsm({ path: '/app/config' }), { API_KEY:
|
|
838
|
+
[awsSecrets({ secretId: 'app/secrets' }), { DATABASE_URL: postgres() }],
|
|
839
|
+
[awsSsm({ path: '/app/config' }), { API_KEY: string() }]
|
|
778
840
|
);
|
|
779
841
|
```
|
|
780
842
|
|
|
@@ -785,18 +847,19 @@ See the [AWS package documentation](../node-env-resolver-aws/README.md) for deta
|
|
|
785
847
|
Cache expensive operations like AWS API calls:
|
|
786
848
|
|
|
787
849
|
```ts
|
|
788
|
-
import {
|
|
850
|
+
import { resolveAsync } from 'node-env-resolver';
|
|
789
851
|
import { cached, TTL } from 'node-env-resolver/utils';
|
|
852
|
+
import { postgres, string } from 'node-env-resolver/resolvers';
|
|
790
853
|
import { awsSecrets } from 'node-env-resolver-aws';
|
|
791
854
|
|
|
792
855
|
export const getConfig = async () => {
|
|
793
|
-
return await
|
|
856
|
+
return await resolveAsync(
|
|
794
857
|
[cached(
|
|
795
858
|
awsSecrets({ secretId: 'app/secrets' }),
|
|
796
859
|
{ ttl: TTL.minutes5 }
|
|
797
860
|
), {
|
|
798
|
-
DATABASE_URL:
|
|
799
|
-
API_KEY:
|
|
861
|
+
DATABASE_URL: postgres(),
|
|
862
|
+
API_KEY: string(),
|
|
800
863
|
}]
|
|
801
864
|
);
|
|
802
865
|
};
|
|
@@ -823,34 +886,48 @@ Performance:
|
|
|
823
886
|
|----------|-----------|----------|
|
|
824
887
|
| `resolve()` | Sync, throws on error | Most applications (reading from process.env) |
|
|
825
888
|
| `safeResolve()` | Sync, returns result object | Graceful error handling |
|
|
826
|
-
| `
|
|
827
|
-
| `
|
|
889
|
+
| `resolveAsync()` | Async, throws on error | Multiple sources (dotenv, AWS, etc.) |
|
|
890
|
+
| `safeResolveAsync()` | Async, returns result object | Multiple sources with error handling |
|
|
828
891
|
|
|
829
|
-
###
|
|
892
|
+
### Resolver API Reference
|
|
893
|
+
|
|
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
|
+
```
|
|
830
903
|
|
|
831
904
|
| Syntax | Type | Description |
|
|
832
905
|
|--------|------|-------------|
|
|
833
|
-
| `
|
|
834
|
-
| `
|
|
835
|
-
| `
|
|
836
|
-
| `
|
|
837
|
-
| `
|
|
838
|
-
| `
|
|
839
|
-
| `
|
|
840
|
-
| `
|
|
841
|
-
| `
|
|
842
|
-
| `
|
|
843
|
-
| `
|
|
844
|
-
| `
|
|
845
|
-
| `
|
|
846
|
-
| `
|
|
847
|
-
| `
|
|
848
|
-
| `
|
|
849
|
-
| `
|
|
850
|
-
| `
|
|
851
|
-
| `
|
|
852
|
-
| `
|
|
853
|
-
| `
|
|
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`) |
|
|
854
931
|
|
|
855
932
|
## Configuration Options
|
|
856
933
|
|
|
@@ -865,8 +942,8 @@ const config = resolve(schema, {
|
|
|
865
942
|
enableAudit: true
|
|
866
943
|
});
|
|
867
944
|
|
|
868
|
-
// Multiple sources - options as last parameter to
|
|
869
|
-
const config = await
|
|
945
|
+
// Multiple sources - options as last parameter to resolveAsync()
|
|
946
|
+
const config = await resolveAsync(
|
|
870
947
|
[resolver1, schema],
|
|
871
948
|
[resolver2, schema],
|
|
872
949
|
{
|
|
@@ -879,7 +956,7 @@ const config = await resolve.with(
|
|
|
879
956
|
);
|
|
880
957
|
```
|
|
881
958
|
|
|
882
|
-
**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.
|
|
883
960
|
|
|
884
961
|
### `interpolate`
|
|
885
962
|
|
|
@@ -889,16 +966,19 @@ const config = await resolve.with(
|
|
|
889
966
|
|
|
890
967
|
**Why:** Keeps configuration DRY and maintainable.
|
|
891
968
|
|
|
892
|
-
**Default:** `true` in `
|
|
969
|
+
**Default:** `true` in `resolveAsync()`, `false` in `resolve()`
|
|
893
970
|
|
|
894
971
|
```ts
|
|
972
|
+
import { resolve } from 'node-env-resolver';
|
|
973
|
+
import { url } from 'node-env-resolver/resolvers';
|
|
974
|
+
|
|
895
975
|
// With interpolation
|
|
896
976
|
process.env.BASE_URL = 'https://api.example.com';
|
|
897
977
|
process.env.API_ENDPOINT = '${BASE_URL}/v1';
|
|
898
978
|
|
|
899
979
|
const config = resolve({
|
|
900
|
-
BASE_URL:
|
|
901
|
-
API_ENDPOINT:
|
|
980
|
+
BASE_URL: url(),
|
|
981
|
+
API_ENDPOINT: url()
|
|
902
982
|
}, {
|
|
903
983
|
interpolate: true
|
|
904
984
|
});
|
|
@@ -931,14 +1011,14 @@ const flakyResolver = {
|
|
|
931
1011
|
};
|
|
932
1012
|
|
|
933
1013
|
// ❌ Throws immediately
|
|
934
|
-
await
|
|
1014
|
+
await resolveAsync(
|
|
935
1015
|
[flakyResolver, schema],
|
|
936
1016
|
[processEnv(), schema],
|
|
937
1017
|
{ strict: true } // default
|
|
938
1018
|
);
|
|
939
1019
|
|
|
940
1020
|
// ✅ Continues with processEnv()
|
|
941
|
-
await
|
|
1021
|
+
await resolveAsync(
|
|
942
1022
|
[flakyResolver, schema],
|
|
943
1023
|
[processEnv(), schema],
|
|
944
1024
|
{ strict: false } // graceful degradation
|
|
@@ -961,17 +1041,21 @@ await resolve.with(
|
|
|
961
1041
|
**Default:** `'last'`
|
|
962
1042
|
|
|
963
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
|
+
|
|
964
1048
|
// Production: AWS secrets override process.env
|
|
965
|
-
await
|
|
966
|
-
[processEnv(), { DATABASE_URL:
|
|
967
|
-
[awsSecrets(), { DATABASE_URL:
|
|
1049
|
+
await resolveAsync(
|
|
1050
|
+
[processEnv(), { DATABASE_URL: postgres() }],
|
|
1051
|
+
[awsSecrets(), { DATABASE_URL: postgres() }]
|
|
968
1052
|
// priority: 'last' (default) - AWS wins
|
|
969
1053
|
);
|
|
970
1054
|
|
|
971
1055
|
// Development: Local .env overrides cloud
|
|
972
|
-
await
|
|
973
|
-
[dotenv(), { DATABASE_URL:
|
|
974
|
-
[awsSecrets(), { DATABASE_URL:
|
|
1056
|
+
await resolveAsync(
|
|
1057
|
+
[dotenv(), { DATABASE_URL: postgres() }],
|
|
1058
|
+
[awsSecrets(), { DATABASE_URL: postgres() }],
|
|
975
1059
|
{ priority: 'first' } // dotenv wins
|
|
976
1060
|
);
|
|
977
1061
|
```
|
|
@@ -989,7 +1073,7 @@ await resolve.with(
|
|
|
989
1073
|
**Default:** `undefined` (no policies enforced)
|
|
990
1074
|
|
|
991
1075
|
```ts
|
|
992
|
-
await
|
|
1076
|
+
await resolveAsync(
|
|
993
1077
|
[processEnv(), schema],
|
|
994
1078
|
[awsSecrets(), schema],
|
|
995
1079
|
{
|
|
@@ -1024,7 +1108,7 @@ await resolve.with(
|
|
|
1024
1108
|
**Default:** `false` (disabled in development), automatically `true` in production (`NODE_ENV === 'production'`)
|
|
1025
1109
|
|
|
1026
1110
|
```ts
|
|
1027
|
-
await
|
|
1111
|
+
await resolveAsync(
|
|
1028
1112
|
[processEnv(), schema],
|
|
1029
1113
|
[awsSecrets(), schema],
|
|
1030
1114
|
{ enableAudit: true } // Explicitly enable in development
|
|
@@ -1043,7 +1127,7 @@ const logs = getAuditLog();
|
|
|
1043
1127
|
|
|
1044
1128
|
```ts
|
|
1045
1129
|
interface ResolveOptions {
|
|
1046
|
-
interpolate?: boolean; // Variable interpolation (default: true in .
|
|
1130
|
+
interpolate?: boolean; // Variable interpolation (default: true in .async())
|
|
1047
1131
|
strict?: boolean; // Fail-fast behaviour (default: true)
|
|
1048
1132
|
priority?: 'first' | 'last'; // Merge strategy (default: 'last')
|
|
1049
1133
|
policies?: PolicyOptions; // Security policies (default: undefined)
|
|
@@ -1064,11 +1148,12 @@ interface PolicyOptions {
|
|
|
1064
1148
|
```ts
|
|
1065
1149
|
import express from 'express';
|
|
1066
1150
|
import { resolve } from 'node-env-resolver';
|
|
1151
|
+
import { postgres, string } from 'node-env-resolver/resolvers';
|
|
1067
1152
|
|
|
1068
1153
|
const config = resolve({
|
|
1069
1154
|
PORT: 3000,
|
|
1070
|
-
DATABASE_URL:
|
|
1071
|
-
SESSION_SECRET:
|
|
1155
|
+
DATABASE_URL: postgres(),
|
|
1156
|
+
SESSION_SECRET: string()
|
|
1072
1157
|
});
|
|
1073
1158
|
|
|
1074
1159
|
const app = express();
|
|
@@ -1080,14 +1165,15 @@ app.listen(config.PORT);
|
|
|
1080
1165
|
```ts
|
|
1081
1166
|
// env.mjs
|
|
1082
1167
|
import { resolve } from 'node-env-resolver/nextjs';
|
|
1168
|
+
import { postgres, string, url } from 'node-env-resolver/resolvers';
|
|
1083
1169
|
|
|
1084
1170
|
export const env = resolve({
|
|
1085
1171
|
server: {
|
|
1086
|
-
DATABASE_URL:
|
|
1087
|
-
API_SECRET:
|
|
1172
|
+
DATABASE_URL: postgres(),
|
|
1173
|
+
API_SECRET: string(),
|
|
1088
1174
|
},
|
|
1089
1175
|
client: {
|
|
1090
|
-
NEXT_PUBLIC_APP_URL:
|
|
1176
|
+
NEXT_PUBLIC_APP_URL: url(),
|
|
1091
1177
|
}
|
|
1092
1178
|
});
|
|
1093
1179
|
```
|
|
@@ -1095,17 +1181,18 @@ export const env = resolve({
|
|
|
1095
1181
|
### AWS Lambda
|
|
1096
1182
|
|
|
1097
1183
|
```ts
|
|
1098
|
-
import {
|
|
1184
|
+
import { resolveAsync } from 'node-env-resolver';
|
|
1099
1185
|
import { cached, TTL } from 'node-env-resolver/utils';
|
|
1186
|
+
import { postgres } from 'node-env-resolver/resolvers';
|
|
1100
1187
|
import { awsSecrets } from 'node-env-resolver-aws';
|
|
1101
1188
|
|
|
1102
1189
|
const getConfig = async () => {
|
|
1103
|
-
return await
|
|
1190
|
+
return await resolveAsync(
|
|
1104
1191
|
[cached(
|
|
1105
1192
|
awsSecrets({ secretId: 'lambda/config' }),
|
|
1106
1193
|
{ ttl: TTL.minutes5 }
|
|
1107
1194
|
), {
|
|
1108
|
-
DATABASE_URL:
|
|
1195
|
+
DATABASE_URL: postgres(),
|
|
1109
1196
|
}]
|
|
1110
1197
|
);
|
|
1111
1198
|
};
|
|
@@ -1127,10 +1214,13 @@ By default, `.env` files are **completely blocked in production** for security.
|
|
|
1127
1214
|
**Default behavior (secure):**
|
|
1128
1215
|
|
|
1129
1216
|
```ts
|
|
1217
|
+
import { resolveAsync } from 'node-env-resolver';
|
|
1218
|
+
import { dotenv, postgres } from 'node-env-resolver/resolvers';
|
|
1219
|
+
|
|
1130
1220
|
// In production (NODE_ENV=production)
|
|
1131
|
-
const config = await
|
|
1221
|
+
const config = await resolveAsync(
|
|
1132
1222
|
[dotenv(), {
|
|
1133
|
-
DATABASE_URL:
|
|
1223
|
+
DATABASE_URL: postgres(),
|
|
1134
1224
|
}]
|
|
1135
1225
|
);
|
|
1136
1226
|
// ❌ Throws: "DATABASE_URL cannot be sourced from .env files in production"
|
|
@@ -1139,9 +1229,12 @@ const config = await resolve.with(
|
|
|
1139
1229
|
**Allow all .env variables (NOT recommended):**
|
|
1140
1230
|
|
|
1141
1231
|
```ts
|
|
1142
|
-
|
|
1232
|
+
import { resolveAsync } from 'node-env-resolver';
|
|
1233
|
+
import { dotenv, postgres } from 'node-env-resolver/resolvers';
|
|
1234
|
+
|
|
1235
|
+
const config = await resolveAsync(
|
|
1143
1236
|
[dotenv(), {
|
|
1144
|
-
DATABASE_URL:
|
|
1237
|
+
DATABASE_URL: postgres(),
|
|
1145
1238
|
}],
|
|
1146
1239
|
{
|
|
1147
1240
|
policies: {
|
|
@@ -1154,10 +1247,13 @@ const config = await resolve.with(
|
|
|
1154
1247
|
**Allow specific variables only (recommended if needed):**
|
|
1155
1248
|
|
|
1156
1249
|
```ts
|
|
1157
|
-
|
|
1250
|
+
import { resolveAsync } from 'node-env-resolver';
|
|
1251
|
+
import { dotenv, postgres } from 'node-env-resolver/resolvers';
|
|
1252
|
+
|
|
1253
|
+
const config = await resolveAsync(
|
|
1158
1254
|
[dotenv(), {
|
|
1159
1255
|
PORT: 3000,
|
|
1160
|
-
DATABASE_URL:
|
|
1256
|
+
DATABASE_URL: postgres(),
|
|
1161
1257
|
}],
|
|
1162
1258
|
{
|
|
1163
1259
|
policies: {
|
|
@@ -1173,16 +1269,17 @@ const config = await resolve.with(
|
|
|
1173
1269
|
Restrict sensitive variables to specific resolvers (e.g., force secrets to come from AWS):
|
|
1174
1270
|
|
|
1175
1271
|
```ts
|
|
1176
|
-
import {
|
|
1272
|
+
import { resolveAsync, processEnv } from 'node-env-resolver';
|
|
1273
|
+
import { string } from 'node-env-resolver/resolvers';
|
|
1177
1274
|
import { awsSecrets } from 'node-env-resolver-aws';
|
|
1178
1275
|
|
|
1179
|
-
const config = await
|
|
1276
|
+
const config = await resolveAsync(
|
|
1180
1277
|
[processEnv(), {
|
|
1181
1278
|
PORT: 3000,
|
|
1182
1279
|
}],
|
|
1183
1280
|
[awsSecrets({ secretId: 'prod/secrets' }), {
|
|
1184
|
-
DATABASE_PASSWORD:
|
|
1185
|
-
API_KEY:
|
|
1281
|
+
DATABASE_PASSWORD: string(),
|
|
1282
|
+
API_KEY: string(),
|
|
1186
1283
|
}],
|
|
1187
1284
|
{
|
|
1188
1285
|
policies: {
|
|
@@ -1199,15 +1296,16 @@ const config = await resolve.with(
|
|
|
1199
1296
|
**Use case:** Ensure production secrets only come from AWS Secrets Manager, never from `.env` or `process.env`:
|
|
1200
1297
|
|
|
1201
1298
|
```ts
|
|
1202
|
-
import {
|
|
1299
|
+
import { resolveAsync, processEnv } from 'node-env-resolver';
|
|
1300
|
+
import { string } from 'node-env-resolver/resolvers';
|
|
1203
1301
|
import { cached, TTL } from 'node-env-resolver/utils';
|
|
1204
1302
|
import { awsSecrets } from 'node-env-resolver-aws';
|
|
1205
1303
|
|
|
1206
|
-
const config = await
|
|
1304
|
+
const config = await resolveAsync(
|
|
1207
1305
|
[processEnv(), {}],
|
|
1208
1306
|
[cached(awsSecrets({ secretId: 'prod/db' }), { ttl: TTL.minutes5 }), {
|
|
1209
|
-
DATABASE_PASSWORD:
|
|
1210
|
-
STRIPE_SECRET:
|
|
1307
|
+
DATABASE_PASSWORD: string(),
|
|
1308
|
+
STRIPE_SECRET: string(),
|
|
1211
1309
|
}],
|
|
1212
1310
|
{
|
|
1213
1311
|
policies: {
|
|
@@ -1257,11 +1355,12 @@ Track environment variable resolution for security and compliance monitoring.
|
|
|
1257
1355
|
|
|
1258
1356
|
```ts
|
|
1259
1357
|
import { resolve, getAuditLog, clearAuditLog } from 'node-env-resolver';
|
|
1358
|
+
import { postgres, string } from 'node-env-resolver/resolvers';
|
|
1260
1359
|
|
|
1261
1360
|
// Option 1: Explicitly enable (works in any environment)
|
|
1262
1361
|
const config = resolve({
|
|
1263
|
-
DATABASE_URL:
|
|
1264
|
-
API_KEY:
|
|
1362
|
+
DATABASE_URL: postgres(),
|
|
1363
|
+
API_KEY: string(),
|
|
1265
1364
|
}, {
|
|
1266
1365
|
enableAudit: true // ← Enable audit logging
|
|
1267
1366
|
});
|
|
@@ -1269,7 +1368,7 @@ const config = resolve({
|
|
|
1269
1368
|
console.log(getAuditLog());
|
|
1270
1369
|
// [
|
|
1271
1370
|
// { type: 'env_loaded', key: 'DATABASE_URL', source: 'process.env', timestamp: ... },
|
|
1272
|
-
// { type: 'env_loaded', key: 'API_KEY', source: '
|
|
1371
|
+
// { type: 'env_loaded', key: 'API_KEY', source: 'process.env', timestamp: ... },
|
|
1273
1372
|
// { type: 'validation_success', metadata: { variableCount: 2 }, timestamp: ... }
|
|
1274
1373
|
// ]
|
|
1275
1374
|
|
|
@@ -1278,17 +1377,66 @@ clearAuditLog();
|
|
|
1278
1377
|
```
|
|
1279
1378
|
|
|
1280
1379
|
```ts
|
|
1380
|
+
import { resolve, getAuditLog } from 'node-env-resolver';
|
|
1381
|
+
import { postgres } from 'node-env-resolver/resolvers';
|
|
1382
|
+
|
|
1281
1383
|
// Option 2: Automatic in production
|
|
1282
1384
|
process.env.NODE_ENV = 'production';
|
|
1283
1385
|
|
|
1284
1386
|
const config = resolve({
|
|
1285
|
-
DATABASE_URL:
|
|
1387
|
+
DATABASE_URL: postgres(),
|
|
1286
1388
|
});
|
|
1287
1389
|
|
|
1288
1390
|
// Audit automatically enabled in production
|
|
1289
1391
|
console.log(getAuditLog());
|
|
1290
1392
|
```
|
|
1291
1393
|
|
|
1394
|
+
### Per-Config Audit Tracking
|
|
1395
|
+
|
|
1396
|
+
When you have multiple `resolve()` calls, you can now get audit logs **specific to each config object**:
|
|
1397
|
+
|
|
1398
|
+
```ts
|
|
1399
|
+
import { resolve, getAuditLog } from 'node-env-resolver';
|
|
1400
|
+
import { string, postgres } from 'node-env-resolver/resolvers';
|
|
1401
|
+
|
|
1402
|
+
const appConfig = resolve({
|
|
1403
|
+
PORT: 3000,
|
|
1404
|
+
API_KEY: string()
|
|
1405
|
+
}, { enableAudit: true });
|
|
1406
|
+
|
|
1407
|
+
const dbConfig = resolve({
|
|
1408
|
+
DATABASE_URL: postgres(),
|
|
1409
|
+
DB_POOL_SIZE: 10
|
|
1410
|
+
}, { enableAudit: true });
|
|
1411
|
+
|
|
1412
|
+
// Get audit logs for specific config (NEW!)
|
|
1413
|
+
const appAudit = getAuditLog(appConfig); // Only PORT and API_KEY events
|
|
1414
|
+
const dbAudit = getAuditLog(dbConfig); // Only DATABASE_URL and DB_POOL_SIZE events
|
|
1415
|
+
|
|
1416
|
+
// Still works: get ALL audit events (backward compatible)
|
|
1417
|
+
const allAudit = getAuditLog(); // All events from both configs
|
|
1418
|
+
|
|
1419
|
+
console.log('App config audit:', appAudit);
|
|
1420
|
+
// [
|
|
1421
|
+
// { type: 'env_loaded', key: 'PORT', source: 'process.env', ... },
|
|
1422
|
+
// { type: 'env_loaded', key: 'API_KEY', source: 'process.env', ... },
|
|
1423
|
+
// { type: 'validation_success', ... }
|
|
1424
|
+
// ]
|
|
1425
|
+
|
|
1426
|
+
console.log('DB config audit:', dbAudit);
|
|
1427
|
+
// [
|
|
1428
|
+
// { type: 'env_loaded', key: 'DATABASE_URL', source: 'process.env', ... },
|
|
1429
|
+
// { type: 'env_loaded', key: 'DB_POOL_SIZE', source: 'process.env', ... },
|
|
1430
|
+
// { type: 'validation_success', ... }
|
|
1431
|
+
// ]
|
|
1432
|
+
```
|
|
1433
|
+
|
|
1434
|
+
**Use cases:**
|
|
1435
|
+
- Multi-tenant applications with separate configs per tenant
|
|
1436
|
+
- Microservices with different config sources
|
|
1437
|
+
- Testing/debugging specific configuration loads
|
|
1438
|
+
- Isolating audit trails in complex applications
|
|
1439
|
+
|
|
1292
1440
|
### Audit Event Types
|
|
1293
1441
|
|
|
1294
1442
|
| Event Type | When It Occurs |
|
|
@@ -1304,13 +1452,14 @@ console.log(getAuditLog());
|
|
|
1304
1452
|
Audit logs include cache metadata to help monitor performance:
|
|
1305
1453
|
|
|
1306
1454
|
```ts
|
|
1307
|
-
import {
|
|
1455
|
+
import { resolveAsync, getAuditLog } from 'node-env-resolver';
|
|
1308
1456
|
import { cached } from 'node-env-resolver/utils';
|
|
1457
|
+
import { postgres } from 'node-env-resolver/resolvers';
|
|
1309
1458
|
import { awsSecrets } from 'node-env-resolver-aws';
|
|
1310
1459
|
|
|
1311
|
-
const config = await
|
|
1460
|
+
const config = await resolveAsync(
|
|
1312
1461
|
[cached(awsSecrets({ secretId: 'prod/db' }), { ttl: 300000 }), {
|
|
1313
|
-
DATABASE_URL:
|
|
1462
|
+
DATABASE_URL: postgres(),
|
|
1314
1463
|
}],
|
|
1315
1464
|
{ enableAudit: true }
|
|
1316
1465
|
);
|
|
@@ -1332,15 +1481,16 @@ logs.forEach(log => {
|
|
|
1332
1481
|
Use audit logs for compliance and security monitoring:
|
|
1333
1482
|
|
|
1334
1483
|
```ts
|
|
1335
|
-
import {
|
|
1484
|
+
import { resolveAsync, processEnv, getAuditLog } from 'node-env-resolver';
|
|
1485
|
+
import { string } from 'node-env-resolver/resolvers';
|
|
1336
1486
|
import { awsSecrets } from 'node-env-resolver-aws';
|
|
1337
1487
|
|
|
1338
1488
|
// In production
|
|
1339
|
-
const config = await
|
|
1489
|
+
const config = await resolveAsync(
|
|
1340
1490
|
[processEnv(), {}],
|
|
1341
1491
|
[awsSecrets(), {
|
|
1342
|
-
DATABASE_PASSWORD:
|
|
1343
|
-
API_KEY:
|
|
1492
|
+
DATABASE_PASSWORD: string(),
|
|
1493
|
+
API_KEY: string(),
|
|
1344
1494
|
}],
|
|
1345
1495
|
{
|
|
1346
1496
|
policies: {
|