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
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 Parameter | Default type |
|---|---|
InputMessage | - |
AdditionalProperties | - |
OutputMessage | InputMessage |
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
| Parameter | Type | Description |
|---|---|---|
consumer | Consumer<InputMessage, AdditionalProperties> | Consumer to wrap, implementation will reuse downstream implementation except the message validation. |
validator | ZodType<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
Implementation of
queue
Get Signature
get queue():
Queue
Defined in: src/extensions/zod/zod-validated-consumer.ts:74
The queue this consumer reads from.
Returns
Implementation of
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
| 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<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
| Parameter | Type | Description |
|---|---|---|
callback | ConsumerCallbackFn<OutputMessage, AdditionalProperties> | Callback to call when the message is validated. |
Returns
Promise<ZodValidatedConsumer<InputMessage, AdditionalProperties, OutputMessage>>