UNPKG

express-lambda

Version:

Make AWS lambda behave like an express app

59 lines (50 loc) 2.06 kB
"use strict"; 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 _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var Lambda = require("./lambda"); var Node = function () { function Node(parent, fullpath, path) { var _this = this; _classCallCheck(this, Node); this.parent = parent; this.fullpath = fullpath; this.path = path; this.children = {}; this.methods = []; this.lambda = null; ["get", "put", "post", "delete", "head", "patch", "options"].forEach(function (verb) { _this[verb] = _this.add.bind(_this, verb.toUpperCase()); }); } _createClass(Node, [{ key: "add", value: function add(method, path, lambda) { var node = this.use(path); node.methods.push(method); if (lambda) { node.lambda = lambda instanceof Lambda ? lambda : new Lambda({ src: lambda }); } } }, { key: "use", value: function use(path, cb) { path = path.replace(/^\//, ""); var segments = path.split("/"), part = segments.shift(), node = this, fullpath = this.fullpath; while (part) { fullpath = fullpath + "/" + part; if (!node.children[part]) { node.children[part] = this.make(node, fullpath, part); } node = node.children[part]; part = segments.shift(); } cb && cb(node); return node; } }]); return Node; }(); module.exports = Node;