@kleb/config (0.3.0)
Published 2026-07-03 18:19:16 +02:00 by kleb
Installation
@kleb:registry=npm install @kleb/config@0.3.0"@kleb/config": "0.3.0"About this package
@kleb/config
Typed configuration loading for TypeScript projects.
Use the server builder when the library should own creation, loading, updates, reloads, and saves:
import { field, kConfig } from "@kleb/config/server";
const appConfig = kConfig("server.json")
.fields({
port: field.number().int().min(1).max(65535).default(3000).env("PORT"),
mode: field.enum(["dev", "prod"] as const).default("dev"),
})
.section(
"http",
kConfig("http.json").fields({
host: field.string().default("127.0.0.1"),
enabled: field.boolean().default(true),
}),
);
const config = appConfig.load({ env: process.env, logger });
config.value.port;
config.update((draft) => {
draft.http.host = "0.0.0.0";
});
config.save();
config.reload();
JSON files are strict: a number field must be a JSON number. Environment and CLI
values are parsed only for fields that declare .env() or .cli(). Unknown
file keys are warnings, and saves write the declared config shape.
Validate caller-provided configuration with any Standard Schema-compatible schema:
import { loadConfig } from "@kleb/config";
const config = loadConfig(schema, {
PORT: "3000",
LOG_LEVEL: "info",
});
Use the server entrypoint to read process.env:
import { loadEnvConfig } from "@kleb/config/server";
const config = loadEnvConfig(schema, {
prefix: "APP_",
});
Prefix filtering strips the prefix by default before validation:
const config = loadEnvConfig(schema, {
prefix: "APP_",
keys: ["PORT", "LOG_LEVEL"],
});
Validation errors report paths and messages, but not raw config values.