From 129f2d80ec679216d04ce027ba121c6b8e719fe9 Mon Sep 17 00:00:00 2001 From: David Luecke Date: Wed, 14 Sep 2016 19:26:44 -0700 Subject: [PATCH] Rename internal route id property --- src/index.js | 2 +- src/wrappers.js | 6 +++--- test/index.test.js | 30 ++++++++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/src/index.js b/src/index.js index 0b46514..64bdc00 100644 --- a/src/index.js +++ b/src/index.js @@ -30,7 +30,7 @@ export default function rest(handler = formatter) { app.providers.push(function (path, service, options) { const uri = path.indexOf('/') === 0 ? path : `/${path}`; const baseRoute = app.route(uri); - const idRoute = app.route(`${uri}/:id`); + const idRoute = app.route(`${uri}/:__feathersId`); let middleware = (options || {}).middleware || {}; let before = middleware.before || []; diff --git a/src/wrappers.js b/src/wrappers.js index db5400e..e606e58 100644 --- a/src/wrappers.js +++ b/src/wrappers.js @@ -39,7 +39,7 @@ function getHandler (method, getArgs, service) { } let params = Object.assign({}, req.params || {}); - delete params.id; + delete params.__feathersId; // Grab the service parameters. Use req.feathers and set the query to req.query params = Object.assign({ query: req.query || {} }, params, req.feathers); @@ -79,12 +79,12 @@ function reqNone () { // Returns the leading parameters for a `get` or `remove` request (the id) function reqId (req) { - return [ req.params.id || null ]; + return [ req.params.__feathersId || null ]; } // Returns the leading parameters for an `update` or `patch` request (id, data) function reqUpdate (req) { - return [ req.params.id || null, req.body ]; + return [ req.params.__feathersId || null, req.body ]; } // Returns the leading parameters for a `create` request (data) diff --git a/test/index.test.js b/test/index.test.js index 88874c7..2b12592 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -450,4 +450,34 @@ describe('REST provider', function () { server.close(done); }); }); + + it('Extend params with route params and allows id property (#76, #407)', done => { + const todoService = { + get(id, params) { + return Promise.resolve({ + id, + appId: params.appId, + paramsId: params.id + }); + } + }; + + const app = feathers() + .configure(rest()) + .use('/:appId/:id/todo', todoService); + + const expected = { + id: 'dishes', + appId: 'theApp', + paramsId: 'myId' + }; + + const server = app.listen(6880).on('listening', function () { + request('http://localhost:6880/theApp/myId/todo/' + expected.id, (error, response, body) => { + assert.ok(response.statusCode === 200, 'Got OK status code'); + assert.deepEqual(expected, JSON.parse(body)); + server.close(done); + }); + }); + }); });