GithubHelp home page GithubHelp logo

Bug: Async XHR Response about requestly HOT 5 OPEN

anlinguist avatar anlinguist commented on May 28, 2024
Bug: Async XHR Response

from requestly.

Comments (5)

anlinguist avatar anlinguist commented on May 28, 2024

@wrongsahil any ideas on this? Async fetch works no problem but I can't get async xhr to work properly.

from requestly.

wrongsahil avatar wrongsahil commented on May 28, 2024

Hey @anlinguist. Sorry for the delay in response. Currently, we only support async code in requests triggered using fetch. XHR isn’t supported at the moment.

We're discussing and testing a potential solution to enable support for XHR as well. We'll keep you posted on any developments.

from requestly.

anlinguist avatar anlinguist commented on May 28, 2024

hey, thanks for getting back to me @wrongsahil. For my purposes, I was able to proxy and monkey patch XHR to allow async operations on the XHR response data.

In short, my approach pauses the original XHR request before it is sent, creates and runs a fetch request from it, takes the fetch request response and freely applies async operations on it, resumes the original XHR request, and overrides the XHR response using the now available response from the fetch operation. Here's how I did it:

  1. Save a reference to the original XMLHttpRequest object and its methods (send, open, setRequestHeader).
  2. Override the global XMLHttpRequest object with a custom constructor function.
  3. Inside the custom constructor, capture the request details (URL, method, headers) by overriding the open and setRequestHeader methods and storing them in a requestDetails object.
  4. Override the send method to defer sending the request. It attempts to retrieve a response rule based on the request URL, request data, and method.
  5. If a response rule is found, it enters an async logic; otherwise, it sends the request as-is.
  6. In the async logic, a separate fetch request is made to the original URL using the captured request details. The response is then modified based on the response rule's modification function, if specified.
  7. The modified response is stored in a responseOverrides object, and getters are defined for the response, responseText, status, and statusText properties of the xhr object to return the modified response.
  8. Finally, the original send method is called to resume the original XHR request, but with the overridden response properties returning the modified response to the caller.

Of course, this approach does make an extra request to the original URL in order to fetch and modify the response data. This additional request may have performance implications, especially if the response modifications are extensive or if there are a large number of XHR requests being intercepted. For my purposes, this is acceptable.

from requestly.

lazyvab avatar lazyvab commented on May 28, 2024

@anlinguist If API request is not idempotent in nature, firing it multiple times may lead to unexpected results. For instance, a POST request which creates new data on server may create it multiple times.

from requestly.

anlinguist avatar anlinguist commented on May 28, 2024

@lazyvab Yes, that's true, so for many usecases, it may not be an ideal solution. If the request you're targeting is an XHR and ends up creating/updating/deleting data on a server, this would not be an ideal approach.

However, if the request you're targeting is for obtaining data from a server, it works. For my purposes, this was an acceptable workaround - I have rules around which XHR requests duplicate the request using the fetch request approach, and all of my rules really focus on getting data from the server rather than posting/putting/deleting/etc.

Due to the synchronous nature of XHR's readystatechange, I could not figure out another way to perform asynchronous logic directly on the XHR request.

from requestly.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.