envconfig-kit 0.1.0 → 0.2.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 +136 -82
- package/dist/index.cjs +164 -44
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +74 -11
- package/dist/index.d.ts +74 -11
- package/dist/index.js +163 -44
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -13,12 +13,12 @@
|
|
|
13
13
|
|
|
14
14
|
- **Built-in .env loading** - no need for dotenv
|
|
15
15
|
- **Type-safe** - full TypeScript inference
|
|
16
|
-
- **Zero dependencies** - 3KB minified
|
|
16
|
+
- **Zero dependencies** - ~3KB minified
|
|
17
17
|
- **Fail-fast validation** - catches errors at startup
|
|
18
18
|
- **Actionable error messages** - tells you exactly how to fix issues
|
|
19
|
-
- **
|
|
20
|
-
- **
|
|
21
|
-
- **
|
|
19
|
+
- **Framework presets** - common patterns for database, redis, auth
|
|
20
|
+
- **Secret masking** - hide sensitive values in error messages
|
|
21
|
+
- **Custom validation** - add your own validation logic
|
|
22
22
|
|
|
23
23
|
## Install
|
|
24
24
|
|
|
@@ -33,7 +33,7 @@ import { env } from 'envconfig-kit'
|
|
|
33
33
|
|
|
34
34
|
const config = env({
|
|
35
35
|
PORT: { type: 'number', default: 3000 },
|
|
36
|
-
API_KEY: { type: 'string' },
|
|
36
|
+
API_KEY: { type: 'string', secret: true },
|
|
37
37
|
DEBUG: { type: 'boolean', default: false },
|
|
38
38
|
DATABASE_URL: { type: 'url' },
|
|
39
39
|
})
|
|
@@ -45,42 +45,141 @@ const config = env({
|
|
|
45
45
|
|
|
46
46
|
No need for `dotenv` - envconfig-kit automatically loads `.env` files.
|
|
47
47
|
|
|
48
|
-
## Built-in .env Loading
|
|
49
|
-
|
|
50
|
-
envconfig-kit automatically loads environment variables from:
|
|
51
|
-
- `.env`
|
|
52
|
-
- `.env.local`
|
|
53
|
-
- `.env.development` (based on NODE_ENV)
|
|
54
|
-
- `.env.production` (based on NODE_ENV)
|
|
55
|
-
|
|
56
|
-
```typescript
|
|
57
|
-
const config = env({
|
|
58
|
-
PORT: { type: 'number' }
|
|
59
|
-
})
|
|
60
|
-
// Automatically loads from .env files
|
|
61
|
-
```
|
|
62
|
-
|
|
63
48
|
## Supported Types
|
|
64
49
|
|
|
65
50
|
| Type | Validates | Example |
|
|
66
51
|
|------|-----------|---------|
|
|
67
52
|
| `string` | Any string | `"hello"` |
|
|
68
|
-
| `number` | Valid number | `"
|
|
53
|
+
| `number` | Valid number | `"3.14"` → `3.14` |
|
|
54
|
+
| `integer` | Whole numbers only | `"42"` → `42` |
|
|
69
55
|
| `boolean` | true/false, 1/0, yes/no | `"true"` → `true` |
|
|
70
56
|
| `url` | Valid URL with protocol | `"https://api.example.com"` |
|
|
71
57
|
| `email` | Valid email format | `"user@example.com"` |
|
|
72
58
|
| `port` | Number between 0-65535 | `"3000"` → `3000` |
|
|
59
|
+
| `json` | Valid JSON string | `'{"key":"value"}'` → `{key: "value"}` |
|
|
60
|
+
| `array` | Comma-separated values | `"a,b,c"` → `["a", "b", "c"]` |
|
|
61
|
+
| `enum` | One of allowed values | `"production"` |
|
|
62
|
+
| `regex` | Matches pattern | `"ABC-123"` |
|
|
63
|
+
|
|
64
|
+
## New in v0.2.0
|
|
65
|
+
|
|
66
|
+
### Enum Type
|
|
67
|
+
|
|
68
|
+
```typescript
|
|
69
|
+
const config = env({
|
|
70
|
+
NODE_ENV: {
|
|
71
|
+
type: 'enum',
|
|
72
|
+
values: ['development', 'staging', 'production'] as const,
|
|
73
|
+
default: 'development'
|
|
74
|
+
},
|
|
75
|
+
})
|
|
76
|
+
// config.NODE_ENV is typed as 'development' | 'staging' | 'production'
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
### Array Type
|
|
80
|
+
|
|
81
|
+
```typescript
|
|
82
|
+
const config = env({
|
|
83
|
+
ALLOWED_HOSTS: { type: 'array' }, // comma-separated by default
|
|
84
|
+
TAGS: { type: 'array', separator: '|' }, // custom separator
|
|
85
|
+
})
|
|
86
|
+
// config.ALLOWED_HOSTS is string[]
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### JSON Type
|
|
90
|
+
|
|
91
|
+
```typescript
|
|
92
|
+
const config = env({
|
|
93
|
+
FEATURE_FLAGS: { type: 'json' },
|
|
94
|
+
})
|
|
95
|
+
// Parse JSON from env var: FEATURE_FLAGS='{"darkMode":true}'
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
### Regex Type
|
|
99
|
+
|
|
100
|
+
```typescript
|
|
101
|
+
const config = env({
|
|
102
|
+
API_VERSION: {
|
|
103
|
+
type: 'regex',
|
|
104
|
+
pattern: /^v\d+\.\d+$/
|
|
105
|
+
},
|
|
106
|
+
})
|
|
107
|
+
// Only accepts values like "v1.0", "v2.1"
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
### Secret Masking
|
|
111
|
+
|
|
112
|
+
```typescript
|
|
113
|
+
const config = env({
|
|
114
|
+
API_KEY: { type: 'string', secret: true },
|
|
115
|
+
DATABASE_PASSWORD: { type: 'string', secret: true },
|
|
116
|
+
})
|
|
117
|
+
// Error messages show **** instead of actual values
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
### Custom Validation
|
|
121
|
+
|
|
122
|
+
```typescript
|
|
123
|
+
const config = env({
|
|
124
|
+
PORT: {
|
|
125
|
+
type: 'number',
|
|
126
|
+
validate: (value) => value >= 1024 && value <= 65535
|
|
127
|
+
},
|
|
128
|
+
})
|
|
129
|
+
// Throws if PORT is not in valid range
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
### Framework Presets
|
|
133
|
+
|
|
134
|
+
```typescript
|
|
135
|
+
import { env, presets } from 'envconfig-kit'
|
|
136
|
+
|
|
137
|
+
const config = env({
|
|
138
|
+
...presets.server(), // PORT, HOST, NODE_ENV
|
|
139
|
+
...presets.database(), // DATABASE_URL, DATABASE_HOST, etc.
|
|
140
|
+
...presets.redis(), // REDIS_URL, REDIS_HOST, etc.
|
|
141
|
+
...presets.auth(), // AUTH_JWT_SECRET, etc.
|
|
142
|
+
...presets.aws(), // AWS_ACCESS_KEY_ID, etc.
|
|
143
|
+
...presets.smtp(), // SMTP_HOST, SMTP_PORT, etc.
|
|
144
|
+
...presets.cors(), // CORS_ORIGINS, CORS_METHODS, etc.
|
|
145
|
+
})
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
Custom prefixes:
|
|
149
|
+
|
|
150
|
+
```typescript
|
|
151
|
+
const config = env({
|
|
152
|
+
...presets.database('DB_'), // DB_URL, DB_HOST, etc.
|
|
153
|
+
...presets.redis('CACHE_'), // CACHE_URL, CACHE_HOST, etc.
|
|
154
|
+
})
|
|
155
|
+
```
|
|
73
156
|
|
|
74
157
|
## Schema Options
|
|
75
158
|
|
|
76
159
|
```typescript
|
|
77
160
|
env({
|
|
161
|
+
// Required string
|
|
78
162
|
API_KEY: { type: 'string' },
|
|
163
|
+
|
|
164
|
+
// With default value
|
|
79
165
|
PORT: { type: 'number', default: 3000 },
|
|
166
|
+
|
|
167
|
+
// Optional (can be undefined)
|
|
80
168
|
OPTIONAL_VAR: { type: 'string', optional: true },
|
|
169
|
+
|
|
170
|
+
// Secret (masked in errors)
|
|
171
|
+
PASSWORD: { type: 'string', secret: true },
|
|
172
|
+
|
|
173
|
+
// With transform
|
|
81
174
|
TAGS: {
|
|
82
|
-
type: '
|
|
83
|
-
transform: (
|
|
175
|
+
type: 'array',
|
|
176
|
+
transform: (arr) => arr.map(s => s.toUpperCase())
|
|
177
|
+
},
|
|
178
|
+
|
|
179
|
+
// With custom validation
|
|
180
|
+
COUNT: {
|
|
181
|
+
type: 'integer',
|
|
182
|
+
validate: (n) => n > 0 && n < 100
|
|
84
183
|
},
|
|
85
184
|
})
|
|
86
185
|
```
|
|
@@ -96,13 +195,26 @@ When validation fails, you get clear errors with fix suggestions:
|
|
|
96
195
|
➜ Fix: Add to .env file
|
|
97
196
|
Example: API_KEY=your-value-here
|
|
98
197
|
|
|
99
|
-
PORT:
|
|
198
|
+
PORT: Invalid number: "abc"
|
|
100
199
|
➜ Fix: Provide a valid number
|
|
101
200
|
Example: PORT=3000
|
|
102
201
|
|
|
103
202
|
Run your app with valid environment variables or add them to .env
|
|
104
203
|
```
|
|
105
204
|
|
|
205
|
+
## Built-in .env Loading
|
|
206
|
+
|
|
207
|
+
envconfig-kit automatically loads environment variables from:
|
|
208
|
+
- `.env`
|
|
209
|
+
- `.env.local`
|
|
210
|
+
- `.env.${NODE_ENV}` (e.g., `.env.development`, `.env.production`)
|
|
211
|
+
|
|
212
|
+
Priority order (highest to lowest):
|
|
213
|
+
1. `process.env`
|
|
214
|
+
2. `.env.local`
|
|
215
|
+
3. `.env.${NODE_ENV}`
|
|
216
|
+
4. `.env`
|
|
217
|
+
|
|
106
218
|
## CLI Tools
|
|
107
219
|
|
|
108
220
|
```bash
|
|
@@ -119,35 +231,6 @@ npx envconfig-kit generate
|
|
|
119
231
|
npx envconfig-kit doctor
|
|
120
232
|
```
|
|
121
233
|
|
|
122
|
-
## Built-in .env Loading
|
|
123
|
-
|
|
124
|
-
envconfig-kit automatically loads environment variables from:
|
|
125
|
-
- `.env`
|
|
126
|
-
- `.env.local`
|
|
127
|
-
- `.env.development` (based on NODE_ENV)
|
|
128
|
-
- `.env.production` (based on NODE_ENV)
|
|
129
|
-
|
|
130
|
-
Priority order:
|
|
131
|
-
1. `process.env` (highest)
|
|
132
|
-
2. `.env.local`
|
|
133
|
-
3. `.env.${NODE_ENV}`
|
|
134
|
-
4. `.env` (lowest)
|
|
135
|
-
|
|
136
|
-
## Transform Values
|
|
137
|
-
|
|
138
|
-
```typescript
|
|
139
|
-
const config = env({
|
|
140
|
-
ALLOWED_ORIGINS: {
|
|
141
|
-
type: 'string',
|
|
142
|
-
transform: (value) => value.split(',')
|
|
143
|
-
},
|
|
144
|
-
API_URL: {
|
|
145
|
-
type: 'url',
|
|
146
|
-
transform: (value) => value.replace(/\/$/, '')
|
|
147
|
-
}
|
|
148
|
-
})
|
|
149
|
-
```
|
|
150
|
-
|
|
151
234
|
## Export Schema
|
|
152
235
|
|
|
153
236
|
```typescript
|
|
@@ -168,35 +251,6 @@ console.log(generateDotenvExample(schema))
|
|
|
168
251
|
console.log(schemaToJSON(schema))
|
|
169
252
|
```
|
|
170
253
|
|
|
171
|
-
## Advanced Usage
|
|
172
|
-
|
|
173
|
-
```typescript
|
|
174
|
-
import { env } from 'envconfig-kit'
|
|
175
|
-
|
|
176
|
-
const config = env({
|
|
177
|
-
DATABASE_URL: { type: 'url' },
|
|
178
|
-
API_KEY: { type: 'string' },
|
|
179
|
-
PORT: { type: 'number', default: 3000 },
|
|
180
|
-
HOST: { type: 'string', default: 'localhost' },
|
|
181
|
-
DEBUG: { type: 'boolean', default: false },
|
|
182
|
-
REDIS_URL: { type: 'url', optional: true },
|
|
183
|
-
CORS_ORIGINS: {
|
|
184
|
-
type: 'string',
|
|
185
|
-
default: 'http://localhost:3000',
|
|
186
|
-
transform: (value) => value.split(',').map(s => s.trim())
|
|
187
|
-
},
|
|
188
|
-
})
|
|
189
|
-
|
|
190
|
-
// TypeScript knows the exact types
|
|
191
|
-
config.PORT // number
|
|
192
|
-
config.API_KEY // string
|
|
193
|
-
config.DEBUG // boolean
|
|
194
|
-
config.REDIS_URL // string | undefined
|
|
195
|
-
config.CORS_ORIGINS // string[] (after transform)
|
|
196
|
-
```
|
|
197
|
-
|
|
198
254
|
## License
|
|
199
255
|
|
|
200
256
|
MIT
|
|
201
|
-
|
|
202
|
-
|
package/dist/index.cjs
CHANGED
|
@@ -24,6 +24,7 @@ __export(index_exports, {
|
|
|
24
24
|
generateDotenvExample: () => generateDotenvExample,
|
|
25
25
|
loadDotenv: () => loadDotenv,
|
|
26
26
|
parseDotenv: () => parseDotenv,
|
|
27
|
+
presets: () => presets,
|
|
27
28
|
schemaToJSON: () => schemaToJSON,
|
|
28
29
|
schemaToMarkdown: () => schemaToMarkdown,
|
|
29
30
|
validators: () => validators
|
|
@@ -34,22 +35,29 @@ module.exports = __toCommonJS(index_exports);
|
|
|
34
35
|
var import_path = require("path");
|
|
35
36
|
|
|
36
37
|
// src/validators.ts
|
|
38
|
+
var TRUTHY_VALUES = /* @__PURE__ */ new Set(["true", "1", "yes"]);
|
|
39
|
+
var FALSY_VALUES = /* @__PURE__ */ new Set(["false", "0", "no"]);
|
|
40
|
+
var EMAIL_REGEX = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
37
41
|
var validators = {
|
|
38
42
|
string: (value) => value,
|
|
39
43
|
number: (value) => {
|
|
40
|
-
if (value
|
|
41
|
-
throw new Error("Cannot parse empty string as number");
|
|
42
|
-
}
|
|
44
|
+
if (!value) throw new Error("Cannot parse empty string as number");
|
|
43
45
|
const num = Number(value);
|
|
44
|
-
if (isNaN(num)) {
|
|
45
|
-
|
|
46
|
+
if (Number.isNaN(num)) throw new Error(`Invalid number: "${value}"`);
|
|
47
|
+
return num;
|
|
48
|
+
},
|
|
49
|
+
integer: (value) => {
|
|
50
|
+
if (!value) throw new Error("Cannot parse empty string as integer");
|
|
51
|
+
const num = Number(value);
|
|
52
|
+
if (Number.isNaN(num) || !Number.isInteger(num)) {
|
|
53
|
+
throw new Error(`Invalid integer: "${value}"`);
|
|
46
54
|
}
|
|
47
55
|
return num;
|
|
48
56
|
},
|
|
49
57
|
boolean: (value) => {
|
|
50
58
|
const lower = value.toLowerCase();
|
|
51
|
-
if (lower
|
|
52
|
-
if (lower
|
|
59
|
+
if (TRUTHY_VALUES.has(lower)) return true;
|
|
60
|
+
if (FALSY_VALUES.has(lower)) return false;
|
|
53
61
|
throw new Error(`Invalid boolean: "${value}"`);
|
|
54
62
|
},
|
|
55
63
|
url: (value) => {
|
|
@@ -61,53 +69,72 @@ var validators = {
|
|
|
61
69
|
}
|
|
62
70
|
},
|
|
63
71
|
email: (value) => {
|
|
64
|
-
|
|
65
|
-
if (!emailRegex.test(value)) {
|
|
66
|
-
throw new Error(`Invalid email: "${value}"`);
|
|
67
|
-
}
|
|
72
|
+
if (!EMAIL_REGEX.test(value)) throw new Error(`Invalid email: "${value}"`);
|
|
68
73
|
return value;
|
|
69
74
|
},
|
|
70
75
|
port: (value) => {
|
|
71
76
|
const num = Number(value);
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
}
|
|
77
|
+
const isValidPort = !Number.isNaN(num) && Number.isInteger(num) && num >= 0 && num <= 65535;
|
|
78
|
+
if (!isValidPort) throw new Error(`Invalid port: "${value}"`);
|
|
75
79
|
return num;
|
|
80
|
+
},
|
|
81
|
+
json: (value) => {
|
|
82
|
+
try {
|
|
83
|
+
return JSON.parse(value);
|
|
84
|
+
} catch {
|
|
85
|
+
throw new Error(`Invalid JSON: "${value}"`);
|
|
86
|
+
}
|
|
87
|
+
},
|
|
88
|
+
array: (value, config) => {
|
|
89
|
+
const separator = config?.separator ?? ",";
|
|
90
|
+
return value.split(separator).map((item) => item.trim()).filter(Boolean);
|
|
91
|
+
},
|
|
92
|
+
enum: (value, config) => {
|
|
93
|
+
const values = config?.values;
|
|
94
|
+
if (!values) throw new Error('Enum type requires "values" array');
|
|
95
|
+
if (!values.includes(value)) {
|
|
96
|
+
throw new Error(`Invalid enum value: "${value}". Expected one of: ${values.join(", ")}`);
|
|
97
|
+
}
|
|
98
|
+
return value;
|
|
99
|
+
},
|
|
100
|
+
regex: (value, config) => {
|
|
101
|
+
const pattern = config?.pattern;
|
|
102
|
+
if (!pattern) throw new Error('Regex type requires "pattern"');
|
|
103
|
+
if (!pattern.test(value)) {
|
|
104
|
+
throw new Error(`Value "${value}" does not match pattern ${pattern}`);
|
|
105
|
+
}
|
|
106
|
+
return value;
|
|
76
107
|
}
|
|
77
108
|
};
|
|
78
109
|
|
|
79
110
|
// src/dotenv.ts
|
|
80
111
|
var import_fs = require("fs");
|
|
81
|
-
|
|
82
|
-
if (!(0, import_fs.existsSync)(path)) {
|
|
83
|
-
return {};
|
|
84
|
-
}
|
|
112
|
+
var loadDotenv = (path) => {
|
|
113
|
+
if (!(0, import_fs.existsSync)(path)) return {};
|
|
85
114
|
try {
|
|
86
115
|
const content = (0, import_fs.readFileSync)(path, "utf-8");
|
|
87
116
|
return parseDotenv(content);
|
|
88
117
|
} catch {
|
|
89
118
|
return {};
|
|
90
119
|
}
|
|
91
|
-
}
|
|
92
|
-
|
|
120
|
+
};
|
|
121
|
+
var parseDotenv = (content) => {
|
|
93
122
|
const result = {};
|
|
94
|
-
const
|
|
95
|
-
for (const line of lines) {
|
|
123
|
+
for (const line of content.split("\n")) {
|
|
96
124
|
const trimmed = line.trim();
|
|
97
|
-
if (!trimmed || trimmed.startsWith("#"))
|
|
98
|
-
continue;
|
|
99
|
-
}
|
|
125
|
+
if (!trimmed || trimmed.startsWith("#")) continue;
|
|
100
126
|
const match = trimmed.match(/^([^=]+)=(.*)$/);
|
|
101
127
|
if (!match) continue;
|
|
102
128
|
const key = match[1].trim();
|
|
103
129
|
let value = match[2].trim();
|
|
104
|
-
|
|
130
|
+
const isQuoted = value.startsWith('"') && value.endsWith('"') || value.startsWith("'") && value.endsWith("'");
|
|
131
|
+
if (isQuoted) {
|
|
105
132
|
value = value.slice(1, -1);
|
|
106
133
|
}
|
|
107
134
|
result[key] = value;
|
|
108
135
|
}
|
|
109
136
|
return result;
|
|
110
|
-
}
|
|
137
|
+
};
|
|
111
138
|
|
|
112
139
|
// src/errors.ts
|
|
113
140
|
function formatValidationErrors(errors) {
|
|
@@ -129,10 +156,15 @@ function createMissingError(key, type) {
|
|
|
129
156
|
const examples = {
|
|
130
157
|
string: `${key}=your-value-here`,
|
|
131
158
|
number: `${key}=3000`,
|
|
159
|
+
integer: `${key}=42`,
|
|
132
160
|
boolean: `${key}=true`,
|
|
133
161
|
url: `${key}=https://example.com`,
|
|
134
162
|
email: `${key}=user@example.com`,
|
|
135
|
-
port: `${key}=3000
|
|
163
|
+
port: `${key}=3000`,
|
|
164
|
+
json: `${key}={"key":"value"}`,
|
|
165
|
+
array: `${key}=item1,item2,item3`,
|
|
166
|
+
enum: `${key}=value`,
|
|
167
|
+
regex: `${key}=matching-value`
|
|
136
168
|
};
|
|
137
169
|
return {
|
|
138
170
|
key,
|
|
@@ -145,18 +177,28 @@ function createTypeError(key, value, type, details) {
|
|
|
145
177
|
const suggestions = {
|
|
146
178
|
string: "Provide a string value",
|
|
147
179
|
number: "Provide a valid number",
|
|
180
|
+
integer: "Provide a whole number (no decimals)",
|
|
148
181
|
boolean: "Use true/false, 1/0, yes/no",
|
|
149
182
|
url: "Provide a valid URL with protocol",
|
|
150
183
|
email: "Provide a valid email address",
|
|
151
|
-
port: "Provide a number between 0-65535"
|
|
184
|
+
port: "Provide a number between 0-65535",
|
|
185
|
+
json: "Provide valid JSON",
|
|
186
|
+
array: "Provide comma-separated values",
|
|
187
|
+
enum: "Provide one of the allowed values",
|
|
188
|
+
regex: "Provide a value matching the pattern"
|
|
152
189
|
};
|
|
153
190
|
const examples = {
|
|
154
191
|
string: `${key}=hello`,
|
|
155
192
|
number: `${key}=42`,
|
|
193
|
+
integer: `${key}=42`,
|
|
156
194
|
boolean: `${key}=true`,
|
|
157
195
|
url: `${key}=https://example.com`,
|
|
158
196
|
email: `${key}=user@example.com`,
|
|
159
|
-
port: `${key}=3000
|
|
197
|
+
port: `${key}=3000`,
|
|
198
|
+
json: `${key}={"key":"value"}`,
|
|
199
|
+
array: `${key}=a,b,c`,
|
|
200
|
+
enum: `${key}=allowed-value`,
|
|
201
|
+
regex: `${key}=matching-value`
|
|
160
202
|
};
|
|
161
203
|
return {
|
|
162
204
|
key,
|
|
@@ -167,11 +209,14 @@ function createTypeError(key, value, type, details) {
|
|
|
167
209
|
}
|
|
168
210
|
|
|
169
211
|
// src/env.ts
|
|
170
|
-
|
|
212
|
+
var env = (schema, options = {}) => {
|
|
171
213
|
const vars = loadEnvFiles();
|
|
172
214
|
const source = { ...vars, ...process.env };
|
|
173
215
|
const result = {};
|
|
174
216
|
const errors = [];
|
|
217
|
+
const secrets = new Set(
|
|
218
|
+
Object.entries(schema).filter(([, config]) => config.secret).map(([key]) => key)
|
|
219
|
+
);
|
|
175
220
|
for (const [key, config] of Object.entries(schema)) {
|
|
176
221
|
const rawValue = source[key];
|
|
177
222
|
if (rawValue === void 0 || rawValue === "") {
|
|
@@ -187,36 +232,48 @@ function env(schema) {
|
|
|
187
232
|
continue;
|
|
188
233
|
}
|
|
189
234
|
try {
|
|
190
|
-
let validated = validators[config.type](rawValue);
|
|
235
|
+
let validated = validators[config.type](rawValue, config);
|
|
191
236
|
if (config.transform) {
|
|
192
237
|
validated = config.transform(validated);
|
|
193
238
|
}
|
|
239
|
+
if (config.validate && !config.validate(validated)) {
|
|
240
|
+
const displayValue = shouldMask(key, secrets, options) ? "****" : String(rawValue);
|
|
241
|
+
errors.push({
|
|
242
|
+
key,
|
|
243
|
+
message: `Custom validation failed for value "${displayValue}"`,
|
|
244
|
+
suggestion: "Check the validate function requirements"
|
|
245
|
+
});
|
|
246
|
+
continue;
|
|
247
|
+
}
|
|
194
248
|
result[key] = validated;
|
|
195
249
|
} catch (error) {
|
|
250
|
+
const isMasked = shouldMask(key, secrets, options);
|
|
251
|
+
const displayValue = isMasked ? "****" : String(rawValue);
|
|
196
252
|
const details = error instanceof Error ? error.message : String(error);
|
|
197
|
-
|
|
253
|
+
const maskedDetails = isMasked ? details.replace(new RegExp(escapeRegExp(rawValue), "g"), "****") : details;
|
|
254
|
+
errors.push(createTypeError(key, displayValue, config.type, maskedDetails));
|
|
198
255
|
}
|
|
199
256
|
}
|
|
200
257
|
if (errors.length > 0) {
|
|
201
258
|
throw new Error(formatValidationErrors(errors));
|
|
202
259
|
}
|
|
203
260
|
return result;
|
|
204
|
-
}
|
|
205
|
-
|
|
261
|
+
};
|
|
262
|
+
var shouldMask = (key, secrets, options) => options.maskSecrets !== false && secrets.has(key);
|
|
263
|
+
var escapeRegExp = (str) => str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
264
|
+
var loadEnvFiles = () => {
|
|
206
265
|
const cwd = process.cwd();
|
|
207
266
|
const envName = process.env.NODE_ENV;
|
|
208
267
|
const paths = [
|
|
209
268
|
(0, import_path.resolve)(cwd, ".env"),
|
|
210
|
-
envName
|
|
269
|
+
envName && (0, import_path.resolve)(cwd, `.env.${envName}`),
|
|
211
270
|
(0, import_path.resolve)(cwd, ".env.local")
|
|
212
|
-
].filter((path) => path
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
return vars;
|
|
219
|
-
}
|
|
271
|
+
].filter((path) => Boolean(path));
|
|
272
|
+
return paths.reduce(
|
|
273
|
+
(vars, path) => ({ ...vars, ...loadDotenv(path) }),
|
|
274
|
+
{}
|
|
275
|
+
);
|
|
276
|
+
};
|
|
220
277
|
|
|
221
278
|
// src/export.ts
|
|
222
279
|
function schemaToJSON(schema) {
|
|
@@ -260,12 +317,75 @@ function generateDotenvExample(schema) {
|
|
|
260
317
|
}
|
|
261
318
|
return sections.join("\n");
|
|
262
319
|
}
|
|
320
|
+
|
|
321
|
+
// src/presets.ts
|
|
322
|
+
var presets = {
|
|
323
|
+
server: (prefix = "") => ({
|
|
324
|
+
[`${prefix}PORT`]: { type: "port", default: 3e3 },
|
|
325
|
+
[`${prefix}HOST`]: { type: "string", default: "localhost" },
|
|
326
|
+
[`${prefix}NODE_ENV`]: {
|
|
327
|
+
type: "enum",
|
|
328
|
+
values: ["development", "staging", "production", "test"],
|
|
329
|
+
default: "development"
|
|
330
|
+
}
|
|
331
|
+
}),
|
|
332
|
+
database: (prefix = "DATABASE_") => ({
|
|
333
|
+
[`${prefix}URL`]: { type: "url" },
|
|
334
|
+
[`${prefix}HOST`]: { type: "string", default: "localhost", optional: true },
|
|
335
|
+
[`${prefix}PORT`]: { type: "port", default: 5432, optional: true },
|
|
336
|
+
[`${prefix}NAME`]: { type: "string", optional: true },
|
|
337
|
+
[`${prefix}USER`]: { type: "string", optional: true },
|
|
338
|
+
[`${prefix}PASSWORD`]: { type: "string", secret: true, optional: true },
|
|
339
|
+
[`${prefix}SSL`]: { type: "boolean", default: false, optional: true }
|
|
340
|
+
}),
|
|
341
|
+
redis: (prefix = "REDIS_") => ({
|
|
342
|
+
[`${prefix}URL`]: { type: "url", optional: true },
|
|
343
|
+
[`${prefix}HOST`]: { type: "string", default: "localhost", optional: true },
|
|
344
|
+
[`${prefix}PORT`]: { type: "port", default: 6379, optional: true },
|
|
345
|
+
[`${prefix}PASSWORD`]: { type: "string", secret: true, optional: true },
|
|
346
|
+
[`${prefix}DB`]: { type: "integer", default: 0, optional: true },
|
|
347
|
+
[`${prefix}TLS`]: { type: "boolean", default: false, optional: true }
|
|
348
|
+
}),
|
|
349
|
+
auth: (prefix = "AUTH_") => ({
|
|
350
|
+
[`${prefix}JWT_SECRET`]: { type: "string", secret: true },
|
|
351
|
+
[`${prefix}JWT_EXPIRES_IN`]: { type: "string", default: "1d", optional: true },
|
|
352
|
+
[`${prefix}BCRYPT_ROUNDS`]: { type: "integer", default: 10, optional: true }
|
|
353
|
+
}),
|
|
354
|
+
aws: (prefix = "AWS_") => ({
|
|
355
|
+
[`${prefix}ACCESS_KEY_ID`]: { type: "string", secret: true },
|
|
356
|
+
[`${prefix}SECRET_ACCESS_KEY`]: { type: "string", secret: true },
|
|
357
|
+
[`${prefix}REGION`]: { type: "string", default: "us-east-1" },
|
|
358
|
+
[`${prefix}S3_BUCKET`]: { type: "string", optional: true }
|
|
359
|
+
}),
|
|
360
|
+
smtp: (prefix = "SMTP_") => ({
|
|
361
|
+
[`${prefix}HOST`]: { type: "string" },
|
|
362
|
+
[`${prefix}PORT`]: { type: "port", default: 587 },
|
|
363
|
+
[`${prefix}USER`]: { type: "string", optional: true },
|
|
364
|
+
[`${prefix}PASSWORD`]: { type: "string", secret: true, optional: true },
|
|
365
|
+
[`${prefix}FROM`]: { type: "email", optional: true },
|
|
366
|
+
[`${prefix}SECURE`]: { type: "boolean", default: false, optional: true }
|
|
367
|
+
}),
|
|
368
|
+
cors: (prefix = "CORS_") => ({
|
|
369
|
+
[`${prefix}ORIGINS`]: {
|
|
370
|
+
type: "array",
|
|
371
|
+
default: ["http://localhost:3000"],
|
|
372
|
+
optional: true
|
|
373
|
+
},
|
|
374
|
+
[`${prefix}METHODS`]: {
|
|
375
|
+
type: "array",
|
|
376
|
+
default: ["GET", "POST", "PUT", "DELETE"],
|
|
377
|
+
optional: true
|
|
378
|
+
},
|
|
379
|
+
[`${prefix}CREDENTIALS`]: { type: "boolean", default: true, optional: true }
|
|
380
|
+
})
|
|
381
|
+
};
|
|
263
382
|
// Annotate the CommonJS export names for ESM import in node:
|
|
264
383
|
0 && (module.exports = {
|
|
265
384
|
env,
|
|
266
385
|
generateDotenvExample,
|
|
267
386
|
loadDotenv,
|
|
268
387
|
parseDotenv,
|
|
388
|
+
presets,
|
|
269
389
|
schemaToJSON,
|
|
270
390
|
schemaToMarkdown,
|
|
271
391
|
validators
|