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.
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, and a `paramsSerializer` function.
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`.
17
17
18
18
-`baseUrl`_(required)_
19
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.
@@ -38,6 +38,8 @@ It takes all standard options from fetch's [`RequestInit`](https://developer.moz
38
38
- 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
39
-`fetchFn`_(optional)_
40
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.
41
43
42
44
```ts title="Return types of fetchBaseQuery" no-transpile
43
45
Promise<{
@@ -114,6 +116,7 @@ There is more behavior that you can define on a per-request basis that extends t
By default, `fetchBaseQuery` has no default timeout value set, meaning your requests will stay pending until your api resolves the request(s) or it reaches the browser's default timeout (normally 5 minutes). Most of the time, this isn't what you'll want. When using `fetchBaseQuery`, you have the ability to set a `timeout` on the `baseQuery` or on individual endpoints. When specifying both options, the endpoint value will take priority.
baseQuery: fetchBaseQuery({ baseUrl: '/api/', timeout: 10000 }), // Set a default timeout of 10 seconds
244
+
endpoints: (builder) => ({
245
+
getUsers: builder.query({
246
+
query: () => ({
247
+
url: `users`,
248
+
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.
* An optional predicate function to determine if `JSON.stringify()` should be called on the `body` arg of `FetchArgs`
181
195
*
182
196
* @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.
197
+
*
198
+
* @param {number} timeout
199
+
* A number in milliseconds that represents the maximum time a request can take before timing out.
183
200
*/
184
201
exportfunctionfetchBaseQuery({
185
202
baseUrl,
@@ -188,6 +205,7 @@ export function fetchBaseQuery({
188
205
paramsSerializer,
189
206
isJsonContentType =defaultIsJsonContentType,
190
207
jsonContentType ='application/json',
208
+
timeout: defaultTimeout,
191
209
...baseFetchOptions
192
210
}: FetchBaseQueryArgs={}): BaseQueryFn<
193
211
string|FetchArgs,
@@ -212,6 +230,7 @@ export function fetchBaseQuery({
212
230
params =undefined,
213
231
responseHandler ='json'asconst,
214
232
validateStatus =defaultValidateStatus,
233
+
timeout =defaultTimeout,
215
234
...rest
216
235
}=typeofarg=='string' ? {url: arg} : arg
217
236
letconfig: RequestInit={
@@ -256,11 +275,26 @@ export function fetchBaseQuery({
256
275
constrequestClone=request.clone()
257
276
meta={request: requestClone}
258
277
259
-
letresponse
278
+
letresponse,
279
+
timedOut=false,
280
+
timeoutId=
281
+
timeout&&
282
+
setTimeout(()=>{
283
+
timedOut=true
284
+
api.abort()
285
+
},timeout)
260
286
try{
261
287
response=awaitfetchFn(request)
262
288
}catch(e){
263
-
return{error: {status: 'FETCH_ERROR',error: String(e)}, meta }
0 commit comments