Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

type-env

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

type-env

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

  • 1.1.0
  • latest
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

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,
    }
})

FAQs

Package last updated on 13 Jun 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc