0.2.0 • Published 3 years ago

@rettgerst/env-proxy v0.2.0

Weekly downloads
-
License
ISC
Repository
-
Last release
3 years ago

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
	};
}
0.2.0

3 years ago

0.1.2

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago