Skip to content
/ type-cfg Public

Declare your configuration schema using classes and decorators.

License

Notifications You must be signed in to change notification settings

m19c/type-cfg

Repository files navigation



Declare your configuration schema using classes and decorators.

Motivation

We all know the pain that comes with using environment variables in your application. Since each variable is a string you have to cast the content of it to actually use it.

With type-cfg there is just one declartion for your entire application: one config to rule them all.

Installation

  1. Install the node package: npm install type-cfg --save OR yarn add type-cfg
  2. You also need to install reflect-metadata shim: npm install reflect-metadata --save OR yarn add reflect-metadata
  3. Add reflect-metadata to your app-entry file: import 'reflect-metadata';
  4. 🔥 Enjoy!

Documentation

Basic Usage

import TypeConfig, { Definition, Property } from 'type-cfg';

@Definition()
class Config extends TypeConfig {
  @Property({ source: 'NODE_ENV' })
  environment: string;
}

const config = new Config();

if (config.environment === 'development') {
  // ...
}

Examples

Decorators

@Definition

Scope: Class Decorator

Configuration

Usage

@Definition();

@Property

Scope: Property Decorator

Configuration

Property Required Default Description
source - The environment variable / object key as a string of the value you want to acquire
delimiter , The delimiter used to split the value into an array
required true Marks the property as required
defaultValue - The default value. Note that the defaultValue will be applied even if the value is required

Usage

@Property();
@Property(options: PropertyOptions);
@Property(typeFunction: TypeFunction);
@Property(typeFunction: TypeFunction, options: PropertyOptions);

Accumulate

Once your configuration is decorated you can accumulate it...

...by using a function call

import { accumulate } from 'type-cfg';

const config = new MyConfig();
accumulate(config);

// ...

...by using the abstract class

import TypeConfig, { Definition, Property } from 'type-cfg';

class MyConfig extends TypeConfig {
  // ...
}

const config = new MyConfig();

// ...

Thank you

A huge thanks goes to the creators of TypeGraphQL and TypeORM. They gave me the inspiration to not only manage GraphQL schemas and Database relations but also configurations.