zenvx 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 +142 -0
- package/dist/index.cjs +175 -0
- package/dist/index.d.cts +50 -0
- package/dist/index.d.ts +50 -0
- package/dist/index.js +139 -0
- package/package.json +53 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Sean Wilfred T. Custodio
|
|
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,142 @@
|
|
|
1
|
+
# zenvx
|
|
2
|
+
|
|
3
|
+
**Type-safe environment variables with Zen-like peace of mind.**
|
|
4
|
+
|
|
5
|
+
`zenvx` is a lightweight wrapper around **Zod** and **dotenv** designed to fix the common headaches of environment variables in Node.js / TypeScript. It provides strict validation (blocking `"123"` as an API Key), smart coercion, and beautiful, human-readable error reporting.
|
|
6
|
+
|
|
7
|
+

|
|
8
|
+

|
|
9
|
+

|
|
10
|
+
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
## Why zenvx?
|
|
14
|
+
|
|
15
|
+
Standard Zod is great, but environment variables are always strings. This leads to common pitfalls:
|
|
16
|
+
|
|
17
|
+
- `z.string()` accepts `"12345"`, which is usually a mistake for API keys
|
|
18
|
+
- `z.boolean()` fails on `"true"` strings
|
|
19
|
+
- Validation errors are often ugly JSON dumps that clog your terminal
|
|
20
|
+
|
|
21
|
+
**zenvx solves this:**
|
|
22
|
+
|
|
23
|
+
1. **Strict Validators** – `tx.string()` ensures values are text, not just numbers
|
|
24
|
+
2. **Smart Coercion** – Automatically handles ports, numbers, and booleans
|
|
25
|
+
3. **Beautiful Errors** – Formatting that tells you exactly what to fix
|
|
26
|
+
|
|
27
|
+
---
|
|
28
|
+
|
|
29
|
+
## Installation
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
npm install zenvx zod
|
|
33
|
+
# or
|
|
34
|
+
pnpm add zenvx zod
|
|
35
|
+
# or
|
|
36
|
+
yarn add zenvx zod
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
> Note: Zod is a peer dependency, so you must have it installed.
|
|
40
|
+
|
|
41
|
+
## Quick Start
|
|
42
|
+
|
|
43
|
+
Create a file (e.g., src/env.ts) and export your configuration:
|
|
44
|
+
|
|
45
|
+
```javascript
|
|
46
|
+
import { defineEnv, tx } from "zenvx";
|
|
47
|
+
|
|
48
|
+
export const env = defineEnv({
|
|
49
|
+
// 1. Smart Coercion
|
|
50
|
+
PORT: tx.port(), // Coerces "3000" -> 3000
|
|
51
|
+
DEBUG: tx.bool(), // Coerces "true"/"1" -> true
|
|
52
|
+
|
|
53
|
+
// 2. Strict Validation
|
|
54
|
+
DATABASE_URL: tx.url(), // Must be a valid URL
|
|
55
|
+
API_KEY: tx.string(), // "12345" will FAIL (Must be text)
|
|
56
|
+
|
|
57
|
+
// 3. Native Zod Support (optional)
|
|
58
|
+
NODE_ENV: tx.enum(["development", "production"]),
|
|
59
|
+
});
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
Now use it anywhere in your app:
|
|
63
|
+
|
|
64
|
+
```javascript
|
|
65
|
+
import { env } from "./env";
|
|
66
|
+
|
|
67
|
+
console.log(`Server running on port ${env.PORT}`);
|
|
68
|
+
// TypeScript knows env.PORT is a number!
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
## Beautiful Error Handling
|
|
72
|
+
|
|
73
|
+
If your .env file is missing values or has invalid types, zenvx stops the process immediately and prints a clear message:
|
|
74
|
+
|
|
75
|
+
```
|
|
76
|
+
┌──────────────────────────────────────────────┐
|
|
77
|
+
│ ❌ INVALID ENVIRONMENT VARIABLES DETECTED │
|
|
78
|
+
└──────────────────────────────────────────────┘
|
|
79
|
+
PORT: Must be a valid port (1-65535)
|
|
80
|
+
API_KEY: Value should be text, but looks like a number.
|
|
81
|
+
DATABASE_URL: Must be a valid URL
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
# The `tx` Validator Helper
|
|
85
|
+
|
|
86
|
+
`zenvx` provides a `tx` object with pre-configured Zod schemas optimized for `.env` files.
|
|
87
|
+
|
|
88
|
+
| Validator | Description | Example Input (.env) | Result (JS) |
|
|
89
|
+
| ---------------- | ------------------------------------------------------------------ | -------------------- | --------------- |
|
|
90
|
+
| `tx.string()` | Strict string. Rejects purely numeric values (prevents lazy keys). | `abc_key` | `"abc_key"` |
|
|
91
|
+
| `tx.number()` | Coerces string to number. | `"50"` | `50` |
|
|
92
|
+
| `tx.bool()` | Smart boolean. Accepts true, false, 1, 0. | `"true"`, `"1"` | `true` |
|
|
93
|
+
| `tx.port()` | Validates port range (1-65535). | `"3000"` | `3000` |
|
|
94
|
+
| `tx.url()` | Strict URL validation. | `"https://site.com"` | `"https://..."` |
|
|
95
|
+
| `tx.email()` | Valid email address. | `"admin@app.com"` | `"admin@..."` |
|
|
96
|
+
| `tx.json()` | Parses a JSON string into an Object. | `{"foo":"bar"}` | `{foo: "bar"}` |
|
|
97
|
+
| `tx.enum([...])` | Strict allow-list. | `"PROD"` | `"PROD"` |
|
|
98
|
+
| `tx.ip()` | Validates IPv4 format. | `"192.168.1.1"` | `"192..."` |
|
|
99
|
+
|
|
100
|
+
## Customizing Error Messages
|
|
101
|
+
|
|
102
|
+
Every tx validator accepts an optional custom error message.
|
|
103
|
+
|
|
104
|
+
```javascript
|
|
105
|
+
export const env = defineEnv({
|
|
106
|
+
API_KEY: tx.string("Please provide a REAL API Key, not just numbers!"),
|
|
107
|
+
PORT: tx.port("Port is invalid or out of range"),
|
|
108
|
+
});
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
## Advanced Configuration
|
|
112
|
+
|
|
113
|
+
Custom .env Path
|
|
114
|
+
|
|
115
|
+
By default, zenvx looks for .env in your project root. You can change this:
|
|
116
|
+
|
|
117
|
+
```javascript
|
|
118
|
+
export const env = defineEnv(
|
|
119
|
+
{
|
|
120
|
+
PORT: tx.port(),
|
|
121
|
+
},
|
|
122
|
+
{
|
|
123
|
+
path: "./config/.env.production",
|
|
124
|
+
},
|
|
125
|
+
);
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
## Mixing with Standard Zod
|
|
129
|
+
|
|
130
|
+
You can mix tx helpers with standard Zod schemas if you need specific logic.
|
|
131
|
+
|
|
132
|
+
```javascript
|
|
133
|
+
import { defineEnv, tx } from "zenvx";
|
|
134
|
+
import { z } from "zod";
|
|
135
|
+
|
|
136
|
+
export const env = defineEnv({
|
|
137
|
+
PORT: tx.port(),
|
|
138
|
+
|
|
139
|
+
// Standard Zod Schema
|
|
140
|
+
APP_NAME: z.string().min(5).default("My Super App"),
|
|
141
|
+
});
|
|
142
|
+
```
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
defineEnv: () => defineEnv,
|
|
34
|
+
tx: () => tx
|
|
35
|
+
});
|
|
36
|
+
module.exports = __toCommonJS(index_exports);
|
|
37
|
+
var import_zod2 = require("zod");
|
|
38
|
+
|
|
39
|
+
// src/loaders/dotenv.ts
|
|
40
|
+
var import_dotenv = __toESM(require("dotenv"), 1);
|
|
41
|
+
var import_fs = __toESM(require("fs"), 1);
|
|
42
|
+
function loadDotEnv(path = ".env") {
|
|
43
|
+
if (!import_fs.default.existsSync(path)) {
|
|
44
|
+
throw new Error(
|
|
45
|
+
`[envx] .env file not found at path "${path}"
|
|
46
|
+
Tip: Set { dotenv: false } if you manage environment variables yourself.`
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
const result = import_dotenv.default.config({ path });
|
|
50
|
+
if (result.error) {
|
|
51
|
+
throw result.error;
|
|
52
|
+
}
|
|
53
|
+
return result.parsed ?? {};
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// src/core/parser.ts
|
|
57
|
+
var import_zod = require("zod");
|
|
58
|
+
function parseEnv(schema, source) {
|
|
59
|
+
const result = schema.safeParse(source);
|
|
60
|
+
if (!result.success) {
|
|
61
|
+
const issues = result.error.issues.map((i) => `\u274C ${i.path.join(".")}: ${i.message}`).join("\n");
|
|
62
|
+
throw new Error(
|
|
63
|
+
[
|
|
64
|
+
"",
|
|
65
|
+
"\u250C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510",
|
|
66
|
+
"\u2502 \u274C INVALID ENVIRONMENT VARIABLES DETECTED \u2502",
|
|
67
|
+
"\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518",
|
|
68
|
+
issues,
|
|
69
|
+
""
|
|
70
|
+
].join("\n")
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
return result.data;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// src/core/proxy.ts
|
|
77
|
+
function createTypedProxy(obj) {
|
|
78
|
+
return new Proxy(obj, {
|
|
79
|
+
get(target, prop) {
|
|
80
|
+
if (!(prop in target)) {
|
|
81
|
+
console.error(
|
|
82
|
+
`\u274C Tried to access undefined environment variable "${prop}"`
|
|
83
|
+
);
|
|
84
|
+
process.exit(1);
|
|
85
|
+
}
|
|
86
|
+
return target[prop];
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// src/index.ts
|
|
92
|
+
var tx = {
|
|
93
|
+
/**
|
|
94
|
+
* STRICT STRING:
|
|
95
|
+
* Rejects purely numeric strings (e.g. "12345").
|
|
96
|
+
* Good for API Keys.
|
|
97
|
+
*/
|
|
98
|
+
string: (message) => import_zod2.z.string().refine((val) => !/^\d+$/.test(val), {
|
|
99
|
+
error: message || "Value should be text, but looks like a number."
|
|
100
|
+
}),
|
|
101
|
+
/**
|
|
102
|
+
* SMART NUMBER:
|
|
103
|
+
* Automatically converts "3000" -> 3000.
|
|
104
|
+
* Fails if the value is not a valid number (e.g. "abc").
|
|
105
|
+
*/
|
|
106
|
+
number: (message) => import_zod2.z.coerce.number({ error: message || "Must be a number" }),
|
|
107
|
+
/**
|
|
108
|
+
* PORT VALIDATOR:
|
|
109
|
+
* Coerces to number and ensures it is between 1 and 65535.
|
|
110
|
+
*/
|
|
111
|
+
port: (message) => import_zod2.z.coerce.number().min(1).max(65535).catch((ctx) => {
|
|
112
|
+
ctx.error;
|
|
113
|
+
return -1;
|
|
114
|
+
}).refine((val) => val >= 1 && val <= 65535, {
|
|
115
|
+
error: message || "Must be a valid port (1-65535)."
|
|
116
|
+
}),
|
|
117
|
+
/**
|
|
118
|
+
* SMART BOOLEAN:
|
|
119
|
+
* Handles "true", "TRUE", "1" -> true
|
|
120
|
+
* Handles "false", "FALSE", "0" -> false
|
|
121
|
+
* Throws error on anything else.
|
|
122
|
+
*/
|
|
123
|
+
bool: (message) => import_zod2.z.string().transform((val, ctx) => {
|
|
124
|
+
const v = val.toLowerCase();
|
|
125
|
+
if (v === "true" || v === "1") return true;
|
|
126
|
+
if (v === "false" || v === "0") return false;
|
|
127
|
+
ctx.addIssue({
|
|
128
|
+
code: import_zod2.z.ZodIssueCode.custom,
|
|
129
|
+
error: message || "Must be a boolean (true/false or 1/0)."
|
|
130
|
+
});
|
|
131
|
+
return import_zod2.z.NEVER;
|
|
132
|
+
}),
|
|
133
|
+
/**
|
|
134
|
+
* URL:
|
|
135
|
+
* Strict URL checking.
|
|
136
|
+
*/
|
|
137
|
+
url: (message) => import_zod2.z.url({ error: message || "Must be a valid URL (e.g. https://...)" }),
|
|
138
|
+
/**
|
|
139
|
+
* EMAIL:
|
|
140
|
+
* Strict Email checking.
|
|
141
|
+
*/
|
|
142
|
+
email: (message) => import_zod2.z.email({ error: message || "Must be a valid email address." }),
|
|
143
|
+
positiveNumber: (message) => import_zod2.z.coerce.number().refine((val) => val > 0, { error: message || "Must be > 0" }),
|
|
144
|
+
nonEmptyString: (message) => import_zod2.z.string().min(1, { error: message || "Cannot be empty" }),
|
|
145
|
+
semver: (message) => import_zod2.z.string().refine((val) => /^\d+\.\d+\.\d+(-[0-9A-Za-z-.]+)?$/.test(val), {
|
|
146
|
+
error: message || "Must be valid semver"
|
|
147
|
+
}),
|
|
148
|
+
path: (message) => import_zod2.z.string().min(1, { error: message || "Must be a valid path" }),
|
|
149
|
+
enum: (values, message) => import_zod2.z.string().refine((val) => values.includes(val), {
|
|
150
|
+
error: message || `Must be one of: ${values.join(", ")}`
|
|
151
|
+
}),
|
|
152
|
+
json: (message) => import_zod2.z.string().transform((val, ctx) => {
|
|
153
|
+
try {
|
|
154
|
+
return JSON.parse(val);
|
|
155
|
+
} catch {
|
|
156
|
+
ctx.addIssue({
|
|
157
|
+
code: import_zod2.z.ZodIssueCode.custom,
|
|
158
|
+
message: message || "Must be valid JSON"
|
|
159
|
+
});
|
|
160
|
+
return import_zod2.z.NEVER;
|
|
161
|
+
}
|
|
162
|
+
})
|
|
163
|
+
};
|
|
164
|
+
function defineEnv(shape, options) {
|
|
165
|
+
const fileEnv = loadDotEnv(options?.path);
|
|
166
|
+
const merged = { ...fileEnv, ...process.env };
|
|
167
|
+
const schema = import_zod2.z.object(shape);
|
|
168
|
+
const parsed = parseEnv(schema, merged);
|
|
169
|
+
return createTypedProxy(parsed);
|
|
170
|
+
}
|
|
171
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
172
|
+
0 && (module.exports = {
|
|
173
|
+
defineEnv,
|
|
174
|
+
tx
|
|
175
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
|
|
3
|
+
interface DefineEnvOptions {
|
|
4
|
+
path?: string;
|
|
5
|
+
}
|
|
6
|
+
declare const tx: {
|
|
7
|
+
/**
|
|
8
|
+
* STRICT STRING:
|
|
9
|
+
* Rejects purely numeric strings (e.g. "12345").
|
|
10
|
+
* Good for API Keys.
|
|
11
|
+
*/
|
|
12
|
+
string: (message?: string) => z.ZodString;
|
|
13
|
+
/**
|
|
14
|
+
* SMART NUMBER:
|
|
15
|
+
* Automatically converts "3000" -> 3000.
|
|
16
|
+
* Fails if the value is not a valid number (e.g. "abc").
|
|
17
|
+
*/
|
|
18
|
+
number: (message?: string) => z.ZodCoercedNumber<unknown>;
|
|
19
|
+
/**
|
|
20
|
+
* PORT VALIDATOR:
|
|
21
|
+
* Coerces to number and ensures it is between 1 and 65535.
|
|
22
|
+
*/
|
|
23
|
+
port: (message?: string) => z.ZodCatch<z.ZodCoercedNumber<unknown>>;
|
|
24
|
+
/**
|
|
25
|
+
* SMART BOOLEAN:
|
|
26
|
+
* Handles "true", "TRUE", "1" -> true
|
|
27
|
+
* Handles "false", "FALSE", "0" -> false
|
|
28
|
+
* Throws error on anything else.
|
|
29
|
+
*/
|
|
30
|
+
bool: (message?: string) => z.ZodPipe<z.ZodString, z.ZodTransform<boolean, string>>;
|
|
31
|
+
/**
|
|
32
|
+
* URL:
|
|
33
|
+
* Strict URL checking.
|
|
34
|
+
*/
|
|
35
|
+
url: (message?: string) => z.ZodURL;
|
|
36
|
+
/**
|
|
37
|
+
* EMAIL:
|
|
38
|
+
* Strict Email checking.
|
|
39
|
+
*/
|
|
40
|
+
email: (message?: string) => z.ZodEmail;
|
|
41
|
+
positiveNumber: (message?: string) => z.ZodCoercedNumber<unknown>;
|
|
42
|
+
nonEmptyString: (message?: string) => z.ZodString;
|
|
43
|
+
semver: (message?: string) => z.ZodString;
|
|
44
|
+
path: (message?: string) => z.ZodString;
|
|
45
|
+
enum: <T extends readonly [string, ...string[]]>(values: T, message?: string) => z.ZodString;
|
|
46
|
+
json: (message?: string) => z.ZodPipe<z.ZodString, z.ZodTransform<any, string>>;
|
|
47
|
+
};
|
|
48
|
+
declare function defineEnv<T extends z.ZodRawShape>(shape: T, options?: DefineEnvOptions): { [K in keyof T]: z.core.output<T[K]>; };
|
|
49
|
+
|
|
50
|
+
export { defineEnv, tx };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
|
|
3
|
+
interface DefineEnvOptions {
|
|
4
|
+
path?: string;
|
|
5
|
+
}
|
|
6
|
+
declare const tx: {
|
|
7
|
+
/**
|
|
8
|
+
* STRICT STRING:
|
|
9
|
+
* Rejects purely numeric strings (e.g. "12345").
|
|
10
|
+
* Good for API Keys.
|
|
11
|
+
*/
|
|
12
|
+
string: (message?: string) => z.ZodString;
|
|
13
|
+
/**
|
|
14
|
+
* SMART NUMBER:
|
|
15
|
+
* Automatically converts "3000" -> 3000.
|
|
16
|
+
* Fails if the value is not a valid number (e.g. "abc").
|
|
17
|
+
*/
|
|
18
|
+
number: (message?: string) => z.ZodCoercedNumber<unknown>;
|
|
19
|
+
/**
|
|
20
|
+
* PORT VALIDATOR:
|
|
21
|
+
* Coerces to number and ensures it is between 1 and 65535.
|
|
22
|
+
*/
|
|
23
|
+
port: (message?: string) => z.ZodCatch<z.ZodCoercedNumber<unknown>>;
|
|
24
|
+
/**
|
|
25
|
+
* SMART BOOLEAN:
|
|
26
|
+
* Handles "true", "TRUE", "1" -> true
|
|
27
|
+
* Handles "false", "FALSE", "0" -> false
|
|
28
|
+
* Throws error on anything else.
|
|
29
|
+
*/
|
|
30
|
+
bool: (message?: string) => z.ZodPipe<z.ZodString, z.ZodTransform<boolean, string>>;
|
|
31
|
+
/**
|
|
32
|
+
* URL:
|
|
33
|
+
* Strict URL checking.
|
|
34
|
+
*/
|
|
35
|
+
url: (message?: string) => z.ZodURL;
|
|
36
|
+
/**
|
|
37
|
+
* EMAIL:
|
|
38
|
+
* Strict Email checking.
|
|
39
|
+
*/
|
|
40
|
+
email: (message?: string) => z.ZodEmail;
|
|
41
|
+
positiveNumber: (message?: string) => z.ZodCoercedNumber<unknown>;
|
|
42
|
+
nonEmptyString: (message?: string) => z.ZodString;
|
|
43
|
+
semver: (message?: string) => z.ZodString;
|
|
44
|
+
path: (message?: string) => z.ZodString;
|
|
45
|
+
enum: <T extends readonly [string, ...string[]]>(values: T, message?: string) => z.ZodString;
|
|
46
|
+
json: (message?: string) => z.ZodPipe<z.ZodString, z.ZodTransform<any, string>>;
|
|
47
|
+
};
|
|
48
|
+
declare function defineEnv<T extends z.ZodRawShape>(shape: T, options?: DefineEnvOptions): { [K in keyof T]: z.core.output<T[K]>; };
|
|
49
|
+
|
|
50
|
+
export { defineEnv, tx };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { z as z2 } from "zod";
|
|
3
|
+
|
|
4
|
+
// src/loaders/dotenv.ts
|
|
5
|
+
import dotenv from "dotenv";
|
|
6
|
+
import fs from "fs";
|
|
7
|
+
function loadDotEnv(path = ".env") {
|
|
8
|
+
if (!fs.existsSync(path)) {
|
|
9
|
+
throw new Error(
|
|
10
|
+
`[envx] .env file not found at path "${path}"
|
|
11
|
+
Tip: Set { dotenv: false } if you manage environment variables yourself.`
|
|
12
|
+
);
|
|
13
|
+
}
|
|
14
|
+
const result = dotenv.config({ path });
|
|
15
|
+
if (result.error) {
|
|
16
|
+
throw result.error;
|
|
17
|
+
}
|
|
18
|
+
return result.parsed ?? {};
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// src/core/parser.ts
|
|
22
|
+
import "zod";
|
|
23
|
+
function parseEnv(schema, source) {
|
|
24
|
+
const result = schema.safeParse(source);
|
|
25
|
+
if (!result.success) {
|
|
26
|
+
const issues = result.error.issues.map((i) => `\u274C ${i.path.join(".")}: ${i.message}`).join("\n");
|
|
27
|
+
throw new Error(
|
|
28
|
+
[
|
|
29
|
+
"",
|
|
30
|
+
"\u250C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510",
|
|
31
|
+
"\u2502 \u274C INVALID ENVIRONMENT VARIABLES DETECTED \u2502",
|
|
32
|
+
"\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518",
|
|
33
|
+
issues,
|
|
34
|
+
""
|
|
35
|
+
].join("\n")
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
return result.data;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// src/core/proxy.ts
|
|
42
|
+
function createTypedProxy(obj) {
|
|
43
|
+
return new Proxy(obj, {
|
|
44
|
+
get(target, prop) {
|
|
45
|
+
if (!(prop in target)) {
|
|
46
|
+
console.error(
|
|
47
|
+
`\u274C Tried to access undefined environment variable "${prop}"`
|
|
48
|
+
);
|
|
49
|
+
process.exit(1);
|
|
50
|
+
}
|
|
51
|
+
return target[prop];
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// src/index.ts
|
|
57
|
+
var tx = {
|
|
58
|
+
/**
|
|
59
|
+
* STRICT STRING:
|
|
60
|
+
* Rejects purely numeric strings (e.g. "12345").
|
|
61
|
+
* Good for API Keys.
|
|
62
|
+
*/
|
|
63
|
+
string: (message) => z2.string().refine((val) => !/^\d+$/.test(val), {
|
|
64
|
+
error: message || "Value should be text, but looks like a number."
|
|
65
|
+
}),
|
|
66
|
+
/**
|
|
67
|
+
* SMART NUMBER:
|
|
68
|
+
* Automatically converts "3000" -> 3000.
|
|
69
|
+
* Fails if the value is not a valid number (e.g. "abc").
|
|
70
|
+
*/
|
|
71
|
+
number: (message) => z2.coerce.number({ error: message || "Must be a number" }),
|
|
72
|
+
/**
|
|
73
|
+
* PORT VALIDATOR:
|
|
74
|
+
* Coerces to number and ensures it is between 1 and 65535.
|
|
75
|
+
*/
|
|
76
|
+
port: (message) => z2.coerce.number().min(1).max(65535).catch((ctx) => {
|
|
77
|
+
ctx.error;
|
|
78
|
+
return -1;
|
|
79
|
+
}).refine((val) => val >= 1 && val <= 65535, {
|
|
80
|
+
error: message || "Must be a valid port (1-65535)."
|
|
81
|
+
}),
|
|
82
|
+
/**
|
|
83
|
+
* SMART BOOLEAN:
|
|
84
|
+
* Handles "true", "TRUE", "1" -> true
|
|
85
|
+
* Handles "false", "FALSE", "0" -> false
|
|
86
|
+
* Throws error on anything else.
|
|
87
|
+
*/
|
|
88
|
+
bool: (message) => z2.string().transform((val, ctx) => {
|
|
89
|
+
const v = val.toLowerCase();
|
|
90
|
+
if (v === "true" || v === "1") return true;
|
|
91
|
+
if (v === "false" || v === "0") return false;
|
|
92
|
+
ctx.addIssue({
|
|
93
|
+
code: z2.ZodIssueCode.custom,
|
|
94
|
+
error: message || "Must be a boolean (true/false or 1/0)."
|
|
95
|
+
});
|
|
96
|
+
return z2.NEVER;
|
|
97
|
+
}),
|
|
98
|
+
/**
|
|
99
|
+
* URL:
|
|
100
|
+
* Strict URL checking.
|
|
101
|
+
*/
|
|
102
|
+
url: (message) => z2.url({ error: message || "Must be a valid URL (e.g. https://...)" }),
|
|
103
|
+
/**
|
|
104
|
+
* EMAIL:
|
|
105
|
+
* Strict Email checking.
|
|
106
|
+
*/
|
|
107
|
+
email: (message) => z2.email({ error: message || "Must be a valid email address." }),
|
|
108
|
+
positiveNumber: (message) => z2.coerce.number().refine((val) => val > 0, { error: message || "Must be > 0" }),
|
|
109
|
+
nonEmptyString: (message) => z2.string().min(1, { error: message || "Cannot be empty" }),
|
|
110
|
+
semver: (message) => z2.string().refine((val) => /^\d+\.\d+\.\d+(-[0-9A-Za-z-.]+)?$/.test(val), {
|
|
111
|
+
error: message || "Must be valid semver"
|
|
112
|
+
}),
|
|
113
|
+
path: (message) => z2.string().min(1, { error: message || "Must be a valid path" }),
|
|
114
|
+
enum: (values, message) => z2.string().refine((val) => values.includes(val), {
|
|
115
|
+
error: message || `Must be one of: ${values.join(", ")}`
|
|
116
|
+
}),
|
|
117
|
+
json: (message) => z2.string().transform((val, ctx) => {
|
|
118
|
+
try {
|
|
119
|
+
return JSON.parse(val);
|
|
120
|
+
} catch {
|
|
121
|
+
ctx.addIssue({
|
|
122
|
+
code: z2.ZodIssueCode.custom,
|
|
123
|
+
message: message || "Must be valid JSON"
|
|
124
|
+
});
|
|
125
|
+
return z2.NEVER;
|
|
126
|
+
}
|
|
127
|
+
})
|
|
128
|
+
};
|
|
129
|
+
function defineEnv(shape, options) {
|
|
130
|
+
const fileEnv = loadDotEnv(options?.path);
|
|
131
|
+
const merged = { ...fileEnv, ...process.env };
|
|
132
|
+
const schema = z2.object(shape);
|
|
133
|
+
const parsed = parseEnv(schema, merged);
|
|
134
|
+
return createTypedProxy(parsed);
|
|
135
|
+
}
|
|
136
|
+
export {
|
|
137
|
+
defineEnv,
|
|
138
|
+
tx
|
|
139
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "zenvx",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "./dist/index.cjs",
|
|
6
|
+
"module": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"author": {
|
|
9
|
+
"name": "Sean Wilfred T. Custodio",
|
|
10
|
+
"email": "custodiocsean@gmail.com"
|
|
11
|
+
},
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"import": "./dist/index.js",
|
|
15
|
+
"require": "./dist/index.cjs",
|
|
16
|
+
"types": "./dist/index.d.ts"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"dist"
|
|
21
|
+
],
|
|
22
|
+
"scripts": {
|
|
23
|
+
"build": "tsup src/index.ts --format esm,cjs --dts --external zod,dotenv",
|
|
24
|
+
"dev": "tsup src/index.ts --watch --external zod,dotenv",
|
|
25
|
+
"lint": "eslint .",
|
|
26
|
+
"lint:fix": "eslint . --fix",
|
|
27
|
+
"prepublishOnly": "npm run lint && npm run build",
|
|
28
|
+
"clean": "rimraf dist",
|
|
29
|
+
"test": "vitest run"
|
|
30
|
+
},
|
|
31
|
+
"peerDependencies": {
|
|
32
|
+
"zod": "^4.3.5"
|
|
33
|
+
},
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"dotenv": "^16.4.7"
|
|
36
|
+
},
|
|
37
|
+
"peerDependenciesMeta": {
|
|
38
|
+
"zod": {
|
|
39
|
+
"optional": true
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"@types/node": "^25.0.9",
|
|
44
|
+
"@typescript-eslint/eslint-plugin": "^8.53.1",
|
|
45
|
+
"@typescript-eslint/parser": "^8.53.1",
|
|
46
|
+
"dotenv": "^17.2.3",
|
|
47
|
+
"eslint": "^9.39.2",
|
|
48
|
+
"rimraf": "^5.0.0",
|
|
49
|
+
"tsup": "^8.5.1",
|
|
50
|
+
"vitest": "^4.0.17",
|
|
51
|
+
"zod": "^4.3.5"
|
|
52
|
+
}
|
|
53
|
+
}
|