-
Notifications
You must be signed in to change notification settings - Fork 27.4k
Add request and requestError interception in ngResource #13273
Conversation
Thanks for your pull request. It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). 📝 Please visit https://cla.developers.google.com/ to sign. Once you've signed, please reply here (e.g.
|
CLAs look good, thanks! |
@@ -593,7 +601,13 @@ angular.module('ngResource', ['ng']). | |||
extend({}, extractParams(data, action.params || {}), params), | |||
action.url); | |||
|
|||
var promise = $http(httpConfig).then(function(response) { | |||
var promise = $q.resolve(httpConfig); |
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.
Why not directly: $q.when(requestInterceptor(httpConfig), null, requestErrorInterceptor)
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.
Well, that's how my brain addressed the problem. First, peel everything into a promise chain. Second, add the desired step into that chain. While I can collapse it easily enough, I personally find it more legible, as each statement is discrete and easily understood.
I don't mind updating the commit though, if there's consensus that your proposed syntax is more desirable.
As an additional note: The reason I'd like to land this in angular, is because I'd like to build resources who get their root API url from a promise.
Since I live in a multi-api environment, resolving this in the resource made the most sense. |
@petebacondarwin Heya- with 1.5 out now, what are the next steps for this PR? Should I rebase, update, add docs... any guidance? |
@krotscheck, now that 1.5.0 is out (and since this is on the 1.6.x milestone), I will take a look soon and get back to you. You don't need to rebase for now. I'll let you know if you need to update anything. Thx for working on this ! |
@gkalpak Awesome! Looking forward to your comments :) |
@@ -664,7 +672,9 @@ angular.module('ngResource', ['ng']). | |||
extend({}, extractParams(data, action.params || {}), params), | |||
action.url); | |||
|
|||
var promise = $http(httpConfig).then(function(response) { | |||
var promise = $q.when(requestInterceptor(httpConfig), null, requestErrorInterceptor); |
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.
This will not catch errors thrown in the requestInterceptor
. The requestErrorInterceptor
will only be called if requestInterceptor
returns a rejecting promise.
I think the following is better:
$q.when(httpConfig).then(requestInterceptor).catch(requestErrorInterceptor)
@krotscheck, I left a couple of comments. I think it's a good change overall, but I would like to have more tests. E.g.:
Feel free to ping me when/if you make any changes, so I can take another look. BTW, there is some ongoing discussion about response interceptors (or interceptors in general) in the context of |
@gkalpak Tests added, feedback incorporated. I also concatenated the promise chain, which had the unexpected impact of creating a gross diff (indentation had to be updated). I can go back and separate the two if you'd like me to. |
@krotscheck, thx ! The diff is fine if you ignore whitespace 😉 |
|
||
// Ensure the resource promise was rejected | ||
expect(card.$resolved).toBeTruthy(); | ||
expect(card.$promise.$$state.status).toEqual(2); |
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.
This is private API surface. We don't need to verify that. (It might change anytime anyway.)
I left a couple of comments. Additionally, where applicable, I think it's a good idea to use success/error callbacks to verify that the appropriate callback has been called in each case. |
@gkalpak Alright, the most recent patch has the changes you asked for. |
I left some minor comments, but generally LGTM. WRT #13273 (comment), I think we should do it. |
Okies, I'm on it. |
This patch adds request and requestError interception for ngResource actions, as per the documentation found for $http interceptors. It is important to note that returning an error at this stage of the request - before the call to $http - will completely bypass any global interceptors and/or recovery handlers, as those are added to a separate context. This is intentional; intercepting a request before it is passed to the HTTP stack indicates that the resource itself has made a decision, and that it accepts the responsibility for recovery. Closes #5146
Theeeere we go. Was temporarily derailed by #13123, but used the workaround for great justice. |
@gkalpak Hey there! It's been a month since my last patch, which incorporates all the changes you asked for. Is there anything else we're missing? |
@krotscheck: Time 😃 I still wish to get the ngResource interceptors/transforms story fixed for 1.6, but I have to talk people into this 😛 |
BTW, this should also fix #12095. I would be nice to add a testcase for that (i.e. testing that the request interceptor will be able to modify the headers). |
Oh, all you need is time? I had some of that lying around here somewhere... now where did it... hold on... drat! A few questions:
|
The aim is to release 1.6 in September. So there is time (of one kind or another) but perhaps not the kind of time that you need :-) |
If this is a response to my "I have to talk people into this", I meant I people on the ng1 team 😃
The latter
Review --> LGTM --> LGT another team member --> Merge
We don't merge - we rebase 😛
I hope not.
It's not necessary, but people have reported it helps 😛 |
@gkalpak When do you think this will be released? Currently it's not possible to transform the request headers with $resource dynamically.
Declaration above doesn't work. It transforms the header but the final request does not have these new headers. I ended up wrapping the $resource with another function as mentioned here : http://stackoverflow.com/questions/20566593/dynamic-resource-headers Thanks for the PR @krotscheck |
The milestone is |
isArray: true, | ||
interceptor: { | ||
request: function(httpConfig) { | ||
callback(httpConfig); |
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.
Is the callback defined somewhere?
There's now a merge conflict (due to 39a90a9 I think). Must be resolved, but should be easy. Can you update the PR @krotscheck ? |
Sadly, I've switched jobs. You'll need to find someone else to update this patch. |
Ok. Thanks for the head's up
|
I wrote this in Slack but it must have been overlooked: I know we talked previously about how the interceptor API is different between ngResource and $http. Did you take this into consideration when approving the PR @gkalpak ? I wonder if we could theoretically move the intereption to $http. Basically, have request-level interceptors in http, which are followed by the app-global interceptors? We don't have to do this in this PR, I just don't want to introduce an API that is blocking us in the future |
Yeah, I have proposed something similar here and there. My "problem" with the current implementation is that is is too Even if we decided to make this an So, I think we are good. |
Regarding I guess you mean, "would not make it more difficult"? |
That's what I said 😛 |
Here is the rebased version btw: #15674 |
Closing in favour of #15674 |
This patch adds request and requestError interception for ngResource actions,
as per the documentation found for $http interceptors. It is important to
note that returning an error at this stage of the request - before the
call to $http - will completely bypass any global interceptors and/or recovery
handlers, as those are added to a separate context. This is intentional; intercepting
a request before it is passed to the HTTP stack indicates that the resource itself
has made a decision, and that it accepts the responsibility for recovery.
Closes #5146