You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is a very small wrapper around [`fetch`](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) that aims to simplify requests. It is not a full-blown replacement for `axios`, `superagent`, or any other more heavy-weight library, but it will cover the large majority of your needs.
14
+
This is a very small wrapper around [`fetch`](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) that aims to simplify HTTP requests. It is not a full-blown replacement for `axios`, `superagent`, or any other more heavyweight library, but it will cover the vast majority of your HTTP request needs.
15
15
16
-
It takes all standard options from fetch's [`RequestInit`](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch) interface, as well as `baseUrl`, a `prepareHeaders` function, an optional `fetch` function, a `paramsSerializer` function, and a `timeout`.
16
+
`fetchBaseQuery` is a factory function that generates a data fetching method compatible with RTK Query's `baseQuery` confiugration option. It takes all standard options from fetch's [`RequestInit`](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch) interface, as well as `baseUrl`, a `prepareHeaders` function, an optional `fetch` function, a `paramsSerializer` function, and a `timeout`.
17
17
18
-
-`baseUrl`_(required)_
19
-
- Typically a string like `https://api.your-really-great-app.com/v1/`. If you don't provide a `baseUrl`, it defaults to a relative path from where the request is being made. You should most likely _always_ specify this.
20
-
-`prepareHeaders`_(optional)_
18
+
## Basic Usage
21
19
22
-
- Allows you to inject headers on every request. You can specify headers at the endpoint level, but you'll typically want to set common headers like `authorization` here. As a convenience mechanism, the second argument allows you to use `getState` to access your redux store in the event you store information you'll need there such as an auth token. Additionally, it provides access to `extra`, `endpoint`, `type`, and `forced` to unlock more granular conditional behaviors.
- A function that can be used to apply custom transformations to the data passed into [`params`](#setting-the-query-string). If you don't provide this, `params` will be given directly to `new URLSearchParms()`. With some API integrations, you may need to leverage this to use something like the [`query-string`](https://github.com/sindresorhus/query-string) library to support different array types.
39
-
-`fetchFn`_(optional)_
40
-
- A fetch function that overrides the default on the window. Can be useful in SSR environments where you may need to leverage `isomorphic-fetch` or `cross-fetch`.
41
-
-`timeout`_(optional)_
42
-
- A number in milliseconds that represents the maximum time a request can take before timing out.
43
-
44
-
```ts title="Return types of fetchBaseQuery" no-transpile
45
-
Promise<{
46
-
data:any;
47
-
error?:undefined;
48
-
meta?: { request:Request; response:Response };
49
-
} | {
50
-
error: {
51
-
status:number;
52
-
data:any;
53
-
};
54
-
data?:undefined;
55
-
meta?: { request:Request; response:Response };
56
-
}>
57
-
```
58
-
59
-
### Using `fetchBaseQuery`
60
-
61
-
To use it, import it when you are [creating an API service definition](../../tutorials/rtk-query#create-an-api-service).
20
+
To use it, import it when you are [creating an API service definition](../../tutorials/rtk-query#create-an-api-service), call it as `fetchBaseQuery(options)`, and pass the result as the `baseQuery` field in `createApi`:
Typically a string like `https://api.your-really-great-app.com/v1/`. If you don't provide a `baseUrl`, it defaults to a relative path from where the request is being made. **You should most likely _always_ specify this**.
103
+
104
+
### `prepareHeaders`
105
+
106
+
_(optional)_
107
+
108
+
Allowsyoutoinjectheadersoneveryrequest. Youcanspecifyheadersattheendpointlevel, butyou'll typically want to set common headers like `authorization` here. As a convenience mechanism, the second argument allows you to use `getState` to access your redux store in the event you store information you'llneedtheresuchasanauthtoken. Additionally, itprovidesaccessto`extra`, `endpoint`, `type`, and`forced`tounlockmoregranularconditionalbehaviors.
Afunction that can be used to apply custom transformations to the data passed into [`params`](#setting-the-query-string). If you don't provide this, `params` will be given directly to `new URLSearchParms()`. With some API integrations, you may need to leverage this to use something like the [`query-string`](https://github.com/sindresorhus/query-string) library to support different array types.
@@ -189,7 +262,19 @@ By default, `fetchBaseQuery` assumes that every request you make will be `json`,
189
262
190
263
### ParsingaResponse
191
264
192
-
By default, `fetchBaseQuery` assumes that every `Response` you get will be parsed as `json`. In the event that you don't want that to happen, you can specify an alternative response handler like `text`, or take complete control and use a custom function that accepts the raw `Response` object — allowing you to use any [`Response` method](https://developer.mozilla.org/en-US/docs/Web/API/Response).
265
+
Bydefault, `fetchBaseQuery`assumesthatevery`Response`yougetwillbeparsedas`json`. Intheeventthatyoudon't want that to happen, you can customize the behavior by specifying an alternative response handler like `text`, or take complete control and use a custom function that accepts the raw `Response` object — allowing you to use any [`Response` method](https://developer.mozilla.org/en-US/docs/Web/API/Response).
timeout: 1000, // We know the users endpoint is _really fast_ because it's always cached. We can assume if its over > 1000ms, something is wrong and we should abort the request.
338
+
// Example: we know the users endpoint is _really fast_ because it's always cached.
339
+
// We can assume if its over > 1000ms, something is wrong and we should abort the request.
* An optional predicate function to determine if `JSON.stringify()` should be called on the `body` arg of `FetchArgs`
178
178
*
179
-
* @param {string} jsonContentType Defaults to `application/json`. Used when automatically setting the content-type header for a request with a jsonifiable body that does not have an explicit content-type header.
179
+
* @param {string} jsonContentType Used when automatically setting the content-type header for a request with a jsonifiable body that does not have an explicit content-type header. Defaults to `application/json`.
180
180
*
181
181
* @param {number} timeout
182
182
* A number in milliseconds that represents the maximum time a request can take before timing out.
0 commit comments