-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Use module definition to build dynamic module
BREAKING CHANGE: Methods renamed `forRoot(Async)` to `register(Async)`
- Loading branch information
Showing
5 changed files
with
68 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { ConfigurableModuleBuilder } from '@nestjs/common'; | ||
|
||
import { NestologOptions } from './nestolog-options'; | ||
|
||
export const { ConfigurableModuleClass, MODULE_OPTIONS_TOKEN } = | ||
new ConfigurableModuleBuilder<Partial<NestologOptions>>({ | ||
optionsInjectionToken: 'NESTOLOG_OPTIONS', | ||
}).build(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,10 @@ | ||
import { DynamicModule, Module } from '@nestjs/common'; | ||
import { Logger, Module } from '@nestjs/common'; | ||
|
||
// import { asyncStorageProvider } from './async-storage.provider'; | ||
import { | ||
NESTOLOG_OPTIONS, | ||
NestologOptions, | ||
nestologOptionsDefaults, | ||
} from './nestolog-options'; | ||
import { ConfigurableModuleClass } from './nestolog.module-definition'; | ||
import { NestoLogger } from './nestologger.service'; | ||
|
||
@Module({}) | ||
export class NestologModule { | ||
static forRoot(options: Partial<NestologOptions> = {}): DynamicModule { | ||
options = { ...nestologOptionsDefaults, ...options }; | ||
return { | ||
module: NestologModule, | ||
providers: [ | ||
// { | ||
// provide: APP_INTERCEPTOR, | ||
// useClass: AsyncStorageInterceptor, | ||
// }, | ||
// asyncStorageProvider, | ||
{ | ||
provide: NESTOLOG_OPTIONS, | ||
useValue: options, | ||
}, | ||
NestoLogger, | ||
], | ||
exports: [NestoLogger], | ||
}; | ||
} | ||
} | ||
@Module({ | ||
providers: [NestoLogger, Logger], | ||
exports: [NestoLogger, Logger], | ||
}) | ||
export class NestologModule extends ConfigurableModuleClass {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { Injectable, Logger, Module } from '@nestjs/common'; | ||
import { NestFactory } from '@nestjs/core'; | ||
|
||
import { NestologModule } from './nestolog.module'; | ||
import { NestoLogger } from './nestologger.service'; | ||
|
||
const loggerModule = NestologModule.register({}); | ||
|
||
@Injectable() | ||
class UserService { | ||
constructor( | ||
private readonly logger: Logger, | ||
private readonly nestoLogger: NestoLogger, | ||
) { | ||
nestoLogger.log('Hello from user service constructor'); | ||
logger.log('Hello from user service constructor'); | ||
} | ||
|
||
testLog() { | ||
this.nestoLogger.log('Hello from user service testLog'); | ||
this.logger.log('Hello from user service testLog'); | ||
} | ||
} | ||
|
||
@Module({ | ||
imports: [loggerModule], | ||
providers: [UserService], | ||
}) | ||
export class UserModule {} | ||
|
||
@Module({ | ||
imports: [UserModule, loggerModule], | ||
}) | ||
export class AppModule {} | ||
|
||
const bootstrap = async (): Promise<void> => { | ||
const app = await NestFactory.create(AppModule); | ||
const port = process.env.PORT ?? 3333; | ||
app.useLogger(app.get(NestoLogger)); | ||
|
||
await app.listen(port, () => { | ||
Logger.log(`Listening at http://localhost:${port}`); | ||
|
||
app.get(UserService).testLog(); | ||
}); | ||
}; | ||
|
||
void bootstrap(); |