No description
| .husky | ||
| src | ||
| testing | ||
| .eslintrc.json | ||
| .gitignore | ||
| .gitlab-ci.yml | ||
| jest.config.cjs | ||
| nest-cli.json | ||
| package.json | ||
| pnpm-lock.yaml | ||
| README.md | ||
| tsconfig.build.json | ||
| tsconfig.json | ||
Gettings started
Installing the package along with @nestjs/config:
pnpm install @nestjs/config @devdroplets/nestjs-easy-config
Create a class that defines your Config schema:
// src/app-config.ts
import { ConfigType } from '@devdroplets/nestjs-easy-config';
import { IsString, IsInt } from 'class-validator';
@ConfigType()
export class AppConfig {
// Validate using class-validator!
@IsInt()
PORT: number;
// Define defaults!
@IsString()
HOST = 'localhost';
}
Import the ConfigModule and register your schema:
// src/app.module.ts
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { AppConfig } from './app-config';
@Module({
imports: [ConfigModule.forRoot(AppConfig)],
})
export class AppModule {}
Now in any service or controller, you can inject the config using your decorated config class:
// src/app.service.ts
import { Injectable } from '@nestjs/common';
import { AppConfig } from './app-config';
@Injectable()
export class AppService {
constructor(private readonly config: AppConfig) {}
getHello(): string {
// Typed, validated and populated with defaults!
return `Hello ${this.config.HOST}:${this.config.PORT}!`;
}
}