/env-decorator

:clubs: Decorator for environment based configurations

Primary LanguageTypeScriptMIT LicenseMIT

Env Decorator

npm version Build Status npm

Type your environment variables with decorators for environment based configurations

📦 Installation

To install the module from npm:

npm install --save env-decorator

📘 Usage

Config class

import { loadConfig, Env } from 'env-decorator'

export class Config {
	
  @Env({ required: true })
  NODE_ENV: string

  // Use the 'number' or 'boolean' type
  @Env({ type: 'number' })
  PORT: number

  @Env()
  ELASTICSEARCH_URL: string

  @Env('APP_DEBUG', { type: 'boolean' })
  DEBUG: boolean = true

  @Env([ 'MONGO_URL', 'MONGO_URI', 'MONGODB_ADDON_URI' ])
  MONGO_URL: string
}

export const config = loadConfig(Config)

Using nested configuration

export class MongoConfig {
  @Env()
  MONGO_URL: string

  @Env()
  MONGO_USER: string

  @Env()
  MONGO_PWD: string
}

export class Config {
	
  @Env({ required: true })
  NODE_ENV: string

  // Use the 'number' or 'boolean' type
  @Env({ type: 'number' })
  PORT: number

  @Env()
  ELASTICSEARCH_URL: string

  @Env('APP_DEBUG', { type: 'boolean' })
  DEBUG: boolean = true

  @Load()
  MONGO: MongoConfig
}

Import

import * as elasticsearch from 'elasticsearch'
import { config } from './config'

const client = new elasticsearch.Client({
  host: config.ELASTICSEARCH_URL
})

📝 License

MIT