Kind: Class
Source: packages/microservices/record-builders/nats.record-builder.ts
Part of: Microservices
NatsRecordBuilder creates a NatsRecord for NATS-based microservice messages. Use it to attach message data and optional NATS headers before building the record passed to a NATS client.
Methods
| Method | Signature | Returns |
|---|---|---|
setHeaders | setHeaders(headers: THeaders) | this |
setData | setData(data: TData) | this |
build | build() | NatsRecord |
Diagram
mermaidgraph LR A[Application Data] --> B[NatsRecordBuilder] C[NATS Headers] --> B B -->|setData / setHeaders| D[build()] D --> E[NatsRecord] E --> F[NATS Client Message]
Usage
tsimport { NatsRecordBuilder } from '@nestjs/microservices';
import { headers } from 'nats';
const messageHeaders = headers();
messageHeaders.set('x-request-id', 'request-123');
messageHeaders.set('x-source', 'orders-service');
const record = new NatsRecordBuilder()
.setData({
orderId: 'order-456',
status: 'created',
})
.setHeaders(messageHeaders)
.build();
// Example: pass the record to a NATS client
client.emit('orders.created', record);
AI Coding Instructions
- Use
setData()to provide the payload that NATS consumers should receive. - Use
setHeaders()only with valid NATS header objects, not plain JavaScript objects. - Call
build()after configuring the builder; send the resultingNatsRecordthrough the NATS client. - Preserve correlation, tracing, and request metadata in headers when integrating with distributed services.
- Avoid mutating the payload or headers after building when the record may be reused or sent asynchronously.
Used by
1 reference from 1 file. Each is a place in this repository where the symbol is actually used — go read one rather than trusting an example.
Imported by (1)
NatsController—integration/microservices/src/nats/nats.controller.ts:19
Was this page helpful?