Skip to content

Commit

Permalink
refactor: Use module definition to build dynamic module
Browse files Browse the repository at this point in the history
BREAKING CHANGE:
Methods renamed `forRoot(Async)` to `register(Async)`
  • Loading branch information
unlight committed May 9, 2024
1 parent 379a464 commit 5e3a082
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 35 deletions.
2 changes: 0 additions & 2 deletions src/nestolog-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import ololog from 'ololog';

import { Entry } from './types';

export const NESTOLOG_OPTIONS = Symbol('NESTOLOG_OPTIONS');

export type NestologOptions = typeof nestologOptionsDefaults &
Parameters<typeof ololog.configure>[0];

Expand Down
8 changes: 8 additions & 0 deletions src/nestolog.module-definition.ts
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();
36 changes: 7 additions & 29 deletions src/nestolog.module.ts
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 {}
9 changes: 5 additions & 4 deletions src/nestologger.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import tinydate from 'tinydate';
import wrapAnsi from 'wrap-ansi';

import { messageColumnWidth } from './message-column-width';
import { MODULE_OPTIONS_TOKEN } from './nestolog.module-definition';
import {
type NestologOptions,
customLocateDefault,
NESTOLOG_OPTIONS,
type NestologOptions,
nestologOptionsDefaults,
} from './nestolog-options';
import { bullet, stringify } from './string';
import { Entry } from './types';
Expand All @@ -19,14 +20,14 @@ export class NestoLogger implements LoggerService {
verbose = this.debug.bind(this);

constructor(
@Inject(NESTOLOG_OPTIONS) private readonly options: NestologOptions,
@Inject(MODULE_OPTIONS_TOKEN) private readonly options: NestologOptions,
@Inject('ololog') @Optional() private readonly logger = ololog,
) {
this.logger = this.createLogger(logger);
}

private createLogger(logger: ololog) {
logger = logger.configure(this.options);
logger = logger.configure({ ...nestologOptionsDefaults, ...this.options });
const width =
this.options.messageColumnWidth ||
messageColumnWidth(this.options, logger);
Expand Down
48 changes: 48 additions & 0 deletions src/test-app.ts
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();

0 comments on commit 5e3a082

Please sign in to comment.