diff --git a/lib/application.js b/lib/application.js index 79ae421c01..5ebec1b14c 100644 --- a/lib/application.js +++ b/lib/application.js @@ -18,7 +18,7 @@ module.exports = { }); }, - service: function(location, service) { + service: function(location, service, options) { var protoService = Proto.extend(service); var self = this; @@ -31,15 +31,17 @@ module.exports = { // Run the provider functions to register the service _.each(this.providers, function (provider) { - provider(location, protoService); + provider(location, protoService, options); }); this.services[location] = protoService; return this; }, - use: function (location, service) { - + use: function () { + var args = _.toArray(arguments); + var location = args.shift(); + var service = args.pop(); var hasMethod = function() { return _.some(arguments, function(name) { return (service && typeof service[name] === 'function'); @@ -51,8 +53,10 @@ module.exports = { return this._super.apply(this, arguments); } - return this.service(location, service); - + return this.service(location, service, { + // Any arguments left over are other middleware that we want to pass to the providers + middleware: args + }); }, lookup: function (location) { diff --git a/lib/providers/rest/index.js b/lib/providers/rest/index.js index bad83bf789..be2744e89e 100644 --- a/lib/providers/rest/index.js +++ b/lib/providers/rest/index.js @@ -14,7 +14,7 @@ module.exports = function (config) { }); }; - if(typeof config === 'function') { + if (typeof config === 'function') { handler = config; } @@ -31,28 +31,29 @@ module.exports = function (config) { app.rest = wrappers; // Register the REST provider - app.providers.push(function (path, service) { + app.providers.push(function (path, service, options) { if (app.disabled('feathers rest')) { return; } + var middleware = (options && options.middleware) || []; var uri = path.indexOf('/') === 0 ? path : '/' + path; - - app.route(uri) - // GET / -> service.find(cb, params) - .get(app.rest.find(service)) - // POST -> service.create(data, params, cb) - .post(app.rest.create(service)); - - app.route(uri + '/:id') - // GET /:id -> service.get(id, params, cb) - .get(app.rest.get(service)) - // PUT /:id -> service.update(id, data, params, cb) - .put(app.rest.update(service)) - // PATCH /:id -> service.patch(id, data, params, callback) - .patch(app.rest.patch(service)) - // DELETE /:id -> service.remove(id, params, cb) - .delete(app.rest.remove(service)); + var baseRoute = app.route(uri); + var idRoute = app.route(uri + '/:id'); + + // GET / -> service.find(cb, params) + baseRoute.get.apply(baseRoute, middleware.concat(app.rest.find(service))); + // POST -> service.create(data, params, cb) + baseRoute.post.apply(baseRoute, middleware.concat(app.rest.create(service))); + + // GET /:id -> service.get(id, params, cb) + idRoute.get.apply(idRoute, middleware.concat(app.rest.get(service))); + // PUT /:id -> service.update(id, data, params, cb) + idRoute.put.apply(idRoute, middleware.concat(app.rest.update(service))); + // PATCH /:id -> service.patch(id, data, params, callback) + idRoute.patch.apply(idRoute, middleware.concat(app.rest.patch(service))); + // DELETE /:id -> service.remove(id, params, cb) + idRoute.delete.apply(idRoute, middleware.concat(app.rest.remove(service))); app.use(uri, handler); }); diff --git a/test/application.test.js b/test/application.test.js index f08a2ec63a..c5e5639df3 100644 --- a/test/application.test.js +++ b/test/application.test.js @@ -97,6 +97,41 @@ describe('Feathers application', function () { }); }); + it('uses custom middleware (#21)', function (done) { + var todoService = { + get: function (name, params, callback) { + callback(null, { + id: name, + description: "You have to do " + name + "!", + stuff: params.stuff + }); + } + }; + + var app = feathers() + .configure(feathers.rest()) + .use('/todo', function(req, res, next) { + req.feathers.stuff = 'custom middleware'; + next(); + }, todoService) + .use('/otherTodo', todoService); + + var server = app.listen(6995).on('listening', function () { + request('http://localhost:6995/todo/dishes', function (error, response, body) { + assert.ok(response.statusCode === 200, 'Got OK status code'); + var data = JSON.parse(body); + assert.equal(data.stuff, 'custom middleware', 'Custom middleware updated params'); + + request('http://localhost:6995/otherTodo/dishes', function (error, response, body) { + assert.ok(response.statusCode === 200, 'Got OK status code'); + var data = JSON.parse(body); + assert.ok(!data.stuff, 'Custom middleware not run for different service.'); + server.close(done); + }); + }); + }); + }); + it('REST and SocketIO with SSL server (#25)', function(done) { // For more info on Reqest HTTPS settings see https://github.com/mikeal/request/issues/418 // This needs to be set so that the SocektIO client can connect