AlariCode / nestjs-rmq

A custom library for NestJS microservice. It allows you to use RabbitMQ or AMQP.
https://purpleschool.ru
MIT License
285 stars 38 forks source link

Post and preprocessors feature #31

Closed milovidov983 closed 3 years ago

milovidov983 commented 3 years ago

I propose to implement the following post and pre handlers in the library in which the library user would be able to handle calls at his discretion:

public enum MessageProcessResult {
    Ask,
    Reject,
    Nack,
}

/**
 * @summary
 * Raw payload from message broker
 */
interface IDeliveredMessage {}

/**
 * @summary
 * Common post-pre handlers lib configuration
 */
interface IQueueHandlersConfig {
    /**
     * @summary
     * The handler is started last in the chain, and you can change the result of the previous MessageProcessResult in it
     */
    afterExecute: (
        handler: (dm: IDeliveredMessage, result: MessageProcessResult) => Promise<MessageProcessResult>
    ) => void;
    /**
     * @summary
     * The handler is started first in the call chain.
     * Depending on the needs of the library user, the message can either be passed down the chain of handlers
     * `return true`, or the message `return false` can be ignored
     */
    beforeExecute: (handler: (dm: IDeliveredMessage) => Promise<boolean>) => void;
    /**
     * @summary
     * The handler is called in case of an exception in the client code
     *
     */
    onException: (handler: (error: Error, dm: IDeliveredMessage) => Promise<boolean>) => void;
    /**
     * @summary
     * Called if a message has arrived for which a handler is not assigned
     */
    onUnexpectedTopic: (handelr: (dm: IDeliveredMessage) => Promise<MessageProcessResult>) => void;
}

In general, I really need onException.

Let's discuss it, and I could implement this improvement myself, if possible, once we approve the interface and other details.

AlariCode commented 3 years ago

This functionality is already exists inside library. You can use: