@orveth/config

Parse strongly typed configuration objects from a ConfigSource using an explicit field map. Parsers are small composable functions that throw ConfigError when input is missing or invalid.

Core types

TypeRole
ConfigSourceInterface with get(key: string): string | undefined.
ConfigParser<T>Function from string | undefined to T, expected to throw on invalid input.
ConfigField<T>Combines a source key with a parse function.
ConfigShape<T>Maps output property names to ConfigField definitions.

Sources

  • createMapSource(values) — plain object backing for tests and deterministic tooling.
  • createProcessEnvSource(env?) — wraps process.env (or a compatible object).

Built-in parsers

ParserBehavior
requiredString(key)Trims input; throws when missing or empty after trim.
optionalString(key, defaultValue?)Returns undefined or default when missing/blank.
coercedInteger(key, options?)Parses base-10 integers via Number.parseInt; optional default when missing/blank.
coercedBoolean(key, options?)Accepts true/false/1/0/yes/no/on/off (case-insensitive); optional default when missing/blank.

Example

config/runtime.ts
import {
  coercedInteger,
  createMapSource,
  parseConfig,
  requiredString,
} from "@orveth/config";

const source = createMapSource({
  PORT: "8080",
  SERVICE_NAME: "billing-api",
});

export const runtimeConfig = parseConfig(source, {
  port: { key: "PORT", parse: coercedInteger("PORT") },
  serviceName: { key: "SERVICE_NAME", parse: requiredString("SERVICE_NAME") },
});