Skip to content

Commit

Permalink
feat: ability to add a logger token
Browse files Browse the repository at this point in the history
  • Loading branch information
vbshnsk committed Sep 16, 2022
1 parent 6364ff8 commit d107b23
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
30 changes: 30 additions & 0 deletions src/pubsub-transport.module.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { PubSub } from '@google-cloud/pubsub';
import { Type } from '@nestjs/common';
import { Test } from '@nestjs/testing';
import { WinstonLogger } from 'nest-winston';

import { PubSubTransport, PubSubTransportConfig, PubsubTransportModule } from '..';

Expand Down Expand Up @@ -41,6 +43,34 @@ describe('PubsubTransportModule', () => {
expect(app.get(PubSubTransport)).toBeDefined();
});

it('bootstraps module with a custom logger', async () => {
const mockLogger = {
log: () => ({}),
};
const MockLoggerCtr = class {
constructor() {
return mockLogger;
}
} as never as Type<WinstonLogger>;

const app = await Test.createTestingModule({
imports: [
PubsubTransportModule.forRootAsync({
logger: MockLoggerCtr,
useFactory: () => ({
pubsub: config.pubsub,
topic: 'topic',
subscription: 'sub',
}),
}),
],
}).compile();

const transport = app.get(PubSubTransport);
expect(transport).toBeDefined();
expect((transport as never as Record<string, unknown>)['log']).toBe(mockLogger);
});

it('bootstraps two modules using symbol token (async)', async () => {
const serviceA = Symbol('ServiceA');
const serviceB = Symbol('ServiceB');
Expand Down
7 changes: 5 additions & 2 deletions src/pubsub-transport.module.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { DynamicModule, Module, ModuleMetadata, Type } from '@nestjs/common';
import { WinstonLogger } from 'nest-winston';

import { PubSubTransport, PubSubTransportConfig } from './pubsub-transport';
import { LoggerToken, PubSubTransport, PubSubTransportConfig } from './pubsub-transport';

export interface PubSubTransportAsyncOptions extends Pick<ModuleMetadata, 'imports'> {
useExisting?: Type<PubSubTransportConfig>;
useClass?: Type<PubSubTransportConfig>;
useFactory?: (...args: any[]) => Promise<PubSubTransportConfig> | PubSubTransportConfig;
inject?: any[];
token?: symbol;
logger?: Type<WinstonLogger>;
}

@Module({})
Expand Down Expand Up @@ -62,7 +64,7 @@ export class PubsubTransportModule {
*/

static forRootAsync(options: PubSubTransportAsyncOptions): DynamicModule {
const { imports = [], useClass, useFactory, useExisting, inject, token } = options;
const { imports = [], useClass, useFactory, useExisting, inject, token, logger } = options;
return {
module: PubsubTransportModule,
global: true,
Expand All @@ -75,6 +77,7 @@ export class PubsubTransportModule {
useExisting,
provide: PubSubTransportConfig,
},
{ provide: LoggerToken, useClass: logger || WinstonLogger },
token ? { useClass: PubSubTransport, provide: token } : PubSubTransport,
],
exports: [token || PubSubTransport],
Expand Down
7 changes: 2 additions & 5 deletions src/pubsub-transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { Message, PubSub, Subscription, Topic } from '@google-cloud/pubsub';
import { Inject, Injectable, Optional } from '@nestjs/common';
import { CustomTransportStrategy, Server } from '@nestjs/microservices';
import errorToJSON from 'error-to-json';
import { WINSTON_MODULE_PROVIDER } from 'nest-winston';
import { firstValueFrom } from 'rxjs';
import { Logger } from 'winston';

export const LoggerToken = Symbol('Logger');
export const PubSubTransportConfig = Symbol('PubSubTransportConfig');
export type PubSubTransportConfig =
| {
Expand All @@ -17,7 +17,6 @@ export type PubSubTransportConfig =
getPattern?: (msg: Message) => string;
deserializeMessage?: (msg: Message) => any;
ackIfNoHandler?: boolean;
logger?: Logger;
}
| {
pubsub?: undefined;
Expand All @@ -28,7 +27,6 @@ export type PubSubTransportConfig =
getPattern?: (msg: Message) => string;
deserializeMessage?: (msg: Message) => any;
ackIfNoHandler?: boolean;
logger?: Logger;
}
| {
pubsub: PubSub;
Expand All @@ -39,7 +37,6 @@ export type PubSubTransportConfig =
getPattern?: (msg: Message) => string;
deserializeMessage?: (msg: Message) => any;
ackIfNoHandler?: boolean;
logger?: Logger;
};

@Injectable()
Expand All @@ -65,7 +62,7 @@ export class PubSubTransport extends Server implements CustomTransportStrategy {
ackIfNoHandler,
}: PubSubTransportConfig,
@Optional()
@Inject(WINSTON_MODULE_PROVIDER)
@Inject(LoggerToken)
private log?: Logger
) {
super();
Expand Down

0 comments on commit d107b23

Please sign in to comment.