@rettgerst/env-proxy
TypeScript icon, indicating that this package has built-in type declarations

0.2.0 • Public • Published

env-proxy

uses proxies to simplify reading from process.env in node.

example

import { ConnectionOptions } from '@mikro-orm/core';

import env from '@rettgerst/env-proxy';

let connection: ConnectionOptions;

// typed as boolean | undefined
const { VERCEL } = env.bool;

if (VERCEL) {
	// typed as string, will throw if not defined
	const {
		VERCEL_GIT_COMMIT_REF: branch,
		VERCEL_ENV: stage,
		DB_HOST: host,
		DB_USER: user,
		DB_PASSWORD: password
	} = env.required.string;

	// typed as number, parsed with parseInt, will throw if undefined or NaN
	const { DB_PORT: port } = env.required.int;

	let dbName: string;

	if (stage === 'production') dbName = 'myapp-production';
	else if (stage === 'preview') dbName = `myapp-preview-${branch}`;
	else if (stage === 'development') dbName = `myapp-dev-${branch}`;
	else throw new Error(`Unrecognized vercel deployment stage ${stage}`);

	connection = { host, port, user, password, dbName };
} else {
	const { DB_HOST: host = 'localhost' } = env.string;
	const { DB_USER: user, DB_PASSWORD: password } = env.required.string;

	connection = {
		dbName: 'myapp',
		host,
		user,
		password
	};
}

Readme

Keywords

none

Package Sidebar

Install

npm i @rettgerst/env-proxy

Weekly Downloads

8

Version

0.2.0

License

ISC

Unpacked Size

13.3 kB

Total Files

15

Last publish

Collaborators

  • rettgerst