Skip to content
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

How to read request method from response #15

Open
windwalker opened this issue Nov 13, 2017 · 2 comments
Open

How to read request method from response #15

windwalker opened this issue Nov 13, 2017 · 2 comments
Labels

Comments

@windwalker
Copy link

I'm writing a hook on fetch response. I need both the URL and the method of the request to decide whether the hook should be called or not.

However, in the response hook, I can only get URL. Do you know where could I get the method('post', 'get', 'put', etc.) that the request is using in the response hook?

fetchIntercept.register({
  response: function(response) {
      if(response.url === predefinedURL   &&   response.method /* I don't know how to get it */  === predefinedMethod) {
           //do something here
      }
  }
});

Many Thanks

@mlegenhausen
Copy link
Owner

This is currently not possible you can take a look at the angular $http inteceptors how they doing something like this.

@windwalker
Copy link
Author

Thanks. I guess the only way around is to proxy fetch. I end up writing some code like this:

window.fetch = (function(oldFetch) {
  if (oldFetch.__mocked__) {
        return oldFetch
  } else {
        var mock = function(url, config) {
              var custom = {url, config};
              return oldFetch(url, config).then(function(res) {
                    res.__params__ = custom;
                    return res;
             });
        mock.__mocked__ = true;
        return mock;
  }
})(window.fetch)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants