diff --git a/lib/application.js b/lib/application.js index 5b87c54c21..0b5aa156de 100644 --- a/lib/application.js +++ b/lib/application.js @@ -11,7 +11,7 @@ var stripSlashes = function (name) { module.exports = { init: function () { _.extend(this, { - methods: ['find', 'get', 'create', 'update', 'remove'], + methods: ['find', 'get', 'create', 'update', 'patch', 'remove'], mixins: mixins, services: {}, providers: [] diff --git a/lib/providers/rest/index.js b/lib/providers/rest/index.js index a0042ddbc1..77d8d44e4e 100644 --- a/lib/providers/rest/index.js +++ b/lib/providers/rest/index.js @@ -36,14 +36,16 @@ module.exports = function (config) { // GET / -> service.find(cb, params) app.get(uri, app.rest.find(service)) - // GET /:id -> service.get(cb, id, params) + // GET /:id -> service.get(id, params, cb) .get(uri + '/:id', app.rest.get(service)) - // POST -> service.create(cb, data, params) + // POST -> service.create(data, params, cb) .post(uri, app.rest.create(service)) - // PUT /:id -> service.update(cb, id, data, params) + // PUT /:id -> service.update(id, data, params, cb) .put(uri + '/:id', app.rest.update(service)) - // DELETE /:id -> service.remove(cb, id, params) - .del(uri + '/:id', app.rest.remove(service)); + // DELETE /:id -> service.remove(id, params, cb) + .del(uri + '/:id', app.rest.remove(service)) + // PATCH /:id -> service.patch(id, data, params, callback) + .patch(uri + '/:id', app.rest.patch(service)); app.use(uri, responder); }); diff --git a/lib/providers/rest/wrappers.js b/lib/providers/rest/wrappers.js index d5b7442f3a..fdd37c7f3e 100644 --- a/lib/providers/rest/wrappers.js +++ b/lib/providers/rest/wrappers.js @@ -102,6 +102,17 @@ module.exports = { }; }, + patch: function(service) { + return function(req, res, next) { + var error = checkMethod(res, service, 'patch'); + if (error) { + return next(error); + } + + service.patch(req.params.id, req.body, getParams(req), wrap(req, res, next)); + }; + }, + remove: function(service) { return function(req, res, next) { var error = checkMethod(res, service, 'remove'); diff --git a/test/providers/primus.test.js b/test/providers/primus.test.js index 457656e979..d8603695bd 100644 --- a/test/providers/primus.test.js +++ b/test/providers/primus.test.js @@ -65,6 +65,18 @@ describe('Primus provider', function () { }); }); + it('::patch', function (done) { + var original = { + name: 'patching' + }; + + socket.send('todo::patch', 25, original, {}, function (error, data) { + verify.patch(25, original, data); + + done(error); + }); + }); + it('::remove', function (done) { socket.send('todo::remove', 11, {}, function (error, data) { verify.remove(11, data); diff --git a/test/providers/rest.test.js b/test/providers/rest.test.js index bcec86d708..d3e9c074ce 100644 --- a/test/providers/rest.test.js +++ b/test/providers/rest.test.js @@ -77,6 +77,26 @@ describe('REST provider', function () { }); }); + it('PATCH .patch', function (done) { + var original = { + description: 'PATCH .patch' + }; + + request({ + url: 'http://localhost:4777/todo/544', + method: 'patch', + body: JSON.stringify(original), + headers: { + 'Content-Type': 'application/json' + } + }, function (error, response, body) { + assert.ok(response.statusCode === 200, 'Got OK status code'); + verify.patch(544, original, JSON.parse(body)); + + done(error); + }); + }); + it('DELETE .remove', function (done) { request({ url: 'http://localhost:4777/todo/233', diff --git a/test/providers/service-fixture.js b/test/providers/service-fixture.js index 49a7628de7..eaec52ae10 100644 --- a/test/providers/service-fixture.js +++ b/test/providers/service-fixture.js @@ -37,6 +37,13 @@ exports.Service = { callback(null, result); }, + patch: function (id, data, params, callback) { + var result = _.clone(data); + result.id = id; + result.status = 'patched'; + callback(null, result); + }, + remove: function (id, params, callback) { callback(null, { id: id @@ -70,6 +77,14 @@ exports.verify = { assert.deepEqual(expected, current, 'Data ran through .update as expected'); }, + patch: function (id, original, current) { + var expected = _.extend({}, original, { + id: id, + status: 'patched' + }); + assert.deepEqual(expected, current, 'Data ran through .patch as expected'); + }, + remove: function (id, data) { assert.deepEqual({ id: id diff --git a/test/providers/socketio.test.js b/test/providers/socketio.test.js index c1c708f287..3623b9efa2 100644 --- a/test/providers/socketio.test.js +++ b/test/providers/socketio.test.js @@ -74,6 +74,18 @@ describe('SocketIO provider', function () { }); }); + it('::patch', function (done) { + var original = { + name: 'patching' + }; + + socket.emit('todo::patch', 25, original, {}, function (error, data) { + verify.patch(25, original, data); + + done(error); + }); + }); + it('::remove', function (done) { socket.emit('todo::remove', 11, {}, function (error, data) { verify.remove(11, data);