@depup/reduxjs__toolkit 2.12.0-depup.28

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (173) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +33 -0
  3. package/changes.json +18 -0
  4. package/dist/cjs/index.js +6 -0
  5. package/dist/cjs/redux-toolkit.development.cjs +2406 -0
  6. package/dist/cjs/redux-toolkit.development.cjs.map +1 -0
  7. package/dist/cjs/redux-toolkit.production.min.cjs +3 -0
  8. package/dist/cjs/redux-toolkit.production.min.cjs.map +1 -0
  9. package/dist/index.d.mts +2665 -0
  10. package/dist/index.d.ts +2665 -0
  11. package/dist/query/cjs/index.js +6 -0
  12. package/dist/query/cjs/rtk-query.development.cjs +3092 -0
  13. package/dist/query/cjs/rtk-query.development.cjs.map +1 -0
  14. package/dist/query/cjs/rtk-query.production.min.cjs +2 -0
  15. package/dist/query/cjs/rtk-query.production.min.cjs.map +1 -0
  16. package/dist/query/index.d.mts +3066 -0
  17. package/dist/query/index.d.ts +3066 -0
  18. package/dist/query/react/cjs/index.js +6 -0
  19. package/dist/query/react/cjs/rtk-query-react.development.cjs +748 -0
  20. package/dist/query/react/cjs/rtk-query-react.development.cjs.map +1 -0
  21. package/dist/query/react/cjs/rtk-query-react.production.min.cjs +2 -0
  22. package/dist/query/react/cjs/rtk-query-react.production.min.cjs.map +1 -0
  23. package/dist/query/react/index.d.mts +1009 -0
  24. package/dist/query/react/index.d.ts +1009 -0
  25. package/dist/query/react/rtk-query-react.browser.mjs +2 -0
  26. package/dist/query/react/rtk-query-react.browser.mjs.map +1 -0
  27. package/dist/query/react/rtk-query-react.legacy-esm.js +740 -0
  28. package/dist/query/react/rtk-query-react.legacy-esm.js.map +1 -0
  29. package/dist/query/react/rtk-query-react.modern.mjs +705 -0
  30. package/dist/query/react/rtk-query-react.modern.mjs.map +1 -0
  31. package/dist/query/rtk-query.browser.mjs +2 -0
  32. package/dist/query/rtk-query.browser.mjs.map +1 -0
  33. package/dist/query/rtk-query.legacy-esm.js +3117 -0
  34. package/dist/query/rtk-query.legacy-esm.js.map +1 -0
  35. package/dist/query/rtk-query.modern.mjs +3052 -0
  36. package/dist/query/rtk-query.modern.mjs.map +1 -0
  37. package/dist/react/cjs/index.js +6 -0
  38. package/dist/react/cjs/redux-toolkit-react.development.cjs +55 -0
  39. package/dist/react/cjs/redux-toolkit-react.development.cjs.map +1 -0
  40. package/dist/react/cjs/redux-toolkit-react.production.min.cjs +2 -0
  41. package/dist/react/cjs/redux-toolkit-react.production.min.cjs.map +1 -0
  42. package/dist/react/index.d.mts +22 -0
  43. package/dist/react/index.d.ts +22 -0
  44. package/dist/react/redux-toolkit-react.browser.mjs +2 -0
  45. package/dist/react/redux-toolkit-react.browser.mjs.map +1 -0
  46. package/dist/react/redux-toolkit-react.legacy-esm.js +47 -0
  47. package/dist/react/redux-toolkit-react.legacy-esm.js.map +1 -0
  48. package/dist/react/redux-toolkit-react.modern.mjs +28 -0
  49. package/dist/react/redux-toolkit-react.modern.mjs.map +1 -0
  50. package/dist/redux-toolkit.browser.mjs +3 -0
  51. package/dist/redux-toolkit.browser.mjs.map +1 -0
  52. package/dist/redux-toolkit.legacy-esm.js +2351 -0
  53. package/dist/redux-toolkit.legacy-esm.js.map +1 -0
  54. package/dist/redux-toolkit.modern.mjs +2330 -0
  55. package/dist/redux-toolkit.modern.mjs.map +1 -0
  56. package/dist/uncheckedindexed.ts +16 -0
  57. package/package.json +310 -0
  58. package/query/package.json +13 -0
  59. package/query/react/package.json +13 -0
  60. package/react/package.json +13 -0
  61. package/skills/build-modern-redux-apps/modern-redux/SKILL.md +304 -0
  62. package/skills/build-modern-redux-apps/modern-redux/references/store-lifetime.md +53 -0
  63. package/skills/build-modern-redux-apps/redux-dataflow/SKILL.md +264 -0
  64. package/skills/evolve-and-diagnose-redux-apps/debug-redux-toolkit-apps/SKILL.md +269 -0
  65. package/skills/evolve-and-diagnose-redux-apps/migrate-to-modern-redux/SKILL.md +226 -0
  66. package/skills/manage-server-data/adopt-rtk-query/SKILL.md +385 -0
  67. package/skills/manage-server-data/adopt-rtk-query/references/endpoint-lifecycle.md +36 -0
  68. package/skills/model-redux-state/build-slices-and-selectors/SKILL.md +364 -0
  69. package/skills/model-redux-state/build-slices-and-selectors/references/slice-patterns.md +59 -0
  70. package/skills/model-redux-state/design-state-ownership/SKILL.md +322 -0
  71. package/skills/model-redux-state/design-state-ownership/references/state-ownership.md +28 -0
  72. package/skills/orchestrate-side-effects/handle-side-effects/SKILL.md +271 -0
  73. package/skills/orchestrate-side-effects/handle-side-effects/references/listener-workflows.md +34 -0
  74. package/src/actionCreatorInvariantMiddleware.ts +34 -0
  75. package/src/autoBatchEnhancer.ts +146 -0
  76. package/src/combineSlices.ts +487 -0
  77. package/src/configureStore.ts +248 -0
  78. package/src/createAction.ts +324 -0
  79. package/src/createAsyncThunk.ts +791 -0
  80. package/src/createDraftSafeSelector.ts +30 -0
  81. package/src/createReducer.ts +217 -0
  82. package/src/createSlice.ts +1079 -0
  83. package/src/devtoolsExtension.ts +241 -0
  84. package/src/dynamicMiddleware/index.ts +93 -0
  85. package/src/dynamicMiddleware/react/index.ts +101 -0
  86. package/src/dynamicMiddleware/types.ts +82 -0
  87. package/src/entities/create_adapter.ts +47 -0
  88. package/src/entities/entity_state.ts +38 -0
  89. package/src/entities/index.ts +8 -0
  90. package/src/entities/models.ts +198 -0
  91. package/src/entities/sorted_state_adapter.ts +266 -0
  92. package/src/entities/state_adapter.ts +58 -0
  93. package/src/entities/state_selectors.ts +73 -0
  94. package/src/entities/unsorted_state_adapter.ts +204 -0
  95. package/src/entities/utils.ts +68 -0
  96. package/src/formatProdErrorMessage.ts +13 -0
  97. package/src/getDefaultEnhancers.ts +31 -0
  98. package/src/getDefaultMiddleware.ts +113 -0
  99. package/src/immerImports.ts +7 -0
  100. package/src/immutableStateInvariantMiddleware.ts +274 -0
  101. package/src/index.ts +213 -0
  102. package/src/listenerMiddleware/exceptions.ts +20 -0
  103. package/src/listenerMiddleware/index.ts +562 -0
  104. package/src/listenerMiddleware/task.ts +100 -0
  105. package/src/listenerMiddleware/types.ts +886 -0
  106. package/src/listenerMiddleware/utils.ts +30 -0
  107. package/src/mapBuilders.ts +298 -0
  108. package/src/matchers.ts +365 -0
  109. package/src/nanoid.ts +20 -0
  110. package/src/query/HandledError.ts +6 -0
  111. package/src/query/apiTypes.ts +120 -0
  112. package/src/query/baseQueryTypes.ts +101 -0
  113. package/src/query/core/apiState.ts +377 -0
  114. package/src/query/core/buildInitiate.ts +596 -0
  115. package/src/query/core/buildMiddleware/batchActions.ts +207 -0
  116. package/src/query/core/buildMiddleware/cacheCollection.ts +204 -0
  117. package/src/query/core/buildMiddleware/cacheLifecycle.ts +379 -0
  118. package/src/query/core/buildMiddleware/devMiddleware.ts +34 -0
  119. package/src/query/core/buildMiddleware/index.ts +162 -0
  120. package/src/query/core/buildMiddleware/invalidationByTags.ts +139 -0
  121. package/src/query/core/buildMiddleware/polling.ts +187 -0
  122. package/src/query/core/buildMiddleware/queryLifecycle.ts +508 -0
  123. package/src/query/core/buildMiddleware/types.ts +173 -0
  124. package/src/query/core/buildMiddleware/windowEventHandling.ts +64 -0
  125. package/src/query/core/buildSelectors.ts +413 -0
  126. package/src/query/core/buildSlice.ts +735 -0
  127. package/src/query/core/buildThunks.ts +1124 -0
  128. package/src/query/core/index.ts +58 -0
  129. package/src/query/core/module.ts +727 -0
  130. package/src/query/core/rtkImports.ts +24 -0
  131. package/src/query/core/setupListeners.ts +118 -0
  132. package/src/query/createApi.ts +506 -0
  133. package/src/query/defaultSerializeQueryArgs.ts +52 -0
  134. package/src/query/endpointDefinitions.ts +1717 -0
  135. package/src/query/fakeBaseQuery.ts +21 -0
  136. package/src/query/fetchBaseQuery.ts +385 -0
  137. package/src/query/index.ts +105 -0
  138. package/src/query/react/ApiProvider.tsx +69 -0
  139. package/src/query/react/buildHooks.ts +2297 -0
  140. package/src/query/react/constants.ts +2 -0
  141. package/src/query/react/index.ts +45 -0
  142. package/src/query/react/module.ts +275 -0
  143. package/src/query/react/namedHooks.ts +59 -0
  144. package/src/query/react/reactImports.ts +10 -0
  145. package/src/query/react/reactReduxImports.ts +1 -0
  146. package/src/query/react/rtkqImports.ts +8 -0
  147. package/src/query/react/useSerializedStableValue.ts +17 -0
  148. package/src/query/react/useShallowStableValue.ts +13 -0
  149. package/src/query/retry.ts +233 -0
  150. package/src/query/standardSchema.ts +35 -0
  151. package/src/query/tsHelpers.ts +48 -0
  152. package/src/query/utils/capitalize.ts +3 -0
  153. package/src/query/utils/copyWithStructuralSharing.ts +27 -0
  154. package/src/query/utils/countObjectKeys.ts +14 -0
  155. package/src/query/utils/filterMap.ts +27 -0
  156. package/src/query/utils/getCurrent.ts +6 -0
  157. package/src/query/utils/getOrInsert.ts +41 -0
  158. package/src/query/utils/immerImports.ts +9 -0
  159. package/src/query/utils/index.ts +11 -0
  160. package/src/query/utils/isAbsoluteUrl.ts +9 -0
  161. package/src/query/utils/isDocumentVisible.ts +12 -0
  162. package/src/query/utils/isNotNullish.ts +7 -0
  163. package/src/query/utils/isOnline.ts +12 -0
  164. package/src/query/utils/isValidUrl.ts +9 -0
  165. package/src/query/utils/joinUrls.ts +26 -0
  166. package/src/query/utils/signals.ts +33 -0
  167. package/src/react/index.ts +7 -0
  168. package/src/reduxImports.ts +8 -0
  169. package/src/reselectImports.ts +1 -0
  170. package/src/serializableStateInvariantMiddleware.ts +285 -0
  171. package/src/tsHelpers.ts +214 -0
  172. package/src/uncheckedindexed.ts +16 -0
  173. package/src/utils.ts +125 -0
@@ -0,0 +1,30 @@
1
+ import { current, isDraft } from './immerImports'
2
+ import { createSelectorCreator, weakMapMemoize } from './reselectImports'
3
+
4
+ export const createDraftSafeSelectorCreator: typeof createSelectorCreator = (
5
+ ...args: unknown[]
6
+ ) => {
7
+ const createSelector = (createSelectorCreator as any)(...args)
8
+ const createDraftSafeSelector = Object.assign(
9
+ (...args: unknown[]) => {
10
+ const selector = createSelector(...args)
11
+ const wrappedSelector = (value: unknown, ...rest: unknown[]) =>
12
+ selector(isDraft(value) ? current(value) : value, ...rest)
13
+ Object.assign(wrappedSelector, selector)
14
+ return wrappedSelector as any
15
+ },
16
+ { withTypes: () => createDraftSafeSelector },
17
+ )
18
+ return createDraftSafeSelector
19
+ }
20
+
21
+ /**
22
+ * "Draft-Safe" version of `reselect`'s `createSelector`:
23
+ * If an `immer`-drafted object is passed into the resulting selector's first argument,
24
+ * the selector will act on the current draft value, instead of returning a cached value
25
+ * that might be possibly outdated if the draft has been modified since.
26
+ * @public
27
+ */
28
+ export const createDraftSafeSelector =
29
+ /* @__PURE__ */
30
+ createDraftSafeSelectorCreator(weakMapMemoize)
@@ -0,0 +1,217 @@
1
+ import type { Draft } from 'immer'
2
+ import type { Action, Reducer, UnknownAction } from 'redux'
3
+ import { createNextState, isDraft, isDraftable } from './immerImports'
4
+ import type { ActionReducerMapBuilder } from './mapBuilders'
5
+ import { executeReducerBuilderCallback } from './mapBuilders'
6
+ import type { TypeGuard } from './tsHelpers'
7
+ import { freezeDraftable } from './utils'
8
+
9
+ /**
10
+ * Defines a mapping from action types to corresponding action object shapes.
11
+ *
12
+ * @deprecated This should not be used manually - it is only used for internal
13
+ * inference purposes and should not have any further value.
14
+ * It might be removed in the future.
15
+ * @public
16
+ */
17
+ export type Actions<T extends keyof any = string> = Record<T, Action>
18
+
19
+ export type ActionMatcherDescription<S, A extends Action> = {
20
+ matcher: TypeGuard<A>
21
+ reducer: CaseReducer<S, NoInfer<A>>
22
+ }
23
+
24
+ export type ReadonlyActionMatcherDescriptionCollection<S> = ReadonlyArray<
25
+ ActionMatcherDescription<S, any>
26
+ >
27
+
28
+ export type ActionMatcherDescriptionCollection<S> = Array<
29
+ ActionMatcherDescription<S, any>
30
+ >
31
+
32
+ /**
33
+ * A *case reducer* is a reducer function for a specific action type. Case
34
+ * reducers can be composed to full reducers using `createReducer()`.
35
+ *
36
+ * Unlike a normal Redux reducer, a case reducer is never called with an
37
+ * `undefined` state to determine the initial state. Instead, the initial
38
+ * state is explicitly specified as an argument to `createReducer()`.
39
+ *
40
+ * In addition, a case reducer can choose to mutate the passed-in `state`
41
+ * value directly instead of returning a new state. This does not actually
42
+ * cause the store state to be mutated directly; instead, thanks to
43
+ * [immer](https://github.com/mweststrate/immer), the mutations are
44
+ * translated to copy operations that result in a new state.
45
+ *
46
+ * @public
47
+ */
48
+ export type CaseReducer<S = any, A extends Action = UnknownAction> = (
49
+ state: Draft<S>,
50
+ action: A,
51
+ ) => NoInfer<S> | void | Draft<NoInfer<S>>
52
+
53
+ /**
54
+ * A mapping from action types to case reducers for `createReducer()`.
55
+ *
56
+ * @deprecated This should not be used manually - it is only used
57
+ * for internal inference purposes and using it manually
58
+ * would lead to type erasure.
59
+ * It might be removed in the future.
60
+ * @public
61
+ */
62
+ export type CaseReducers<S, AS extends Actions> = {
63
+ [T in keyof AS]: AS[T] extends Action ? CaseReducer<S, AS[T]> : void
64
+ }
65
+
66
+ export type NotFunction<T> = T extends Function ? never : T
67
+
68
+ function isStateFunction<S>(x: unknown): x is () => S {
69
+ return typeof x === 'function'
70
+ }
71
+
72
+ export type ReducerWithInitialState<S extends NotFunction<any>> = Reducer<S> & {
73
+ getInitialState: () => S
74
+ }
75
+
76
+ /**
77
+ * A utility function that allows defining a reducer as a mapping from action
78
+ * type to *case reducer* functions that handle these action types. The
79
+ * reducer's initial state is passed as the first argument.
80
+ *
81
+ * @remarks
82
+ * The body of every case reducer is implicitly wrapped with a call to
83
+ * `produce()` from the [immer](https://github.com/mweststrate/immer) library.
84
+ * This means that rather than returning a new state object, you can also
85
+ * mutate the passed-in state object directly; these mutations will then be
86
+ * automatically and efficiently translated into copies, giving you both
87
+ * convenience and immutability.
88
+ *
89
+ * @overloadSummary
90
+ * This function accepts a callback that receives a `builder` object as its argument.
91
+ * That builder provides `addCase`, `addMatcher` and `addDefaultCase` functions that may be
92
+ * called to define what actions this reducer will handle.
93
+ *
94
+ * @param initialState - `State | (() => State)`: The initial state that should be used when the reducer is called the first time. This may also be a "lazy initializer" function, which should return an initial state value when called. This will be used whenever the reducer is called with `undefined` as its state value, and is primarily useful for cases like reading initial state from `localStorage`.
95
+ * @param builderCallback - `(builder: Builder) => void` A callback that receives a *builder* object to define
96
+ * case reducers via calls to `builder.addCase(actionCreatorOrType, reducer)`.
97
+ * @example
98
+ * ```ts
99
+ * import type { PayloadAction, UnknownAction } from '@reduxjs/toolkit';
100
+ * import { createAction, createReducer } from '@reduxjs/toolkit';
101
+ *
102
+ * const increment = createAction<number>('increment');
103
+ * const decrement = createAction<number>('decrement');
104
+ *
105
+ * function isActionWithNumberPayload(
106
+ * action: UnknownAction,
107
+ * ): action is PayloadAction<number> {
108
+ * return typeof action.payload === 'number';
109
+ * }
110
+ *
111
+ * const reducer = createReducer(
112
+ * {
113
+ * counter: 0,
114
+ * sumOfNumberPayloads: 0,
115
+ * unhandledActions: 0,
116
+ * },
117
+ * (builder) => {
118
+ * builder
119
+ * .addCase(increment, (state, action) => {
120
+ * // action is inferred correctly here
121
+ * state.counter += action.payload;
122
+ * })
123
+ * // You can chain calls, or have separate `builder.addCase()` lines each time
124
+ * .addCase(decrement, (state, action) => {
125
+ * state.counter -= action.payload;
126
+ * })
127
+ * // You can apply a "matcher function" to incoming actions
128
+ * .addMatcher(isActionWithNumberPayload, (state, action) => {})
129
+ * // and provide a default case if no other handlers matched
130
+ * .addDefaultCase((state, action) => {});
131
+ * },
132
+ * );
133
+ * ```
134
+ * @public
135
+ */
136
+ export function createReducer<S extends NotFunction<any>>(
137
+ initialState: S | (() => S),
138
+ mapOrBuilderCallback: (builder: ActionReducerMapBuilder<S>) => void,
139
+ ): ReducerWithInitialState<S> {
140
+ if (process.env.NODE_ENV !== 'production') {
141
+ if (typeof mapOrBuilderCallback === 'object') {
142
+ throw new Error(
143
+ "The object notation for `createReducer` has been removed. Please use the 'builder callback' notation instead: https://redux-toolkit.js.org/api/createReducer",
144
+ )
145
+ }
146
+ }
147
+
148
+ let [actionsMap, finalActionMatchers, finalDefaultCaseReducer] =
149
+ executeReducerBuilderCallback(mapOrBuilderCallback)
150
+
151
+ // Ensure the initial state gets frozen either way (if draftable)
152
+ let getInitialState: () => S
153
+ if (isStateFunction(initialState)) {
154
+ getInitialState = () => freezeDraftable(initialState())
155
+ } else {
156
+ const frozenInitialState = freezeDraftable(initialState)
157
+ getInitialState = () => frozenInitialState
158
+ }
159
+
160
+ function reducer(state = getInitialState(), action: any): S {
161
+ let caseReducers = [
162
+ actionsMap[action.type],
163
+ ...finalActionMatchers
164
+ .filter(({ matcher }) => matcher(action))
165
+ .map(({ reducer }) => reducer),
166
+ ]
167
+ if (caseReducers.filter((cr) => !!cr).length === 0) {
168
+ caseReducers = [finalDefaultCaseReducer]
169
+ }
170
+
171
+ return caseReducers.reduce((previousState, caseReducer): S => {
172
+ if (caseReducer) {
173
+ if (isDraft(previousState)) {
174
+ // If it's already a draft, we must already be inside a `createNextState` call,
175
+ // likely because this is being wrapped in `createReducer`, `createSlice`, or nested
176
+ // inside an existing draft. It's safe to just pass the draft to the mutator.
177
+ const draft = previousState as Draft<S> // We can assume this is already a draft
178
+ const result = caseReducer(draft, action)
179
+
180
+ if (result === undefined) {
181
+ return previousState
182
+ }
183
+
184
+ return result as S
185
+ } else if (!isDraftable(previousState)) {
186
+ // If state is not draftable (ex: a primitive, such as 0), we want to directly
187
+ // return the caseReducer func and not wrap it with produce.
188
+ const result = caseReducer(previousState as any, action)
189
+
190
+ if (result === undefined) {
191
+ if (previousState === null) {
192
+ return previousState
193
+ }
194
+ throw Error(
195
+ 'A case reducer on a non-draftable value must not return undefined',
196
+ )
197
+ }
198
+
199
+ return result as S
200
+ } else {
201
+ // @ts-ignore createNextState() produces an Immutable<Draft<S>> rather
202
+ // than an Immutable<S>, and TypeScript cannot find out how to reconcile
203
+ // these two types.
204
+ return createNextState(previousState, (draft: Draft<S>) => {
205
+ return caseReducer(draft, action)
206
+ })
207
+ }
208
+ }
209
+
210
+ return previousState
211
+ }, state)
212
+ }
213
+
214
+ reducer.getInitialState = getInitialState
215
+
216
+ return reducer as ReducerWithInitialState<S>
217
+ }