-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Inject remoting context to options arg #2762
Conversation
Create a function that can be used in definition of "accepts" arguments to build an "options" argument from the request context. The "options" object built by this function has the following properties: - `options.remotingContext` is the instance of strong-remoting context that typically contains `req` and `res` objects along other data. - `options.currentUserId` is the id of the user making the request or `null` for anonymous requests. - `options.accessToken` is the access-token used to authenticate the client or `null` for anonymous requests.
description: 'Model instance data', | ||
http: { source: 'body' }, | ||
}, | ||
{ arg: 'options', type: 'object', http: optionsFromContext }, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will this be handled correctly by the swagger generation side of things? Is there something we can add here to hint that this argument is not an API parameter?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// Filter out parameters that are generated from the incoming request,
// or generated by functions that use those resources.
accepts = accepts.filter(function(arg) {
if (!arg.http) return true;
// Don't show derived arguments.
if (typeof arg.http === 'function') return false;
// Don't show arguments set to the incoming http request.
// Please note that body needs to be shown, such as User.create().
if (arg.http.source === 'req' ||
arg.http.source === 'res' ||
arg.http.source === 'context') {
return false;
}
return true;
});
|
||
function buildOptionsFromRemotingContext(ctx) { | ||
var accessToken = ctx.req.accessToken; | ||
var options = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be great if ctx.options
was extensible through a model or mixin. Eg. being able to implement the constructor of the options
object.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1, minxin is great. Actually, most of my scenarios need currentUser with full property, not only currentUserId. someone maybe another.
Looks good so far, but I'd like to see the docs before final sign off. |
Small complaint - I realize that the docs claim it's unimplemented, however we've been happily declaring our remote methods inside the Model JSON files for some time and I am aware of other projects doing the same. We would run into problems/annoyances here w/ the need to specify a function handle for Why not expand the vocabulary slightly, allowing |
Another thought - what about middleware access to |
@@ -14,6 +14,7 @@ var RemoteObjects = require('strong-remoting'); | |||
var SharedClass = require('strong-remoting').SharedClass; | |||
var extend = require('util')._extend; | |||
var format = require('util').format; | |||
var optionsFromContext = require('./options-from-context'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The optionsFromContext
variable should be removed and use loopback.optionsFromContext directly.
The user could customize the loopback,optionsFromContext
via replacing this.
Thank you all for great feedback!
What if the It should make it easier to declare the
In my view, there is no such concept as I'll think about this more.
Right now, middleware operates at much lower level of abstraction. It can access One can pass data from middleware to options this way:
Setting full However, once we allow customization of |
I like that it solves my problem. It feels a little weird for
Ok, yeah I can see what you mean.
This sounds appealing and somehow more "whole" of a solution.
I agree with you that that's the right way. However, middleware already has access to the full request context (
What feels wrong here is that this requires us to build a customized A final question/thought - is it perhaps wrong for |
Closing in favour of #3023 |
In this pull request, I am proposing a solution for Injecting Remote Context via Options:
Methods accepting an
options
argument should declare this argument in their remoting metadata and use a special methodloopback.optionsFromContext
as thehttp
mapping. This way there is no magic happening inbeforeRemote
hooks and method authors are in full control of how theoptions
argument is (or is not) initialized.An example using trimmed-down metadata of
PersistedModel.create
:The method
optionsFromContext
returns anoptions
object with the following properties:remotingContext
- the full context as provided by strong-remoting, typically an instance ofHttpContext
accessToken
- the access token parsed from the request ornull
currentUserId
- the id of the user making the request,null
for anonymous requestsAn example operation observer accessing the current user:
Tasks
optionsFromContext
PersistedModel
methodsRequires strongloop/strong-remoting#355 to prevent timeout failures in unit-tests.
Connect to #1495
@ritch please review