Skip to content

Commit

Permalink
rest middleware: clean up context config
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Miroslav Bajtoš committed Nov 4, 2014
1 parent a87eab5 commit f112c1e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
19 changes: 9 additions & 10 deletions lib/middleware/rest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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
Expand Down
5 changes: 3 additions & 2 deletions test/rest.middleware.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand All @@ -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);
});
Expand Down

0 comments on commit f112c1e

Please sign in to comment.