0.1.2 • Published 10 months ago

envmagic v0.1.2

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

Env Magic

Automated Tests

Manage your environment variables like magic. Just import env from envmagic and use it like you would process.env. By default, accessing undefined or empty environment variables throws an error.

import { env } from "envmagic";

// Throws an error if NODE_ENV is undefined or ""
const { NODE_ENV } = env;

Motivation

I think we've all seen and written code that looks a lot like this.

import assert from "node:assert";

const { FOO } = process.env;

assert(FOO, "FOO is required!");

This is error prone, ugly, and can easily become outdated. It's time to automate your environment variable access.

Customization

Env Magic aims to be highly customizable. You may import the configuration object and change setting in your application code, or you can set most setting via environment variables.

PropertyEnvDefaultDescription
onMissingMAGIC_ENV_ON_MISSINGerrorAction taken for missing (undefined) variables
onEmptyMAGIC_ENV_ON_EMPTYerrorAction taken for missing ("") variables
optionalMAGIC_ENV_OPTIONAL[]Variables for which no action should be taken
loggern/aconsole.warnCustom logger function
import { configuration } from "envmagic";

configuration.set("onMissing", "warn");
configuration.set("onEmpty", "warn");
configuration.set("logger", (message) => {
  // implement custom logger here
});
configuration.set("optional", ["OPTIONAL_VARIABLE"]);

Actions

Actions are the things that Env Magic can do in response to missing or empty environment variables.

  • error: Throws an error indicating a given variable is missing or empty
  • warn: Logs a warning indicating a given variable is missing or empty
  • null: Returns null when the given environment variable is missing or empty
  • undefined: Returns undefined when the given environment variable is missing or
0.1.2

10 months ago

0.1.1

10 months ago

0.1.0

10 months ago