import { z } from 'zod'; import { Connection, type AsyncMessage, type ConnectionOptions, type Publisher } from 'rabbitmq-client'; import { Logger } from '@nestjs/common'; import type { Jsonifiable } from 'type-fest'; import { RabbitmqExchange } from './rabbitmq-exchange'; import { RabbitmqQueue } from './rabbitmq-queue'; import { RabbitmqSubscriber } from './rabbitmq-subscriber'; export type RabbitmqConstructorParams = ConnectionOptions & {}; export type RabbitmqResponse = 'ack' | 'drop' | 'requeue'; export type RabbitmqQueueStats = { name: string; messages: number; consumers: number; }; export type RabbitmqMessageValidation = { schema: z.ZodTypeAny; onFail?: RabbitmqResponse; }; export type RabbitmqSubscribeParams = { id?: string; queue: RabbitmqQueue; requeue?: boolean; concurrency?: number; /** The client can request that messages be sent in advance so that when * the client finishes processing a message, the following message is * already held locally, rather than needing to be sent down the channel. * Prefetching gives a performance improvement. This field specifies the * prefetch window size in octets. The server will send a message in * advance if it is equal to or smaller in size than the available prefetch * size (and also falls into other prefetch limits). May be set to zero, * meaning "no specific limit", although other prefetch limits may still * apply. The prefetch-size is ignored if the no-ack option is set. */ prefetchSize?: number; /** Specifies a prefetch window in terms of whole messages. This field may * be used in combination with the prefetch-size field; a message will only * be sent in advance if both prefetch windows (and those at the channel * and connection level) allow it. The prefetch-count is ignored if the * no-ack option is set. */ prefetchCount?: number; validation?: RabbitmqMessageValidation; autoStart?: boolean; }; export type RabbitmqSubscriberFunction = { instance: any; methodName: string; } | ((data: any, message: AsyncMessage) => RabbitmqResponse | Promise); export type RabbitmqPublishOptions = { exchange: RabbitmqExchange; routingKey: string; ttlMs?: number; durable?: boolean; } & ({ type: 'raw'; message: string | Buffer; } | { type: 'json'; message: Jsonifiable; }); export declare class Rabbitmq { protected readonly logger: Logger; protected readonly connection: Connection; protected readonly exchanges: Map; protected readonly queues: Map; protected readonly subscribers: Map; protected publisher: Publisher | null; constructor(connection: Connection); constructor(options: RabbitmqConstructorParams); /** * @param {RabbitmqExchange} exchange * @returns {Promise} */ declareExchange(exchange: RabbitmqExchange): Promise; /** * @param {RabbitmqQueue} queue * @returns {Promise} */ declareQueue(queue: RabbitmqQueue): Promise; /** * @param {RabbitmqQueue} queue * @returns {Promise} * @protected */ protected setupQueueBindings(queue: RabbitmqQueue): Promise; /** * @param {RabbitmqQueue | string} queue * @return {Promise} */ queueStats(queue: RabbitmqQueue | string): Promise; /** * @param {RabbitmqQueue} queue * @return {Promise} */ purgeQueue(queue: RabbitmqQueue): Promise; /** * @param {RabbitmqSubscribeParams} params * @param {RabbitmqSubscriberFunction} subscriber * @return {Promise} */ subscribe(params: RabbitmqSubscribeParams, subscriber: RabbitmqSubscriberFunction): Promise; /** * @param {string} id * @return {RabbitmqSubscriber | null} */ getSubscriberById(id: string): RabbitmqSubscriber | null; /** * @param {string} id * @returns {void} */ startSubscriber(id: string): void; /** * @param {string} id * @return {Promise} */ stopSubscriber(id: string): Promise; /** * @returns {void} */ startPendingSubscribers(): void; /** * @param {RabbitmqPublishOptions} options * @return {Promise} */ publish(options: RabbitmqPublishOptions): Promise; /** * @return {Promise} */ close(): Promise; }