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
Typically, you should only have one API slice per base URL that your application needs to communicate with. For example, if your site fetches data from both `/api/posts` and `/api/users`, you would have a single API slice with `/api/` as the base URL, and separate endpoint definitions for `posts` and `users`. This allows you to effectively take advantage of [automated re-fetching](../usage/automated-refetching.mdx) by defining [tag](../usage/automated-refetching.mdx#tags) relationships across endpoints.
18
18
19
+
This is because:
20
+
21
+
- Automatic tag invalidation only works within a single API slice. If you have multiple API slices, the automatic invalidation won't work across them.
22
+
- Every `createApi` call generates its own middleware, and each middleware added to the store will run checks against every dispatched action. That has a perf cost that adds up. So, if you called `createApi` 10 times and added 10 separate API middleware to the store, that will be noticeably slower perf-wise.
23
+
19
24
For maintainability purposes, you may wish to split up endpoint definitions across multiple files, while still maintaining a single API slice which includes all of these endpoints. See [code splitting](../usage/code-splitting.mdx) for how you can use the `injectEndpoints` property to inject API endpoints from other files into a single API slice definition.
Copy file name to clipboardExpand all lines: docs/rtk-query/api/created-api/overview.mdx
+5Lines changed: 5 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -19,6 +19,11 @@ This section documents the contents of that API structure, with the different fi
19
19
20
20
Typically, you should only have one API slice per base URL that your application needs to communicate with. For example, if your site fetches data from both `/api/posts` and `/api/users`, you would have a single API slice with `/api/` as the base URL, and separate endpoint definitions for `posts` and `users`. This allows you to effectively take advantage of [automated re-fetching](../../usage/automated-refetching.mdx) by defining [tag](../../usage/automated-refetching.mdx#tags) relationships across endpoints.
21
21
22
+
This is because:
23
+
24
+
- Automatic tag invalidation only works within a single API slice. If you have multiple API slices, the automatic invalidation won't work across them.
25
+
- Every `createApi` call generates its own middleware, and each middleware added to the store will run checks against every dispatched action. That has a perf cost that adds up. So, if you called `createApi` 10 times and added 10 separate API middleware to the store, that will be noticeably slower perf-wise.
26
+
22
27
For maintainability purposes, you may wish to split up endpoint definitions across multiple files, while still maintaining a single API slice which includes all of these endpoints. See [code splitting](../../usage/code-splitting.mdx) for how you can use the `injectEndpoints` property to inject API endpoints from other files into a single API slice definition.
Copy file name to clipboardExpand all lines: docs/rtk-query/api/fetchBaseQuery.mdx
+64-6Lines changed: 64 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -64,7 +64,7 @@ type FetchBaseQueryArgs = {
64
64
api:Pick<
65
65
BaseQueryApi,
66
66
'getState'|'extra'|'endpoint'|'type'|'forced'
67
-
>,
67
+
>& { arg:string|FetchArgs },
68
68
) =>MaybePromise<Headers|void>
69
69
fetchFn?: (
70
70
input:RequestInfo,
@@ -83,14 +83,60 @@ type FetchBaseQueryResult = Promise<
83
83
meta?: { request:Request; response:Response }
84
84
}
85
85
| {
86
-
error: {
87
-
status:number
88
-
data:any
89
-
}
86
+
error:FetchBaseQueryError
90
87
data?:undefined
91
88
meta?: { request:Request; response:Response }
92
89
}
93
90
>
91
+
92
+
typeFetchBaseQueryError=
93
+
| {
94
+
/**
95
+
* * `number`:
96
+
* HTTP status code
97
+
*/
98
+
status:number
99
+
data:unknown
100
+
}
101
+
| {
102
+
/**
103
+
* * `"FETCH_ERROR"`:
104
+
* An error that occurred during execution of `fetch` or the `fetchFn` callback option
105
+
**/
106
+
status:'FETCH_ERROR'
107
+
data?:undefined
108
+
error:string
109
+
}
110
+
| {
111
+
/**
112
+
* * `"PARSING_ERROR"`:
113
+
* An error happened during parsing.
114
+
* Most likely a non-JSON-response was returned with the default `responseHandler` "JSON",
115
+
* or an error occurred while executing a custom `responseHandler`.
116
+
**/
117
+
status:'PARSING_ERROR'
118
+
originalStatus:number
119
+
data:string
120
+
error:string
121
+
}
122
+
| {
123
+
/**
124
+
* * `"TIMEOUT_ERROR"`:
125
+
* Request timed out
126
+
**/
127
+
status:'TIMEOUT_ERROR'
128
+
data?:undefined
129
+
error:string
130
+
}
131
+
| {
132
+
/**
133
+
* * `"CUSTOM_ERROR"`:
134
+
* A custom error type that you can return from your `queryFn` where another error might not make sense.
135
+
**/
136
+
status:'CUSTOM_ERROR'
137
+
data?:unknown
138
+
error:string
139
+
}
94
140
```
95
141
96
142
## Parameters
@@ -105,7 +151,7 @@ Typically a string like `https://api.your-really-great-app.com/v1/`. If you don'
105
151
106
152
_(optional)_
107
153
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.
154
+
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`arg`, `extra`, `endpoint`, `type`, and`forced`tounlockmoregranularconditionalbehaviors.
Bydefault, `fetchBaseQuery`will`reject`any`Response`thatdoesnothaveastatuscodeof`2xx`andsetitto`error`. Thisisthesamebehavioryou've most likely experienced with `axios` and other popular libraries. In the event that you have a non-standard API you'redealingwith, youcanusethe`validateStatus`optiontocustomizethisbehavior.
Copy file name to clipboardExpand all lines: docs/usage/immer-reducers.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -150,7 +150,7 @@ Writing immutable update logic by hand _is_ hard, and **accidentally mutating st
150
150
Immer provides a function called `produce`, which accepts two arguments: your original `state`, and a callback function. The callback function is given a "draft" version of that state, and inside the callback, it is safe to write code that mutates the draft value. Immer tracks all attempts to mutate the draft value and then replays those mutations using their immutable equivalents to create a safe, immutably updated result:
0 commit comments