@thexjs/env 1.0.1 → 1.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/README.md +21 -1
- package/dist/chunk-AI3FAL4R.js +65 -0
- package/dist/index.js +1 -1
- package/dist/validators.d.ts +27 -6
- package/dist/validators.js +1 -1
- package/package.json +15 -3
- package/dist/chunk-4H2EYJTE.js +0 -62
package/README.md
CHANGED
|
@@ -48,6 +48,26 @@ Environment validation failed:
|
|
|
48
48
|
| `oneOf([...values])` | one of the given string literals | return type is narrowed to the literal union |
|
|
49
49
|
| `url()` | any string parseable by `new URL(...)` | |
|
|
50
50
|
|
|
51
|
+
Built-in validators also expose two chaining combinators:
|
|
52
|
+
|
|
53
|
+
- `.optional()` — a missing variable becomes `undefined` instead of failing;
|
|
54
|
+
a present-but-invalid value still throws. `num().optional()` is typed
|
|
55
|
+
`number | undefined`.
|
|
56
|
+
- `.default(fallback)` — a missing variable yields `fallback`; a
|
|
57
|
+
present-but-invalid value still throws. `num().default(3000)` stays typed
|
|
58
|
+
`number`.
|
|
59
|
+
|
|
60
|
+
```ts
|
|
61
|
+
const env = createEnv({
|
|
62
|
+
server: {
|
|
63
|
+
PORT: num().default(3000),
|
|
64
|
+
SENTRY_DSN: url().optional(),
|
|
65
|
+
},
|
|
66
|
+
runtimeEnv: {},
|
|
67
|
+
});
|
|
68
|
+
// env.PORT === 3000, env.SENTRY_DSN === undefined
|
|
69
|
+
```
|
|
70
|
+
|
|
51
71
|
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
72
|
|
|
53
73
|
```ts
|
|
@@ -71,7 +91,7 @@ When used with `@thexjs/core`, keep the default `THEXJS_PUBLIC_` prefix: the fra
|
|
|
71
91
|
|
|
72
92
|
## Notes
|
|
73
93
|
|
|
74
|
-
-
|
|
94
|
+
- Optional variables use `.optional()`; optional-with-fallback use `.default(v)` (see above). A required variable that's unset still fails validation.
|
|
75
95
|
- `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.
|
|
76
96
|
|
|
77
97
|
## License
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
// src/validators.ts
|
|
2
|
+
function chainable(parse) {
|
|
3
|
+
return {
|
|
4
|
+
parse,
|
|
5
|
+
optional: () => chainable((input) => input === void 0 ? void 0 : parse(input)),
|
|
6
|
+
default: (fallback) => chainable(
|
|
7
|
+
(input) => (
|
|
8
|
+
// `parse` only yields `undefined` for a missing input, which the
|
|
9
|
+
// fallback path already covered, so this branch is never undefined.
|
|
10
|
+
input === void 0 ? fallback : parse(input)
|
|
11
|
+
)
|
|
12
|
+
)
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
function str() {
|
|
16
|
+
return chainable((input) => {
|
|
17
|
+
if (input === void 0) throw new Error("Expected a string, got undefined");
|
|
18
|
+
return input;
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
function num() {
|
|
22
|
+
return chainable((input) => {
|
|
23
|
+
if (input === void 0) throw new Error("Expected a number, got undefined");
|
|
24
|
+
if (input.trim() === "") throw new Error(`Expected a number, got "${input}"`);
|
|
25
|
+
const n = Number(input);
|
|
26
|
+
if (Number.isNaN(n)) throw new Error(`Expected a number, got "${input}"`);
|
|
27
|
+
return n;
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
function bool() {
|
|
31
|
+
return chainable((input) => {
|
|
32
|
+
if (input === void 0) throw new Error("Expected a boolean, got undefined");
|
|
33
|
+
if (input === "true" || input === "1") return true;
|
|
34
|
+
if (input === "false" || input === "0") return false;
|
|
35
|
+
throw new Error(`Expected a boolean, got "${input}"`);
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
function oneOf(values) {
|
|
39
|
+
return chainable((input) => {
|
|
40
|
+
if (input === void 0) throw new Error(`Expected one of ${values.join(", ")}`);
|
|
41
|
+
if (!values.includes(input)) {
|
|
42
|
+
throw new Error(`Expected one of ${values.join(", ")}, got "${input}"`);
|
|
43
|
+
}
|
|
44
|
+
return input;
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
function url() {
|
|
48
|
+
return chainable((input) => {
|
|
49
|
+
if (input === void 0) throw new Error("Expected a URL, got undefined");
|
|
50
|
+
try {
|
|
51
|
+
new URL(input);
|
|
52
|
+
return input;
|
|
53
|
+
} catch (_) {
|
|
54
|
+
throw new Error(`Expected a valid URL, got "${input}"`);
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export {
|
|
60
|
+
str,
|
|
61
|
+
num,
|
|
62
|
+
bool,
|
|
63
|
+
oneOf,
|
|
64
|
+
url
|
|
65
|
+
};
|
package/dist/index.js
CHANGED
package/dist/validators.d.ts
CHANGED
|
@@ -1,10 +1,31 @@
|
|
|
1
1
|
interface EnvValidator<T = unknown> {
|
|
2
2
|
parse: (input: string | undefined) => T;
|
|
3
3
|
}
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
4
|
+
/**
|
|
5
|
+
* Chaining combinators layered on top of a validator. `{ parse }` alone keeps
|
|
6
|
+
* `EnvValidator` bivariant-friendly in user code; the combinators are exposed
|
|
7
|
+
* through an intersection so generic consumers typed against `EnvValidator`
|
|
8
|
+
* keep working.
|
|
9
|
+
*/
|
|
10
|
+
interface ChainableValidator<T> {
|
|
11
|
+
/**
|
|
12
|
+
* Treat a missing variable as `undefined` instead of failing. A present but
|
|
13
|
+
* invalid value still fails validation (e.g. `num().optional()` with "abc").
|
|
14
|
+
*/
|
|
15
|
+
optional: () => EnvValidator<T | undefined> & ChainableValidator<T | undefined>;
|
|
16
|
+
/**
|
|
17
|
+
* Substitute `fallback` when the variable is missing. A present but invalid
|
|
18
|
+
* value still fails validation. `undefined` is removed from the chained
|
|
19
|
+
* result, so `num().optional().default(0)` stays typed `number` and
|
|
20
|
+
* `default(undefined)` is a compile-time error.
|
|
21
|
+
*/
|
|
22
|
+
default: (fallback: Exclude<T, undefined>) => EnvValidator<Exclude<T, undefined>> & ChainableValidator<Exclude<T, undefined>>;
|
|
23
|
+
}
|
|
24
|
+
type ScalarValidator<T> = EnvValidator<T> & ChainableValidator<T>;
|
|
25
|
+
declare function str(): ScalarValidator<string>;
|
|
26
|
+
declare function num(): ScalarValidator<number>;
|
|
27
|
+
declare function bool(): ScalarValidator<boolean>;
|
|
28
|
+
declare function oneOf<T extends string>(values: readonly T[]): ScalarValidator<T>;
|
|
29
|
+
declare function url(): ScalarValidator<string>;
|
|
9
30
|
|
|
10
|
-
export { type EnvValidator, bool, num, oneOf, str, url };
|
|
31
|
+
export { type ChainableValidator, type EnvValidator, bool, num, oneOf, str, url };
|
package/dist/validators.js
CHANGED
package/package.json
CHANGED
|
@@ -1,8 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@thexjs/env",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "Type-safe environment variable loading and validation for the x framework.",
|
|
5
|
-
"keywords": [
|
|
5
|
+
"keywords": [
|
|
6
|
+
"environment-variables",
|
|
7
|
+
"env",
|
|
8
|
+
"validation",
|
|
9
|
+
"typescript",
|
|
10
|
+
"config",
|
|
11
|
+
"bun",
|
|
12
|
+
"x"
|
|
13
|
+
],
|
|
6
14
|
"license": "MIT",
|
|
7
15
|
"type": "module",
|
|
8
16
|
"sideEffects": false,
|
|
@@ -19,7 +27,11 @@
|
|
|
19
27
|
"import": "./dist/validators.js"
|
|
20
28
|
}
|
|
21
29
|
},
|
|
22
|
-
"files": [
|
|
30
|
+
"files": [
|
|
31
|
+
"dist",
|
|
32
|
+
"README.md",
|
|
33
|
+
"LICENSE"
|
|
34
|
+
],
|
|
23
35
|
"publishConfig": {
|
|
24
36
|
"access": "public"
|
|
25
37
|
},
|
package/dist/chunk-4H2EYJTE.js
DELETED
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
// src/validators.ts
|
|
2
|
-
function str() {
|
|
3
|
-
return {
|
|
4
|
-
parse(input) {
|
|
5
|
-
if (input === void 0) throw new Error("Expected a string, got undefined");
|
|
6
|
-
return input;
|
|
7
|
-
}
|
|
8
|
-
};
|
|
9
|
-
}
|
|
10
|
-
function num() {
|
|
11
|
-
return {
|
|
12
|
-
parse(input) {
|
|
13
|
-
if (input === void 0) throw new Error("Expected a number, got undefined");
|
|
14
|
-
if (input.trim() === "") throw new Error(`Expected a number, got "${input}"`);
|
|
15
|
-
const n = Number(input);
|
|
16
|
-
if (Number.isNaN(n)) throw new Error(`Expected a number, got "${input}"`);
|
|
17
|
-
return n;
|
|
18
|
-
}
|
|
19
|
-
};
|
|
20
|
-
}
|
|
21
|
-
function bool() {
|
|
22
|
-
return {
|
|
23
|
-
parse(input) {
|
|
24
|
-
if (input === void 0) throw new Error("Expected a boolean, got undefined");
|
|
25
|
-
if (input === "true" || input === "1") return true;
|
|
26
|
-
if (input === "false" || input === "0") return false;
|
|
27
|
-
throw new Error(`Expected a boolean, got "${input}"`);
|
|
28
|
-
}
|
|
29
|
-
};
|
|
30
|
-
}
|
|
31
|
-
function oneOf(values) {
|
|
32
|
-
return {
|
|
33
|
-
parse(input) {
|
|
34
|
-
if (input === void 0) throw new Error(`Expected one of ${values.join(", ")}`);
|
|
35
|
-
if (!values.includes(input)) {
|
|
36
|
-
throw new Error(`Expected one of ${values.join(", ")}, got "${input}"`);
|
|
37
|
-
}
|
|
38
|
-
return input;
|
|
39
|
-
}
|
|
40
|
-
};
|
|
41
|
-
}
|
|
42
|
-
function url() {
|
|
43
|
-
return {
|
|
44
|
-
parse(input) {
|
|
45
|
-
if (input === void 0) throw new Error("Expected a URL, got undefined");
|
|
46
|
-
try {
|
|
47
|
-
new URL(input);
|
|
48
|
-
return input;
|
|
49
|
-
} catch (_) {
|
|
50
|
-
throw new Error(`Expected a valid URL, got "${input}"`);
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
};
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
export {
|
|
57
|
-
str,
|
|
58
|
-
num,
|
|
59
|
-
bool,
|
|
60
|
-
oneOf,
|
|
61
|
-
url
|
|
62
|
-
};
|