Using the Request type scope in a service leaves the other dependencies as undefined in nestJs

Viewed 9

Thanks in advance I have a worker application using modules in versions:

    "@commitlint/cli": "8.1.0",
    "@commitlint/config-conventional": "8.1.0",
    "@golevelup/nestjs-rabbitmq": "1.18.1",
    "@nestjs/axios": "^0.0.3",
    "@nestjs/common": "8.0.5",
    "@nestjs/core": "8.0.5",
    "@nestjs/platform-express": "8.0.0",
    "@nestjs/typeorm": "^8.0.2",
    "@types/amqplib": "^0.8.2",
    "class-transformer": "0.5.1",
    "class-validator": "0.13.2",
    "jsonschema": "1.4.0",
    "mongodb": "3.7.3",
    "newrelic": "8.6.0",
    "reflect-metadata": "^0.1.13",
    "rimraf": "3.0.2",
    "rxjs": "7.2.0"

I have a file in which is the module that consumes messages:

.consumer.ts

@Module({
  imports: [LoggerModule, TypeOrmModule.forFeature([ConsumerRepository])],
  providers: [ConsumerCreateEventService, Consumer],
  exports: [TypeOrmModule],
})
export class ConsumerModule {}

when trying to use the logger module on the consumer:

consumer.ts

export class Consumer {
  constructor(
    private readonly logger: Logger,
    private readonly createEventService: CreateEventService,
  ) {}

  @RabbitSubscribe({
    exchange: Exchange,
    routingKey: [RoutingKey, RoutingKeyForRetry],
    queue: Queue,
    errorHandler: processRetry({
      routingKey: RoutingKeyForRetry,
      expiration: config.rabbit.retry.expiration,
      maxRetry: config.rabbit.retry.maxRetry,
      exchangeDlx: DlxExchange,
      exchangeRetry: RetryExchange,
    }),
  })
  @Dto(TimelineDto)
  @UseInterceptors(MessageInterceptor)
  public async process(msg: TimelineDto, amqpMsg: ConsumeMessage) {
    this.logger.log({
      message: 'Consumer',
      params: {
        msg,
      },
    });
  }
}

the injections show up as undefined if I unlog the injections the service injection works again and also if I change the scope of the logger to scope.Default it works again, but with scope.Default I can't differentiate an id for each message received Can anyone help?

@Injectable({ scope: Scope.REQUEST })
export class Logger {
  private logger: NodeLogger;

  private obfuscator;

  private loggerHelper: LoggerHelper;

  constructor() {
    const reqId = uuid.v4();

    this.loggerHelper = new LoggerHelper();
    this.obfuscator = this.getObfuscator();
    this.logger = logger(reqId);
  }

  private getObfuscator() {
    const opts = { maskAllContent: true };

    return filter.objectFilter(this.loggerHelper.getObfuscateFields(), opts);
  }

  private getRequestId(): string {
    const reqId = this.req.header('request_id') || uuid.v4();

    this.req.res.setHeader('request_id', reqId);
    this.req.headers.request_id = reqId;

    return reqId;
  }

  debug(...msg: string[]): void {
    this.logger.debug(...msg);
  }

  error(...msg: string[]): void {
    this.logger.error(...msg);
  }

  warn(...msg: string[]): void {
    this.logger.warn(...msg);
  }

  info(...msg: string[]): void {
    this.logger.info(...msg);
  }

  log(metadata: Record<string, unknown>, ...msg: string[]): void {
    this.logger.log(this.obfuscator(metadata), ...msg);
  }
}

`

0 Answers
Related