2.0.0 • Published 2 years ago

env-typed v2.0.0

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

env-typed

Utility functions for type-safe environment variables.

import { requireEnv, numberEnv, boolEnv } from 'env-typed';

// The type of 'FOO' will be 'string' instead of 'string | null'.
const FOO = requireEnv('FOO');

Installation

npm:

npm install --save env-typed

yarn:

yarn add env-typed

Usage

basic

import { requireEnv, numberEnv, boolEnv } from 'env-typed';

const FOO = requireEnv('FOO');

The type of FOO is now string instead of string | null. If process.env.FOO is null or undefined, an EnvVarError will be thrown with a helpful message "Missing required env var: FOO.".

const PORT = numberEnv('SERVER_PORT', 5000);

The type of PORT is now number instead of string | null. If process.env.SERVER_PORT is null or undefined, the value of PORT will fall back to 5000.

const IS_PRODUCTION = boolEnv(
	'IS_PRODUCTION',
	() => requireEnv('NODE_ENV') === 'production'
);

The type of IS_PRODUCTION is now boolean instead of string | null. If process.env.IS_PRODUCTION is null or undefined, IS_PRODUCTION will take the value returned by the function used as the second argument.

For more detailed usage, see the api docs.

todo: add more usage docs

API

TODO

2.0.0

2 years ago

1.0.2

3 years ago

1.0.1

3 years ago

0.0.0

3 years ago

1.0.0

3 years ago