2.7.4 • Published 1 year ago

nestjs-easyconfig v2.7.4

Weekly downloads
1,167
License
MIT
Repository
github
Last release
1 year ago

Nestjs-easyconfig loads configs from your .env (Wraps dotenv module) ⚙️ 🔥

Installation

$ npm install nestjs-easyconfig
$ yarn add nestjs-easyconfig

Usage

With config file supplied (basic):

import { Module } from '@nestjs/common';
import { EasyconfigModule } from 'nestjs-easyconfig';

@Module({
  imports: [EasyconfigModule.register({ path: './config/.env' })],
})
export class AppModule {}

With config file supplied and safe set to true:

import { Module } from '@nestjs/common';
import { EasyconfigModule } from 'nestjs-easyconfig';

@Module({
  imports: [EasyconfigModule.register({ path: './config/.env', safe: true })],
})
export class AppModule {}

By default safe is set to false. When safe is set to true, the module compares the supplied env file with the sample env file to find missing keys. If any keys which are in .env.sample but not in the evironment used, it is immediately reported in console.

Note: To use this, a sample env file .env.sample should be placed in the root dir

Other config include dotenv's configs like encoding (Default: utf8) and debug(Default: false)

Without config file supplied:

import { Module } from '@nestjs/common';
import { EasyconfigModule } from 'nestjs-easyconfig';

@Module({
  imports: [EasyconfigModule.register({})],
})
export class AppModule {}

In this case, you have to pass in the NODE_ENV value and the .env file to read will be determined accordingly. Loads environment variables from .env.[development|test|production][.local] files For example, NODE_ENV=dev will make the app read .env.dev

Note: The .env file also has to be in root folder

Getting environment variables

Regardless of how the EasyconfigModule is imported into the app, you can get the variable values using the EasyconfigService.

import { Controller, Get } from '@nestjs/common';
import { EasyconfigService } from 'nestjs-easyconfig';

@Controller('api')
export class AppController {
  constructor (private config: EasyconfigService) {}
  @Get()
  findAll() {
    return {
      value: this.config.get('key')
    };
  }
}

Note: the get method will automatically cast environment variables

Type processing

Example of type processing: Imagine you have a configuration file at .env with the following:

FOO=bar
BAZ=2
BEEP=false
BOOP=some,thing,that,goes,wow
# note how we use an asterisk here to turn off the parsing for this variable
BLEEP=false*
# note how we use an asterisk in the array to turn off parsing for an array key value
PING=ping,true*,2,100
# note a string between bacticks won't be parsed
PONG=`some,thing,that,goes,wow`

After using this plugin, the environment variables are parsed to their proper types.

To test it out, simply log the returned object in your console:

console.log(env);

And you'll see that it outputs the properly parsed variable types:

{
  // String
  FOO: 'bar',
  // Number
  BAZ: 2,
  // Boolean
  BEEP: false,
  // Array
  BOOP: [ 'some', 'thing', 'that', 'goes', 'wow' ],
  // NOTE: this was not parsed due to the * asterisk override above
  BLEEP: 'false',
  // NOTE: only the `true*` above was opted out through the use of an asterisk
  PING: [ 'ping', 'true', 2, 100 ],
  // NOTE: this was not parsed because the string was between bacticks
  PONG: 'some,thing,that,goes,wow'
}

If your configuration line ends in * it will not be parsed by this package, which allows you to keep values as the String variable type if needed. Also when you encapsulate a value between bacticks e.g. `value`, the value won't be parsed and it will return as a String variable. This can be used in situations where you for example have a ',' inside your string and it should not be parsed as an array.

