Skip to content

Commit 42c3475

Browse files
author
Miroslav Bajtoš
committed
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.
1 parent a87eab5 commit 42c3475

File tree

2 files changed

+12
-14
lines changed

2 files changed

+12
-14
lines changed

lib/middleware/rest.js

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,9 @@ module.exports = rest;
2222
* @header loopback.rest()
2323
*/
2424

25-
function rest(options) {
26-
options = options || {};
25+
function rest() {
2726
var tokenParser = null;
2827
var contextHandler = null;
29-
if (options.context) {
30-
var contextOptions = options.context;
31-
if(typeof contextOptions !== 'object') {
32-
contextOptions = {};
33-
}
34-
contextHandler = loopback.context(contextOptions);
35-
}
3628
return function restApiHandler(req, res, next) {
3729
var app = req.app;
3830
var handler = app.handler('rest');
@@ -44,9 +36,16 @@ function rest(options) {
4436
}
4537

4638
var handlers = [];
47-
if (options.context) {
39+
var remotingOptions = app.get('remoting') || {};
40+
41+
var contextOptions = remotingOptions.context;
42+
if (contextOptions !== false && !contextHandler) {
43+
if (typeof contextOptions !== 'object')
44+
contextOptions = {};
45+
contextHandler = loopback.context(contextOptions);
4846
handlers.push(contextHandler);
4947
}
48+
5049
if (app.isAuthEnabled) {
5150
if (!tokenParser) {
5251
// NOTE(bajtos) It would be better to search app.models for a model
@@ -66,5 +65,3 @@ function rest(options) {
6665
}, next);
6766
};
6867
}
69-
70-

test/rest.middleware.test.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ describe('loopback.rest', function() {
136136
}
137137

138138
it('should enable context using loopback.context', function(done) {
139-
app.use(loopback.context({enableHttpContext: true}));
139+
app.use(loopback.context({ enableHttpContext: true }));
140140
app.enableAuth();
141141
app.use(loopback.rest());
142142

@@ -145,7 +145,8 @@ describe('loopback.rest', function() {
145145

146146
it('should enable context with loopback.rest', function(done) {
147147
app.enableAuth();
148-
app.use(loopback.rest({context: {enableHttpContext: true}}));
148+
app.set('remoting', { context: { enableHttpContext: true } });
149+
app.use(loopback.rest());
149150

150151
invokeGetToken(done);
151152
});

0 commit comments

Comments
 (0)