-
Notifications
You must be signed in to change notification settings - Fork 27.4k
feat($resource): add support for request
and requestError
interceptors
#15674
Conversation
So there's good news and bad news. 👍 The good news is that everyone that needs to sign a CLA (the pull request submitter and all commit authors) have done so. Everything is all good there. 😕 The bad news is that it appears that one or more commits were authored by someone other than the pull request submitter. We need to confirm that they're okay with their commits being contributed to this project. Please have them confirm that here in the pull request. Note to project maintainer: This is a terminal state, meaning the |
1 similar comment
So there's good news and bad news. 👍 The good news is that everyone that needs to sign a CLA (the pull request submitter and all commit authors) have done so. Everything is all good there. 😕 The bad news is that it appears that one or more commits were authored by someone other than the pull request submitter. We need to confirm that they're okay with their commits being contributed to this project. Please have them confirm that here in the pull request. Note to project maintainer: This is a terminal state, meaning the |
4ca68a2
to
0416afe
Compare
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.
One question and one minor suggestion.
test/ngResource/resourceSpec.js
Outdated
$httpBackend.flush(); | ||
expect(successSpy).toHaveBeenCalledOnce(); | ||
expect(failureSpy).not.toHaveBeenCalled(); | ||
}); |
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.
Do you need a $httpBackend.verifyNoOutstandingRequest();
here to check that the request is actually made or will the expect(...)
cause an error if the spec completes without this being requested?
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.
There is a verifyNoOutstandingExpectation()
after each test to verify that the request was made.
But I think you are right, we could be more specific in our expectations (e.g. verify call args).
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.
Did I ever mention that I don't like before/afterEach
:-P
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.
Yup 😇
resolve(httpConfig). | ||
then(requestInterceptor). | ||
catch(requestErrorInterceptor). | ||
then($http); |
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.
Interested to know why you "reimplemented" request interceptors here rather than modifying the httpConfig
to add the interceptors to the request httpConfig
(or a clone of it) and rely upon $http
to deal with the intercepting.
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.
Currently $http
does not support per-request interceptors. Are you suggesting add that as a new feature to $http
(and then use it to implement $resource
request interceptors?
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 would actually be my preferred solution, as the resource interceptors do not work 100% like httpInterceptors anyway (see #11409. Note that even though #13273 is linked in this issue as a solution, adding request/Error interceptors does not actually solve the issue); and because http transforms do not support changing all variables (headers cannot be changed, see #12095)
So you would be able to set interceptors
in the http config and httpInterceptors
in ngResource.
Obviously this would be a much bigger change which would make http much more complex.
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 would actually be my preferred solution, as the resource interceptors do not work 100% like httpInterceptors anyway
$resource
response interceptors are special, so indeed do not work exactly as $http
interceptors. But request interceptors work pretty much the same afaict.
So you would be able to set interceptors in the http config and httpInterceptors in ngResource.
Obviously this would be a much bigger change which would make http much more complex.
I agree with both observations 😉
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.
If we go with requestInterceptors (fine with me), then we should take this opportunity to document responseInterceptors of resource and the differences to http behavior, preferrably in the same or a follow up PR.
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.
I didn't realise that we couldn't do per-request interceptors in $http
!
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.
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.
Just saw there's an extra issue for the inconsistency in response intereceptors: #9334 (also in 1.7)
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.
#9334, is about inconsistency between response
/responseError
interceptors in $resource
itself (not about inconsistency against $http
interceptors). #9334 has valid points (that are probably still worth doing something about for 1.7), but is not related to request
/requestError
interceptors (which are doing the "right thing"™ afaict).
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.
Yeah, it's not directly related. I just thought it could be a good time to clarify any docs in general. But it's not a must.
PTAL. |
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.
LGTM
…ptors This commit adds `request` and `requestError` interceptors for `$resource`, 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 `$http` indicates that the resource itself has made a decision, and that it accepts the responsibility for recovery. Closes angular#5146 BREAKING CHANGE: Previously, calling a `$resource` method would synchronously call `$http`. Now, it will be called asynchronously (regardless if a `request`/`requestError` interceptor has been defined. This is not expected to affect applications at runtime, since the overall operation is asynchronous already, but may affect assertions in tests. For example, if you want to assert that `$http` has been called with specific arguments as a result of a `$resource` call, you now need to run a `$digest` first, to ensure the (possibly empty) request interceptor promise has been resolved. Before: ```js it('...', function() { $httpBackend.expectGET('/api/things').respond(...); var Things = $resource('/api/things'); Things.query(); expect($http).toHaveBeenCalledWith(...); }); ``` After: ```js it('...', function() { $httpBackend.expectGET('/api/things').respond(...); var Things = $resource('/api/things'); Things.query(); $rootScope.$digest(); expect($http).toHaveBeenCalledWith(...); }); ```
82196c4
to
173db93
Compare
Just a rebased version of #13273.