Skip to content

amqpx v1.0.1


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 ​

ts
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 ParameterDefault type
InputMessage-
AdditionalProperties-
OutputMessageInputMessage

Implements ​

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 ​

ParameterTypeDescription
consumerBatchConsumer<InputMessage, AdditionalProperties>BatchConsumer to wrap, implementation will reuse downstream implementation except the message validation.
validatorZodType<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 ​

Channel

Implementation of ​

BatchConsumer.channel


queue ​

Get Signature ​

get queue(): Queue

Defined in: src/extensions/zod/zod-validated-batch-consumer.ts:77

The queue this consumer reads from.

Returns ​

Queue

Implementation of ​

BatchConsumer.queue

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 ​

ParameterTypeDescription
timeout?numberMaximum time in milliseconds to wait for in-flight messages to complete. Defaults to 30000.

Returns ​

Promise<void>

Implementation of ​

BatchConsumer.close


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 ​

ParameterTypeDescription
callbackBatchConsumerCallbackFn<OutputMessage, AdditionalProperties>Callback to call when all messages in the batch are validated.

Returns ​

Promise<ZodValidatedBatchConsumer<InputMessage, AdditionalProperties, OutputMessage>>

Implementation of ​

BatchConsumer.listen

Released under the MIT License.