From 1190d0f499a0cd98c7d93e2fac7d7cf3a6925d2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5kon=20Underbakke?= Date: Mon, 5 Aug 2024 10:54:14 +0200 Subject: [PATCH 1/2] docs: Added section describing the default response handler for fetchBaseQuery --- docs/rtk-query/api/fetchBaseQuery.mdx | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/docs/rtk-query/api/fetchBaseQuery.mdx b/docs/rtk-query/api/fetchBaseQuery.mdx index a01e84b389..4bdccdac3a 100644 --- a/docs/rtk-query/api/fetchBaseQuery.mdx +++ b/docs/rtk-query/api/fetchBaseQuery.mdx @@ -297,6 +297,17 @@ export const customApi = createApi({ If you make a `json` request to an API that only returns a `200` with an undefined body, `fetchBaseQuery` will pass that through as `undefined` and will not try to parse it as `json`. This can be common with some APIs, especially on `delete` requests. ::: +#### Default response handler + +The default response handler is `"json"`, which is equivalent to the following function: + +```ts title="Default responseHandler" +const defaultResponseHandler = async (res) => { + const text = await res.text(); + return text.length ? JSON.parse(text) : null; +} +``` + ### Handling non-standard Response status codes By default, `fetchBaseQuery` will `reject` any `Response` that does not have a status code of `2xx` and set it to `error`. This is the same behavior you've most likely experienced with `axios` and other popular libraries. In the event that you have a non-standard API you're dealing with, you can use the `validateStatus` option to customize this behavior. From af16c4a829888e42efe9ac5f52261de9588f7b75 Mon Sep 17 00:00:00 2001 From: Ben Durrant Date: Fri, 13 Sep 2024 11:39:24 +0100 Subject: [PATCH 2/2] Update docs/rtk-query/api/fetchBaseQuery.mdx --- docs/rtk-query/api/fetchBaseQuery.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/rtk-query/api/fetchBaseQuery.mdx b/docs/rtk-query/api/fetchBaseQuery.mdx index 4bdccdac3a..79420f7032 100644 --- a/docs/rtk-query/api/fetchBaseQuery.mdx +++ b/docs/rtk-query/api/fetchBaseQuery.mdx @@ -302,7 +302,7 @@ If you make a `json` request to an API that only returns a `200` with an undefin The default response handler is `"json"`, which is equivalent to the following function: ```ts title="Default responseHandler" -const defaultResponseHandler = async (res) => { +const defaultResponseHandler = async (res: Response) => { const text = await res.text(); return text.length ? JSON.parse(text) : null; }