Skip to content

Commit

Permalink
Merge a201a29 into 64ac2f0
Browse files Browse the repository at this point in the history
  • Loading branch information
abhinav-maheshwari committed Dec 12, 2016
2 parents 64ac2f0 + a201a29 commit de1c7ed
Showing 1 changed file with 19 additions and 18 deletions.
37 changes: 19 additions & 18 deletions index.js 100644 → 100755
Expand Up @@ -10,14 +10,12 @@ var internals = {};
var routerSchema = Joi.object().keys({
path: Joi.string().required(),
method: Joi.string().required(),
handler: Joi.func().arity(2).required()
handler: Joi.func().required()
});

var eventSchema = Joi.object().keys({
context: Joi.object().keys({
'resource-path': Joi.string().required(),
'http-method': Joi.string().required(),
}).unknown().required()
resource: Joi.string().required(),
httpMethod: Joi.string().required(),
}).unknown().required();

module.exports = exports = internals.Router = function(options) {
Expand All @@ -27,19 +25,22 @@ module.exports = exports = internals.Router = function(options) {
};


internals.Router.prototype.register = function(route) {
var routes = this.routes;
internals.Router.prototype.register = function(more_routes) {
for (var i=0; i < more_routes.length; i++) {
var route = more_routes[i];
var routes = this.routes;

Hoek.assert(route, 'route is required');
var result = Joi.validate(route, routerSchema);
if (result.error) {
throw (result.error);
}
Hoek.assert(route, 'route is required');
var result = Joi.validate(route, routerSchema);
if (result.error) {
throw (result.error);
}

if (!routes[route.path]) {
routes[route.path] = {};
if (!routes[route.path]) {
routes[route.path] = {};
}
routes[route.path][route.method.toUpperCase()] = route;
}
routes[route.path][route.method.toUpperCase()] = route;
};

internals.Router.prototype.route = function(event, context) {
Expand All @@ -55,7 +56,7 @@ internals.Router.prototype.route = function(event, context) {
return reject(error);
}

if (event.context && event.context['resource-path']) {
if (event && event.resource) {
let route = this._getRoute(event, context);
if (route) {
return route.handler(event, context)
Expand Down Expand Up @@ -99,9 +100,9 @@ internals.Router.prototype._validationError = function(error) {
}

internals.Router.prototype._getRoute = function(event, context) {
var entry = this.routes[event.context['resource-path']];
var entry = this.routes[event.resource];
if (entry) {
return entry[event.context['http-method'].toUpperCase()];
return entry[event.httpMethod.toUpperCase()];
} else {
return null;
}
Expand Down

0 comments on commit de1c7ed

Please sign in to comment.