Config

    /**
     * path to the file to load.
     * If this is not passed, Easyconfig load the environment file based on
     * the NODE_ENV with the naming convention of `.env.<NODE_ENV>`.
     *
     * For example, if the NODE_ENV is `production`, the file `.env.<NODE_ENV>` will load.
     */
    path ? : string;

    /**
     * path of the file to check the keys against when safe option is set to true.
     * Defaults to .env.sample
     */

    sampleFilePath ? : string;

    /**
     * checks whether the used env file is missing some keys.
     *
     * For example, if the given `.env` file has the following content:
     * ```
     * VAR=true
     * ```
     *
     * and the `.env.sample` file has the following:
     * ```
     * VAR=true
     * VAR2=sample value
     * ```
     *
     * the following error log will be printed:
     * ```
     * MissingEnvVarsError: [VAR2] were defined in .env.example but are not present in the environment:
     *     This may cause the app to misbehave.
     * ```
     */
    safe ? : boolean;

    /**
     * As the lib uses dotenv, You may turn on dotenv's logging to help debug why certain keys or
     * values are not being set as you expect.
     *
     * default : false
     */

    debug ? : boolean;

    /**
     * This turns on parse logs which help debug how keys are being parsed.
     *
     * default : false
     */

    parseLog ? : boolean;

    /**
     * This option lets you specify the encoding of your file containing environment variables.
     *
     * ```
     */

    encoding ? : string;

    /**
     * This option allows you to pass in a pre-defined logger instance.
     * The logger must implement the NestJS LoggerService interface
     */

    logger ? : LoggerService;

    /**
     * This option allows you assign the values to process.env . Defaults to true
     */

    assignToProcessEnv ? : boolean;

    /**
     * This option allows you overide a value on process.env if its alreadt set . Defaults to false
     */

    overrideProcessEnv ? : boolean;

Contributing

In general, we follow the "fork-and-pull" Git workflow.

  1. Fork the repo on GitHub
  2. Clone the project to your own machine
  3. Work on your fork
    1. Make your changes and additions
      • Most of your changes should be focused on src/ and test/ folders and/or README.md.
      • Files in dist/ folder are autogenerated when running tests (npm run build) and need not to be changed manually.
    2. Change or add tests if needed
    3. Run tests and make sure they pass
    4. Add changes to README.md if needed
  4. Commit changes to your own branch
  5. Make sure you merge the latest from "upstream" and resolve conflicts if there is any
  6. Repeat step 3(3) above
  7. git add and run npm run commit and fill in the details accordingly
  8. Push your work back up to your fork
  9. Submit a Pull request so that we can review your changes

Stay in touch

License

The package is MIT licensed.

Support on Beerpay

Hey dude! Help me out for a couple of :beers:!

Beerpay Beerpay

Contributors ✨

Thanks goes to these wonderful people (emoji key):

This project follows the all-contributors specification. Contributions of any kind welcome!

2.7.4

1 year ago

2.7.3

1 year ago

2.7.2

1 year ago

2.7.1

2 years ago

2.6.3

2 years ago

2.7.0

2 years ago

2.6.1

2 years ago

2.6.0

2 years ago

2.5.3

2 years ago

2.5.2

3 years ago

2.5.1

3 years ago

2.5.0

3 years ago

2.4.5

3 years ago

2.4.3

3 years ago

2.4.4

3 years ago

2.4.2

3 years ago

2.4.0

3 years ago

2.3.6

3 years ago

2.3.1

4 years ago

2.3.0

4 years ago

2.2.9

4 years ago

2.2.3

4 years ago

2.2.2

4 years ago

2.2.1

4 years ago

2.2.0

4 years ago

2.1.0

4 years ago

2.0.14

4 years ago

2.0.12

4 years ago

2.0.11

4 years ago

2.0.9

4 years ago

2.0.10

4 years ago

2.0.8

4 years ago

2.0.7

4 years ago

2.0.6

4 years ago

2.0.5

4 years ago

2.0.4

4 years ago

2.0.3

4 years ago

2.0.2

4 years ago

2.0.2-0

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

1.9.6

4 years ago

1.9.5

4 years ago

1.9.4

4 years ago

1.9.3

4 years ago

1.9.0

4 years ago

1.8.13

4 years ago

1.8.14

4 years ago

1.8.12

5 years ago

1.8.10

5 years ago

1.8.9

5 years ago

1.8.8

5 years ago

1.8.7

5 years ago

1.8.6

5 years ago

1.8.5

5 years ago

1.8.0

5 years ago

1.7.1

5 years ago

1.7.0

5 years ago

1.7.0-beta

5 years ago

1.6.1

5 years ago

1.6.0

5 years ago

1.5.1

5 years ago

1.5.0

5 years ago

1.5.0-beta

5 years ago

1.4.0

5 years ago

1.2.2

5 years ago

1.2.1

5 years ago

1.2.0

5 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago