1.1.0 • Published 5 years ago

neconfig v1.1.0

Weekly downloads
9
License
MIT
Repository
github
Last release
5 years ago

Neconfig

Description

Neconfig - configuration module for Nestjs.

Example

neconfig-examples

Installation

npm i neconfig

Features

  • 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

  • getString reads any thing that have toString() method, returns undefined otherwise
  • getInt parses value using parseInt(value, 10), returns undefined if NaN
  • getNumber parses value using parseFloat(value), returns undefined if NaN
  • getBoolean parses value with checks
# Truthy values for boolean
true, 'true', 'yes', 'y', '1', 1

# Falsy values for boolean
false, 'false', 'no', 'n', '0', 0

Readers

There are few readers:

  • env reader, that allows to read from environment variables and if required, from .env files
  • hash reader, 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=3000

Setting environment variable and run

$ export PORT=80
$ npm start
> Listening on http://myhost.com:80

Hash 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 = 80

dev.env file

HOST = 0.0.0.0
PORT = 4000

Run

$ npm start
> config: {HOST: '0.0.0.0', PORT: 4000, APP_NAME: 'Neconfig'}
  • First reader (1) sets HOST to localhost and PORT to 3000
  • Second reader (2) overrides HOST to myhost.com, PORT to 80 and adds new key APP_NAME
  • Third reader (3) overrides HOST to 0.0.0.0 and PORT to 4000

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

1.1.0

5 years ago

1.0.9

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago