Knfg
Knfg is a configuration loader for typescript projects. It uses class property reflection to dynamically load configuration from into a class from some arbitrary configuration backend at runtime.
Config Backends
- Environment Variables
In the future, knfg is prioritizing support for AWS DynamoDB and S3. These will come eventually.
Usage
import Knfg from "knfg";
const k = new Knfg({ backend: "env " });
class Config {
@k.Param({ key: "MONGO_URL" })
public mongoUrl!: string;
@k.Param({ key: "SECRET_PASSWORD", optional: true })
public secretPassword?: string;
}
const config = await k.Load(new Config());
Behavior
By default, all keys are "required", which means that knfg will throw an error if a key is specified
but is missing in the backend. You can mark a key as optional: true
if you'd like it to be
optional and not throw an error if its missing; just be sure to reflect that optionality if the
type of that field with a ?
;
The only types Knfg supports, at this time and the expected future, are strings, booleans, and numbers.
Knfg will attempt to coerce types. That specific behavior is defined on a per-backend basis:
Env
All environment variables are strings, obviously. So:
string
: Its already a string, to no conversion.number
: UsesparseInt(value, 10)
and will throw an error if it fails.boolean
: Accepts"true"
or"false"
, case insensitive on both.
6 years ago