Giter Site home page Giter Site logo

robinbuschmann / type-config Goto Github PK

View Code? Open in Web Editor NEW
12.0 2.0 0.0 155 KB

Type safe way defining configurations fed by environment variables, process arguments or json config files (including deserialization and validation)

License: MIT License

TypeScript 99.85% JavaScript 0.15%
config env type-safety json

type-config's Introduction

Build Status codecov

type-env

Type safe way defining configurations fed by environment variables, process arguments or json config files (including deserialization and validation).

Installation

npm install type-env --save

type-env requires reflect-metadata

npm install reflect-metadata --save

Your tsconfig.json needs the following flags:

{
  "experimentalDecorators": true,
  "emitDecoratorMetadata": true
}

Getting started

Setup configuration class

import {Config, EnvValue} from 'type-env';
@Config
class DataBaseConfiguration {
    @EnvValue('DB_HOST') host: string = 'localhost'; // default value
    @EnvValue('DB_NAME') name: string;
    @EnvValue('DB_PORT') port: number;
}
import {Config, ArgsValue} from 'type-env';
@Config
class LoggingConfiguration {
    @ArgsValue('log-level') level: string;
    @ArgsValue('silent') silent: boolean;
}
{
  "auth": {
    "jwt": {
      "issuer": "type-env"
    },
    "timestamp": "2018-05-27T17:35:54.391Z"
  }
}
import {Config, JsonConfiguration, JsonValue} from 'type-env';
@Config
@JsonConfiguration(`${__dirname}/config.json`)
class AuthConfiguration {
    @JsonValue('auth.jwt.issuer') jwtIssuer: string = 'type-env';
    @JsonValue('auth.timestamp') timestamp: Date;
}

Run application

DB_HOST='127.0.0.1' /
DB_NAME='type-env' /
DB_PORT='1234' /
node app.js --log-level info --silent

Options

import {buildDecorators, NodeEnvConfigSource, NodeArgsConfigSource, JsonConfigSource} from 'type-env';

const {Value, EnvValue, ArgsValue, JsonValue} = buildDecorators({
    /**
     * Enables validation if true. Throws if config value is invalid.
     * @default true
     */
    validate: true,
    
    /**
     * Throws if value does not exist on source.
     * @default true
     */
    required: true,
    
    /**
     * If true, loads config value when property is accessed.
     * @default true
     */
    lazyLoad: true,

    /**
     * Do not throw on validation or requirement errors, but logs a warning instead.
     * @default false
     */
    warnOnly: false,
    
    /**
     * Map of decorator key name and config source.
     * @default {
                    Value: NodeEnvConfigSource,
                    EnvValue: NodeEnvConfigSource,
                    ArgsValue: NodeArgsConfigSource,
                    JsonValue: JsonConfigSource,
                }
     */
    decoratorMeta: {
        Value: NodeEnvConfigSource,
        EnvValue: NodeEnvConfigSource,
        ArgsValue: NodeArgsConfigSource,
        JsonValue: JsonConfigSource,
    }
})

type-config's People

Contributors

robinbuschmann avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

type-config's Issues

Default values not replaced using EnvValue

Using the EnvValue decorator and setting a default value will result in that default value always being used even if an env var with that name exists on process.env.

  • Node 12
  • TypeScript 3.5

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.