Skip to content

amqpx v1.0.1


amqpx / extensions/zod / ZodValidatedConsumer

Class: ZodValidatedConsumer<InputMessage, AdditionalProperties, OutputMessage> ​

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

Consumer 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 { ZodValidatedConsumer } from 'amqpx/zod';

const consumer = new ZodValidatedConsumer(
    new TestConsumer(),
    z.object({
        foo: z.string(),
    }),
);

await consumer.listen((args) => {
    console.log(args.message.foo);
});

consumer.on('handlingFailed', error => {
    if(error instanceof ZodError) {
        // do something
    }
});

Extends ​

  • EventEmitter<ConsumerEventMap>

Type Parameters ​

Type ParameterDefault type
InputMessage-
AdditionalProperties-
OutputMessageInputMessage

Implements ​

  • Consumer<OutputMessage, AdditionalProperties>

Constructors ​

Constructor ​

new ZodValidatedConsumer<InputMessage, AdditionalProperties, OutputMessage>(consumer, validator): ZodValidatedConsumer<InputMessage, AdditionalProperties, OutputMessage>

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

Creates a new instance of ZodValidatedConsumer.

Parameters ​

ParameterTypeDescription
consumerConsumer<InputMessage, AdditionalProperties>Consumer to wrap, implementation will reuse downstream implementation except the message validation.
validatorZodType<OutputMessage, InputMessage>Zod schema to validate the message with. May transform the message into different type.

Returns ​

ZodValidatedConsumer<InputMessage, AdditionalProperties, OutputMessage>

Overrides ​

EventEmitter<ConsumerEventMap>.constructor

Accessors ​

channel ​

Get Signature ​

get channel(): Channel

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

The channel used by this consumer.

Returns ​

Channel

Implementation of ​

Consumer.channel


queue ​

Get Signature ​

get queue(): Queue

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

The queue this consumer reads from.

Returns ​

Queue

Implementation of ​

Consumer.queue

Methods ​

close() ​

close(timeout?): Promise<void>

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

Stops consuming new messages and waits for all in-flight message handlers to finish.

Parameters ​

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

Returns ​

Promise<void>

Implementation of ​

Consumer.close


listen() ​

listen(callback): Promise<ZodValidatedConsumer<InputMessage, AdditionalProperties, OutputMessage>>

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

Receives the message from downstream implementation, validates it using Zod schema, and calls the callback with the validated message.

Parameters ​

ParameterTypeDescription
callbackConsumerCallbackFn<OutputMessage, AdditionalProperties>Callback to call when the message is validated.

Returns ​

Promise<ZodValidatedConsumer<InputMessage, AdditionalProperties, OutputMessage>>

Implementation of ​

Consumer.listen

Released under the MIT License.