zod-envkit 1.0.0 → 1.0.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/CHANGELOG.md +43 -0
- package/LICENSE +21 -0
- package/README.md +15 -0
- package/package.json +22 -9
- package/.env.example +0 -8
- package/ENV.md +0 -7
- package/env.meta.json +0 -5
- package/src/cli.ts +0 -26
- package/src/generate.ts +0 -130
- package/src/index.ts +0 -23
- package/test/basic.test.ts +0 -15
- package/tsconfig.json +0 -10
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
This project follows [Semantic Versioning](https://semver.org/).
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## [1.0.0] – 2026-01-26
|
|
10
|
+
|
|
11
|
+
### Added
|
|
12
|
+
- Initial release of **zod-envkit**
|
|
13
|
+
- Type-safe environment variable validation using Zod
|
|
14
|
+
- `loadEnv` helper for safe runtime parsing of `process.env`
|
|
15
|
+
- `formatZodError` for human-readable error output
|
|
16
|
+
- CLI tool to generate:
|
|
17
|
+
- `.env.example`
|
|
18
|
+
- `ENV.md` documentation
|
|
19
|
+
- Automatic type inference for validated environment variables
|
|
20
|
+
- Support for ESM and CommonJS builds
|
|
21
|
+
- TypeScript declaration files (`.d.ts`) included
|
|
22
|
+
- Basic test suite using Vitest
|
|
23
|
+
- Clear and documented API surface
|
|
24
|
+
- MIT license
|
|
25
|
+
|
|
26
|
+
### Design Decisions
|
|
27
|
+
- Explicit configuration over magic
|
|
28
|
+
- No automatic schema introspection for documentation generation
|
|
29
|
+
- Environment variables treated as an explicit runtime contract
|
|
30
|
+
- Small, framework-agnostic core
|
|
31
|
+
|
|
32
|
+
---
|
|
33
|
+
|
|
34
|
+
[1.0.0]: https://www.npmjs.com/package/zod-envkit/v/1.0.0
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
## [1.0.1] – 2026-01-26
|
|
38
|
+
|
|
39
|
+
### Changed
|
|
40
|
+
- Documentation and packaging improvements
|
|
41
|
+
- Minor CLI/docs adjustments
|
|
42
|
+
|
|
43
|
+
[1.0.1]: https://www.npmjs.com/package/zod-envkit/v/1.0.1
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 nxtxe
|
|
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
CHANGED
|
@@ -1,3 +1,18 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
<br />
|
|
3
|
+
<p>
|
|
4
|
+
<img src="./zod-envkit.svg" width="546" alt="zod-envkit" /><
|
|
5
|
+
</p>
|
|
6
|
+
<br />
|
|
7
|
+
<p>
|
|
8
|
+
<a href="https://www.npmjs.com/package/zod-envkit"><img src="https://img.shields.io/npm/v/zod-envkit.svg?maxAge=100" alt="npm version" /></a>
|
|
9
|
+
<a href="https://www.npmjs.com/package/zod-envkit"><img src="https://img.shields.io/npm/dt/zod-envkit.svg?maxAge=100" alt="npm downloads" /></a>
|
|
10
|
+
</p>
|
|
11
|
+
<p align="center">
|
|
12
|
+
<b> A framework for developing bots based using discord.js. </b>
|
|
13
|
+
</p>
|
|
14
|
+
</div>
|
|
15
|
+
|
|
1
16
|
Type-safe environment variable validation and documentation using Zod.
|
|
2
17
|
|
|
3
18
|
`zod-envkit` is a small library and CLI tool that helps you:
|
package/package.json
CHANGED
|
@@ -1,13 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "zod-envkit",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "Validate environment variables with Zod and generate .env.example",
|
|
5
5
|
"type": "module",
|
|
6
|
-
|
|
7
6
|
"main": "dist/index.cjs",
|
|
8
7
|
"module": "dist/index.js",
|
|
9
8
|
"types": "dist/index.d.ts",
|
|
10
|
-
|
|
11
9
|
"exports": {
|
|
12
10
|
".": {
|
|
13
11
|
"types": "./dist/index.d.ts",
|
|
@@ -15,28 +13,43 @@
|
|
|
15
13
|
"require": "./dist/index.cjs"
|
|
16
14
|
}
|
|
17
15
|
},
|
|
18
|
-
|
|
19
16
|
"bin": {
|
|
20
17
|
"zod-envkit": "dist/cli.js"
|
|
21
18
|
},
|
|
22
|
-
|
|
19
|
+
"files": [
|
|
20
|
+
"dist",
|
|
21
|
+
"README.md",
|
|
22
|
+
"CHANGELOG.md",
|
|
23
|
+
"LICENSE"
|
|
24
|
+
],
|
|
25
|
+
"repository": {
|
|
26
|
+
"type": "git",
|
|
27
|
+
"url": "git+https://github.com/nxtxe/zod-envkit.git"
|
|
28
|
+
},
|
|
29
|
+
"bugs": {
|
|
30
|
+
"url": "https://github.com/nxtxe/zod-envkit/issues"
|
|
31
|
+
},
|
|
32
|
+
"homepage": "https://github.com/nxtxe/zod-envkit#readme",
|
|
23
33
|
"scripts": {
|
|
24
34
|
"build": "tsup src/index.ts src/cli.ts --format esm,cjs --dts",
|
|
25
35
|
"dev": "tsup src/index.ts src/cli.ts --watch --dts",
|
|
26
36
|
"test": "vitest run",
|
|
27
37
|
"prepublishOnly": "npm run test && npm run build"
|
|
28
38
|
},
|
|
29
|
-
|
|
30
|
-
|
|
39
|
+
"keywords": [
|
|
40
|
+
"env",
|
|
41
|
+
"dotenv",
|
|
42
|
+
"zod",
|
|
43
|
+
"validation",
|
|
44
|
+
"cli"
|
|
45
|
+
],
|
|
31
46
|
"author": "",
|
|
32
47
|
"license": "MIT",
|
|
33
|
-
|
|
34
48
|
"dependencies": {
|
|
35
49
|
"commander": "^13.1.0",
|
|
36
50
|
"dotenv": "^17.2.3",
|
|
37
51
|
"zod": "^4.3.6"
|
|
38
52
|
},
|
|
39
|
-
|
|
40
53
|
"devDependencies": {
|
|
41
54
|
"@types/node": "^25.0.10",
|
|
42
55
|
"eslint": "^9.39.2",
|
package/.env.example
DELETED
package/ENV.md
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
# Environment variables
|
|
2
|
-
|
|
3
|
-
| Key | Required | Example | Description |
|
|
4
|
-
| :------------: | :--------: | :----------------------------------------: | :--------------------------: |
|
|
5
|
-
| NODE_ENV | yes | development | Runtime mode |
|
|
6
|
-
| PORT | yes | 3000 | HTTP port |
|
|
7
|
-
| DATABASE_URL | yes | postgresql://user:pass@localhost:5432/db | Postgres connection string |
|
package/env.meta.json
DELETED
package/src/cli.ts
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
import fs from "node:fs";
|
|
3
|
-
import path from "node:path";
|
|
4
|
-
import { Command } from "commander";
|
|
5
|
-
import { generateEnvDocs, generateEnvExample, type EnvMeta } from "./generate.js";
|
|
6
|
-
|
|
7
|
-
const program = new Command();
|
|
8
|
-
|
|
9
|
-
program
|
|
10
|
-
.name("zod-envkit")
|
|
11
|
-
.description("Generate .env.example and ENV.md from env.meta.json")
|
|
12
|
-
.option("-c, --config <file>", "Path to env meta json", "env.meta.json")
|
|
13
|
-
.option("--out-example <file>", "Output file for .env.example", ".env.example")
|
|
14
|
-
.option("--out-docs <file>", "Output file for docs", "ENV.md")
|
|
15
|
-
.action((opts) => {
|
|
16
|
-
const configPath = path.resolve(process.cwd(), opts.config);
|
|
17
|
-
const raw = fs.readFileSync(configPath, "utf8");
|
|
18
|
-
const meta: EnvMeta = JSON.parse(raw);
|
|
19
|
-
|
|
20
|
-
fs.writeFileSync(opts.outExample, generateEnvExample(meta), "utf8");
|
|
21
|
-
fs.writeFileSync(opts.outDocs, generateEnvDocs(meta), "utf8");
|
|
22
|
-
|
|
23
|
-
console.log(`Generated: ${opts.outExample}, ${opts.outDocs}`);
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
program.parse();
|
package/src/generate.ts
DELETED
|
@@ -1,130 +0,0 @@
|
|
|
1
|
-
// src/generate.ts
|
|
2
|
-
|
|
3
|
-
export type EnvMeta = Record<
|
|
4
|
-
string,
|
|
5
|
-
{
|
|
6
|
-
description?: string;
|
|
7
|
-
example?: string;
|
|
8
|
-
required?: boolean; // default: true
|
|
9
|
-
}
|
|
10
|
-
>;
|
|
11
|
-
|
|
12
|
-
function strLen(s: string): number {
|
|
13
|
-
return s.length;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
function padCenter(text: string, width: number): string {
|
|
17
|
-
const t = text ?? "";
|
|
18
|
-
const diff = width - strLen(t);
|
|
19
|
-
if (diff <= 0) return t;
|
|
20
|
-
|
|
21
|
-
const left = Math.floor(diff / 2);
|
|
22
|
-
const right = diff - left;
|
|
23
|
-
return " ".repeat(left) + t + " ".repeat(right);
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
function padRight(text: string, width: number): string {
|
|
27
|
-
const t = text ?? "";
|
|
28
|
-
const diff = width - strLen(t);
|
|
29
|
-
if (diff <= 0) return t;
|
|
30
|
-
return t + " ".repeat(diff);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
function makeDivider(width: number, align: "left" | "center" | "right"): string {
|
|
34
|
-
// Markdown alignment:
|
|
35
|
-
// left : ---
|
|
36
|
-
// center : :---:
|
|
37
|
-
// right : ---:
|
|
38
|
-
if (width < 3) width = 3;
|
|
39
|
-
|
|
40
|
-
if (align === "center") return ":" + "-".repeat(width - 2) + ":";
|
|
41
|
-
if (align === "right") return "-".repeat(width - 1) + ":";
|
|
42
|
-
return "-".repeat(width);
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Генерит .env.example красиво:
|
|
47
|
-
* - комментарий с description
|
|
48
|
-
* - KEY=example
|
|
49
|
-
*/
|
|
50
|
-
export function generateEnvExample(meta: EnvMeta): string {
|
|
51
|
-
const lines: string[] = [];
|
|
52
|
-
|
|
53
|
-
for (const [key, m] of Object.entries(meta)) {
|
|
54
|
-
if (m.description) lines.push(`# ${m.description}`);
|
|
55
|
-
lines.push(`${key}=${m.example ?? ""}`);
|
|
56
|
-
lines.push("");
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
return lines.join("\n").trim() + "\n";
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* Генерит ENV.md как ровную таблицу:
|
|
64
|
-
* - вычисляет ширины колонок по максимальной длине
|
|
65
|
-
* - паддит значения пробелами
|
|
66
|
-
* - ставит выравнивание :---: чтобы центрировалось в Markdown
|
|
67
|
-
*/
|
|
68
|
-
export function generateEnvDocs(meta: EnvMeta): string {
|
|
69
|
-
const headers = ["Key", "Required", "Example", "Description"] as const;
|
|
70
|
-
|
|
71
|
-
const rows = Object.entries(meta).map(([key, m]) => {
|
|
72
|
-
const req = m.required === false ? "no" : "yes";
|
|
73
|
-
return {
|
|
74
|
-
Key: key,
|
|
75
|
-
Required: req,
|
|
76
|
-
Example: m.example ?? "",
|
|
77
|
-
Description: m.description ?? "",
|
|
78
|
-
};
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
const widths = {
|
|
82
|
-
Key: headers[0].length,
|
|
83
|
-
Required: headers[1].length,
|
|
84
|
-
Example: headers[2].length,
|
|
85
|
-
Description: headers[3].length,
|
|
86
|
-
};
|
|
87
|
-
|
|
88
|
-
for (const r of rows) {
|
|
89
|
-
widths.Key = Math.max(widths.Key, strLen(r.Key));
|
|
90
|
-
widths.Required = Math.max(widths.Required, strLen(r.Required));
|
|
91
|
-
widths.Example = Math.max(widths.Example, strLen(r.Example));
|
|
92
|
-
widths.Description = Math.max(widths.Description, strLen(r.Description));
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
widths.Key += 2;
|
|
96
|
-
widths.Required += 2;
|
|
97
|
-
widths.Example += 2;
|
|
98
|
-
widths.Description += 2;
|
|
99
|
-
|
|
100
|
-
const headerLine =
|
|
101
|
-
`| ${padCenter(headers[0], widths.Key)} ` +
|
|
102
|
-
`| ${padCenter(headers[1], widths.Required)} ` +
|
|
103
|
-
`| ${padCenter(headers[2], widths.Example)} ` +
|
|
104
|
-
`| ${padCenter(headers[3], widths.Description)} |`;
|
|
105
|
-
|
|
106
|
-
const dividerLine =
|
|
107
|
-
`| ${makeDivider(widths.Key, "center")} ` +
|
|
108
|
-
`| ${makeDivider(widths.Required, "center")} ` +
|
|
109
|
-
`| ${makeDivider(widths.Example, "center")} ` +
|
|
110
|
-
`| ${makeDivider(widths.Description, "center")} |`;
|
|
111
|
-
|
|
112
|
-
const bodyLines = rows.map((r) => {
|
|
113
|
-
return (
|
|
114
|
-
`| ${padCenter(r.Key, widths.Key)} ` +
|
|
115
|
-
`| ${padCenter(r.Required, widths.Required)} ` +
|
|
116
|
-
`| ${padCenter(r.Example, widths.Example)} ` +
|
|
117
|
-
`| ${padCenter(r.Description, widths.Description)} |`
|
|
118
|
-
);
|
|
119
|
-
});
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
return [
|
|
123
|
-
`# Environment variables`,
|
|
124
|
-
``,
|
|
125
|
-
headerLine,
|
|
126
|
-
dividerLine,
|
|
127
|
-
...bodyLines,
|
|
128
|
-
``,
|
|
129
|
-
].join("\n");
|
|
130
|
-
}
|
package/src/index.ts
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { z } from "zod";
|
|
2
|
-
|
|
3
|
-
export type EnvResult<T extends z.ZodTypeAny> = z.infer<T>;
|
|
4
|
-
|
|
5
|
-
export function loadEnv<T extends z.ZodTypeAny>(
|
|
6
|
-
schema: T,
|
|
7
|
-
opts?: { throwOnError?: boolean }
|
|
8
|
-
):
|
|
9
|
-
| { ok: true; env: z.infer<T> }
|
|
10
|
-
| { ok: false; error: z.ZodError } {
|
|
11
|
-
const parsed = schema.safeParse(process.env);
|
|
12
|
-
|
|
13
|
-
if (parsed.success) return { ok: true, env: parsed.data };
|
|
14
|
-
|
|
15
|
-
if (opts?.throwOnError) throw parsed.error;
|
|
16
|
-
return { ok: false, error: parsed.error };
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export function formatZodError(err: z.ZodError): string {
|
|
20
|
-
return err.issues
|
|
21
|
-
.map((i) => `- ${i.path.join(".") || "(root)"}: ${i.message}`)
|
|
22
|
-
.join("\n");
|
|
23
|
-
}
|
package/test/basic.test.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect } from "vitest";
|
|
2
|
-
import { z } from "zod";
|
|
3
|
-
import { loadEnv } from "../src/index";
|
|
4
|
-
|
|
5
|
-
describe("loadEnv", () => {
|
|
6
|
-
it("parses valid env", () => {
|
|
7
|
-
process.env.PORT = "3000";
|
|
8
|
-
const schema = z.object({ PORT: z.coerce.number() });
|
|
9
|
-
|
|
10
|
-
const res = loadEnv(schema);
|
|
11
|
-
expect(res.ok).toBe(true);
|
|
12
|
-
|
|
13
|
-
if (res.ok) expect(res.env.PORT).toBe(3000);
|
|
14
|
-
});
|
|
15
|
-
});
|