mediatr-ts
Version:
Mimic the famous MediatR csharp library see: (https://github.com/jbogard/MediatR)
55 lines • 2.72 kB
JavaScript
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
import { byOrder, OrderedMappings } from "./orderedMappings.js";
/**
* Mapping for request handlers
*
* @exports
*/
var RequestHandlerMappings = /** @class */ (function (_super) {
__extends(RequestHandlerMappings, _super);
function RequestHandlerMappings() {
return _super !== null && _super.apply(this, arguments) || this;
}
/**
* Gets all mappings for a specific request if provided, otherwise returns all (sorted) mappings
*
* @throws Error if no mappings are found for specified argument
* @param requestClass The request class to get mappings for
* @returns The array of handler classes in the order in which they should be executed.
*/
RequestHandlerMappings.prototype.getAll = function (requestClass) {
var items = this._mappings.filter(function (p) { return p.requestClass === requestClass || !requestClass; });
if (items.length === 0 && requestClass) {
throw new Error("No handler found for request ".concat(requestClass.name, ". Remember to decorate your handler with @requestHandler(").concat(requestClass.name, ")."));
}
return items.sort(byOrder);
};
/**
* Throws an error if the request handler mapping already exists
* @param requestClass The request class to check
* @throws Error if the mapping already exists
*/
RequestHandlerMappings.prototype.throwIfExistingTypeMappings = function (requestClass) {
var existingTypeMappings = this.getAll().filter(function (x) { return x.requestClass === requestClass; });
if (existingTypeMappings.length > 0) {
throw new Error("Request handler for ".concat(requestClass.name, " has been defined twice. Make sure you only have one @requestHandler decorator for each request type."));
}
};
return RequestHandlerMappings;
}(OrderedMappings));
export { RequestHandlerMappings };
//# sourceMappingURL=requestHandlerMappings.js.map