fetch-intercept
fetch-intercept copied to clipboard
How to pass in an arbitrary object as state from request to response or error?
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!
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.
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!
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.