@drsmile1001/config-factory 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/index.ts +1 -0
- package/package.json +14 -0
- package/src/ConfigFactoryEnv.ts +45 -0
package/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./src/ConfigFactoryEnv";
|
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { type TObject, Type as t } from "@sinclair/typebox";
|
|
2
|
+
import { Value } from "@sinclair/typebox/value";
|
|
3
|
+
|
|
4
|
+
export function buildConfigFactoryEnv<TConfigSchema extends TObject>(
|
|
5
|
+
schema: TConfigSchema
|
|
6
|
+
) {
|
|
7
|
+
return () => {
|
|
8
|
+
const input = Value.Clean(schema, Value.Clone(Bun.env));
|
|
9
|
+
const errors = [...Value.Errors(schema, input)];
|
|
10
|
+
|
|
11
|
+
if (errors.length > 0) {
|
|
12
|
+
const errorMessages = errors
|
|
13
|
+
.map((e) => `❌ ${e.path}: ${e.message}`)
|
|
14
|
+
.join("\n");
|
|
15
|
+
|
|
16
|
+
throw new Error(
|
|
17
|
+
`Environment config validation failed:\n${errorMessages}`
|
|
18
|
+
);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
return Value.Decode(schema, input);
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const booleanValues = ["true", "1", "yes", "on", "enabled"];
|
|
26
|
+
|
|
27
|
+
export function envBoolean() {
|
|
28
|
+
return t
|
|
29
|
+
.Transform(t.String())
|
|
30
|
+
.Decode((value) => booleanValues.includes(value.toLowerCase()))
|
|
31
|
+
.Encode((value) => (value ? "true" : "false"));
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function envNumber() {
|
|
35
|
+
return t
|
|
36
|
+
.Transform(t.String())
|
|
37
|
+
.Decode((value) => {
|
|
38
|
+
const num = Number(value);
|
|
39
|
+
if (isNaN(num)) {
|
|
40
|
+
throw new Error(`Invalid numeric value: ${value}`);
|
|
41
|
+
}
|
|
42
|
+
return num;
|
|
43
|
+
})
|
|
44
|
+
.Encode((value) => String(value));
|
|
45
|
+
}
|