dev-env-toolkit 0.1.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/LICENSE +21 -0
- package/README.md +205 -0
- package/dist/index.cjs +498 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +170 -0
- package/dist/index.d.ts +170 -0
- package/dist/index.js +491 -0
- package/dist/index.js.map +1 -0
- package/package.json +70 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Raju Kumar Yadav
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
# dev-env-toolkit
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/dev-env-toolkit)
|
|
4
|
+
[](https://www.npmjs.com/package/dev-env-toolkit)
|
|
5
|
+
[](https://opensource.org/licenses/MIT)
|
|
6
|
+
[](https://www.typescriptlang.org/)
|
|
7
|
+
[](https://www.npmjs.com/package/dev-env-toolkit)
|
|
8
|
+
|
|
9
|
+
> Type-safe environment variables for Node.js and TypeScript
|
|
10
|
+
|
|
11
|
+
A zero-dependency, TypeScript-first toolkit for managing environment variables with full type inference, validation, and helpful error messages.
|
|
12
|
+
|
|
13
|
+
## Features
|
|
14
|
+
|
|
15
|
+
- **Type-Safe**: Full TypeScript support with automatic type inference
|
|
16
|
+
- **Zero Dependencies**: No external runtime dependencies
|
|
17
|
+
- **Chainable API**: Intuitive Zod-like schema definition
|
|
18
|
+
- **Validation**: Built-in validators for strings, numbers, booleans, enums, and arrays
|
|
19
|
+
- **Helpful Errors**: Clear error messages showing exactly what went wrong
|
|
20
|
+
- **.env Support**: Built-in parser for .env files
|
|
21
|
+
|
|
22
|
+
## Installation
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
npm install dev-env-toolkit
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Quick Start
|
|
29
|
+
|
|
30
|
+
```typescript
|
|
31
|
+
import { env } from 'dev-env-toolkit';
|
|
32
|
+
|
|
33
|
+
// Define your environment schema
|
|
34
|
+
const config = env.create({
|
|
35
|
+
PORT: env.number().default(3000),
|
|
36
|
+
DATABASE_URL: env.string().url(),
|
|
37
|
+
NODE_ENV: env.enum(['development', 'production', 'test'] as const),
|
|
38
|
+
DEBUG: env.boolean().default(false),
|
|
39
|
+
API_KEYS: env.array().separator(','),
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
// Fully typed! TypeScript knows the exact types
|
|
43
|
+
console.log(config.PORT); // number
|
|
44
|
+
console.log(config.DATABASE_URL); // string
|
|
45
|
+
console.log(config.NODE_ENV); // 'development' | 'production' | 'test'
|
|
46
|
+
console.log(config.DEBUG); // boolean
|
|
47
|
+
console.log(config.API_KEYS); // string[]
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Validators
|
|
51
|
+
|
|
52
|
+
### String
|
|
53
|
+
|
|
54
|
+
```typescript
|
|
55
|
+
env.string() // Required string
|
|
56
|
+
env.string().optional() // Optional string
|
|
57
|
+
env.string().default('value') // With default value
|
|
58
|
+
env.string().min(5) // Minimum length
|
|
59
|
+
env.string().max(100) // Maximum length
|
|
60
|
+
env.string().url() // Valid URL format
|
|
61
|
+
env.string().email() // Valid email format
|
|
62
|
+
env.string().regex(/^[A-Z]+$/) // Custom regex pattern
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
### Number
|
|
66
|
+
|
|
67
|
+
```typescript
|
|
68
|
+
env.number() // Required number
|
|
69
|
+
env.number().optional() // Optional number
|
|
70
|
+
env.number().default(3000) // With default value
|
|
71
|
+
env.number().min(1) // Minimum value
|
|
72
|
+
env.number().max(65535) // Maximum value
|
|
73
|
+
env.number().int() // Must be integer
|
|
74
|
+
env.number().positive() // Must be positive
|
|
75
|
+
env.number().port() // Valid port (1-65535, integer)
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### Boolean
|
|
79
|
+
|
|
80
|
+
Accepts: `true`, `false`, `1`, `0`, `yes`, `no`, `on`, `off` (case-insensitive)
|
|
81
|
+
|
|
82
|
+
```typescript
|
|
83
|
+
env.boolean() // Required boolean
|
|
84
|
+
env.boolean().optional() // Optional boolean
|
|
85
|
+
env.boolean().default(false) // With default value
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### Enum
|
|
89
|
+
|
|
90
|
+
```typescript
|
|
91
|
+
env.enum(['development', 'production', 'test'] as const)
|
|
92
|
+
env.enum(['small', 'medium', 'large'] as const).default('medium')
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
### Array
|
|
96
|
+
|
|
97
|
+
```typescript
|
|
98
|
+
env.array() // Comma-separated by default
|
|
99
|
+
env.array().separator('|') // Custom separator
|
|
100
|
+
env.array().min(1) // Minimum items
|
|
101
|
+
env.array().max(10) // Maximum items
|
|
102
|
+
env.array().default(['a', 'b']) // Default value
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
## Loading .env Files
|
|
106
|
+
|
|
107
|
+
```typescript
|
|
108
|
+
import { env } from 'dev-env-toolkit';
|
|
109
|
+
|
|
110
|
+
// Load from .env file before parsing
|
|
111
|
+
const config = env.create({
|
|
112
|
+
DATABASE_URL: env.string(),
|
|
113
|
+
}, { path: '.env' });
|
|
114
|
+
|
|
115
|
+
// Or load manually
|
|
116
|
+
env.load('.env');
|
|
117
|
+
env.load('.env.local', { override: true }); // Override existing values
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
## Safe Parsing
|
|
121
|
+
|
|
122
|
+
Use `safeParse` to handle validation errors without throwing:
|
|
123
|
+
|
|
124
|
+
```typescript
|
|
125
|
+
import { env } from 'dev-env-toolkit';
|
|
126
|
+
|
|
127
|
+
const result = env.safeParse({
|
|
128
|
+
PORT: env.number(),
|
|
129
|
+
DATABASE_URL: env.string(),
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
if (result.success) {
|
|
133
|
+
console.log(result.data.PORT);
|
|
134
|
+
} else {
|
|
135
|
+
console.error('Validation failed:', result.errors);
|
|
136
|
+
// result.errors: [{ key: 'DATABASE_URL', message: '...' }]
|
|
137
|
+
}
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
## Error Messages
|
|
141
|
+
|
|
142
|
+
When validation fails, you get clear, actionable error messages:
|
|
143
|
+
|
|
144
|
+
```
|
|
145
|
+
Environment Validation Failed
|
|
146
|
+
|
|
147
|
+
Found 2 errors:
|
|
148
|
+
|
|
149
|
+
DATABASE_URL: Required environment variable is missing
|
|
150
|
+
|
|
151
|
+
PORT: Invalid number format
|
|
152
|
+
Received: not-a-number
|
|
153
|
+
|
|
154
|
+
Tip: Check your .env file or environment variables
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
## TypeScript Integration
|
|
158
|
+
|
|
159
|
+
The library provides full type inference:
|
|
160
|
+
|
|
161
|
+
```typescript
|
|
162
|
+
import { env, type InferEnv } from 'dev-env-toolkit';
|
|
163
|
+
|
|
164
|
+
const schema = {
|
|
165
|
+
PORT: env.number().default(3000),
|
|
166
|
+
DEBUG: env.boolean().optional(),
|
|
167
|
+
NODE_ENV: env.enum(['development', 'production'] as const),
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
// Extract the type
|
|
171
|
+
type Config = InferEnv<typeof schema>;
|
|
172
|
+
// { PORT: number; DEBUG: boolean | undefined; NODE_ENV: 'development' | 'production' }
|
|
173
|
+
|
|
174
|
+
const config = env.create(schema);
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
## API Reference
|
|
178
|
+
|
|
179
|
+
### `env.create(schema, options?)`
|
|
180
|
+
|
|
181
|
+
Create a type-safe configuration object from environment variables.
|
|
182
|
+
|
|
183
|
+
**Options:**
|
|
184
|
+
- `path?: string` - Path to .env file to load
|
|
185
|
+
- `throwOnError?: boolean` - Whether to throw on validation errors (default: `true`)
|
|
186
|
+
- `env?: Record<string, string>` - Custom environment object (default: `process.env`)
|
|
187
|
+
|
|
188
|
+
### `env.safeParse(schema, options?)`
|
|
189
|
+
|
|
190
|
+
Parse environment variables without throwing. Returns a result object.
|
|
191
|
+
|
|
192
|
+
### `env.load(path, options?)`
|
|
193
|
+
|
|
194
|
+
Load environment variables from a .env file into `process.env`.
|
|
195
|
+
|
|
196
|
+
**Options:**
|
|
197
|
+
- `override?: boolean` - Whether to override existing values (default: `false`)
|
|
198
|
+
|
|
199
|
+
### `env.parse(path)`
|
|
200
|
+
|
|
201
|
+
Parse a .env file and return the key-value pairs without modifying `process.env`.
|
|
202
|
+
|
|
203
|
+
## License
|
|
204
|
+
|
|
205
|
+
MIT
|