dotenv-gad 1.4.0 → 1.4.1
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 +92 -112
- package/dist/index.js +2 -1
- package/dist/utils.js +2 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -2,17 +2,18 @@
|
|
|
2
2
|
|
|
3
3
|
[](https://badge.fury.io/js/dotenv-gad)
|
|
4
4
|
[](https://opensource.org/licenses/MIT)
|
|
5
|
-
[](https://kasimlyee.github.io/dotenv-gad/
|
|
5
|
+
[](https://kasimlyee.github.io/dotenv-gad/)
|
|
6
6
|
|
|
7
|
-
**dotenv-gad** is an environment variable validation
|
|
7
|
+
**dotenv-gad** is an environment variable validation library that brings type safety, schema validation, and runtime checks to your Node.js and JavaScript applications. It works with any environment variable source — `.env` files, platform dashboards (Vercel, Railway, Docker), CI/CD pipelines, or `process.env` directly.
|
|
8
8
|
|
|
9
|
-
- Type-safe environment variables
|
|
10
|
-
- Schema validation
|
|
11
|
-
- Schema composition
|
|
12
|
-
- Automatic documentation generation
|
|
13
|
-
- TypeScript support
|
|
14
|
-
- CLI tooling
|
|
15
|
-
-
|
|
9
|
+
- Type-safe environment variables with full IntelliSense
|
|
10
|
+
- Schema validation (string, number, boolean, url, email, ip, port, json, array, object)
|
|
11
|
+
- Schema composition for modular configs
|
|
12
|
+
- Automatic documentation and `.env.example` generation
|
|
13
|
+
- First-class TypeScript support
|
|
14
|
+
- CLI tooling (check, sync, types, init, fix, docs)
|
|
15
|
+
- Sensitive value management and redaction
|
|
16
|
+
- Vite plugin with client-safe filtering and HMR
|
|
16
17
|
|
|
17
18
|
## Installation
|
|
18
19
|
|
|
@@ -55,22 +56,13 @@ const env = loadEnv(schema);
|
|
|
55
56
|
console.log(`Server running on port ${env.PORT}`);
|
|
56
57
|
```
|
|
57
58
|
|
|
58
|
-
|
|
59
|
+
`loadEnv` reads from both `process.env` and your `.env` file (if present). This means it works out of the box on platforms like Vercel, Railway, Docker, and AWS Lambda where variables are injected into `process.env` directly — no `.env` file required.
|
|
59
60
|
|
|
60
|
-
|
|
61
|
+
## Documentation
|
|
61
62
|
|
|
62
|
-
|
|
63
|
+
[](https://kasimlyee.github.io/dotenv-gad/)
|
|
63
64
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
```bash
|
|
67
|
-
npm ci
|
|
68
|
-
npm run docs:serve
|
|
69
|
-
```
|
|
70
|
-
|
|
71
|
-
Docs preview on PRs
|
|
72
|
-
|
|
73
|
-
When you open or update a pull request that changes docs, an automated preview will be published to GitHub Pages under `previews/pr-<number>/` and a comment with the preview link will be posted on the PR. This makes it easy to review documentation changes without merging.
|
|
65
|
+
Full documentation is available at [kasimlyee.github.io/dotenv-gad](https://kasimlyee.github.io/dotenv-gad/).
|
|
74
66
|
|
|
75
67
|
## CLI Commands
|
|
76
68
|
|
|
@@ -90,11 +82,12 @@ npx dotenv-gad types
|
|
|
90
82
|
|
|
91
83
|
## Vite Plugin
|
|
92
84
|
|
|
93
|
-
The Vite plugin
|
|
85
|
+
The Vite plugin validates environment variables at build time and exposes a typed, client-safe subset to your browser code via a virtual module.
|
|
94
86
|
|
|
95
|
-
###
|
|
87
|
+
### Setup
|
|
96
88
|
|
|
97
89
|
```typescript
|
|
90
|
+
// vite.config.ts
|
|
98
91
|
import { defineConfig } from "vite";
|
|
99
92
|
import dotenvGad from "dotenv-gad/vite";
|
|
100
93
|
|
|
@@ -102,38 +95,39 @@ export default defineConfig({
|
|
|
102
95
|
plugins: [
|
|
103
96
|
dotenvGad({
|
|
104
97
|
schemaPath: "./env.schema.ts",
|
|
105
|
-
clientPrefix: "VITE_",
|
|
106
|
-
publicKeys: [],
|
|
107
|
-
generatedTypes: true,
|
|
98
|
+
// clientPrefix: "VITE_", // default — keys matching this prefix are exposed
|
|
99
|
+
// publicKeys: [], // additional non-prefixed keys to expose
|
|
100
|
+
// generatedTypes: true, // generate .d.ts for IntelliSense
|
|
108
101
|
}),
|
|
109
102
|
],
|
|
110
103
|
});
|
|
111
104
|
```
|
|
112
105
|
|
|
113
|
-
###
|
|
106
|
+
### Usage
|
|
114
107
|
|
|
115
108
|
```typescript
|
|
116
109
|
import { env } from "dotenv-gad/client";
|
|
117
110
|
|
|
118
|
-
console.log(env.VITE_API_URL); // Full type safety &
|
|
111
|
+
console.log(env.VITE_API_URL); // Full type safety & autocomplete
|
|
119
112
|
```
|
|
120
113
|
|
|
121
114
|
### Key Features
|
|
122
115
|
|
|
123
|
-
- **Build-time validation
|
|
124
|
-
- **Client-safe filtering
|
|
125
|
-
- **
|
|
126
|
-
- **
|
|
127
|
-
- **HMR support
|
|
116
|
+
- **Build-time validation** — environment checked every dev/build cycle
|
|
117
|
+
- **Client-safe filtering** — only `VITE_*` prefixed variables (or custom `publicKeys`) exposed to browser
|
|
118
|
+
- **Sensitive protection** — variables marked `sensitive: true` are always excluded
|
|
119
|
+
- **Auto-generated types** — `dotenv-gad.d.ts` gives full IntelliSense on `env.`
|
|
120
|
+
- **HMR support** — hot reload on `.env` or schema changes during development
|
|
121
|
+
- **SSR safety** — server-side code gets the full env, not the filtered subset
|
|
128
122
|
|
|
129
123
|
## Features
|
|
130
124
|
|
|
131
125
|
### Core Validation
|
|
132
126
|
|
|
133
|
-
- Type checking (string, number, boolean, array, object)
|
|
134
|
-
- Required/optional fields
|
|
135
|
-
- Default values
|
|
127
|
+
- Type checking (string, number, boolean, array, object, url, email, ip, port, json, date)
|
|
128
|
+
- Required/optional fields with defaults
|
|
136
129
|
- Custom validation functions
|
|
130
|
+
- Value transforms
|
|
137
131
|
- Environment-specific rules
|
|
138
132
|
|
|
139
133
|
### Advanced Types
|
|
@@ -150,13 +144,23 @@ console.log(env.VITE_API_URL); // Full type safety & validation
|
|
|
150
144
|
}
|
|
151
145
|
```
|
|
152
146
|
|
|
153
|
-
###
|
|
147
|
+
### Schema Composition
|
|
148
|
+
|
|
149
|
+
Merge multiple schemas for modular configuration:
|
|
150
|
+
|
|
151
|
+
```typescript
|
|
152
|
+
import { defineSchema, composeSchema } from "dotenv-gad";
|
|
153
|
+
|
|
154
|
+
const baseSchema = defineSchema({
|
|
155
|
+
NODE_ENV: { type: "string", default: "development" },
|
|
156
|
+
});
|
|
154
157
|
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
158
|
+
const dbSchema = defineSchema({
|
|
159
|
+
DATABASE_URL: { type: "string", required: true, sensitive: true },
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
const schema = composeSchema(baseSchema, dbSchema);
|
|
163
|
+
```
|
|
160
164
|
|
|
161
165
|
### Secret Management
|
|
162
166
|
|
|
@@ -164,12 +168,37 @@ console.log(env.VITE_API_URL); // Full type safety & validation
|
|
|
164
168
|
{
|
|
165
169
|
API_KEY: {
|
|
166
170
|
type: 'string',
|
|
167
|
-
sensitive: true,
|
|
171
|
+
sensitive: true, // masked in errors, excluded from .env.example
|
|
168
172
|
validate: (val) => val.startsWith('sk_')
|
|
169
173
|
}
|
|
170
174
|
}
|
|
171
175
|
```
|
|
172
176
|
|
|
177
|
+
### Grouping / Namespaced Envs
|
|
178
|
+
|
|
179
|
+
Group related variables into a single validated object:
|
|
180
|
+
|
|
181
|
+
```typescript
|
|
182
|
+
const schema = defineSchema({
|
|
183
|
+
DATABASE: {
|
|
184
|
+
type: "object",
|
|
185
|
+
envPrefix: "DATABASE_", // optional; defaults to 'DATABASE_'
|
|
186
|
+
properties: {
|
|
187
|
+
DB_NAME: { type: "string", required: true },
|
|
188
|
+
PORT: { type: "port", default: 5432 },
|
|
189
|
+
PWD: { type: "string", sensitive: true },
|
|
190
|
+
},
|
|
191
|
+
},
|
|
192
|
+
});
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
Given `DATABASE_DB_NAME=mydb`, `DATABASE_PORT=5432`, `DATABASE_PWD=supersecret`:
|
|
196
|
+
|
|
197
|
+
```typescript
|
|
198
|
+
const env = loadEnv(schema);
|
|
199
|
+
// { DATABASE: { DB_NAME: 'mydb', PORT: 5432, PWD: 'supersecret' } }
|
|
200
|
+
```
|
|
201
|
+
|
|
173
202
|
## Framework Integrations
|
|
174
203
|
|
|
175
204
|
### Express.js
|
|
@@ -189,8 +218,6 @@ app.listen(env.PORT, () => {
|
|
|
189
218
|
|
|
190
219
|
### Next.js
|
|
191
220
|
|
|
192
|
-
Create `next.config.js`:
|
|
193
|
-
|
|
194
221
|
```javascript
|
|
195
222
|
const { loadEnv } = require("dotenv-gad");
|
|
196
223
|
const schema = require("./env.schema");
|
|
@@ -204,9 +231,7 @@ module.exports = {
|
|
|
204
231
|
};
|
|
205
232
|
```
|
|
206
233
|
|
|
207
|
-
##
|
|
208
|
-
|
|
209
|
-
Example error output:
|
|
234
|
+
## Error Reporting
|
|
210
235
|
|
|
211
236
|
```
|
|
212
237
|
Environment validation failed:
|
|
@@ -215,38 +240,20 @@ Environment validation failed:
|
|
|
215
240
|
- API_KEY: Must start with 'sk_' (received: "invalid")
|
|
216
241
|
```
|
|
217
242
|
|
|
218
|
-
|
|
243
|
+
Sensitive values are always masked in error output. Use `includeRaw` for local debugging:
|
|
219
244
|
|
|
220
|
-
```
|
|
221
|
-
// include raw values in errors (non-sensitive values only)
|
|
222
|
-
import { loadEnv } from "dotenv-gad";
|
|
245
|
+
```typescript
|
|
223
246
|
const env = loadEnv(schema, { includeRaw: true });
|
|
224
247
|
|
|
225
248
|
// or with finer control
|
|
226
249
|
import { EnvValidator } from "dotenv-gad";
|
|
227
|
-
const validator = new EnvValidator(schema, {
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
}
|
|
231
|
-
console.error(String(err));
|
|
232
|
-
}
|
|
250
|
+
const validator = new EnvValidator(schema, {
|
|
251
|
+
includeRaw: true,
|
|
252
|
+
includeSensitive: true,
|
|
253
|
+
});
|
|
233
254
|
```
|
|
234
255
|
|
|
235
|
-
##
|
|
236
|
-
|
|
237
|
-
### Environment-Specific Rules
|
|
238
|
-
|
|
239
|
-
```typescript
|
|
240
|
-
{
|
|
241
|
-
DEBUG: {
|
|
242
|
-
type: 'boolean',
|
|
243
|
-
env: {
|
|
244
|
-
development: { default: true },
|
|
245
|
-
production: { default: false }
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
```
|
|
256
|
+
## More Examples
|
|
250
257
|
|
|
251
258
|
### Custom Validators
|
|
252
259
|
|
|
@@ -260,7 +267,7 @@ try {
|
|
|
260
267
|
}
|
|
261
268
|
```
|
|
262
269
|
|
|
263
|
-
###
|
|
270
|
+
### Transforms
|
|
264
271
|
|
|
265
272
|
```typescript
|
|
266
273
|
{
|
|
@@ -271,49 +278,22 @@ try {
|
|
|
271
278
|
}
|
|
272
279
|
```
|
|
273
280
|
|
|
274
|
-
###
|
|
275
|
-
|
|
276
|
-
You can group related environment variables into a single object using `object` with `properties` and an optional `envPrefix` (defaults to `KEY_`):
|
|
281
|
+
### Environment-Specific Rules
|
|
277
282
|
|
|
278
|
-
```
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
type: '
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
PORT: { type: 'port', default: 5432 },
|
|
286
|
-
PWD: { type: 'string', sensitive: true }
|
|
283
|
+
```typescript
|
|
284
|
+
{
|
|
285
|
+
DEBUG: {
|
|
286
|
+
type: 'boolean',
|
|
287
|
+
env: {
|
|
288
|
+
development: { default: true },
|
|
289
|
+
production: { default: false }
|
|
287
290
|
}
|
|
288
291
|
}
|
|
289
|
-
}
|
|
290
|
-
```
|
|
291
|
-
|
|
292
|
-
Given the following environment:
|
|
293
|
-
|
|
294
|
-
```
|
|
295
|
-
DATABASE_DB_NAME=mydb
|
|
296
|
-
DATABASE_PORT=5432
|
|
297
|
-
DATABASE_PWD=supersecret
|
|
298
|
-
```
|
|
299
|
-
|
|
300
|
-
`loadEnv(schema)` will return:
|
|
301
|
-
|
|
302
|
-
```ts
|
|
303
|
-
{ DATABASE: { DB_NAME: 'mydb', PORT: 5432, PWD: 'supersecret' } }
|
|
292
|
+
}
|
|
304
293
|
```
|
|
305
294
|
|
|
306
|
-
Notes and behavior:
|
|
307
|
-
|
|
308
|
-
- The default `envPrefix` is `${KEY}_` (for `DATABASE` it's `DATABASE_`) if you don't specify `envPrefix`.
|
|
309
|
-
- Prefixed variables take precedence over a JSON top-level env var (e.g., `DATABASE` = '{...}'). If both are present, prefixed variables win and a warning is printed.
|
|
310
|
-
- In strict mode (`{ strict: true }`), unexpected subkeys inside a group (e.g., `DATABASE_EXTRA`) will cause validation to fail.
|
|
311
|
-
- `sensitive` and `includeRaw` behavior still applies for grouped properties: sensitive properties are still masked in errors unless `includeSensitive` is explicitly set.
|
|
312
|
-
|
|
313
|
-
The CLI `sync` command will now generate grouped entries in `.env.example` for object properties so it's easier to scaffold grouped configuration.
|
|
314
|
-
|
|
315
295
|
## License
|
|
316
296
|
|
|
317
297
|
MIT © [Kasim Lyee]
|
|
318
298
|
|
|
319
|
-
[Contributions](https://github.com/kasimlyee/dotenv-gad/blob/main/CONTRIBUTING.md) are welcome
|
|
299
|
+
[Contributions](https://github.com/kasimlyee/dotenv-gad/blob/main/CONTRIBUTING.md) are welcome!
|
package/dist/index.js
CHANGED
|
@@ -8,7 +8,8 @@ export { defineSchema, EnvAggregateError,
|
|
|
8
8
|
/** @deprecated Use `EnvAggregateError` instead. */
|
|
9
9
|
AggregateError, EnvValidationError, EnvValidator, loadEnv, createEnvProxy, composeSchema, };
|
|
10
10
|
export function validateEnv(schema, options) {
|
|
11
|
-
const
|
|
11
|
+
const fileEnv = dotenv.config({ debug: false, path: options?.path }).parsed || {};
|
|
12
|
+
const env = { ...process.env, ...fileEnv };
|
|
12
13
|
const validator = new EnvValidator(schema, options);
|
|
13
14
|
return validator.validate(env);
|
|
14
15
|
}
|
package/dist/utils.js
CHANGED
|
@@ -14,7 +14,8 @@ import { EnvValidator } from "./validator.js";
|
|
|
14
14
|
* @returns {InferEnv<S>} The validated environment variables.
|
|
15
15
|
*/
|
|
16
16
|
export function loadEnv(schema, options) {
|
|
17
|
-
const
|
|
17
|
+
const fileEnv = dotenv.config({ debug: false, path: options?.path }).parsed || {};
|
|
18
|
+
const env = { ...process.env, ...fileEnv };
|
|
18
19
|
const validator = new EnvValidator(schema, options);
|
|
19
20
|
return validator.validate(env);
|
|
20
21
|
}
|