ts-app-env v1.5.0
ts-app-env
A config library for TypeScript.
This is a small library for encapsulating access to an application's config.
Currently just environment variables.
Goals
The main goals are:
Declare all of an application's config in a single place.
This helps understanding and maintaining an application's config vs. grepping for
process.env.FOOcalls spread throughout the codebase.Perform a simple "all of the config values are available" sanity check immediately on application boot.
This prevents an application booting and then ~seconds/minutes later blowing up because a
process.env.VERY_IMPORTANT_SETTINGis not available.
Non-Goals
Loading config from disk.
This library currently doesn't try to load YAML, JSON, TOML, etc. files from disk; it's generally assumed you're running in a Node/container environment where environment variables are the primary means of configuration.
In theory the
Environmenttype decouplests-app-envfrom the actual Node/process/etc. environment, so you could provide other implementations.You can also use something like
dotenvto load files from disk intoprocess.envand then usets-app-envfrom there.
Usage
First declare your config in a class via the string, number, etc. options:
import { string, number } from 'ts-app-env';
const AppEnv = {
PORT: number(),
SOME_URL: string(),
};And then instantiate it:
import { newConfig } from 'ts-app-env';
export const env = newConfig(AppEnv, process.env);newConfig will fail if any non-optional config parameters are not available.
Then in the rest of your application, you can import env:
import { env } from 'env.ts';
env.SOME_URL; // already ensured to be setConfiguration
The library supports both a convention of "property name == environment name" that allow succinct declaration:
const AppEnv = {
PORT: number(),
}As well as customization of each property via an options hash:
const AppEnv = {
port: number({
env: 'CUSTOM_ENV_NAME',
default: 8080,
optional: true,
}),
}Where:
envprovides the environment name to look up; if not provided it defaults to the property name snake-cased (e.g.someThingwill be looked up asSOME_THING)defaultprovides a default value to use if the environment value is not setoptionalmarks the option as optional, and will change the return type e.g. fromport: numbertoport: number | undefined