demeine
Version:
DDDD - Distributed Domain Driven Design
45 lines (44 loc) • 1.54 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "DefaultCommandHandler", {
enumerable: true,
get: function() {
return DefaultCommandHandler;
}
});
var _stringUtils = require("../utils/stringUtils");
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
}
var DefaultCommandHandler = /*#__PURE__*/ function() {
"use strict";
function DefaultCommandHandler() {
_classCallCheck(this, DefaultCommandHandler);
}
var _proto = DefaultCommandHandler.prototype;
_proto.handle = function handle(aggregate, command) {
var type = command.type;
var key = this._extractKey(type);
var funcName = "process".concat((0, _stringUtils.capitalize)(key));
var applier = aggregate[funcName];
if (applier) {
return applier.bind(aggregate)(command);
} else {
throw new Error("Unable to process command: ".concat(type, " looking for: ").concat(funcName));
}
};
_proto._extractKey = function _extractKey(type) {
var parts = type.split(".");
var filteredParts = [];
for(var i = 1; i < parts.length - 1; i++){
filteredParts.push(parts[i]);
}
filteredParts.unshift(filteredParts.pop());
return (0, _stringUtils.camelCase)(filteredParts.join("_"));
};
return DefaultCommandHandler;
}();