Configuration
Load, validate, and inject application configuration through the DI container. Domain services stay on @di-framework/core; this package is the typed config layer.
Features
Sources:
envSource,objectSource,jsonFileSource(deep-merged left → right).Validation: pluggable
ConfigSchema— optional Zod adapter at@di-framework/config/zod.DI registration:
registerConfigexposes the root object plus flattened dotted paths.Decorators:
@Configuration/@Valuematch the rest of the framework.Imperative API:
loadConfig/loadAndRegisterConfigfor scripts and tests.
Installation
Decorators need TypeScript 5 and experimentalDecorators. emitDecoratorMetadata is not required.
Quick Start
With APP_PORT=8080 and APP_DATABASE__HOST=db.internal, db.port is 8080 and db.host is db.internal.
@Configuration builds defaults from class property initializers, merges sources, registers the result under the 'config' token (with flattened paths), and registers the class as a singleton holding the loaded snapshot.
Imperative API
Prefer this when sources are async or you want explicit control in bootstrap code:
Sync variants: loadConfigSync/loadAndRegisterConfigSync (all sources must return plain objects, not Promises).
Zod Validation
Any object with a parse(input) method can implement ConfigSchema. Use schemaFromParse to wrap a plain function.
Env Mapping
Option | Default | Meaning |
|---|---|---|
|
| Only keys with this prefix; prefix is stripped |
|
| Nesting delimiter after strip |
|
| Segment transform ( |
|
| Parse booleans, numbers, JSON literals |
APP_DB__HOST=localhost → { db: { host: 'localhost' } }.
Injecting Values
With flatten: true (the default), every dotted path is a DI token. @Value('database.host') is equivalent to @Component('config.database.host').
API Reference
Export | Description |
|---|---|
| Merge defaults + sources (+ schema) |
| Put config (and paths) on the container |
| Load then register |
| Built-in sources |
| Decorators |
| Schema helpers |
|
|
Non-goals (v1)
YAML/TOML loaders, remote config providers, live reload / watch, and secret managers. Implement ConfigSource/ConfigSchema for those.
Example
A worked example lives in the config example package.