Skip to content

Commit

Permalink
Don't require the callback to the main escort function. Don't use a s…
Browse files Browse the repository at this point in the history
…eparate methods variable for passing into the callback, instead use the router result callback and stick methods on that.
  • Loading branch information
ckknight committed Apr 2, 2011
1 parent 78b0996 commit fe9376b
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 65 deletions.
137 changes: 72 additions & 65 deletions lib/escort.js
Expand Up @@ -430,13 +430,58 @@ var calculateConverterArgs, generateUrlFunction;
res.end();
}
};
}
}

/**
* Attach all the helper HTTP and WebDAV methods as helper functions which will
* ultimately call bind on the provided object.
*
* @param {Object} object the Object to attach the methods to.
* @api private
*/
var attachHttpMethods = function (object) {
ACCEPTABLE_METHODS.forEach(function (method) {
/**
* Bind the provided route with a specific method to the callback provided.
* Since you cannot specify a route more than once, it is required to use bind to provide multiple methods.
*
* @param {String} routeName The name of the route. Should be a JavaScript identifier. Optional.
* @param {String} route The URL for the route.
* @param {Function} callback The callback to be called when the route is accessed.
*
* @throws Error the routeName is already specified
* @throws Error the route is already specified
* @throws Error the route does not start with "/"
* @throws Error an unknown route converter was specified
*
* @api public
*
* @example routes.get("/", function(request, response) {
* response.end("GET /");
* });
* @example routes.post("item", "/{name}", function(request, response, params) {
* response.end("GET /" + params.name);
* });
*/
object[method] = function (routeName, route, callback) {
if (arguments.length === 2) {
callback = route;
route = routeName;
routeName = guessRouteName(route);
}
var descriptor = {};
descriptor[method] = callback;
return this.bind(routeName, route, descriptor);
};
});
object.del = object.delete;
};

/**
* Create the Escort middleware based on the provided options and callback.
*
* @param {Object} options An options object, optional
* @param {Function} fn A callback that is immediately called where the routing configuration is set up.
* @param {Function} fn A callback that is immediately called where the routing configuration is set up. Optional.
* @api public
* @example escort(function(routes) {
* routes.get("/", function(req, res) {
Expand All @@ -449,9 +494,6 @@ var calculateConverterArgs, generateUrlFunction;
fn = options;
options = null;
}
if (!fn) {
throw new Error("Callback function is expected");
}
if (!options) {
options = {};
}
Expand Down Expand Up @@ -493,6 +535,8 @@ var calculateConverterArgs, generateUrlFunction;
*/
var customConverters = options.converters || {};

var bind;

(function () {
/**
* Bind the provided route to the descriptor specified.
Expand Down Expand Up @@ -529,7 +573,7 @@ var calculateConverterArgs, generateUrlFunction;
* }
* });
*/
var bind = function (routeName, route, descriptor, routePrefix) {
bind = function (routeName, route, descriptor, routePrefix) {
if (arguments.length === 2) {
descriptor = route;
route = parseOptionalRoutes(routeName);
Expand Down Expand Up @@ -679,28 +723,6 @@ var calculateConverterArgs, generateUrlFunction;
urlGenerators[routeName] = staticRouteUrlGenerator || dynamicRouteUrlGenerator;
}
};

/**
* The methods which are passed into the callback.
*/
var methods = spawn(escort.prototype, {
bind: bind,
url: urlGenerators,
notFound: function (handler) {
if (!handler) {
throw new Error("Callback function is expected");
}
notFoundHandler = handler;
},
methodNotAllowed: function (handler) {
if (!handler) {
throw new Error("Callback function is expected");
}
methodNotAllowedHandler = handler;
},
submount: makeSubmountFunction(bind, ""),
});
fn(methods);
}());

return (function () {
Expand Down Expand Up @@ -848,49 +870,34 @@ var calculateConverterArgs, generateUrlFunction;
next(err);
}
};
router.toString = function () {
return "Escort router";
};
router.bind = bind;
router.url = urlGenerators;
router.notFound = function (handler) {
if (!handler) {
throw new Error("Callback function is expected");
}
notFoundHandler = handler;
};
router.methodNotAllowed = function (handler) {
if (!handler) {
throw new Error("Callback function is expected");
}
methodNotAllowedHandler = handler;
};
router.submount = makeSubmountFunction(bind, "");
attachHttpMethods(router);
if (fn) {
fn(router);
}

return router;
}());
};
escort.prototype = {};

// attach all the methods as helper methods onto escort.prototype
ACCEPTABLE_METHODS.forEach(function (method) {
/**
* Bind the provided route with a specific method to the callback provided.
* Since you cannot specify a route more than once, it is required to use bind to provide multiple methods.
*
* @param {String} routeName The name of the route. Should be a JavaScript identifier. Optional.
* @param {String} route The URL for the route.
* @param {Function} callback The callback to be called when the route is accessed.
*
* @throws Error the routeName is already specified
* @throws Error the route is already specified
* @throws Error the route does not start with "/"
* @throws Error an unknown route converter was specified
*
* @api public
*
* @example routes.get("/", function(request, response) {
* response.end("GET /");
* });
* @example routes.post("item", "/{name}", function(request, response, params) {
* response.end("GET /" + params.name);
* });
*/
escort.prototype[method] = function (routeName, route, callback) {
if (arguments.length === 2) {
callback = route;
route = routeName;
routeName = guessRouteName(route);
}
var descriptor = {};
descriptor[method] = callback;
return this.bind(routeName, route, descriptor);
};
});
escort.prototype.del = escort.prototype.delete;
attachHttpMethods(escort.prototype);

(function () {
var converters = escort.converters = {};
Expand Down
10 changes: 10 additions & 0 deletions test/escort.test.js
Expand Up @@ -819,5 +819,15 @@ module.exports = {
assert.response(app,
{ url: "/error", method: "GET" },
{ statusCode: 500 });
},
"allow lack of callback": function () {
var routing = escort();
routing.get("/", function (req, res) {
res.end("GET /");
});

assert.response(connect(routing),
{ url: "/", method: "GET" },
{ body: "GET /" });
}
};

0 comments on commit fe9376b

Please sign in to comment.