API cleanup
Rename "per-request-context" middleware to shorter "per-request". Introduce "LoopBackContext.perRequest" as a shortcut for way too long "loopback-context/server/middlewar/per-request"
This commit is contained in:
parent
6e301d6955
commit
28e99e5e2c
|
@ -5,13 +5,13 @@ node-continuation-local-storage.
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
1) Add `per-request-context` middleware to your
|
1) Add `per-request` middleware to your
|
||||||
`server/middleware-config.json`:
|
`server/middleware-config.json`:
|
||||||
|
|
||||||
```json
|
```json
|
||||||
{
|
{
|
||||||
"initial": {
|
"initial": {
|
||||||
"loopback-context#per-request-context": {
|
"loopback-context#per-request": {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -94,3 +94,10 @@ LoopBackContext.createContext = function(scopeName) {
|
||||||
}
|
}
|
||||||
return ns;
|
return ns;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create middleware that sets up a new context for each incoming HTTP request.
|
||||||
|
*
|
||||||
|
* See perRequestContextFactory for more details.
|
||||||
|
*/
|
||||||
|
LoopBackContext.perRequest = require('./middleware/per-request');
|
||||||
|
|
|
@ -7,7 +7,7 @@
|
||||||
|
|
||||||
var LoopBackContext = require('../current-context');
|
var LoopBackContext = require('../current-context');
|
||||||
|
|
||||||
module.exports = context;
|
module.exports = perRequestContextFactory;
|
||||||
|
|
||||||
var name = 'loopback';
|
var name = 'loopback';
|
||||||
|
|
||||||
|
@ -26,14 +26,14 @@ var name = 'loopback';
|
||||||
* @property {Boolean} enableHttpContext Whether HTTP context is enabled. Default is false.
|
* @property {Boolean} enableHttpContext Whether HTTP context is enabled. Default is false.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
function context(options) {
|
function perRequestContextFactory(options) {
|
||||||
options = options || {};
|
options = options || {};
|
||||||
var scope = options.name || name;
|
var scope = options.name || name;
|
||||||
var enableHttpContext = options.enableHttpContext || false;
|
var enableHttpContext = options.enableHttpContext || false;
|
||||||
var ns = LoopBackContext.createContext(scope);
|
var ns = LoopBackContext.createContext(scope);
|
||||||
|
|
||||||
// Return the middleware
|
// Return the middleware
|
||||||
return function contextHandler(req, res, next) {
|
return function perRequestContext(req, res, next) {
|
||||||
if (req.loopbackContext) {
|
if (req.loopbackContext) {
|
||||||
return next();
|
return next();
|
||||||
}
|
}
|
|
@ -5,7 +5,7 @@
|
||||||
|
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var ClsContext = require('..');
|
var LoopBackContext = require('..');
|
||||||
var Domain = require('domain');
|
var Domain = require('domain');
|
||||||
var EventEmitter = require('events').EventEmitter;
|
var EventEmitter = require('events').EventEmitter;
|
||||||
var expect = require('./helpers/expect');
|
var expect = require('./helpers/expect');
|
||||||
|
@ -39,7 +39,7 @@ describe('LoopBack Context', function() {
|
||||||
var app = loopback({localRegistry: true, loadBuiltinModels: true});
|
var app = loopback({localRegistry: true, loadBuiltinModels: true});
|
||||||
app.set('remoting', {context: false});
|
app.set('remoting', {context: false});
|
||||||
app.set('legacyExplorer', false);
|
app.set('legacyExplorer', false);
|
||||||
app.use(require('../server/middleware/per-request-context')());
|
app.use(LoopBackContext.perRequest());
|
||||||
app.use(loopback.rest());
|
app.use(loopback.rest());
|
||||||
app.dataSource('db', {connector: 'memory'});
|
app.dataSource('db', {connector: 'memory'});
|
||||||
|
|
||||||
|
@ -48,7 +48,7 @@ describe('LoopBack Context', function() {
|
||||||
|
|
||||||
// function for remote method
|
// function for remote method
|
||||||
TestModel.test = function(inst, cb) {
|
TestModel.test = function(inst, cb) {
|
||||||
var tmpCtx = ClsContext.getCurrentContext();
|
var tmpCtx = LoopBackContext.getCurrentContext();
|
||||||
if (tmpCtx) tmpCtx.set('data', 'a value stored in context');
|
if (tmpCtx) tmpCtx.set('data', 'a value stored in context');
|
||||||
if (process.domain) cb = process.domain.bind(cb); // IMPORTANT
|
if (process.domain) cb = process.domain.bind(cb); // IMPORTANT
|
||||||
runInOtherDomain(cb);
|
runInOtherDomain(cb);
|
||||||
|
@ -63,7 +63,7 @@ describe('LoopBack Context', function() {
|
||||||
|
|
||||||
// after remote hook
|
// after remote hook
|
||||||
TestModel.afterRemote('**', function(ctxx, inst, next) {
|
TestModel.afterRemote('**', function(ctxx, inst, next) {
|
||||||
var tmpCtx = ClsContext.getCurrentContext();
|
var tmpCtx = LoopBackContext.getCurrentContext();
|
||||||
if (tmpCtx) {
|
if (tmpCtx) {
|
||||||
ctxx.result.data = tmpCtx.get('data');
|
ctxx.result.data = tmpCtx.get('data');
|
||||||
} else {
|
} else {
|
||||||
|
@ -85,12 +85,12 @@ describe('LoopBack Context', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('works outside REST middleware', function(done) {
|
it('works outside REST middleware', function(done) {
|
||||||
ClsContext.runInContext(function() {
|
LoopBackContext.runInContext(function() {
|
||||||
var ctx = ClsContext.getCurrentContext();
|
var ctx = LoopBackContext.getCurrentContext();
|
||||||
expect(ctx).is.an('object');
|
expect(ctx).is.an('object');
|
||||||
ctx.set('test-key', 'test-value');
|
ctx.set('test-key', 'test-value');
|
||||||
process.nextTick(function() {
|
process.nextTick(function() {
|
||||||
var ctx = ClsContext.getCurrentContext();
|
var ctx = LoopBackContext.getCurrentContext();
|
||||||
expect(ctx).is.an('object');
|
expect(ctx).is.an('object');
|
||||||
expect(ctx.get('test-key')).to.equal('test-value');
|
expect(ctx.get('test-key')).to.equal('test-value');
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue