UNPKG

mediatr-ts

Version:

Mimic the famous MediatR csharp library see: (https://github.com/jbogard/MediatR)

83 lines (82 loc) 2.55 kB
import type Notification from "../models/notificationData.js"; import type RequestData from "../models/requestData.js"; import Resolver from "../interfaces/resolver.js"; import { typeMappings } from "../models/mappings/index.js"; type Settings = { resolver: Resolver; }; /** * The mediator class. * Sends request and publishes events. * * @export * @class Mediator * @implements {Mediator} */ export default class Mediator { private readonly _resolver; /** * Gets the notifications. */ get notifications(): OrderedNotificationsMapping; /** * Gets the pipeline behaviors. */ get pipelineBehaviors(): OrderedPipelineBehaviorsMapping; /** * The constructor. * If custom settings are provided, it uses the resolver from those settings; * otherwise, it defaults to a new InstantiationResolver * * @param settings The custom settings */ constructor(settings?: Settings); /** * This method registers types with a resolver. * If custom settings are provided, it uses the resolver from those settings; * otherwise, it defaults to a new InstantiationResolver where add method is not used * * @param resolver The resolver */ private registerTypesInResolver; /** * Send a request to the mediator. * * @template T * @param {RequestData<T>} request The request to send * @returns {Promise<T>} * @memberof Mediator */ send<TResult>(request: RequestData<TResult>): Promise<TResult>; /** * Wraps the given function in a chain of pipeline behavior calls. * * @param func The function to wrap * @param request The request to send * @returns The result of the action * @memberof Mediator * @private * */ private wrapInPipelineBehaviorChainCalls; /** * Get the handler for the given request * * @param {RequestData<TResult>} request The request * @returns {RequestHandler<RequestData<TResult>, TResult>} The handler * @private * @memberof Mediator */ private getRequiredHandlerForRequest; /** * Publish a new message * * @param {Notification} message The message to publish * @returns {Promise<void>} * @memberof Mediator */ publish(message: Notification): Promise<void>; } type OrderedNotificationsMapping = Pick<typeof typeMappings.notifications, "setOrder">; type OrderedPipelineBehaviorsMapping = Pick<typeof typeMappings.pipelineBehaviors, "setOrder">; export {};