-
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(module): adds a
forFeatures
method to the module
This method will allow for devs to provide an array of classes, functions or strings, mixed with an array of objects that provide the name for the context and the options for that provider. Docs to follow shortly.
- Loading branch information
Showing
6 changed files
with
125 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
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,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 {} |
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,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' }; | ||
} | ||
} |
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,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'); | ||
}); | ||
}); | ||
}); |
2 changes: 1 addition & 1 deletion
2
packages/nestjs-module/src/interfaces/ogma-provieder-options.interface.ts
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,3 +1,3 @@ | ||
export interface OgmaProviederOptions { | ||
export interface OgmaProviderOptions { | ||
addRequestId: boolean; | ||
} |
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