tooty
Version:
Simple agnostic message router for node
112 lines (88 loc) • 3.64 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var _util = require("./util");
var _util2 = _interopRequireDefault(_util);
var Builder = (function () {
function Builder() {
var options = arguments[0] === undefined ? {} : arguments[0];
_classCallCheck(this, Builder);
this.options = _util2["default"].options({ prefix: null, separator: ":" }, options);
this.routes = {};
}
_createClass(Builder, [{
key: "namespace",
value: function namespace(ns, definition) {
var nsPath = this.buildPathWithPrefix(ns);
var nsBuilder = new Builder(
// The nested builder inherits the options for this builder, and
// includes the full prefix (including the prefix segments added
// by all upper builders).
_util2["default"].merge(this.options, { prefix: nsPath }));
// Builds and resolves the set of namespaced routes:
var nsRoutes = nsBuilder.buildWithDefinition(definition).resolve();
// resolve() returns a set of properly namespaces routes that
// can be safely merged with the set present in this builder.
this.routes = _util2["default"].merge(this.routes, nsRoutes);
}
}, {
key: "route",
// Defines a single route
//
// builder.route("my:route", Handler.method);
//
value: function route(path, handler) {
var options = arguments[2] === undefined ? {} : arguments[2];
var pathString = this.pathToString(path);
var prefixedPath = this.buildPathWithPrefix(pathString);
this.routes[prefixedPath] = {
route: prefixedPath,
handler: handler,
options: options
};
}
}, {
key: "resolve",
// Returns the list of routes for this builder.
value: function resolve() {
return this.routes;
}
}, {
key: "buildPathWithPrefix",
// If one is defined, prepends a prefix to the route path and
// returns it
value: function buildPathWithPrefix(path) {
var prefix = this.options.prefix;
return prefix && [prefix, path].join(this.options.separator) || path;
}
}, {
key: "pathToString",
value: function pathToString(path) {
if (path.constructor === Array) {
return path.join(this.options.separator);
} else {
return path;
}
}
}, {
key: "buildWithDefinition",
value: function buildWithDefinition(definition) {
definition.call(undefined, this);
return this;
}
}], [{
key: "build",
value: function build(definition) {
var options = arguments[1] === undefined ? {} : arguments[1];
var builder = new Builder(options);
return builder.buildWithDefinition(definition);
}
}]);
return Builder;
})();
exports["default"] = Builder;
module.exports = exports["default"];