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.3.1
  • 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
  • Supports both static and instance properties
  • Type-casting using TypeScript metadata reflection
  • Auto UPPER_SNAKE_CASE conversion
  • Converts environment values "FALSE", "false", "0" to false for boolean types
  • Throws runtime error if variable doesn't exist
  • Default values support
  • Makes decorated properties read-only in runtime

Description

Let's pretend we have very simple

.env

IS_SOMETHING_ENABLED=1

How can we describe it using classenv

environment.ts

import { Env } from 'classenv';

export class Environment {
  @Env() // Auto UPPER_SNAKE_CASE conversion
  static isSomethingEnabled: number; // process.env.IS_SOMETHING_ENABLED

  @Env() // Instance properties supported
  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;
}

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

main.ts

import {Environment} from './environment.ts'

console.log(typeof Environment.isEnabledStr, Environment.isEnabledStr)
 // string 1

console.log(typeof Environment.isEnabledNmbr, Environment.isEnabledNmbr)
 // number 1

console.log(typeof Environment.isEnabledBln, Environment.isEnabledBln)
 // boolean true

console.log(typeof Environment.isSomethingEnabled, Environment.isSomethingEnabled)
 // number 1

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


// Let's check instance properties
const env = new Environment();

console.log(env.isSomethingEnabled) // 1

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 03 May 2022

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