Socket
Book a DemoInstallSign in
Socket

type-envy

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

type-envy

TypeEnvy is a super simple library that gives you **typed environment variables**!

latest
Source
npmnpm
Version
1.2.1
Version published
Maintainers
1
Created
Source

TypeEnvy

TypeEnvy is a super simple library that gives you typed environment variables!

It couldn't be easier to use. Just do:

yarn add type-envy

Then in your code that needs environment variables do:

Quick Start

import { TypeEnvy, TypeEnvyArgument } from "type-envy";

const env = TypeEnvy(types => ({
    // TypeScript Primitives
    SOME_STRING_VARIABLE: types.String,
    SOME_NUMBER_VARIABLE: types.Number,
    // TypeScript Unions
    SOME_CHOICE_VARIABLE: ["yes", "no"] as const,
    SOME_OPTIONAL_STRING_VARIABLE: [types.Nothing, types.String] as const,
    // Custom Types
    SOME_CUSTOM_TYPE: (build: TypeEnvyArgument): SpecialType => ({
        someFlag: build.value ? true | false,
        someValue: build.value ? parseFloat(build.value.toString()) * 100 : NaN
    })
}))

type SpecialType = {
    someFlag: boolean,
    someValue: number,
}

Dealing with Missing Variables

When an environment variable is not available or cannot be converted to your type, you'll get a useful runtime exception:

// Error: Could not safely convert required value THIS_VAR_DOES_NOT_EXIST from <empty> to String.
const env = TypeEnvy(types => ({
    THIS_VAR_DOES_NOT_EXIST: types.String
}))

Roadmap:

  • Error and warning capture for user type functions (TypeEnvyArgument).
  • Optionally defer process.env to make re-exporting TypeEnvy type more developer friendly.

FAQs

Package last updated on 14 Sep 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