Skip to content

Commit ec5d2b2

Browse files
committed
Merge branch 'master' into combineslices-example
2 parents 942f2f0 + 7af5345 commit ec5d2b2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+2328
-1440
lines changed

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ jobs:
4848

4949
# Read existing version, reuse that, add a Git short hash
5050
- name: Set build version to Git commit
51-
run: node scripts/writeGitVersion.mjs $(git rev-parse --short HEAD)
51+
run: yarn tsx scripts/writeGitVersion.mts $(git rev-parse --short HEAD)
5252

5353
- name: Check updated version
5454
run: jq .version package.json

.yarn/releases/yarn-4.1.0.cjs

Lines changed: 0 additions & 893 deletions
This file was deleted.

.yarn/releases/yarn-4.4.1.cjs

Lines changed: 925 additions & 0 deletions
Large diffs are not rendered by default.

.yarnrc.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ compressionLevel: mixed
22

33
enableGlobalCache: false
44

5-
nodeLinker: node-modules
5+
enableTransparentWorkspaces: false
66

7-
yarnPath: .yarn/releases/yarn-4.1.0.cjs
7+
nodeLinker: node-modules
88

9-
enableTransparentWorkspaces: false
9+
yarnPath: .yarn/releases/yarn-4.4.1.cjs

docs/rtk-query/api/createApi.mdx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ description: 'RTK Query > API: createApi reference'
1616

1717
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.
1818

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+
1924
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.
2025

2126
:::
@@ -91,6 +96,7 @@ export const { useGetPokemonByNameQuery } = pokemonApi
9196
- `endpoint` - The name of the endpoint.
9297
- `type` - Type of request (`query` or `mutation`).
9398
- `forced` - Indicates if a query has been forced.
99+
- `queryCacheKey`- The computed query cache key.
94100
- `extraOptions` - The value of the optional `extraOptions` property provided for a given endpoint
95101

96102
#### baseQuery function signature

docs/rtk-query/api/created-api/api-slice-utils.mdx

Lines changed: 143 additions & 56 deletions
Large diffs are not rendered by default.

docs/rtk-query/api/created-api/hooks.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ type UseMutationResult<T> = {
335335
data?: T // Returned result if present
336336
error?: unknown // Error result if present
337337
endpointName?: string // The name of the given endpoint for the mutation
338-
fulfilledTimestamp?: number // Timestamp for when the mutation was completed
338+
fulfilledTimeStamp?: number // Timestamp for when the mutation was completed
339339

340340
// Derived request status booleans
341341
isUninitialized: boolean // Mutation has not been fired yet

docs/rtk-query/api/created-api/overview.mdx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ This section documents the contents of that API structure, with the different fi
1919

2020
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.
2121

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+
2227
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.
2328

2429
:::

docs/rtk-query/api/fetchBaseQuery.mdx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ type FetchBaseQueryArgs = {
6464
api: Pick<
6565
BaseQueryApi,
6666
'getState' | 'extra' | 'endpoint' | 'type' | 'forced'
67-
>,
67+
> & { arg: string | FetchArgs },
6868
) => MaybePromise<Headers | void>
6969
fetchFn?: (
7070
input: RequestInfo,
@@ -105,7 +105,7 @@ Typically a string like `https://api.your-really-great-app.com/v1/`. If you don'
105105

106106
_(optional)_
107107

108-
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.
108+
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 `arg`, `extra`, `endpoint`, `type`, and `forced` to unlock more granular conditional behaviors.
109109

110110
You can mutate the `headers` argument directly, and returning it is optional.
111111

@@ -114,6 +114,7 @@ type prepareHeaders = (
114114
headers: Headers,
115115
api: {
116116
getState: () => unknown
117+
arg: string | FetchArgs
117118
extra: unknown
118119
endpoint: string
119120
type: 'query' | 'mutation'
@@ -297,6 +298,17 @@ export const customApi = createApi({
297298
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.
298299
:::
299300

301+
#### Default response handler
302+
303+
The default response handler is `"json"`, which is equivalent to the following function:
304+
305+
```ts title="Default responseHandler"
306+
const defaultResponseHandler = async (res: Response) => {
307+
const text = await res.text()
308+
return text.length ? JSON.parse(text) : null
309+
}
310+
```
311+
300312
### Handling non-standard Response status codes
301313

302314
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.

docs/rtk-query/internal/overview.mdx

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@ RTK-Query uses a very familiar redux-centric architecture. Where the `api` is a
3838
The slices built inside this "build" are:
3939
_Some of which have their own actions_
4040

41-
- querySlice
42-
- mutationSlice
43-
- invalidationSlice
44-
- subscriptionSlice (used as a dummy slice to generate actions internally)
45-
- internalSubscriptionsSlice
46-
- configSlice (internal tracking of focus state, online state, hydration etc)
41+
- `querySlice`
42+
- `mutationSlice`
43+
- `invalidationSlice`
44+
- `subscriptionSlice` (used as a dummy slice to generate actions internally)
45+
- `internalSubscriptionsSlice`
46+
- `configSlice` (internal tracking of focus state, online state, hydration etc)
4747

4848
buildSlice also exposes the core action `resetApiState` which is subsequently added to the `api.util`
4949

@@ -53,21 +53,21 @@ RTK-Query has a series of custom middlewares established within its store to han
5353

5454
Each middleware built during this step is referred to internally as a "Handler" and are as follows:
5555

56-
- `buildDevCheckHandler
57-
- `buildCacheCollectionHandler
58-
- `buildInvalidationByTagsHandler
59-
- `buildPollingHandler
60-
- `buildCacheLifecycleHandler
61-
- `buildQueryLifecycleHandler
56+
- `buildDevCheckHandler`
57+
- `buildCacheCollectionHandler`
58+
- `buildInvalidationByTagsHandler`
59+
- `buildPollingHandler`
60+
- `buildCacheLifecycleHandler`
61+
- `buildQueryLifecycleHandler`
6262

6363
### buildSelectors
6464

6565
build selectors is a crucial step that exposes to the `api` and utils:
6666

67-
- `buildQuerySelector
68-
- `buildMutationSelector
69-
- `selectInvalidatedBy
70-
- `selectCachedArgsForQuery
67+
- `buildQuerySelector`
68+
- `buildMutationSelector`
69+
- `selectInvalidatedBy`
70+
- `selectCachedArgsForQuery`
7171

7272
### return
7373

0 commit comments

Comments
 (0)