-
Notifications
You must be signed in to change notification settings - Fork 7
Configs
Tim DeHof edited this page Oct 8, 2024
·
5 revisions
Config modules in NestJS allow you to manage configuration settings for your application in a centralized and type-safe manner. They help separate configuration concerns from business logic, making your code more maintainable and easier to test.
Each config module will typically consists of:
- An Interface file
*.interface.ts - A module file
*.module.ts - A schema file
*.schema.tsfor validation - A service file
*.service.tsfor some modules
- OAuth
- app
- auth
- database
- update your local
.envfile with the secrets - If needed, Create a new config module:
- Define the interface in
<moduleName>Config.interface.ts - Create the module in
<moduleName>Config.module.ts - Define the schema for validation in
<moduleName>Config.schema.ts - If needed, create a service in
<moduleName>Config.service.ts
- Import the config module into the main
app.module.ts
import { Module } from '@nestjs/common';
import { AppConfigModule } from './config/app/appConfig.module';
import { AuthConfigModule } from './config/auth/authConfig.module';
// Import other config modules as needed
@Module({
imports: [
AppConfigModule,
AuthConfigModule,
// Add other config modules
],
// ...
})
export class AppModule {}- Use the config in the desired services:
import { Injectable } from '@nestjs/common';
import { AppConfigService } from './config/app/appConfig.service';
@Injectable()
export class YourService {
constructor(private appConfigService: AppConfigService) {}
someMethod() {
const appConfig = this.appConfigService.getConfig();
// Use the config...
}
}- add oauth providers secrets to the
.env - goto
src\config\Oauthfolder - add the oauth secrets types to the "OAuthConfig" interface in
oauthConfig.interface.ts - add the oauth secrets to the provider under the
useFactoryinoauthConfig.module.ts - add the oauth secrets to the schema in
oauthConfig.schema.ts
Now the env variables should type-safe and be used in the project