Nest-kr/config
This library load and validate environment variables
Installation
$ npm install --save @nest-kr/config
How to use
Set config file with @Config(key). you have to pass key for finding your config from ConfigService.
import { Transform } from 'class-transformer';
import { IsNumber } from 'class-validator';
import { Config } from '../src/Config';
@Config('app')
export class AppConfig {
@IsNumber()
public port: number = parseInt(process.env.PORT, 10);
@IsString()
public serviceId: string = process.env.SERVICE_ID;
}
Register your config files to ConfigModule
import {ConfigModule} from '@nest-kr/config';
@Module({
imports: [ConfigModule.forRoot(AppConfig, ...)],
})
export class AppModule {}
Get your config from ConfigService using key
// in bootstrap
import {ConfigService} from '@nest-kr/config';
async function bootstrap(){
const app = await NestFactory.create(AppModule);
const configService: ConfigService = app.get(ConfigService);
const appConfig:AppConfig = configService.get('app');
...
}
// in class
@Injectable()
class TestService{
constructor(private configService: ConfigService){}
async function test(){
const appConfig: AppConfig = this.configService.get('app');
}
}