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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to pass in an arbitrary object as state from request to response or error? #59

Open
falcoagustin opened this issue Jul 5, 2021 · 3 comments
Labels

Comments

@falcoagustin
Copy link

falcoagustin commented Jul 5, 2021

Greetings! First of all, thank you for all your effort on the library, works great 馃槃

I am using version: 2.4.0.

I am using the interceptors to measure response time, so wanted to know a way I could pass in some kind of object as state between the request and the response functions. I am looking for something like this:

fetchIntercept.register({
    request: function (url, config) {
        return [url, config, {state: 'Good!'}]
    },
    response: async function (response, state) {
        console.log(state) // logs {state: "Good!"}
        return response
    }
})

My request comes that if I store the state inside a file, I'll get the values overriden when different respond at different times.

Any way to walk this around? It could also work to access the state from within the response object, as we do with the request object.

Thanks!

@mlegenhausen
Copy link
Owner

Forget my previous answer the problem is the url and config get merged in a fetch#Request object. I think to realize this the current solution needs to use CLS.

@falcoagustin
Copy link
Author

No worries!
Can't a third argument be passed by in between functions?
I created a fork that does that and does the trick, but I think it'd be better if I can use it already with the lib.
Let me know your thoughts!

@ruifcnunes
Copy link

Hey! I believe you can pass your state object to the config on your request, and then on your response and error interceptors you can access it via response.request.{your_state_object} and error.request.{your_state_object}, respectively.

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

3 participants