configuar
TypeScript icon, indicating that this package has built-in type declarations

0.5.0 • Public • Published

Type-safe configuration loader for TypeScript

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

How to use

  1. Install yarn add configuar
  2. Define your application config
import { EnvVariable } from 'configuar';

export default class AppConfig {
  @EnvVariable()
  port: number;

  @EnvVariable()
  dbUrl: string;
}
  1. Define the environment using variables or .env file
// .env
PORT=3001
DB_URL=postgres://localhost:5234/postgres
  1. 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.

Docs

Further plans

  1. Advanced validation: check value formats like Urls, integers etc
  2. More decorator options
  3. Flexible config schema generator: add human readable config docs generator

Package Sidebar

Install

npm i configuar

Weekly Downloads

2

Version

0.5.0

License

ISC

Unpacked Size

128 kB

Total Files

118

Last publish

Collaborators

  • xydens