@nathan.kramer/require-env
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

Require Env

Allows you to easily create config objects from environment variables.

Values are required by default.

E.g getting DB credentials:

const config = requireEnv({
  host: "DB_HOST",
  user: "DB_USER",
  password: "DB_PASS",
  database: "DB_NAME",
  port: ["DB_PORT", positiveInteger],
});

Install

npm install @nathan.kramer/require-env

Usage

Getting a single value

import { requireEnvVar } from '@nathan.kramer/require-env';

const value = requireEnvVar("SOME_ENV_VAR");

Creating a config object

import { requireEnv } from '@nathan.kramer/require-env';

const config = requireEnv({
  someString: "REQUIRED_STRING_VALUE",
  someNum: ["REQUIRED_NUMBER_VALUE", parseInt],
  someOptionalValue: ["OPTIONAL_VALUE", { optional: true }],
  valueWithDefault: ["VALUE_WITH_DEFAULT", "default value"],
});

Built-in parsers for convenience

These are just for convenience. They will error on invalid values.

import { requireEnv, num, bool, positiveInteger } from '@nathan.kramer/require-env';

const config = requireEnv({
  someNum: ["REQUIRED_NUMBER_VALUE", num],
  somePositiveInteger: ["REQUIRED_POSITIVE_INTEGER_VALUE", positiveInteger],
  someBool: ["REQUIRED_BOOLEAN_VALUE", bool],
});

Default values

import { requireEnvVar } from '@nathan.kramer/require-env';

const value = requireEnvVar("SOME_ENV_VAR", "default value");
import { requireEnv } from '@nathan.kramer/require-env';

const config = requireEnv({
  someValue: ["SOME_VALUE", "default value"],
  someOtherValue: ["SOME_INT", { parser: parseInt, defaultValue: 123 }],
});

Package Sidebar

Install

npm i @nathan.kramer/require-env

Weekly Downloads

0

Version

1.0.0

License

MIT

Unpacked Size

9.26 kB

Total Files

5

Last publish

Collaborators

  • nathan.kramer