[ANN] rescript-envsafe - Makes sure you don't accidentally deploy apps with missing or invalid environment variables

I’ve just release rescript-envsafe@1.0.0. It validates access to environment variables and parses them to the right type. So we won’t accidentally deploy apps with missing or invalid environment variables. No need to struggle like in the post anymore: Strategy for accessing Node environment variables.

========================================
❌ Invalid environment variables:
    API_URL ("http//example.com/graphql"): Invalid url
💨 Missing environment variables:
    MY_VAR: Disallowed empty string
    PORT: Missing value
========================================

Heavily inspired by the great project envsafe, but designed with care for ReScript users:

  • Always strict - only access the variables you have defined
  • Built for node.js and the browser
  • Composable parsers with rescript-struct

Basic usage

%%private(let envSafe = EnvSafe.make())

let nodeEnv =
  envSafe->EnvSafe.get(
    ~name="NODE_ENV",
    ~struct=S.union([
      S.literalVariant(String("production"), #production),
      S.literalVariant(String("development"), #development),
      S.literalVariant(String("test"), #test),
    ]),
    ~devFallback=#development,
    (),
  )
let port = envSafe->EnvSafe.get(~name="PORT", ~struct=S.int()->S.Int.port(), ~devFallback=3000, ())
let apiUrl =
  envSafe->EnvSafe.get(
    ~name="API_URL",
    ~struct=S.string()->S.String.url(),
    ~devFallback="https://example.com/graphql",
    (),
  )
let auth0ClientId = envSafe->EnvSafe.get(~name="AUTH0_CLIENT_ID", ~struct=S.string(), ())
let auth0Domain = envSafe->EnvSafe.get(~name="AUTH0_DOMAIN", ~struct=S.string(), ())

envSafe->EnvSafe.close()
10 Likes