Skip to content

Conversation

@lucas-gregoire
Copy link
Contributor

@lucas-gregoire lucas-gregoire commented Nov 3, 2023

Description

@golevelup/nestjs is a really powerful library when using RabbitMQ with NestJS.
It's a way more advanced library compared to @nestjs/microservices for RabbitMQ integration.

Currently it's not compatible with nestjs-i18n because it uses an ExecutionContext type rmq which is not handled by nestjs-i18n.
This PR aims to use the ConsumeMessage as container for the i18nContext, to enable creating custom lang resolvers for RabbitMQ consumers:

import { ExecutionContext } from '@nestjs/common';
import { I18nResolver } from 'nestjs-i18n';

export class RabbitMqHeaderResolver implements I18nResolver {
  resolve(context: ExecutionContext): string | undefined {
    if (context.getType<string>() === 'rmq') {
      const consumeMessage = context.getArgs()[1] as ConsumeMessage;
      return message.properties.headers['x-lang'];
    }
    return undefined;
  }
}

It's not so simple to test this new feature as it requires to setup a RabbitMQ instance in tests.
But it's still possible to do something similar to this setup:

@coveralls
Copy link

Coverage Status

coverage: 90.764% (-0.2%) from 90.925%
when pulling 85c491f on lucas-gregoire:feat/rmq-context
into a861a2f on toonvanstrijp:main.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants