atenv
v1.0.8
Published
A modern dotenv replacement for Typescript projects
Downloads
38
Maintainers
Readme
AtEnv is a modern replacement for parsing env on Typescript projects
How to install
npm install atenv
How to use
Declare a class which you want to parse env to
import 'reflect-metadata'; // required
import { IsEnum } from 'class-validator';
export enum NodeEnv = {
DEV = 'development',
PRODUCTION = 'production'
}
class AWS {
@Env('AWS_SECRET')
secret: string;
@Env('AWS_KEY')
key: string;
}
export class AppConfig {
@Env('PORT')
@IsDefined() //you can use class validator decorators here
port: number; // gets transformed to number automatically
// it is also possible to define child classes for ease of use
@Section(() => AWS)
aws: AWS;
@Env('NODE_ENV')
@IsEnum(NodeEnv); // throws error if not defined
nodeEnv: NodeEnv;
//helper getters are okay too
get isDev() {
return this.nodeEnv === NodeEnv.DEV;
}
}
export const appConfig = parseEnv(AppConfig); //appConfig is now an instance of AppConfig containing values from .env
//now access your env like this
appConfig.port // number
Booleans, and Numbers are automatically transformed, if you need to use custom transformation logic, you can use the
@Transform
Decorator from class-transformer
Default and optional values
it is possible to set default values like this
export enum NodeEnv = {
DEV = 'development',
PRODUCTION = 'production'
}
export class AppConfig {
@Env('NODE_ENV')
@IsEnum(NodeEnv); // throws error if not defined
@IsOptional() // class validator will treat this value as optional
nodeEnv: NodeEnv = NodeEnv.DEV;
}
const myEnv = parseEnv(AppConfig)
myEnv.nodeEnv === 'development' //true
Custom Env files
export enum NodeEnv = {
DEV = 'development',
PRODUCTION = 'production'
}
export class AppConfig {
@Env('NODE_ENV')
@IsEnum(NodeEnv); // throws error if not defined
@IsOptional() // class validator will treat this value as optional
nodeEnv: NodeEnv = NodeEnv.DEV;
}
const myEnv = parseEnv(AppConfig, {
dotEnvOptions: {
path: '.env.development' // or a path to your env
}
})
Additional Options
You can parse additional options for class-validator and class-transformer
parseEnv(YourClass, {
transformOptions: {} // override classToclass options
validatorOptions: {} // override validateSync options
});
Docs for options: