@dumpstate/config
v0.4.1
Published
File and env configuration loader.
Downloads
1
Readme
@dumpstate/config
File and env configuration loader.
Install
Install package:
npm install @dumpstate/config --save
Conventions
Module loads the configuration from:
default.application.json
file under specified directory -./config/
folder of a current working directory by default,application.json
file under specified directory -./config/
folder of a current working directory by default,*.json
file which path is provided asAPPLICATION_CONFIG
environment variable,- environment - all environment variable prefixed with
${APP_NAME}${SEPARATOR}
whereAPP_NAME
is your applicaiton prefix, e.g.FOO
andSEPARATOR
is configured env name separator,__
by default.
All the configuration is validated with ajv. The schema of the config is expected to be defined with JSON TypeDef.
Usage
- Define your schema with JSON TypeDef.
const ConfigSchema = {
properties: {
db: {
properties: {
host: { type: "string" },
port: { type: "int32" },
},
},
},
} as const
- Load the config.
import { loadConfig } from "@dumpstate/config"
const config = loadConfig(ConfigSchema, { appName: "foo" })
Note the config type is derived from the JSON TypeDef. config
object is guaranteed to follow the schema declaration thanks to ajv
.
In case you'd like to pass config reference around, declare the config type:
import { ConfigSchemaType } from "@dumpstate/config"
type Config = ConfigSchemaType<typeof ConfigSchema>
API
loadConfig(schema, opts)
where:
schema
- JSON object of JSON TypeDef schema,opts
- JSON object containing the following parameters:appName
- the name of the application - required as prefix for environment loader (the name is being snake and upper cased),separator
- separator of environment variable -__
by default,targetDir
- target directory of the file loader -${CWD}/config/
by default,configPath
- path to configuration file; could be provided asAPPLICATION_CONFIG
environment variable instead; null by default.