Git Product home page Git Product logo

nestjs-easyconfig's Introduction

Hits Package License NPM Downloads circleci

Description

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

Module uses dotenv-parse-variables lib (https://www.npmjs.com/package/dotenv-parse-variables) to process env file. Example of type processing:

    foo=test
    bar=1
    baz=true
    qux=test,1,true,
    bat=false*,
    qwe=`1,2,3`,
    asd=test,1,true*

will be processed to

    {
        foo: 'test',
        bar: 1,
        baz: true,
        qux: ['test', 1, true],
        bat: 'false',
        qwe: '1,2,3',
        asd: ['test', 1, 'true']
    }

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 🍻!

Beerpay Beerpay

Contributors ✨

Thanks goes to these wonderful people (emoji key):


Dmitry Lukanin

πŸš‡ ⚠️ πŸ’»

Brad

πŸš‡ πŸ’»

Andras Bacsai

πŸš‡

Eugen Istoc

⚠️ πŸ’»

Jay McDoniel

⚠️ πŸ’»

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

nestjs-easyconfig's People

Contributors

rubiin avatar dependabot-preview[bot] avatar allcontributors[bot] avatar dlukanin avatar genu avatar bradtaniguchi avatar andrasbacsai avatar jmcdo29 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.