Is it possible to adjust the request payload in an RPC-based context, as opposed to HTTP-based contexts like this method? This question arises within the context of modifying a message payload received from Kafka in NestJS microservices setup where messages are encoded using Avro.
// code snippet depicting message decoding
const rpcContext = context.switchToRpc().getContext();
const kafkaMessageBuffer: Buffer = context.switchToRpc().getData();
const decodedMessage = await this.schemaRegistry.decode(kafkaMessageBuffer);
// Attempting to replace the buffer value with the decoded data
rpcContext.args[0]['value'] = decodedMessage;
The above approach encounters obstacles due to the fact that args
is protected and cannot be modified.
Are there alternative methods to globally decode all Kafka messages?
Possible alternatives include:
- Developing a custom pipe to transform data in the controller:
@Payload(AvroTransformPipe) message: CustomerMessage
- Manual decoding of messages in the controller before forwarding them to services