Kind: Class
Source: packages/microservices/serializers/rmq-record.serializer.ts
Part of: Microservices
RmqRecordSerializer converts outgoing NestJS microservice packets into RabbitMQ-compatible record payloads. It preserves the request packet shape while extracting or merging RabbitMQ-specific metadata, such as headers and message properties, for transport delivery.
Implements: Serializer
Methods
| Method | Signature | Returns |
|---|---|---|
serialize | serialize(packet: ReadPacket) | ReadPacket & Partial<RmqRecord> |
Diagram
mermaidgraph LR A[Application Message] --> B[RmqRecordSerializer] B --> C[ReadPacket] B --> D[Partial RmqRecord] C --> E[RabbitMQ Transport] D --> E
Usage
tsimport { RmqRecordSerializer } from '@nestjs/microservices';
const serializer = new RmqRecordSerializer();
const packet = serializer.serialize({
pattern: 'orders.created',
data: {
orderId: 'order_123',
customerId: 'customer_456',
},
});
client.send('orders.created', packet.data);
AI Coding Instructions
- Use
RmqRecordSerializerat the RabbitMQ transport boundary when outgoing packets may include RabbitMQ record metadata. - Preserve the standard NestJS
ReadPacketfields, especiallypatternanddata, when extending serialization behavior. - Ensure RabbitMQ-specific options, such as headers or properties, are carried through without mutating the original payload.
- Keep custom serializers compatible with the
serialize(): ReadPacket & Partial<RmqRecord>return contract. - Test serialization with both plain message payloads and
RmqRecord-style payloads containing transport metadata.
How it works
-
RmqRecordSerializeris an exported serializer class implementingSerializer<ReadPacket, ReadPacket & Partial<RmqRecord>>. Itsserializemethod accepts a packet withpatternanddatafields. rmq-record.serializer.ts:6-10 packet.interface.ts:5-8 serializer.interface.ts:10-12 -
It recognizes a packet only when
packet.datais truthy, is a non-null JavaScript object, and is an actualRmqRecordinstance according toinstanceof. rmq-record.serializer.ts:11-15isObjectchecks non-null values whosetypeofisobject. -
For such a packet,
serializereturns a new packet object: it copies the packet’s existing enumerable properties, replacesdatawithRmqRecord.data, and writesRmqRecord.optionsto a top-leveloptionsfield. rmq-record.serializer.ts:16-21RmqRecordstores its payload in readonlydataand can store optionaloptions. rmq.record-builder.ts:25-29 -
If the payload does not meet that runtime check—including plain objects that merely resemble a record—the method returns the original packet reference unchanged. rmq-record.serializer.ts:22-23 The serializer test checks reference identity for a non-
RmqRecordpayload. rmq-record.serializer.spec.ts:31-37 -
The method has no explicit error handling or thrown validation error in its implementation. rmq-record.serializer.ts:10-24 It does not modify the input itself when it unwraps an
RmqRecord; that branch constructs and returns an object literal. rmq-record.serializer.ts:16-21 -
ClientRMQandServerRMQselect this class as their serializer when transport options do not specify a custom serializer. client-rmq.ts:488-490 server-rmq.ts:424-426 -
In the RMQ client send and event paths, the serialized packet’s top-level
optionsis read and then deleted before the remaining packet is JSON-encoded; those options are spread into RabbitMQ send options, with headers merged separately. client-rmq.ts:393-412 client-rmq.ts:446-484 Therefore, when this serializer returns the input unchanged, that later deletion operates on the caller’s packet object. rmq-record.serializer.ts:22-23 client-rmq.ts:447-451
Relationships
- IMPORTS →
isObject
Was this page helpful?