-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Wildcard remote hooks for relation methods #737
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
Comments
@bajtos do you have any experience in this case ? |
I don't think there is a way for specifying a wildcard hook for relation methods. |
thanks, it works. |
Does this mean, that in order to have a consistent API, if I have a remote hook to sharedBooks create, I have to create as many remote hooks as many relation I have to it? Can this be fixed sometime in the future? |
Yes, that's a correct assessment of the current implementation. BTW have you tried using the recently added Operation Hooks instead of remoting hooks? I believe Operation Hooks are correctly invoked for relation methods too.
Well, I'd like to see this fixed too, but the solution gets tricky pretty quickly. Let's say you have "Product hasAndBelongsToMany Categories" and you want to create a remote hook for the "link" method, i.e. Another example: "Product hasMany Categories" and making the request "GET /api/products/1/categories/2". Should we invoke remoting hooks for I'll reopen this issue to keep the discussion going. |
Operation hooks does seem to work correctly, and correct me if I'm wrong, but I didn't seem to find a way to access accessToken or session data inside observe. My exact usecase is that I want to have createdAt, createdBy, modifiedAt, modifiedBy properties, the
And I might set ACLs so you can't access /api/order/{order_id}, only /api/users/{my_user_id}/orders/{order_id}. I think this makes sense, and is the correct use if I don't want other people to access other's orders. Now I don't want to reimplement the same logic when an order is created from different REST endpoints - I don't care which endpoint was called to create an order. It may have been I'm not very familiar with the link method, so I wouldn't know. As for |
That's correct. I think we don't have a standardized way for passing accessToken to operation hooks and model methods yet. There are two possible solutions:
// IIRC, remote hooks are not inherited, you have to set it up on every model instance
// The easiest solution is to override setup() of LoopBack core Model.
var _setup = loopback.Model.setup;
loopback.Model.setup = function() {
_setup.apply(this, arguments);
this.beforeRemote('**', function(ctx, unused, cb) {
ctx.args.options = ctx.args.options || {};
ctx.args.options.accessToken = ctx.req.accessToken;
cb();
});
};
// now in your common/models/order.js
Order.observe('before save', function(ctx, next) {
var token = ctx.options && ctx.options.accessToken;
if (!token) return next(new Error('Authorization is required.'));
// make your changes
next();
}); Please note that I haven't tested the code, it may need few tweaks to get it working I am cc-ing @raymondfeng and @ritch, they may be able to provide additional information on how to access the current accessToken and/or the current user from an operation hook and/or a model method. |
Hi, I have a quite similar problem. Lets say I have Thanks, David |
Hey @tekand , just wondering have you found the solution to the problem above?? I am looking for a similar solution |
@n2sandhu nope, still unsolved. :( |
I am also looking for a way to observe when a new relation is created. Can't find anything in the docs. |
I would like to do the same, a way to observe when a link is created. |
Thanks for the poke, @pulkitsinghal. @tekand @moklick @jwebcat please open a new issue to discuss this question. Let's keep the discussion here focused on remote hooks for relation methods. |
When I put a remote hook on Favorite.afterRemote('find') I expect it will execute when I access /profiles/1/favorites. Can we introduce this as a feature @bajtos? |
Yes, that's what we all would expect. Unfortunately, the way how relation methods are exposed via strong-remoting/REST makes it very difficult to implement such desired behaviour.
Sure, you are welcome to give it a try! See http://loopback.io/doc/en/contrib/index.html to get started. Just be warned, this feature is tricky and complex to implement (at least I think so). |
@bajtos where can find doc about relation model remote hooks , I did not find the format '*.get__sharedBooks' |
@bajtos hey! Sorry to bubble up this issue again... but I read your comment #737 (comment), and noticed that was back on 2014, sooo I was wondering if this is still the case? I tried it and didn't work at all with LB 3. For now, I'm making custom endpoints so this is not super urgent but something I'd like to know how to do. Thanks!!! |
Yes, this issue is fortunately still not addressed. Considering our focus on LB 4, I don't expect this issue to be fixed in LB 3.x ever 🙁 |
My case is:
|
Example:
I make some requests :
POST /Books/{id}/sharedBooks
GET /Books/{id}/sharedBooks/{fk}
DELETE /Books/{id}/sharedBooks/{fk}
As my example you can see the code in SharedBook model like this:
But i can't receive any text message from console log, how can i hook to beforeRemote in this case ?
Please help me, thanks much.
The text was updated successfully, but these errors were encountered: