From f112c1e03137beebae2f828f35699b0ee50f3958 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Bajto=C5=A1?= Date: Tue, 4 Nov 2014 11:27:49 +0100 Subject: [PATCH] rest middleware: clean up context config Modify `loopback.rest()` to read the configuration for `loopback.context` from `app.get('remoting')`, which is the approach used for all other configuration options related to the REST transport. --- lib/middleware/rest.js | 19 +++++++++---------- test/rest.middleware.test.js | 5 +++-- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/lib/middleware/rest.js b/lib/middleware/rest.js index db9a79e4f..f280bfc89 100644 --- a/lib/middleware/rest.js +++ b/lib/middleware/rest.js @@ -22,17 +22,9 @@ module.exports = rest; * @header loopback.rest() */ -function rest(options) { - options = options || {}; +function rest() { var tokenParser = null; var contextHandler = null; - if (options.context) { - var contextOptions = options.context; - if(typeof contextOptions !== 'object') { - contextOptions = {}; - } - contextHandler = loopback.context(contextOptions); - } return function restApiHandler(req, res, next) { var app = req.app; var handler = app.handler('rest'); @@ -44,9 +36,16 @@ function rest(options) { } var handlers = []; - if (options.context) { + var remotingOptions = app.get('remoting') || {}; + + var contextOptions = remotingOptions.context; + if (contextOptions !== false && !contextHandler) { + if (typeof contextOptions !== 'object') + contextOptions = {}; + contextHandler = loopback.context(contextOptions); handlers.push(contextHandler); } + if (app.isAuthEnabled) { if (!tokenParser) { // NOTE(bajtos) It would be better to search app.models for a model diff --git a/test/rest.middleware.test.js b/test/rest.middleware.test.js index 690d6736d..e0025308a 100644 --- a/test/rest.middleware.test.js +++ b/test/rest.middleware.test.js @@ -136,7 +136,7 @@ describe('loopback.rest', function() { } it('should enable context using loopback.context', function(done) { - app.use(loopback.context({enableHttpContext: true})); + app.use(loopback.context({ enableHttpContext: true })); app.enableAuth(); app.use(loopback.rest()); @@ -145,7 +145,8 @@ describe('loopback.rest', function() { it('should enable context with loopback.rest', function(done) { app.enableAuth(); - app.use(loopback.rest({context: {enableHttpContext: true}})); + app.set('remoting', { context: { enableHttpContext: true } }); + app.use(loopback.rest()); invokeGetToken(done); });