0.4.1 • Published 2 years ago
@dumpstate/config v0.4.1
@dumpstate/config
File and env configuration loader.
Install
Install package:
npm install @dumpstate/config --saveConventions
Module loads the configuration from:
default.application.jsonfile under specified directory -./config/folder of a current working directory by default,application.jsonfile under specified directory -./config/folder of a current working directory by default,*.jsonfile which path is provided asAPPLICATION_CONFIGenvironment variable,- environment - all environment variable prefixed with
${APP_NAME}${SEPARATOR}whereAPP_NAMEis your applicaiton prefix, e.g.FOOandSEPARATORis 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_CONFIGenvironment variable instead; null by default.
0.4.1
2 years ago