envsafe
Makes sure you don't accidentally deploy apps with missing or invalid envir...
README
envsafe 🔒
Validate access to environment variables and parse them to the right type. Makes sure you don't accidentally deploy apps with missing or invalid environment variables.
- ```
- ========================================
- ❌ Invalid environment variables:
- API_URL: Invalid url input: "http//example.com/graphql"
- �� Missing environment variables:
- MY_VAR: Missing value or empty string
- PORT: Missing value or empty string
- ========================================
- ```
Heavily inspired by the great project envalid, but with some key differences:
- Written in 100% TypeScript
- Always strict - only access the variables you have defined
- Built for node.js and the browser
- No dependencies - tiny bundle for browser/isomorphic apps
- Install
How to use
Works the same in the browser and in node. See the [./examples](./examples)-folder for more examples.
Install
- ```sh
- yarn add envsafe
- ```
- ```sh
- npm i envsafe --save
- ```
Basic usage
- ```ts
- import { str, envsafe, port, url } from 'envsafe';
- export const env = envsafe({
- NODE_ENV: str({
- devDefault: 'development',
- choices: ['development', 'test', 'production'],
- }),
- PORT: port({
- devDefault: 3000,
- desc: 'The port the app is running on',
- example: 80,
- }),
- API_URL: url({
- devDefault: 'https://example.com/graphql',
- }),
- AUTH0_CLIENT_ID: str({
- devDefault: 'xxxxx',
- }),
- AUTH0_DOMAIN: str({
- devDefault: 'xxxxx.auth0.com',
- }),
- });
- ```
It defaults to using process.env as a base for plucking the vars, but it can be overridden like this:
- ```ts
- export const env = envsafe(
- {
- ENV_VAR: str({
- devDefault: 'myvar',
- }),
- },
- {
- env: window.__ENVIRONMENT__,
- },
- );
- ```
Built-in validators
Function | return | Description |
---|---|---|
--------- | ------------ | ------------------------------------------------------------------------------------------------ |
`str()` | `string` | Passes |
`bool()` | `boolean` | Parses |
`num()` | `number` | Parses |
`port()` | `number` | Ensures |
`url()` | `string` | Ensures |
`email()` | `string` | Ensures |
`json()` | `unknown` | Parses |
Possible options
All optional.
Name | Type | Description |
---|---|---|
------------ | ------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
`choices` | `TValue[]` | Allow-list |
`default` | `TValue` | A |
`devDefault` | `TValue` | A |
`input` | `string` | As |
`allowEmpty` | `boolean` | Default |
These values below are not used by the library and only for description of the variables.
Name | Type | Description |
---|---|---|
--------- | ------------------- | ------------------------------------------------------------------ |
`desc` | `string` | A |
`example` | `string` | An |
`docs` | `string` | A |
Custom validators/parsers
- ```ts
- import { makeValidator, envsafe } from 'envsafe';
- const barParser = makeValidator<'bar'>(input => {
- if (input !== 'bar') {
- throw new InvalidEnvError(`Expected '${input}' to be 'bar'`);
- }
- return 'bar';
- });
- const env = envsafe({
- FOO: barParser(),
- });
- ```
Error reporting
By default the reporter will
- Make a readable summary of your issues
- console.error-log an error
- window.alert() with information about the missing envrionment variable if you're in the browser
- Throws an error (will exit the process with a code 1 in node)
Can be overridden by the reporter-property
- ```ts
- const env = envsafe(
- {
- MY_VAR: str(),
- },
- {
- reporter({ errors, output, env }) {
- // do stuff
- },
- },
- );
- ```
Strict mode (recommended for JS-users)
It wraps the function in Object.freeze and a Proxy that disallows access to any props that aren't defined.
- ``` js
- import { envsafe, str } from 'envsafe';
- export const browserEnv = envsafe(
- {
- MY_ENV: str(),
- },
- {
- strict: true,
- },
- );
- ```