Skip to content

Commit

Permalink
feat(module): adds a forFeatures method to the module
Browse files Browse the repository at this point in the history
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
jmcdo29 committed Sep 12, 2020
1 parent 9ef3d41 commit e7f5df1
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 5 deletions.
17 changes: 17 additions & 0 deletions integration/src/for-feats/app.controller.ts
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();
}
}
19 changes: 19 additions & 0 deletions integration/src/for-feats/app.module.ts
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 {}
12 changes: 12 additions & 0 deletions integration/src/for-feats/app.service.ts
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' };
}
}
47 changes: 47 additions & 0 deletions integration/test/for-feats.spec.ts
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');
});
});
});
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export interface OgmaProviederOptions {
export interface OgmaProviderOptions {
addRequestId: boolean;
}
33 changes: 29 additions & 4 deletions packages/nestjs-module/src/ogma.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand All @@ -33,7 +33,7 @@ export class OgmaModule {
*/
static forFeature(
context: string | (() => any) | Type<any>,
options: OgmaProviederOptions = { addRequestId: false },
options: OgmaProviderOptions = { addRequestId: false },
): DynamicModule {
const providers: Provider[] = this.createProviders(context, options);
return {
Expand All @@ -44,10 +44,35 @@ export class OgmaModule {
};
}

static forFeatures(
contexts: Array<
| {
context: string | (() => any) | Type<any>;
options: OgmaProviderOptions;
}
| string
| (() => any)
| Type<any>
>,
): 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<any>,
options: OgmaProviederOptions = { addRequestId: false },
) {
options: OgmaProviderOptions = { addRequestId: false },
): Provider[] {
if (options.addRequestId) {
return createRequestScopedLoggerProviders(context);
}
Expand Down

0 comments on commit e7f5df1

Please sign in to comment.