0.7.0 • Published 2 years ago
@unifig/env v0.7.0
Env variables & files adapter for Unifig
Table of contents
Installation
npm i @unifig/env
# or
yarn add @unifig/env
Quick Start
# .env
PORT=3000
export class DbSettings {
@From('DB_URL')
@IsString()
url: string;
@From('DB_PASSWORD')
@IsString()
password: string;
}
export class Settings {
@From('PORT')
@IsInt()
port: number;
@Nested(() => DbSettings)
@IsDefined()
db: DbSettings;
}
import { Config } from '@unifig/core';
import { EnvConfigAdapter } from '@unifig/env';
async function bootstrap() {
await Config.register({
template: Settings,
adapter: new EnvConfigAdapter(),
});
console.log(Config.get(Settings).port); // output: 3000
}
bootstrap();
Properties Mapping
Environment variables are mapped to 1D dictionary.
# .env
PORT=3000
DB_HOST=localhost
DB_PORT=4588
// adapter output
{
PORT: 3000,
DB_HOST: 'localhost',
DB_PORT: 4588
}
Options
Property | What it does | Required |
---|---|---|
envFilesPaths | Path to optional environment files to be loaded in given order. Values from them will be overwritten by process envs. | × |
ignoreEnvVars | If "true", environment variables will not be loaded. | × |
expandVariables | See https://www.npmjs.com/package/dotenv-expand. | × |
License
This project is licensed under the MIT License - see the LICENSE file for details.