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

classenv

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

classenv

Describe your environment variables contract with TypeScript class

  • 1.2.3
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
90
increased by38.46%
Maintainers
1
Weekly downloads
 
Created
Source

classenv

A perfect typescript class environment variables library.

  • Strongly-typed declarative class containing your environment data
  • Type-casting using TypeScript metadata reflection
  • Auto UPPER_SNAKE_CASE conversion
  • Throws runtime error if variable doesn't exist
  • Default values support
  • Makes decorated properties read-only in runtime

.env

IS_SOMETHING_ENABLED=1
import { Env } from 'classenv';

class Environment {
  @Env() // Auto UPPER_SNAKE_CASE conversion supported
  static isSomethingEnabled: number;

  @Env() // Won't throw, because got default value
  static withDefault: string = 'yeah its me'

  @Env('IS_SOMETHING_ENABLED')
  static isEnabledStr: string;

  @Env('IS_SOMETHING_ENABLED')
  static isEnabledNmbr: number;

  @Env('IS_SOMETHING_ENABLED')
  static isEnabledBln: boolean;
}

 // string 1
console.log(typeof Environment.isEnabledStr, Environment.isEnabledStr)
 // number 1
console.log(typeof Environment.isEnabledNmbr, Environment.isEnabledNmbr)
 // boolean true
console.log(typeof Environment.isEnabledBln, Environment.isEnabledBln)
 // number 1
console.log(typeof Environment.isSomethingEnabled, Environment.isSomethingEnabled)

Environment.isEnabledBln = false;
// TypeError: Cannot assign to read only property 'isEnabledBln' of function 'class Test{}'

@Env property data type should be scalar (string, number or boolean).

Dependencies

###reflect-metadata

npm i reflect-metadata

And then import it somewhere close to your entry point (index.ts/main.ts/etc...). Should be imported before any of your environment classes.

import 'reflect-metadata';

###tsconfig.json

These settings should be enabled

"emitDecoratorMetadata": true,
"experimentalDecorators": true,

Keywords

FAQs

Package last updated on 14 Jun 2020

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