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.
This commit is contained in:
Miroslav Bajtoš 2014-11-04 11:27:49 +01:00
parent 7a1a3b8592
commit 4fdcbd16af
2 changed files with 23 additions and 25 deletions

View File

@ -22,33 +22,31 @@ module.exports = rest;
* @header loopback.rest() * @header loopback.rest()
*/ */
function rest(options) { function rest() {
options = options || {};
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) { return function restApiHandler(req, res, next) {
var app = req.app; var app = req.app;
var handler = app.handler('rest'); var restHandler = app.handler('rest');
if (req.url === '/routes') { if (req.url === '/routes') {
return res.send(handler.adapter.allRoutes()); return res.send(restHandler.adapter.allRoutes());
} else if (req.url === '/models') { } else if (req.url === '/models') {
return res.send(app.remotes().toJSON()); return res.send(app.remotes().toJSON());
} }
var handlers = []; var preHandlers;
if (options.context) {
handlers.push(contextHandler); if (!preHandlers) {
} preHandlers = [];
if (app.isAuthEnabled) { var remotingOptions = app.get('remoting') || {};
if (!tokenParser) {
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 // NOTE(bajtos) It would be better to search app.models for a model
// of type AccessToken instead of searching all loopback models. // of type AccessToken instead of searching all loopback models.
// Unfortunately that's not supported now. // Unfortunately that's not supported now.
@ -56,12 +54,11 @@ function rest(options) {
// https://github.com/strongloop/loopback/pull/167 // https://github.com/strongloop/loopback/pull/167
// https://github.com/strongloop/loopback/commit/f07446a // https://github.com/strongloop/loopback/commit/f07446a
var AccessToken = loopback.getModelByType(loopback.AccessToken); var AccessToken = loopback.getModelByType(loopback.AccessToken);
tokenParser = loopback.token({ model: AccessToken }); preHandlers.push(loopback.token({ model: AccessToken }));
handlers.push(tokenParser);
} }
} }
handlers.push(handler);
async.eachSeries(handlers, function(handler, done) { async.eachSeries(preHandlers.concat(restHandler), function(handler, done) {
handler(req, res, done); handler(req, res, done);
}, next); }, next);
}; };

View File

@ -178,7 +178,7 @@ describe('loopback.rest', function() {
} }
it('should enable context using loopback.context', function(done) { it('should enable context using loopback.context', function(done) {
app.use(loopback.context({enableHttpContext: true})); app.use(loopback.context({ enableHttpContext: true }));
app.enableAuth(); app.enableAuth();
app.use(loopback.rest()); app.use(loopback.rest());
@ -187,7 +187,8 @@ describe('loopback.rest', function() {
it('should enable context with loopback.rest', function(done) { it('should enable context with loopback.rest', function(done) {
app.enableAuth(); app.enableAuth();
app.use(loopback.rest({context: {enableHttpContext: true}})); app.set('remoting', { context: { enableHttpContext: true } });
app.use(loopback.rest());
invokeGetToken(done); invokeGetToken(done);
}); });