reduxjs / redux-toolkit

The official, opinionated, batteries-included toolset for efficient Redux development
https://redux-toolkit.js.org
MIT License
10.7k stars 1.17k forks source link

Exporting a default response handler for fetchBaseQuery #4544

Open imp-dance opened 2 months ago

imp-dance commented 2 months ago

It seems to me that the default response handler for fetchBaseQuery is "json". As far as I can tell, this is equivalent to parsing the response as text, and then doing JSON.parse on the text after checking its' length. This is not really documented properly anywhere to my knowledge, I figured it out by scanning through the source code.

My original assumption was that the default response handler was async (res) => await res.json()

When implementing a wrapper response handler it's a bit cumbersome and hard to figure out that what I need to actually reimplement to mirror the behavior is this:

responseHandler: async (res) => {
   const text = await res.text();
   return text.length ? JSON.parse(text) : null;
}

I suggest exporting the above function as a defaultResponseHandler, so that wrappers can be easily created as such:

responseHandler: async (res) => {
   // do some stuff
   return await defaultResponseHandler(res);
}
imp-dance commented 2 months ago

Opened a PR to just add the default handler directly to the docs. That would also suffice as an alternative as far as I care. Thoughts?