Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SEMVER-MAJOR] Remove current-context API #2564

Merged
merged 2 commits into from
Aug 10, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions 3.0-RELEASE-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,44 @@ We have removed `loopback#errorhandler` middleware, users should use `strong-err
```

See also strong-error-handler's [options](https://github.com/strongloop/strong-error-handler#options) and the [related code change](https://github.com/strongloop/loopback/pull/2411).

## Remove current context API and middleware

We have removed the following current-context-related APIs:

- `loopback.getCurrentContext`
- `loopback.createContext`
- `loopback.runInContext`

Additionaly, `loopback#context` middleware and `remoting.context` server
config were removed too.

To setup "current context" feature in your LoopBack 3.x application, you
should use [loopback-context](https://www.npmjs.com/package/loopback-context)
module:

1. Add `loopback-context` to your dependencies

2. Configure the new context middleware in your `server/middleware-config.json` file
```json
{
"initial": {
"loopback-context#per-request": {}
}
}
```

3. Replace all usages of `loopback.getCurrentContext` with the following:
```js
// at the top of your file
var LoopBackContext = require('loopback-context');

// in your code
var ctx = LoopBackContext.getCurrentContext();
if (ctx) {
// use the context
}
```

See also [loopback#2564](https://github.com/strongloop/loopback/pull/2564)
and the official [documentation](https://docs.strongloop.com/display/APIC/Using+current+context)
3 changes: 0 additions & 3 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ module.exports = function(grunt) {
common: {
src: ['common/**/*.js'],
},
browser: {
src: ['browser/**/*.js'],
},
server: {
src: ['server/**/*.js'],
},
Expand Down
17 changes: 0 additions & 17 deletions browser/current-context.js

This file was deleted.

33 changes: 33 additions & 0 deletions lib/current-context.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright IBM Corp. 2015,2016. All Rights Reserved.
// Node module: loopback
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

var g = require('strong-globalize')();
var juggler = require('loopback-datasource-juggler');
var remoting = require('strong-remoting');

module.exports = function(loopback) {
juggler.getCurrentContext =
remoting.getCurrentContext =
loopback.getCurrentContext = function() {
throw new Error(g.f(
'%s was removed in version 3.0. See %s for more details.',
'loopback.getCurrentContext()',
'https://docs.strongloop.com/display/APIC/Using%20current%20context'));
};

loopback.runInContext = function(fn) {
throw new Error(g.f(
'%s was removed in version 3.0. See %s for more details.',
'loopback.runInContext()',
'https://docs.strongloop.com/display/APIC/Using%20current%20context'));
};

loopback.createContext = function(scopeName) {
throw new Error(g.f(
'%s was removed in version 3.0. See %s for more details.',
'loopback.createContext()',
'https://docs.strongloop.com/display/APIC/Using%20current%20context'));
};
};
2 changes: 1 addition & 1 deletion lib/loopback.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ loopback.template = function(file) {
});
};

require('../server/current-context')(loopback);
require('../lib/current-context')(loopback);

/**
* Create a named vanilla JavaScript class constructor with an attached
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
"bluebird": "^3.1.1",
"body-parser": "^1.12.0",
"canonical-json": "0.0.4",
"continuation-local-storage": "^3.1.3",
"debug": "^2.1.2",
"depd": "^1.0.0",
"ejs": "^2.3.1",
Expand Down Expand Up @@ -82,6 +81,7 @@
"karma-phantomjs-launcher": "^1.0.0",
"karma-script-launcher": "^1.0.0",
"loopback-boot": "^2.7.0",
"loopback-context": "^1.0.0",
"mocha": "^3.0.0",
"phantomjs-prebuilt": "^2.1.7",
"sinon": "^1.13.0",
Expand All @@ -97,7 +97,6 @@
"browser": {
"express": "./lib/browser-express.js",
"./lib/server-app.js": "./lib/browser-express.js",
"./server/current-context.js": "./browser/current-context.js",
"connect": false,
"nodemailer": false,
"supertest": false,
Expand Down
141 changes: 0 additions & 141 deletions server/current-context.js

This file was deleted.

58 changes: 6 additions & 52 deletions server/middleware/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,55 +3,9 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

var loopback = require('../../lib/loopback');

module.exports = context;

var name = 'loopback';

/**
* Context middleware.
* ```js
* var app = loopback();
* app.use(loopback.context(options);
* app.use(loopback.rest());
* app.listen();
* ```
* @options {Object} [options] Options for context
* @property {String} name Context scope name.
* @property {Boolean} enableHttpContext Whether HTTP context is enabled. Default is false.
* @header loopback.context([options])
*/

function context(options) {
options = options || {};
var scope = options.name || name;
var enableHttpContext = options.enableHttpContext || false;
var ns = loopback.createContext(scope);

// Return the middleware
return function contextHandler(req, res, next) {
if (req.loopbackContext) {
return next();
}

loopback.runInContext(function processRequestInContext(ns, domain) {
req.loopbackContext = ns;

// Bind req/res event emitters to the given namespace
ns.bindEmitter(req);
ns.bindEmitter(res);

// Add req/res event emitters to the current domain
domain.add(req);
domain.add(res);

// Run the code in the context of the namespace
if (enableHttpContext) {
// Set up the transport context
ns.set('http', { req: req, res: res });
}
next();
});
};
}
module.exports = function() {
throw new Error(g.f(
'%s middleware was removed in version 3.0. See %s for more details.',
'loopback#context',
'https://docs.strongloop.com/display/APIC/Using%20current%20context'));
};
12 changes: 6 additions & 6 deletions server/middleware/rest.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

var loopback = require('../../lib/loopback');
var async = require('async');
var deprecate = require('depd')('loopback');
var g = require('strong-globalize')();

/*!
* Export the middleware.
Expand Down Expand Up @@ -40,11 +40,11 @@ function rest() {
var remotingOptions = app.get('remoting') || {};

var contextOptions = remotingOptions.context;
if (contextOptions !== false) {
if (typeof contextOptions !== 'object') {
contextOptions = {};
}
handlers.push(loopback.context(contextOptions));
if (contextOptions !== undefined && contextOptions !== false) {
throw new Error(g.f(
'%s was removed in version 3.0. See %s for more details.',
'remoting.context option',
'https://docs.strongloop.com/display/APIC/Using%20current%20context'));
}

if (app.isAuthEnabled) {
Expand Down
2 changes: 1 addition & 1 deletion server/middleware/token.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ function token(options) {
TokenModel.findForRequest(req, options, function(err, token) {
req.accessToken = token || null;
rewriteUserLiteral(req, currentUserLiteral);
var ctx = loopback.getCurrentContext();
var ctx = req.loopbackContext;
if (ctx) ctx.set('accessToken', token);
next(err);
});
Expand Down
Loading