@thexjs/env 0.1.0 → 0.1.2
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 +77 -0
- package/package.json +10 -1
package/README.md
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
# @thexjs/env
|
|
2
|
+
|
|
3
|
+
Type-safe environment variable validation for [x](https://www.npmjs.com/package/@thexjs/core) framework apps. Define a schema once, get parsed/typed values back, and fail fast with a clear error if something's missing or malformed — instead of finding out at runtime deep in your app.
|
|
4
|
+
|
|
5
|
+
```sh
|
|
6
|
+
bun add @thexjs/env
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
## Quick start
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
// env.ts
|
|
13
|
+
import { createEnv, str, num, bool, oneOf, url } from "@thexjs/env";
|
|
14
|
+
|
|
15
|
+
export const env = createEnv({
|
|
16
|
+
server: {
|
|
17
|
+
DATABASE_URL: url(),
|
|
18
|
+
PORT: num(),
|
|
19
|
+
NODE_ENV: oneOf(["development", "production", "test"]),
|
|
20
|
+
},
|
|
21
|
+
client: {
|
|
22
|
+
NEXT_PUBLIC_API_URL: url(),
|
|
23
|
+
},
|
|
24
|
+
clientPrefix: "NEXT_PUBLIC_",
|
|
25
|
+
runtimeEnv: process.env,
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
env.DATABASE_URL; // string, validated as a URL
|
|
29
|
+
env.PORT; // number
|
|
30
|
+
env.NODE_ENV; // "development" | "production" | "test"
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
If any variable is missing or fails validation, `createEnv` throws a single `Error` listing every failure at once:
|
|
34
|
+
|
|
35
|
+
```
|
|
36
|
+
Environment validation failed:
|
|
37
|
+
server.DATABASE_URL: Expected a valid URL, got "not-a-url"
|
|
38
|
+
server.PORT: Expected a number, got undefined
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Validators
|
|
42
|
+
|
|
43
|
+
| Validator | Accepts | Notes |
|
|
44
|
+
|---|---|---|
|
|
45
|
+
| `str()` | any non-`undefined` string | |
|
|
46
|
+
| `num()` | numeric strings | rejects `NaN` |
|
|
47
|
+
| `bool()` | `"true"` / `"1"` → `true`, `"false"` / `"0"` → `false` | anything else throws |
|
|
48
|
+
| `oneOf([...values])` | one of the given string literals | return type is narrowed to the literal union |
|
|
49
|
+
| `url()` | any string parseable by `new URL(...)` | |
|
|
50
|
+
|
|
51
|
+
Each validator is just `{ parse(input: string | undefined): T }` (the `EnvValidator<T>` interface, exported as a type) — write your own for anything not covered above:
|
|
52
|
+
|
|
53
|
+
```ts
|
|
54
|
+
import type { EnvValidator } from "@thexjs/env";
|
|
55
|
+
|
|
56
|
+
function json<T>(): EnvValidator<T> {
|
|
57
|
+
return {
|
|
58
|
+
parse(input) {
|
|
59
|
+
if (input === undefined) throw new Error("Expected JSON, got undefined");
|
|
60
|
+
return JSON.parse(input) as T;
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## `client` / `clientPrefix`
|
|
67
|
+
|
|
68
|
+
The `client` schema is for variables you're comfortable exposing to the browser. `clientPrefix` enforces that every client key actually starts with that prefix (e.g. `NEXT_PUBLIC_`, `PUBLIC_`) — a key that doesn't match fails validation, so you can't accidentally leak a server-only variable through the client schema by mistake.
|
|
69
|
+
|
|
70
|
+
## Notes
|
|
71
|
+
|
|
72
|
+
- Every field is currently **required** — there's no built-in `optional()`/`default()` wrapper. If a variable is genuinely optional in your app, read it from `process.env` directly rather than through `createEnv`, or write a validator whose `parse` returns a fallback instead of throwing on `undefined`.
|
|
73
|
+
- `runtimeEnv` is passed in explicitly (rather than read internally) so this works the same whether you're on Bun, in a bundler that inlines `process.env.*` at build time, or in a test with a mocked env object.
|
|
74
|
+
|
|
75
|
+
## License
|
|
76
|
+
|
|
77
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,7 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@thexjs/env",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "Type-safe environment variable loading and validation for the x framework.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"environment-variables",
|
|
7
|
+
"env",
|
|
8
|
+
"validation",
|
|
9
|
+
"typescript",
|
|
10
|
+
"config",
|
|
11
|
+
"bun",
|
|
12
|
+
"x"
|
|
13
|
+
],
|
|
5
14
|
"license": "MIT",
|
|
6
15
|
"type": "module",
|
|
7
16
|
"sideEffects": false,
|