1.0.0 • Published 4 years ago

enw v1.0.0

Weekly downloads
5
License
MIT
Repository
github
Last release
4 years ago

enw

npm

Reusable environment variable validator.

Full docs is coming soon...

Installation

$ npm install enw

Usage

In a Reusable Module

// src/index.ts

export * from './types';
export { default as readEnv } from './read-env';
// src/types.ts

export interface Config {
  host: string;
  port: number;
}
// src/read-env.ts

import * as enw from 'enw';
import { Config } from './types';

function readEnv(prefix: string = 'DB'): enw.ReadFn<Config> {
  return enw.scope(prefix, {
    host: enw.host('HOST', 'localhost'),
    port: enw.port('PORT', 5432),
  });
}

In an Application

// src/app.ts

import getConfig from './get-config';

const config = getConfig('APP_NAME', process.env);
// src/types.ts

import { Config as Db } from 'reusable-database-module';

export interface Config {
  db: Db;
}
// src/get-config.ts

import * as enw from 'enw';
import { readEnv as db } from 'reusable-database-module';
import { Config } from './types';

export default function getConfig(prefix: string, env: NodeJS.ProcessEnv): Config {
  const readEnv = enw.scope(prefix, {
    db: db(),
  });
  const config = readEnv(env);
  
  return config;
}