toonvanstrijp / nestjs-i18n

The i18n module for nestjs.
https://nestjs-i18n.com
Other
625 stars 102 forks source link

feat: add compatibility with @golevelup/rabbitmq #595

Closed lucas-gregoire closed 8 months ago

lucas-gregoire commented 8 months ago

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 commented 8 months ago

Coverage Status

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