Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

feat($resource): add support for request and requestError interceptors #15674

Merged
merged 2 commits into from
Dec 18, 2017

Conversation

gkalpak
Copy link
Member

@gkalpak gkalpak commented Feb 3, 2017

Just a rebased version of #13273.

@googlebot
Copy link

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 cla/google commit status will not change from this state. It's up to you to confirm consent of the commit author(s) and merge this pull request when appropriate.

1 similar comment
@googlebot
Copy link

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 cla/google commit status will not change from this state. It's up to you to confirm consent of the commit author(s) and merge this pull request when appropriate.

Copy link
Contributor

@petebacondarwin petebacondarwin left a 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.

$httpBackend.flush();
expect(successSpy).toHaveBeenCalledOnce();
expect(failureSpy).not.toHaveBeenCalled();
});
Copy link
Contributor

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?

Copy link
Member Author

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).

Copy link
Contributor

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

Copy link
Member Author

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);
Copy link
Contributor

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.

Copy link
Member Author

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?

Copy link
Contributor

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.

Copy link
Member Author

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 😉

Copy link
Contributor

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.

Copy link
Contributor

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!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gkalpak I'll have to check, but since #11409 is still open I assumed the docs are not clear, or a workaround for this is not covered by the docs.

Copy link
Contributor

@Narretz Narretz Dec 8, 2017

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)

Copy link
Member Author

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).

Copy link
Contributor

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.

@gkalpak
Copy link
Member Author

gkalpak commented Dec 7, 2017

PTAL.

Copy link
Contributor

@petebacondarwin petebacondarwin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Michael Krotscheck and others added 2 commits December 18, 2017 14:14
…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(...);
});
```
@petebacondarwin petebacondarwin merged commit 240a3dd into angular:master Dec 18, 2017
@gkalpak gkalpak deleted the rebase-13273 branch January 3, 2018 22:42
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants