Skip to content

Commit a39d645

Browse files
committed
Merge branch 'master' into combineslices-example
2 parents dd45e4e + e848a55 commit a39d645

Some content is hidden

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

47 files changed

+1294
-308
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
@@ -26,7 +26,7 @@ import { createReducer } from './createReducer'
2626
import type { ActionReducerMapBuilder, TypedActionCreator } from './mapBuilders'
2727
import { executeReducerBuilderCallback } from './mapBuilders'
2828
import type { Id, TypeGuard } from './tsHelpers'
29-
import { emplace } from './utils'
29+
import { getOrInsertComputed } from './utils'
3030

3131
const asyncThunkSymbol = /* @__PURE__ */ Symbol.for(
3232
'rtk-slice-createasyncthunk',
@@ -769,25 +769,25 @@ export function buildCreateSlice({ creators }: BuildCreateSliceConfig = {}) {
769769
function getSelectors(
770770
selectState: (rootState: any) => State = selectSelf,
771771
) {
772-
const selectorCache = emplace(injectedSelectorCache, injected, {
773-
insert: () => new WeakMap(),
774-
})
775-
776-
return emplace(selectorCache, selectState, {
777-
insert: () => {
778-
const map: Record<string, Selector<any, any>> = {}
779-
for (const [name, selector] of Object.entries(
780-
options.selectors ?? {},
781-
)) {
782-
map[name] = wrapSelector(
783-
selector,
784-
selectState,
785-
getInitialState,
786-
injected,
787-
)
788-
}
789-
return map
790-
},
772+
const selectorCache = getOrInsertComputed(
773+
injectedSelectorCache,
774+
injected,
775+
() => new WeakMap(),
776+
)
777+
778+
return getOrInsertComputed(selectorCache, selectState, () => {
779+
const map: Record<string, Selector<any, any>> = {}
780+
for (const [name, selector] of Object.entries(
781+
options.selectors ?? {},
782+
)) {
783+
map[name] = wrapSelector(
784+
selector,
785+
selectState,
786+
getInitialState,
787+
injected,
788+
)
789+
}
790+
return map
791791
}) as any
792792
}
793793
return {

0 commit comments

Comments
 (0)