diff --git a/integration/src/for-feats/app.controller.ts b/integration/src/for-feats/app.controller.ts new file mode 100644 index 000000000..9580923cc --- /dev/null +++ b/integration/src/for-feats/app.controller.ts @@ -0,0 +1,17 @@ +import { Controller, Get } from '@nestjs/common'; +import { OgmaLogger, OgmaService } from '@ogma/nestjs-module'; +import { AppService } from './app.service'; + +@Controller() +export class AppController { + constructor( + private readonly service: AppService, + @OgmaLogger(AppController) private readonly logger: OgmaService, + ) {} + + @Get() + sayHello() { + this.logger.log('Hello'); + return this.service.hello(); + } +} diff --git a/integration/src/for-feats/app.module.ts b/integration/src/for-feats/app.module.ts new file mode 100644 index 000000000..d782aacdc --- /dev/null +++ b/integration/src/for-feats/app.module.ts @@ -0,0 +1,19 @@ +import { Module } from '@nestjs/common'; +import { OgmaModule } from '@ogma/nestjs-module'; +import { AppController } from './app.controller'; +import { AppService } from './app.service'; + +@Module({ + imports: [ + OgmaModule.forFeatures([AppService, AppController]), + OgmaModule.forRoot({ + service: { + application: 'forFeatures', + }, + interceptor: false, + }), + ], + providers: [AppService], + controllers: [AppController], +}) +export class ForFeatsModule {} diff --git a/integration/src/for-feats/app.service.ts b/integration/src/for-feats/app.service.ts new file mode 100644 index 000000000..e9332e688 --- /dev/null +++ b/integration/src/for-feats/app.service.ts @@ -0,0 +1,12 @@ +import { Injectable } from '@nestjs/common'; +import { OgmaLogger, OgmaService } from '@ogma/nestjs-module'; + +@Injectable() +export class AppService { + constructor(@OgmaLogger(AppService) private readonly logger: OgmaService) {} + + hello() { + this.logger.log('Hello'); + return { hello: 'world' }; + } +} diff --git a/integration/test/for-feats.spec.ts b/integration/test/for-feats.spec.ts new file mode 100644 index 000000000..f5db1462d --- /dev/null +++ b/integration/test/for-feats.spec.ts @@ -0,0 +1,47 @@ +import { INestApplication } from '@nestjs/common'; +import { Test } from '@nestjs/testing'; +import { createProviderToken, OgmaService } from '@ogma/nestjs-module'; +import { AppController } from '../src/for-feats/app.controller'; +import { ForFeatsModule } from '../src/for-feats/app.module'; +import { AppService } from '../src/for-feats/app.service'; +import { httpPromise } from './utils'; + +describe('OgmaModule.forFeatures()', () => { + let ogmaAppService: OgmaService; + let ogmaAppController: OgmaService; + let app: INestApplication; + + beforeAll(async () => { + const modRef = await Test.createTestingModule({ + imports: [ForFeatsModule], + }).compile(); + ogmaAppService = modRef.get(createProviderToken(AppService.name)); + ogmaAppController = modRef.get(createProviderToken(AppController.name)); + app = modRef.createNestApplication(); + await app.listen(0); + }); + + afterAll(async () => { + await app.close(); + }); + + describe('call endpoint', () => { + let appUrl: string; + + beforeAll(async () => { + appUrl = await app.getUrl(); + }); + + it('should log with different loggers for service and controller', async () => { + const controllerSpy = jest.spyOn(ogmaAppController, 'log'); + const serviceSpy = jest.spyOn(ogmaAppService, 'log'); + expect(controllerSpy).toHaveBeenCalledTimes(0); + expect(serviceSpy).toHaveBeenCalledTimes(0); + await httpPromise(appUrl + '/', { method: 'GET' }); + expect(controllerSpy).toHaveBeenCalledTimes(1); + expect(controllerSpy).toHaveBeenCalledWith('Hello'); + expect(serviceSpy).toHaveBeenCalledTimes(1); + expect(serviceSpy).toHaveBeenCalledWith('Hello'); + }); + }); +}); diff --git a/packages/nestjs-module/src/interfaces/ogma-provieder-options.interface.ts b/packages/nestjs-module/src/interfaces/ogma-provieder-options.interface.ts index e75d34d4a..f46eeda04 100644 --- a/packages/nestjs-module/src/interfaces/ogma-provieder-options.interface.ts +++ b/packages/nestjs-module/src/interfaces/ogma-provieder-options.interface.ts @@ -1,3 +1,3 @@ -export interface OgmaProviederOptions { +export interface OgmaProviderOptions { addRequestId: boolean; } diff --git a/packages/nestjs-module/src/ogma.module.ts b/packages/nestjs-module/src/ogma.module.ts index d54fa3927..c01a7808e 100644 --- a/packages/nestjs-module/src/ogma.module.ts +++ b/packages/nestjs-module/src/ogma.module.ts @@ -6,7 +6,7 @@ import { createRequestScopedLoggerProviders, } from './ogma.provider'; import { OgmaCoreModule } from './ogma-core.module'; -import { OgmaProviederOptions } from './interfaces/ogma-provieder-options.interface'; +import { OgmaProviderOptions } from './interfaces/ogma-provieder-options.interface'; @Module({ /* imports: [OgmaCoreModule.Deferred], @@ -33,7 +33,7 @@ export class OgmaModule { */ static forFeature( context: string | (() => any) | Type, - options: OgmaProviederOptions = { addRequestId: false }, + options: OgmaProviderOptions = { addRequestId: false }, ): DynamicModule { const providers: Provider[] = this.createProviders(context, options); return { @@ -44,10 +44,35 @@ export class OgmaModule { }; } + static forFeatures( + contexts: Array< + | { + context: string | (() => any) | Type; + options: OgmaProviderOptions; + } + | string + | (() => any) + | Type + >, + ): DynamicModule { + const providers: Provider[] = contexts.map((ctx) => { + if (typeof ctx === 'object') { + return this.createProviders(ctx.context, ctx.options)[0]; + } + return this.createProviders(ctx)[0]; + }); + return { + module: OgmaModule, + imports: [OgmaCoreModule.externallyConfigured(OgmaCoreModule, 0)], + providers, + exports: providers, + }; + } + private static createProviders( context: string | (() => any) | Type, - options: OgmaProviederOptions = { addRequestId: false }, - ) { + options: OgmaProviderOptions = { addRequestId: false }, + ): Provider[] { if (options.addRequestId) { return createRequestScopedLoggerProviders(context); }