A simple environment variable parser with type safety and validation out of the box.
Using yarn:
$ yarn add @kldzj/env
Using npm:
$ npm i -S @kldzj/env
- Type safety
- Custom parsers
- Optional (and default) values
- Passing a custom environment object
import { parseEnv } from '@kldzj/env';
const env = parseEnv({
PORT: {
type: 'number',
optional: true,
default: 3000,
},
NODE_ENV: {
type: 'string',
optional: true,
},
DB_URL: {
type: 'string',
},
});
console.log(env.PORT); // typed as number
console.log(env.NODE_ENV); // typed as string | undefined
console.log(env.DB_URL); // typed as string, an error is thrown in case it's missing
The parser is a function that takes a string and returns a value of any type. It can be used to parse environment variables that are not strings.
If a parser is present, the type of the variable is ignored and instead the return type of the parser is used.
import { parseEnv } from '@kldzj/env';
const env = parseEnv({
DATE: {
parser: (value) => new Date(value),
},
});
console.log(env.DATE); // typed as Date
const env = parseEnv(
{
PORT: {
type: 'number',
},
},
{
env: {
...process.env,
...someOtherEnv,
},
throwOnNaN: true,
defaultParser: (value, item) => {
// The default parser must honor the type of the variable (item.type) if it is present
// ...
return newValue;
},
}
);
An object that contains the environment variables. If not provided, process.env
is used.
If true
, an error is thrown if a variable type is number and is parsed as NaN
. Defaults to false
.
Allows you to override the default parser. The default parser is used when a variable does not have a custom parser.