Skip to content

Commit

Permalink
feat(backend): introduce NestJS libraries and implement config library
Browse files Browse the repository at this point in the history
  • Loading branch information
chownces committed Sep 6, 2023
1 parent 77fbbdc commit c491ce0
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 19 deletions.
11 changes: 3 additions & 8 deletions backend/apps/gateway/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,11 @@ import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { ClientProxyFactory } from '@nestjs/microservices';
import gatewayConfiguration from './config/configuration';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { ConfigService } from '@nestjs/config';
import { ConfigModule } from '@app/config';

@Module({
imports: [
ConfigModule.forRoot({
load: [gatewayConfiguration],
ignoreEnvFile: ['staging', 'production'].includes(process.env.NODE_ENV),
envFilePath: process.env.NODE_ENV === 'test' ? '.env.test' : '.env',
}),
],
imports: [ConfigModule.loadConfiguration(gatewayConfiguration)],
controllers: [AppController],
providers: [
{
Expand Down
3 changes: 3 additions & 0 deletions backend/apps/gateway/test/jest-e2e.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@
"testRegex": ".e2e-spec.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
"moduleNameMapper": {
"^@app/config(|/.*)$": "<rootDir>/../../../libs/config/src/$1"
}
}
9 changes: 2 additions & 7 deletions backend/apps/question/src/question.module.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
import { Module } from '@nestjs/common';
import { QuestionController } from './question.controller';
import { QuestionService } from './question.service';
import { ConfigModule } from '@nestjs/config';
import questionConfiguration from './config/configuration';
import { ConfigModule } from '@app/config';

@Module({
imports: [
ConfigModule.forRoot({
load: [questionConfiguration],
envFilePath: process.env.NODE_ENV === 'test' ? '.env.test' : '.env',
}),
],
imports: [ConfigModule.loadConfiguration(questionConfiguration)],
controllers: [QuestionController],
providers: [QuestionService],
})
Expand Down
23 changes: 23 additions & 0 deletions backend/libs/config/src/config.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { DynamicModule } from '@nestjs/common';
import {
ConfigService,
ConfigModule as NestConfigModule,
} from '@nestjs/config';

export class ConfigModule extends NestConfigModule {
static loadConfiguration(configuration: () => object): DynamicModule {
return {
imports: [
NestConfigModule.forRoot({
load: [configuration],
ignoreEnvFile: ['staging', 'production'].includes(
process.env.NODE_ENV,
),
envFilePath: process.env.NODE_ENV === 'test' ? '.env.test' : '.env',
}),
],
module: ConfigModule,
providers: [ConfigService],
};
}
}
1 change: 1 addition & 0 deletions backend/libs/config/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './config.module';
9 changes: 9 additions & 0 deletions backend/libs/config/tsconfig.lib.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"declaration": true,
"outDir": "../../dist/libs/config"
},
"include": ["src/**/*"],
"exclude": ["node_modules", "dist", "test", "**/*spec.ts"]
}
9 changes: 9 additions & 0 deletions backend/nest-cli.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@
"compilerOptions": {
"tsConfigPath": "apps/question/tsconfig.app.json"
}
},
"config": {
"type": "library",
"root": "libs/config",
"entryFile": "index",
"sourceRoot": "libs/config/src",
"compilerOptions": {
"tsConfigPath": "libs/config/tsconfig.lib.json"
}
}
}
}
7 changes: 3 additions & 4 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,22 +60,21 @@
"json",
"ts"
],
"rootDir": "src",
"rootDir": ".",
"testRegex": ".*\\.spec\\.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
"collectCoverageFrom": [
"**/*.(t|j)s"
],
"coverageDirectory": "../coverage",
"testEnvironment": "node",
"roots": [
"<rootDir>/apps/",
"<rootDir>/libs/"
],
"moduleNameMapper": {
"^@app/utils-lib(|/.*)$": "<rootDir>/libs/utils-lib/src/$1"
"^@app/config(|/.*)$": "<rootDir>/libs/config/src/$1"
}
}
}
}
6 changes: 6 additions & 0 deletions backend/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@
],
"@app/utils-lib/*": [
"libs/utils-lib/src/*"
],
"@app/config": [
"libs/config/src"
],
"@app/config/*": [
"libs/config/src/*"
]
}
}
Expand Down

0 comments on commit c491ce0

Please sign in to comment.