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 5, 2014
1 parent 7a1a3b8 commit 4fdcbd1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 25 deletions.
43 changes: 20 additions & 23 deletions lib/middleware/rest.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,46 +22,43 @@ module.exports = rest;
* @header loopback.rest()
*/

function rest(options) {
options = options || {};
var tokenParser = null;
var contextHandler = null;
if (options.context) {
var contextOptions = options.context;
if(typeof contextOptions !== 'object') {
contextOptions = {};
}
contextHandler = loopback.context(contextOptions);
}
function rest() {
return function restApiHandler(req, res, next) {
var app = req.app;
var handler = app.handler('rest');
var restHandler = app.handler('rest');

if (req.url === '/routes') {
return res.send(handler.adapter.allRoutes());
return res.send(restHandler.adapter.allRoutes());
} else if (req.url === '/models') {
return res.send(app.remotes().toJSON());
}

var handlers = [];
if (options.context) {
handlers.push(contextHandler);
}
if (app.isAuthEnabled) {
if (!tokenParser) {
var preHandlers;

if (!preHandlers) {
preHandlers = [];
var remotingOptions = app.get('remoting') || {};

var contextOptions = remotingOptions.context;
if (contextOptions !== false) {
if (typeof contextOptions !== 'object')
contextOptions = {};
preHandlers.push(loopback.context(contextOptions));
}

if (app.isAuthEnabled) {
// NOTE(bajtos) It would be better to search app.models for a model
// of type AccessToken instead of searching all loopback models.
// Unfortunately that's not supported now.
// Related discussions:
// https://github.com/strongloop/loopback/pull/167
// https://github.com/strongloop/loopback/commit/f07446a
var AccessToken = loopback.getModelByType(loopback.AccessToken);
tokenParser = loopback.token({ model: AccessToken });
handlers.push(tokenParser);
preHandlers.push(loopback.token({ model: AccessToken }));
}
}
handlers.push(handler);
async.eachSeries(handlers, function(handler, done) {

async.eachSeries(preHandlers.concat(restHandler), function(handler, done) {
handler(req, res, done);
}, next);
};
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 @@ -178,7 +178,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 @@ -187,7 +187,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 4fdcbd1

Please sign in to comment.