npm.io
0.1.3 • Published 3 years ago

env-plus

Licence
ISC
Version
0.1.3
Deps
2
Size
22 kB
Vulns
0
Weekly
0
Stars
3

env-plus

This is work in progress. Api changes frequently

Getting started

Run your favourite package manager

npm install env-plus
# yarn add env-plus
# pnpm install env-plus

Make sure you have these properties in typescript config

"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"strictPropertyInitialization": false

Create .env file with example variables

MESSAGE="hello there"
PORT="3000"
LOGGER="true"

Create your first env schema

import * as dotenv from 'dotenv'
dotenv.config()

import { EnvSchema, EnvProperty, EnvManager } from 'env-plus'

@EnvSchema()
export class MyEnvSchema {
  @EnvProperty('MESSAGE')
  message: string

  @EnvProperty('PORT')
  port: number

  @EnvProperty('LOGGER')
  logger: boolean
}

const env = EnvManager.getPlainInstance(MyEnvSchema)

console.log(env)

Returns an object with parsed values

{
  message: 'hello there',
  port: 3000,
  logger: true
}

Keywords