Skip to content

Commit

Permalink
omnirouter with complete dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
nicosommi committed Jun 26, 2015
1 parent 4dd0c84 commit e43799c
Show file tree
Hide file tree
Showing 8 changed files with 1,391 additions and 26 deletions.
42 changes: 42 additions & 0 deletions es5/lib/http.statuses.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
[
{
"name": "ok",
"code": 200
},
{
"name": "created",
"code": 201
},
{
"name": "noContent",
"code": 204
},
{
"name": "badRequest",
"code": 400
},
{
"name": "unauthorized",
"code": 401
},
{
"name": "forbidden",
"code": 403
},
{
"name": "notFound",
"code": 404
},
{
"name": "conflict",
"code": 409
},
{
"name": "internalServerError",
"code": 500
},
{
"name": "notImplemented",
"code": 501
}
]
98 changes: 98 additions & 0 deletions es5/lib/response.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
"use strict";

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 _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var loadDynamicMethods = Symbol();

var Response = (function () {
function Response(expressResponse, middlewares) {
_classCallCheck(this, Response);

Object.defineProperties(this, {
"_response": {
enumerable: false,
value: expressResponse
},
"_middlewares": {
enumerable: false,
value: middlewares
}
});

this[loadDynamicMethods]();
}

_createClass(Response, [{
key: loadDynamicMethods,
value: function value() {
var _this = this;

var statuses = require("./http.statuses.json");
if (Array.isArray(statuses)) {
statuses.forEach(function (status) {
_this[status.name] = function (data) {
//call hook for data format middleware in a pipeline
_this._middlewares.forEach(function (middleware) {
if (middleware.formatResponse && typeof middleware.formatResponse === "function") {
middleware.formatResponse(_this);
}

if (middleware.format && typeof middleware.format === "function") {
data = middleware.format(data);
}
});

_this.status(status.code).send(data);
};
});
}
}
}, {
key: "end",
value: function end(message) {
this._response.end(message);
}
}, {
key: "status",
value: function status(code) {
this._response.status(code);
return this;
}
}, {
key: "json",
value: function json(data) {
this._response.json(data);
}
}, {
key: "send",
value: function send(data) {
this._response.send(data);
}
}, {
key: "download",
value: function download(data) {
this._response.download(data);
}
}, {
key: "set",
value: function set(key, value) {
this._response.set(key, value);
}
}, {
key: "get",
value: function get(key) {
return this._response.get(key);
}
}]);

return Response;
})();

exports["default"] = Response;
module.exports = exports["default"];
167 changes: 167 additions & 0 deletions es5/lib/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
"use strict";

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 _responseJs = require("./response.js");

var _responseJs2 = _interopRequireDefault(_responseJs);

var express = require("express");
var bodyParser = require("body-parser");

var _createRequest = Symbol(),
_createResponse = Symbol();

var Router = (function () {
function Router() {
_classCallCheck(this, Router);

for (var _len = arguments.length, routerOptions = Array(_len), _key = 0; _key < _len; _key++) {
routerOptions[_key] = arguments[_key];
}

Object.defineProperties(this, {
"_express": {
enumerable: false,
value: express()
},
"_options": {
enumerable: false,
value: routerOptions
},
"_server": {
writable: true,
enumerable: false,
value: undefined
},
"_middlewares": {
enumerable: false,
value: []
}
});

this._express.disable("x-powered-by");
//TYPE is not working by somehow, despites the website says it does
//https://github.com/expressjs/body-parser
this._express.use(bodyParser.json({ type: "application/vnd.api+json" }));

this.initialize.apply(this, routerOptions);
}

_createClass(Router, [{
key: "initialize",
value: function initialize() {}
}, {
key: "listen",
// Stubbed

value: function listen(portNumber, callback) {
this._server = this._express.listen(portNumber, callback);
}
}, {
key: "close",
value: function close(callback) {
this._server.close(callback);
}
}, {
key: _createRequest,
value: function value(expressRequest) {
return new Request(expressRequest);
}
}, {
key: _createResponse,
value: function value(expressResponse) {
//propagates the middleware to response formatters
return new _responseJs2["default"](expressResponse, this._middlewares);
}
}, {
key: "get",
value: function get(path, callback) {
var _this = this;

this._express.get(path, function (expressRequest, expressResponse) {
callback(_this[_createRequest](expressRequest), _this[_createResponse](expressResponse));
});
}
}, {
key: "post",
value: function post(path, callback) {
var _this2 = this;

this._express.post(path, function (expressRequest, expressResponse) {
callback(_this2[_createRequest](expressRequest), _this2[_createResponse](expressResponse));
});
}
}, {
key: "put",
value: function put(path, callback) {
var _this3 = this;

this._express.put(path, function (expressRequest, expressResponse) {
callback(_this3[_createRequest](expressRequest), _this3[_createResponse](expressResponse));
});
}
}, {
key: "delete",
value: function _delete(path, callback) {
var _this4 = this;

this._express["delete"](path, function (expressRequest, expressResponse) {
callback(_this4[_createRequest](expressRequest), _this4[_createResponse](expressResponse));
});
}
}, {
key: "use",
value: function use(middlewareClass) {
this._middlewares.push(Object.create(middlewareClass.prototype));
}
}]);

return Router;
})();

exports["default"] = Router;

var Request = (function () {
function Request(expressRequest) {
_classCallCheck(this, Request);

Object.defineProperties(this, {
"_request": {
enumerable: false,
value: expressRequest
},
"body": {
enumerable: true,
value: expressRequest.body
},
"params": {
enumerable: true,
value: expressRequest.params
}
});

if (typeof this.body === "string") {
throw Error("express JSON parsing middleware appears to be missing");
}
}

_createClass(Request, [{
key: "header",
value: function header(headerName) {
return this._request.get(headerName);
}
}]);

return Request;
})();

exports.Request = Request;
Loading

0 comments on commit e43799c

Please sign in to comment.