1.0.1 • Published 3 years ago

decode-environment v1.0.1

Weekly downloads
5
License
ISC
Repository
github
Last release
3 years ago

decode-environment

License NPM Package Build status Code Coverage

Decode environment variables with io-ts

Install

npm install decode-environment

Use

import { decodeEnvironment } from 'decode-environment'
import * as t from 'io-ts'

const codec = t.intersection([
    t.type({
        environment: t.string
    }),
    t.partial({
        retries: t.Int
    })
])

decodeEnvironment(
    codec,
    (env) => ({
        environment: env('NODE_ENV'),
        retries: env('NUM_RETRIES')
    })
)
//=> { _tag: 'Right', right: { environment: 'development', retries: 1 } }

The type-signature of decodeEnvironment is as follows

export function decodeEnvironment<C extends t.TypeC<any> | t.PartialC<any> | t.IntersectionC<any>>(
    codec: C,
    struct: (env: (key: string) => (environment: NodeJS.ProcessEnv) => string | undefined) =>
        { [K in keyof Props<C>]: (environment: NodeJS.ProcessEnv) => string | undefined },
    environment: NodeJS.ProcessEnv = process.env
): E.Either<t.Errors, t.TypeOf<C>>

Features

  • specify environment from which to source variables
  • support t.TypeC codecs
  • support t.PartialC codecs
  • support t.IntersectionC codecs
  • support nested objects in struct
  • support non-env literals in struct

Acknowledgments

1.0.1

3 years ago

1.0.0

3 years ago