@bashleigh/nest-config

A singular config for NestJS

Stats

stars 🌟issues ⚠️updated 🛠created 🐣size 🏋️‍♀️
51820Jul 26, 2021Feb 24, 2018Minified + gzip package size for @bashleigh/nest-config in KB

Readme

Nest Config

GitHub tag
A config component for nestJS.

Install

yarn add @bashleigh/nest-config
npx add --save @bashleigh/nest-config

create a .env file and insert your configurations

touch .env && echo 'APP_TEST=true' >> .env

How to use

Get

Get a parameter from the config

const test = this.config.get('APP_TEST');

With a default option

const test = this.config.get('APP_TEST', false);

Has

Check your config has a parameter defined

this.config.has('APP_TEST');

Root

Get a root path like /var/www/mypath

this.config.root('mypath'); //returns /var/www/mypath
this.config.root('file.txt'); //returns /var/www/file.txt

Src

Get a path within src (uses root)

this.config.src('mypath'); //returns /var/www/src/mypath

Integrating with modules

import {
    Injectable,
} from '@nestjs/common';

import ConfigModule from '@bashleigh/nest-config';

@Injectable({
  imports: [
      ConfigModule,
  ],
  controllers: [],
  providers: [],
})
export class ApplicationModule {
  constructor() {}
}

Injection

Inject the component into a controller


import {
    Controller,
} from '@nestjs/common';

import {
    ConfigService,
} from '@bashleigh/nest-config';

@Controller('user')
export default class UserController {
    constructor(private readonly config: ConfigService) {}
    
}

Injection with decorator

import {
    Controller,
} from '@nestjs/common';
import {
    InjectConfig,
} from '@bashleigh/nest-config';

@Controller('user')
export default class UserController {
    constructor(@InjectConfig() private readonly config) {}
}

Built by AS

If you find any bugs or have a feature request, please open an issue on github!

The npm package download data comes from npm's download counts api and package details come from npms.io.