@specialblend/env
v0.0.11
Published
compile- & runtime- safe, "monadic" node `process.env` parser utility for javascript and typescript
Downloads
7
Readme
@specialblend/env
compile- & runtime- safe, "monadic" node process.env
parser utility for javascript and typescript
install
npm install @specialblend/env
usage
#!/usr/bin/env node-ts
/* eslint-disable new-cap,no-inline-comments */
import env, { BooleanParameter, InvalidParameterException, NumericParameter, StringParameter } from '@specialblend/env';
(async function main() {
// define your environment schema as a TypeScript interface
interface EnvSchema {
EXAMPLE_AWS_ACCESS_KEY_ID: string;
EXAMPLE_AWS_SECRET_ACCESS_KEY: string;
EXAMPLE_TIMEOUT: number;
}
const schema = {
EXAMPLE_AWS_ACCESS_KEY_ID: StringParameter('EXAMPLE_AWS_ACCESS_KEY_ID', 'DEFAULT_EXAMPLE_AWS_ACCESS_KEY_ID'),
EXAMPLE_AWS_SECRET_ACCESS_KEY: StringParameter('EXAMPLE_AWS_ACCESS_KEY_ID'),
EXAMPLE_CACHE_TIMEOUT: NumericParameter('EXAMPLE_CACHE_TIMEOUT', 1234),
EXAMPLE_CACHE_ENABLED: BooleanParameter('EXAMPLE_CACHE_ENABLED', false),
};
// eslint-disable-next-line no-process-env
const myEnv = env<EnvSchema>(process.env, schema);
// typescript definitions enable autocomplete of myEnv[<keyof EnvSchema>]
console.log(await myEnv.EXAMPLE_AWS_ACCESS_KEY_ID); // => process.env.EXAMPLE_AWS_ACCESS_KEY_ID or 'DEFAULT_EXAMPLE_AWS_ACCESS_KEY_ID'
console.log(await myEnv.EXAMPLE_AWS_SECRET_ACCESS_KEY); // => process.env.EXAMPLE_AWS_SECRET_ACCESS_KEY or throws {InvalidParameterException}
console.log(await myEnv.EXAMPLE_CACHE_TIMEOUT); // => process.env.EXAMPLE_CACHE_TIMEOUT or `1234`
console.log(await myEnv.EXAMPLE_CACHE_ENABLED); // => process.env.EXAMPLE_CACHE_ENABLED as boolean or `false`
}()).catch(console.log);