UNPKG

@hexadrop/query

Version:

Hexagonal architecture utils library

62 lines (59 loc) 3.27 kB
import { QueryBusCallback, QueryHandler } from './bus.js'; import Query, { QueryClass } from './query.js'; import QueryHandlers from './query-handlers.js'; import '@hexadrop/either'; import '@hexadrop/error'; import '@hexadrop/types/class'; import '@hexadrop/types/primitives'; /** * InMemoryQueryHandlers is a class that extends QueryHandlers. * It is used to handle queries in memory. * * @property {Map<string, QueryBusCallback>} queryHandlersMap - A map that stores the query handlers. */ declare class InMemoryQueryHandlers extends QueryHandlers { private readonly queryHandlersMap; /** * Constructs a new instance of the InMemoryQueryHandlers class. */ constructor(); /** * @static * @method merge * @description Merges multiple InMemoryQueryHandlers instances into one. * @param {...InMemoryCommandHandlers[]} handlers - The instances to merge. * @returns {InMemoryCommandHandlers} The merged InMemoryQueryHandlers instance. */ static merge(...handlers: InMemoryQueryHandlers[]): InMemoryQueryHandlers; /** * This method is used to register a query handler. * It stores the handler in the queryHandlersMap with the query's QUERY_NAME as the key. * * @param {QueryClass<ResponseType, QueryType>} query - The QueryClass to be registered. * @param {QueryBusCallback<ResponseType, QueryType> | QueryHandler<ResponseType, QueryType>} handlerOrCallback - The handler or callback to be registered. * @template ResponseType - The type of the response that the query handler should return. * @template QueryType - The type of the Query that extends Query<ResponseType>. */ register<ResponseType, QueryType extends Query<ResponseType>>(query: QueryClass<ResponseType, QueryType>, handlerOrCallback: QueryBusCallback<ResponseType, QueryType> | QueryHandler<ResponseType, QueryType>): void; /** * This method is used to search for a query handler. * It retrieves the handler from the queryHandlersMap using the query's QUERY_NAME or queryName as the key. * If no handler is found, it throws a QueryNotRegisteredError. * * @param {QueryType | QueryClass<ResponseType, QueryType>} query - The Query or QueryClass to be searched for. * @returns {QueryBusCallback<ResponseType, QueryType>} - The found query handler. * @throws {InvalidQueryError} - If the query does not have a QUERY_NAME or queryName. * @throws {QueryNotRegisteredError} - If no handler is found for the query. * @template ResponseType - The type of the response that the query handler should return. * @template QueryType - The type of the Query that extends Query<ResponseType>. */ search<ResponseType, QueryType extends Query<ResponseType>>(query: QueryClass<ResponseType, QueryType> | QueryType): QueryBusCallback<ResponseType, QueryType>; /** * This method is used to unregister a query handler. * It removes the handler from the queryHandlersMap using the query's QUERY_NAME as the key. * * @param {QueryClass<unknown, Query<unknown>>} query - The QueryClass to be unregistered. */ unregister(query: QueryClass<unknown, Query<unknown>>): void; } export { InMemoryQueryHandlers as default };