amqpx / extensions/zod / ZodValidatedBatchConsumer
Class: ZodValidatedBatchConsumer<InputMessage, AdditionalProperties, OutputMessage>
Defined in: src/extensions/zod/zod-validated-batch-consumer.ts:36
BatchConsumer wrapper that validates received messages using Zod.
Note that the schema may transform the message into different type (using e.g. refine), and this type will be reflected in generic types.
Example
import { ZodValidatedBatchConsumer } from 'amqpx/zod';
const consumer = new ZodValidatedBatchConsumer(
new TestBatchConsumer(),
z.object({
foo: z.string(),
}),
);
await consumer.listen((args) => {
for (const { message } of args.messages) {
console.log(message.foo);
}
});
consumer.on('handlingFailed', error => {
if(error instanceof ZodError) {
// do something
}
});Extends
EventEmitter<BatchConsumerEventMap>
Type Parameters
| Type Parameter | Default type |
|---|---|
InputMessage | - |
AdditionalProperties | - |
OutputMessage | InputMessage |
Implements
BatchConsumer<OutputMessage,AdditionalProperties>
Constructors
Constructor
new ZodValidatedBatchConsumer<
InputMessage,AdditionalProperties,OutputMessage>(consumer,validator):ZodValidatedBatchConsumer<InputMessage,AdditionalProperties,OutputMessage>
Defined in: src/extensions/zod/zod-validated-batch-consumer.ts:42
Creates a new instance of ZodValidatedBatchConsumer.
Parameters
| Parameter | Type | Description |
|---|---|---|
consumer | BatchConsumer<InputMessage, AdditionalProperties> | BatchConsumer to wrap, implementation will reuse downstream implementation except the message validation. |
validator | ZodType<OutputMessage, InputMessage> | Zod schema to validate each message with. May transform the message into different type. |
Returns
ZodValidatedBatchConsumer<InputMessage, AdditionalProperties, OutputMessage>
Overrides
EventEmitter<BatchConsumerEventMap>.constructor
Accessors
channel
Get Signature
get channel():
Channel
Defined in: src/extensions/zod/zod-validated-batch-consumer.ts:81
The channel used by this consumer.
Returns
Implementation of
queue
Get Signature
get queue():
Queue
Defined in: src/extensions/zod/zod-validated-batch-consumer.ts:77
The queue this consumer reads from.
Returns
Implementation of
Methods
close()
close(
timeout?):Promise<void>
Defined in: src/extensions/zod/zod-validated-batch-consumer.ts:54
Stops consuming new messages and waits for all in-flight batches to finish processing.
Parameters
| Parameter | Type | Description |
|---|---|---|
timeout? | number | Maximum time in milliseconds to wait for in-flight messages to complete. Defaults to 30000. |
Returns
Promise<void>
Implementation of
listen()
listen(
callback):Promise<ZodValidatedBatchConsumer<InputMessage,AdditionalProperties,OutputMessage>>
Defined in: src/extensions/zod/zod-validated-batch-consumer.ts:64
Receives messages from downstream implementation, validates each using Zod schema, and calls the callback with the validated messages.
Parameters
| Parameter | Type | Description |
|---|---|---|
callback | BatchConsumerCallbackFn<OutputMessage, AdditionalProperties> | Callback to call when all messages in the batch are validated. |
Returns
Promise<ZodValidatedBatchConsumer<InputMessage, AdditionalProperties, OutputMessage>>