1.1.0 • Published 6 years ago
neconfig v1.1.0
Neconfig
Description
Neconfig - configuration module for Nestjs.
Example
Installation
npm i neconfigFeatures
- Reading environment variables
- Reading from multiple .env files (using dotenv)
- Reading plain js objects
- Providing default values
- Providing different config per module
// String
const host: string | undefined = config.getString('host'); // optional
const host2: string = config.getString('host', 'localhost'); // default value
const host3: string = config.getStringOrThrow('host'); // required, throwing error if host didn't provided
const host4: string = config.getStringOrThrow('host', 'Host is required'); // specified error message
// Integer
const port: number | undefined = config.getInt('prot');
const port2: number = config.getInt('port', 3000);
const port3: number = config.getIntOrThrow('port');
// Number
const price: number | undefined = config.getNumber('price');
const price2: number = config.getNumber('price', 1.99);
const price3: number = config.getNumberOrThrow('price');
// Boolean
const logsEnabled: boolean | undefined = config.getBoolean('logs_enabled');
const logsEnabled2: boolean = config.getBoolean('logs_enabled', true);
const logsEnabled3: boolean = config.getBooleanOrThrow('logs_enabled');Types
getStringreads any thing that have toString() method, returns undefined otherwisegetIntparses value usingparseInt(value, 10), returns undefined if NaNgetNumberparses value usingparseFloat(value), returns undefined if NaNgetBooleanparses value with checks
# Truthy values for boolean
true, 'true', 'yes', 'y', '1', 1
# Falsy values for boolean
false, 'false', 'no', 'n', '0', 0Readers
There are few readers:
envreader, that allows to read from environment variables and if required, from .env fileshashreader, that allows to read from plain js objects
Env reader
NeconfigModule.register({
readers: [
{ name: 'env', file: path.resolve(process.cwd(), '.env') }
]
})Here, file is optional, allowing to read config from files (using dotenv)
If the file option provided and file exists, Neconfig will parse that file content with dotenv and merge with process.env
Variables from file will be overridden by environment variables. Example:
.env file
HOST=myhost.com
PORT=3000Setting environment variable and run
$ export PORT=80
$ npm start
> Listening on http://myhost.com:80Hash reader
NeconfigModule.register({
readers: [
{
name: 'hash',
data: { HOST: 'localhost', PORT: 3000 }
}
]
})Overriding config
You can register NeconfigModule with several readers:
NeconfigModule.register({
readers: [
{ name: 'hash', data: { HOST: 'localhost', PORT: 3000 } }, // (1)
{ name: 'env', file: path.resolve(process.cwd(), '.env') } // (2)
{ name: 'env', file: path.resolve(process.cwd(), 'dev.env') } // (3)
]
}).env file
APP_NAME = Neconfig
HOST = myhost.com
PORT = 80dev.env file
HOST = 0.0.0.0
PORT = 4000Run
$ npm start
> config: {HOST: '0.0.0.0', PORT: 4000, APP_NAME: 'Neconfig'}- First reader
(1)setsHOSTtolocalhostandPORTto3000 - Second reader
(2)overridesHOSTtomyhost.com,PORTto80and adds new keyAPP_NAME - Third reader
(3)overridesHOSTto0.0.0.0andPORTto4000
If you remove dev.env file you will see:
$ npm start
> config: {HOST: 'myhost.com', PORT: 80, APP_NAME: 'Neconfig'}Provide different config per modules
app.module.ts
@Module({
imports: [
NeconfigModule.register({
readers: [
{ name: 'env', file: path.resolve(process.cwd(), 'configs', 'app.env') }
]
})
]
})
export class AppModule {}feat.module.ts
@Module({
imports: [
NeconfigModule.register({
readers: [
{ name: 'env', file: path.resolve(process.cwd(), 'configs', 'feat.env') }
]
})
]
})
export class FeatModule {}AppModule and FeatModule have different instances of ConfigReader
Contributing
You are welcome with this project for contributing, just make a PR