Skip to content

Commit 2f5500b

Browse files
committed
Merge branch 'entity-methods-creator' into per-slice-creator
2 parents 0d28d41 + 6194741 commit 2f5500b

Some content is hidden

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

53 files changed

+1343
-294
lines changed

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ jobs:
111111
fail-fast: false
112112
matrix:
113113
node: ['20.x']
114-
ts: ['4.7', '4.8', '4.9', '5.0', '5.1', '5.2', '5.3', '5.4', '5.5']
114+
ts: ['5.0', '5.1', '5.2', '5.3', '5.4', '5.5']
115115
steps:
116116
- name: Checkout repo
117117
uses: actions/checkout@v4

docs/rtk-query/api/fetchBaseQuery.mdx

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,60 @@ type FetchBaseQueryResult = Promise<
8383
meta?: { request: Request; response: Response }
8484
}
8585
| {
86-
error: {
87-
status: number
88-
data: any
89-
}
86+
error: FetchBaseQueryError
9087
data?: undefined
9188
meta?: { request: Request; response: Response }
9289
}
9390
>
91+
92+
type FetchBaseQueryError =
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+
}
94140
```
95141
96142
## Parameters

docs/rtk-query/usage/customizing-queries.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,7 @@ const staggeredBaseQueryWithBailOut = retry(
636636
// bail out of re-tries immediately if unauthorized,
637637
// because we know successive re-retries would be redundant
638638
if (result.error?.status === 401) {
639-
retry.fail(result.error)
639+
retry.fail(result.error, result.meta)
640640
}
641641

642642
return result

examples/query/react/basic/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"react-scripts": "5.0.1"
1414
},
1515
"devDependencies": {
16-
"@testing-library/react": "^13.3.0",
16+
"@testing-library/dom": "^10.4.0",
17+
"@testing-library/react": "^16.0.1",
1718
"@types/jest": "^26.0.23",
1819
"@types/react": "^18.0.5",
1920
"@types/react-dom": "^18.0.5",

examples/query/react/kitchen-sink/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@
1515
"react-scripts": "5.0.1"
1616
},
1717
"devDependencies": {
18+
"@testing-library/dom": "^10.4.0",
1819
"@testing-library/jest-dom": "^5.11.5",
19-
"@testing-library/react": "^13.3.0",
20+
"@testing-library/react": "^16.0.1",
2021
"@types/jest": "^26.0.23",
2122
"@types/node": "^14.14.6",
2223
"@types/react": "^18.0.5",

packages/toolkit/package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@reduxjs/toolkit",
3-
"version": "2.3.0",
3+
"version": "2.4.0",
44
"description": "The official, opinionated, batteries-included toolset for efficient Redux development",
55
"author": "Mark Erikson <mark@isquaredsoftware.com>",
66
"license": "MIT",
@@ -56,8 +56,9 @@
5656
"@phryneas/ts-version": "^1.0.2",
5757
"@size-limit/file": "^11.0.1",
5858
"@size-limit/webpack": "^11.0.1",
59-
"@testing-library/react": "^13.3.0",
60-
"@testing-library/user-event": "^13.1.5",
59+
"@testing-library/dom": "^10.4.0",
60+
"@testing-library/react": "^16.0.1",
61+
"@testing-library/user-event": "^14.5.2",
6162
"@types/babel__core": "^7.20.5",
6263
"@types/babel__helper-module-imports": "^7.18.3",
6364
"@types/json-stringify-safe": "^5.0.0",

packages/toolkit/src/autoBatchEnhancer.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,6 @@ const createQueueWithTimer = (timeout: number) => {
1515
}
1616
}
1717

18-
// requestAnimationFrame won't exist in SSR environments.
19-
// Fall back to a vague approximation just to keep from erroring.
20-
const rAF =
21-
typeof window !== 'undefined' && window.requestAnimationFrame
22-
? window.requestAnimationFrame
23-
: createQueueWithTimer(10)
24-
2518
export type AutoBatchOptions =
2619
| { type: 'tick' }
2720
| { type: 'timer'; timeout: number }
@@ -66,7 +59,10 @@ export const autoBatchEnhancer =
6659
options.type === 'tick'
6760
? queueMicrotask
6861
: options.type === 'raf'
69-
? rAF
62+
? // requestAnimationFrame won't exist in SSR environments. Fall back to a vague approximation just to keep from erroring.
63+
typeof window !== 'undefined' && window.requestAnimationFrame
64+
? window.requestAnimationFrame
65+
: createQueueWithTimer(10)
7066
: options.type === 'callback'
7167
? options.queueNotification
7268
: createQueueWithTimer(options.timeout)

packages/toolkit/src/combineSlices.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import type {
88
UnionToIntersection,
99
WithOptionalProp,
1010
} from './tsHelpers'
11-
import { emplace } from './utils'
11+
import { getOrInsertComputed } from './utils'
1212

1313
type SliceLike<ReducerPath extends string, State> = {
1414
reducerPath: ReducerPath
@@ -324,8 +324,10 @@ const createStateProxy = <State extends object>(
324324
state: State,
325325
reducerMap: Partial<Record<string, Reducer>>,
326326
) =>
327-
emplace(stateProxyMap, state, {
328-
insert: () =>
327+
getOrInsertComputed(
328+
stateProxyMap,
329+
state,
330+
() =>
329331
new Proxy(state, {
330332
get: (target, prop, receiver) => {
331333
if (prop === ORIGINAL_STATE) return target
@@ -350,7 +352,7 @@ const createStateProxy = <State extends object>(
350352
return result
351353
},
352354
}),
353-
}) as State
355+
) as State
354356

355357
const original = (state: any) => {
356358
if (!isStateProxy(state)) {

packages/toolkit/src/createAsyncThunk.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,9 @@ export type OverrideThunkApiConfigs<OldConfig, NewConfig> = Id<
437437
NewConfig & Omit<OldConfig, keyof NewConfig>
438438
>
439439

440-
type CreateAsyncThunk<CurriedThunkApiConfig extends AsyncThunkConfig> = {
440+
export type CreateAsyncThunkFunction<
441+
CurriedThunkApiConfig extends AsyncThunkConfig,
442+
> = {
441443
/**
442444
*
443445
* @param typePrefix
@@ -481,12 +483,15 @@ type CreateAsyncThunk<CurriedThunkApiConfig extends AsyncThunkConfig> = {
481483
ThunkArg,
482484
OverrideThunkApiConfigs<CurriedThunkApiConfig, ThunkApiConfig>
483485
>
484-
485-
withTypes<ThunkApiConfig extends AsyncThunkConfig>(): CreateAsyncThunk<
486-
OverrideThunkApiConfigs<CurriedThunkApiConfig, ThunkApiConfig>
487-
>
488486
}
489487

488+
type CreateAsyncThunk<CurriedThunkApiConfig extends AsyncThunkConfig> =
489+
CreateAsyncThunkFunction<CurriedThunkApiConfig> & {
490+
withTypes<ThunkApiConfig extends AsyncThunkConfig>(): CreateAsyncThunk<
491+
OverrideThunkApiConfigs<CurriedThunkApiConfig, ThunkApiConfig>
492+
>
493+
}
494+
490495
export const createAsyncThunk = /* @__PURE__ */ (() => {
491496
function createAsyncThunk<
492497
Returned,

packages/toolkit/src/createSlice.ts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import type {
3131
TypeGuard,
3232
UnionToIntersection,
3333
} from './tsHelpers'
34-
import { emplace } from './utils'
34+
import { getOrInsertComputed } from './utils'
3535

3636
export enum ReducerType {
3737
reducer = 'reducer',
@@ -1167,25 +1167,25 @@ export function buildCreateSlice<
11671167
function getSelectors(
11681168
selectState: (rootState: any) => State = selectSelf,
11691169
) {
1170-
const selectorCache = emplace(injectedSelectorCache, injected, {
1171-
insert: () => new WeakMap(),
1172-
})
1173-
1174-
return emplace(selectorCache, selectState, {
1175-
insert: () => {
1176-
const map: Record<string, Selector<any, any>> = {}
1177-
for (const [name, selector] of Object.entries(
1178-
options.selectors ?? {},
1179-
)) {
1180-
map[name] = wrapSelector(
1181-
selector,
1182-
selectState,
1183-
getInitialState,
1184-
injected,
1185-
)
1186-
}
1187-
return map
1188-
},
1170+
const selectorCache = getOrInsertComputed(
1171+
injectedSelectorCache,
1172+
injected,
1173+
() => new WeakMap(),
1174+
)
1175+
1176+
return getOrInsertComputed(selectorCache, selectState, () => {
1177+
const map: Record<string, Selector<any, any>> = {}
1178+
for (const [name, selector] of Object.entries(
1179+
options.selectors ?? {},
1180+
)) {
1181+
map[name] = wrapSelector(
1182+
selector,
1183+
selectState,
1184+
getInitialState,
1185+
injected,
1186+
)
1187+
}
1188+
return map
11891189
}) as any
11901190
}
11911191
return {

0 commit comments

Comments
 (0)