This package helps you:
- define you app config type using classes&decorators
- validate the config according to class property types
- read config values from environment and other sources like .env files
- automatically generate documentation about the app config
- Install
yarn add configuar
- Define your application config
import { EnvVariable } from 'configuar';
export default class AppConfig {
@EnvVariable()
port: number;
@EnvVariable()
dbUrl: string;
}
- Define the environment using variables or
.env
file
// .env
PORT=3001
DB_URL=postgres://localhost:5234/postgres
- Read the configuration
const config = ConfigLoader.getConfig<AppConfig>({ ctor: AppConfig });
/* AppConfig {
port: 3001,
dbUrl: `postgres://localhost:5234/postgres`
} */
getConfig
throws a validation error, if there are missing variables or wrong value types.
5. Create config schema to share it with your colleagues
npx configuar get-schema ./dist/app-config.ts
You will get:
{
"type": "object",
"properties": {
"PORT": {
"type": "number"
},
"DB_URL": {
"type": "string"
}
},
"required": ["PORT", "DB_URL"]
}
Put the schema to README.md to explain your colleagues which env variables are required to run the application.
This was just a basic example. You can define nested config objects, use optional params and use it within a Nestjs application.
- Advanced validation: check value formats like Urls, integers etc
- More decorator options
- Flexible config schema generator: add human readable config docs generator