1.2.1 • Published 2 years ago

type-envy v1.2.1

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

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.
1.2.1

2 years ago

1.2.0

3 years ago

1.1.8

3 years ago

1.1.7

3 years ago

1.1.6

3 years ago

1.1.5

3 years ago

1.1.3

3 years ago

1.1.2

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago