npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

io-ts-env

v0.4.1

Published

An io-ts decoder combinator that adapts a decoder to receive a flattened environment variable object.

Downloads

8

Readme

This package contains an io-ts decoder combinator that turns a decoder expecting possibly nested objects into a decoder expecting a flat object whose keys are styled in UPPER_SNAKE_CASE, like how environment variables are defined.

This combinator will therefore let you compose an environment variable parser from smaller object decoder units -- perhaps controlling different parts of your application -- and taking care of converting names for you, as long as the property names of your object decoder units consistently follow one of the supported casing conventions.

import { forEnv } from "io-ts-env";
import { string } from "io-ts/lib/Decoder";
import { pipe } from "fp-ts/pipeable";

const decodeEnv = pipe(
  type({
    foo: string,
    barBaz: string,
    nested: type({
      innerProperty: string
    })
  }),
  forEnv()
);

The resulting decoder will first preproces and environment variable object (for instance, process.env, or a parsed .env file using e.g. envfile) looking like this:

{
  FOO: "some value",
  BAR_BAZ: "another value",
  NESTED__INNER_PROPERTY: "nested value"
}

The resulting environment variable decoder will have convert names with words from the original properties (inferred from casing conventions) separated by _ and nested object property names are concatnated separated by __. Both separators can be overridden. For instance, the combinator below will convert the variable NESTED_INNER-PROPERTY=value into { Nested: { InnerProperty: "value" }}

/**
 * This will now match environment variables like:
 * `NESTED_INNER-PROPERTY` and convert it into
 *
 */
forEnv({
  wordSeparator: "-",
  objectSeparator: "_",
  wordCasing: "upperCamelCase"
});

Supported casing conventions

  • UpperCamelCase
  • lowerCamelCase (default)
  • UPPER-KEBAB-CASE
  • lower-kebab-case
  • UPPER_SNAKE_CASE
  • lower_snake_case

Error reporting

The package attempts to map back key errors from io-ts into the environment variable name, so that you won't have to remember the convention you set up. So a property fooBar should appear as FOO_BAR in the error report.

However, there are limitations. By design of io-ts Decoder which swallows all internal definitions and exposes only the decoder method, it is not possible to get the generated full path of the environment variable that is missing in a nested object, because the type decoder will bail out as soon as an outer key is missing. So the variable NESTED_INNER-PROPERTY would first fail saying that the key NESTED is missing, but once you have that, it would complain that NESTED_INNER-PROPERTY is missing. This also means that it is not possible to list all environment variables from the decoder, since the information of its properties cannot be accessed.

Issues like these could be circumvented by using io-ts Type or Codec, where property definitions are still accessible. A combinator for them may be implemented in future.