1.0.0 • Published 7 years ago
@bashleigh/nest-config v1.0.0
Nest Config
A config component for nestJS.
Install
yarn add @bashleigh/nest-confignpx add --save @bashleigh/nest-configcreate a .env file and insert your configurations
touch .env && echo 'APP_TEST=true' >> .envHow 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.txtSrc
Get a path within src (uses root)
this.config.src('mypath'); //returns /var/www/src/mypathIntegrating 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