@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,596 @@
1
+ import type {
2
+ AsyncThunkAction,
3
+ SafePromise,
4
+ SerializedError,
5
+ ThunkAction,
6
+ UnknownAction,
7
+ } from '@reduxjs/toolkit'
8
+ import type { Dispatch } from 'redux'
9
+ import { asSafePromise } from '../../tsHelpers'
10
+ import { getEndpointDefinition, type Api, type ApiContext } from '../apiTypes'
11
+ import type { BaseQueryError, QueryReturnValue } from '../baseQueryTypes'
12
+ import type { InternalSerializeQueryArgs } from '../defaultSerializeQueryArgs'
13
+ import {
14
+ ENDPOINT_QUERY,
15
+ isQueryDefinition,
16
+ type EndpointDefinition,
17
+ type EndpointDefinitions,
18
+ type InfiniteQueryArgFrom,
19
+ type InfiniteQueryDefinition,
20
+ type MutationDefinition,
21
+ type PageParamFrom,
22
+ type QueryArgFrom,
23
+ type QueryDefinition,
24
+ type ResultTypeFrom,
25
+ } from '../endpointDefinitions'
26
+ import { filterNullishValues } from '../utils'
27
+ import type {
28
+ InfiniteData,
29
+ InfiniteQueryConfigOptions,
30
+ InfiniteQueryDirection,
31
+ SubscriptionOptions,
32
+ } from './apiState'
33
+ import type {
34
+ InfiniteQueryResultSelectorResult,
35
+ QueryResultSelectorResult,
36
+ } from './buildSelectors'
37
+ import type {
38
+ InfiniteQueryThunk,
39
+ InfiniteQueryThunkArg,
40
+ MutationThunk,
41
+ QueryThunk,
42
+ QueryThunkArg,
43
+ ThunkApiMetaConfig,
44
+ } from './buildThunks'
45
+ import type { ApiEndpointQuery } from './module'
46
+ import type { InternalMiddlewareState } from './buildMiddleware/types'
47
+
48
+ export type BuildInitiateApiEndpointQuery<
49
+ Definition extends QueryDefinition<any, any, any, any, any>,
50
+ > = {
51
+ initiate: StartQueryActionCreator<Definition>
52
+ }
53
+
54
+ export type BuildInitiateApiEndpointInfiniteQuery<
55
+ Definition extends InfiniteQueryDefinition<any, any, any, any, any>,
56
+ > = {
57
+ initiate: StartInfiniteQueryActionCreator<Definition>
58
+ }
59
+
60
+ export type BuildInitiateApiEndpointMutation<
61
+ Definition extends MutationDefinition<any, any, any, any, any>,
62
+ > = {
63
+ initiate: StartMutationActionCreator<Definition>
64
+ }
65
+
66
+ export const forceQueryFnSymbol = Symbol('forceQueryFn')
67
+ export const isUpsertQuery = (arg: QueryThunkArg) =>
68
+ typeof arg[forceQueryFnSymbol] === 'function'
69
+
70
+ export type StartQueryActionCreatorOptions = {
71
+ subscribe?: boolean
72
+ forceRefetch?: boolean | number
73
+ subscriptionOptions?: SubscriptionOptions
74
+ [forceQueryFnSymbol]?: () => QueryReturnValue
75
+ }
76
+
77
+ type RefetchOptions = {
78
+ refetchCachedPages?: boolean
79
+ }
80
+
81
+ export type StartInfiniteQueryActionCreatorOptions<
82
+ D extends InfiniteQueryDefinition<any, any, any, any, any>,
83
+ > = StartQueryActionCreatorOptions & {
84
+ direction?: InfiniteQueryDirection
85
+ param?: unknown
86
+ } & Partial<
87
+ Pick<
88
+ Partial<
89
+ InfiniteQueryConfigOptions<
90
+ ResultTypeFrom<D>,
91
+ PageParamFrom<D>,
92
+ InfiniteQueryArgFrom<D>
93
+ >
94
+ >,
95
+ 'initialPageParam' | 'refetchCachedPages'
96
+ >
97
+ >
98
+
99
+ type AnyQueryActionCreator<D extends EndpointDefinition<any, any, any, any>> = (
100
+ arg: any,
101
+ options?: StartQueryActionCreatorOptions,
102
+ ) => ThunkAction<AnyActionCreatorResult, any, any, UnknownAction>
103
+
104
+ type StartQueryActionCreator<
105
+ D extends QueryDefinition<any, any, any, any, any>,
106
+ > = (
107
+ arg: QueryArgFrom<D>,
108
+ options?: StartQueryActionCreatorOptions,
109
+ ) => ThunkAction<QueryActionCreatorResult<D>, any, any, UnknownAction>
110
+
111
+ export type StartInfiniteQueryActionCreator<
112
+ D extends InfiniteQueryDefinition<any, any, any, any, any>,
113
+ > = (
114
+ arg: InfiniteQueryArgFrom<D>,
115
+ options?: StartInfiniteQueryActionCreatorOptions<D>,
116
+ ) => ThunkAction<InfiniteQueryActionCreatorResult<D>, any, any, UnknownAction>
117
+
118
+ type QueryActionCreatorFields = {
119
+ requestId: string
120
+ subscriptionOptions: SubscriptionOptions | undefined
121
+ abort(): void
122
+ unsubscribe(): void
123
+ updateSubscriptionOptions(options: SubscriptionOptions): void
124
+ queryCacheKey: string
125
+ }
126
+
127
+ type AnyActionCreatorResult = SafePromise<any> &
128
+ QueryActionCreatorFields & {
129
+ arg: any
130
+ unwrap(): Promise<any>
131
+ refetch(options?: RefetchOptions): AnyActionCreatorResult
132
+ }
133
+
134
+ export type QueryActionCreatorResult<
135
+ D extends QueryDefinition<any, any, any, any>,
136
+ > = SafePromise<QueryResultSelectorResult<D>> &
137
+ QueryActionCreatorFields & {
138
+ arg: QueryArgFrom<D>
139
+ unwrap(): Promise<ResultTypeFrom<D>>
140
+ refetch(): QueryActionCreatorResult<D>
141
+ }
142
+
143
+ export type InfiniteQueryActionCreatorResult<
144
+ D extends InfiniteQueryDefinition<any, any, any, any, any>,
145
+ > = SafePromise<InfiniteQueryResultSelectorResult<D>> &
146
+ QueryActionCreatorFields & {
147
+ arg: InfiniteQueryArgFrom<D>
148
+ unwrap(): Promise<InfiniteData<ResultTypeFrom<D>, PageParamFrom<D>>>
149
+ refetch(
150
+ options?: Pick<
151
+ StartInfiniteQueryActionCreatorOptions<D>,
152
+ 'refetchCachedPages'
153
+ >,
154
+ ): InfiniteQueryActionCreatorResult<D>
155
+ }
156
+
157
+ type StartMutationActionCreator<
158
+ D extends MutationDefinition<any, any, any, any>,
159
+ > = (
160
+ arg: QueryArgFrom<D>,
161
+ options?: {
162
+ /**
163
+ * If this mutation should be tracked in the store.
164
+ * If you just want to manually trigger this mutation using `dispatch` and don't care about the
165
+ * result, state & potential errors being held in store, you can set this to false.
166
+ * (defaults to `true`)
167
+ */
168
+ track?: boolean
169
+ fixedCacheKey?: string
170
+ },
171
+ ) => ThunkAction<MutationActionCreatorResult<D>, any, any, UnknownAction>
172
+
173
+ export type MutationActionCreatorResult<
174
+ D extends MutationDefinition<any, any, any, any>,
175
+ > = SafePromise<
176
+ | {
177
+ data: ResultTypeFrom<D>
178
+ error?: undefined
179
+ }
180
+ | {
181
+ data?: undefined
182
+ error:
183
+ | Exclude<
184
+ BaseQueryError<
185
+ D extends MutationDefinition<any, infer BaseQuery, any, any>
186
+ ? BaseQuery
187
+ : never
188
+ >,
189
+ undefined
190
+ >
191
+ | SerializedError
192
+ }
193
+ > & {
194
+ /** @internal */
195
+ arg: {
196
+ /**
197
+ * The name of the given endpoint for the mutation
198
+ */
199
+ endpointName: string
200
+ /**
201
+ * The original arguments supplied to the mutation call
202
+ */
203
+ originalArgs: QueryArgFrom<D>
204
+ /**
205
+ * Whether the mutation is being tracked in the store.
206
+ */
207
+ track?: boolean
208
+ fixedCacheKey?: string
209
+ }
210
+ /**
211
+ * A unique string generated for the request sequence
212
+ */
213
+ requestId: string
214
+
215
+ /**
216
+ * A method to cancel the mutation promise. Note that this is not intended to prevent the mutation
217
+ * that was fired off from reaching the server, but only to assist in handling the response.
218
+ *
219
+ * Calling `abort()` prior to the promise resolving will force it to reach the error state with
220
+ * the serialized error:
221
+ * `{ name: 'AbortError', message: 'Aborted' }`
222
+ *
223
+ * @example
224
+ * ```ts
225
+ * const [updateUser] = useUpdateUserMutation();
226
+ *
227
+ * useEffect(() => {
228
+ * const promise = updateUser(id);
229
+ * promise
230
+ * .unwrap()
231
+ * .catch((err) => {
232
+ * if (err.name === 'AbortError') return;
233
+ * // else handle the unexpected error
234
+ * })
235
+ *
236
+ * return () => {
237
+ * promise.abort();
238
+ * }
239
+ * }, [id, updateUser])
240
+ * ```
241
+ */
242
+ abort(): void
243
+ /**
244
+ * Unwraps a mutation call to provide the raw response/error.
245
+ *
246
+ * @remarks
247
+ * If you need to access the error or success payload immediately after a mutation, you can chain .unwrap().
248
+ *
249
+ * @example
250
+ * ```ts
251
+ * // codeblock-meta title="Using .unwrap"
252
+ * addPost({ id: 1, name: 'Example' })
253
+ * .unwrap()
254
+ * .then((payload) => console.log('fulfilled', payload))
255
+ * .catch((error) => console.error('rejected', error));
256
+ * ```
257
+ *
258
+ * @example
259
+ * ```ts
260
+ * // codeblock-meta title="Using .unwrap with async await"
261
+ * try {
262
+ * const payload = await addPost({ id: 1, name: 'Example' }).unwrap();
263
+ * console.log('fulfilled', payload)
264
+ * } catch (error) {
265
+ * console.error('rejected', error);
266
+ * }
267
+ * ```
268
+ */
269
+ unwrap(): Promise<ResultTypeFrom<D>>
270
+ /**
271
+ * A method to manually unsubscribe from the mutation call, meaning it will be removed from cache after the usual caching grace period.
272
+ The value returned by the hook will reset to `isUninitialized` afterwards.
273
+ */
274
+ reset(): void
275
+ }
276
+
277
+ export function buildInitiate({
278
+ serializeQueryArgs,
279
+ queryThunk,
280
+ infiniteQueryThunk,
281
+ mutationThunk,
282
+ api,
283
+ context,
284
+ getInternalState,
285
+ }: {
286
+ serializeQueryArgs: InternalSerializeQueryArgs
287
+ queryThunk: QueryThunk
288
+ infiniteQueryThunk: InfiniteQueryThunk<any>
289
+ mutationThunk: MutationThunk
290
+ api: Api<any, EndpointDefinitions, any, any>
291
+ context: ApiContext<EndpointDefinitions>
292
+ getInternalState: (dispatch: Dispatch) => InternalMiddlewareState
293
+ }) {
294
+ const getRunningQueries = (dispatch: Dispatch) =>
295
+ getInternalState(dispatch)?.runningQueries
296
+ const getRunningMutations = (dispatch: Dispatch) =>
297
+ getInternalState(dispatch)?.runningMutations
298
+
299
+ const {
300
+ unsubscribeQueryResult,
301
+ removeMutationResult,
302
+ updateSubscriptionOptions,
303
+ } = api.internalActions
304
+ return {
305
+ buildInitiateQuery,
306
+ buildInitiateInfiniteQuery,
307
+ buildInitiateMutation,
308
+ getRunningQueryThunk,
309
+ getRunningMutationThunk,
310
+ getRunningQueriesThunk,
311
+ getRunningMutationsThunk,
312
+ }
313
+
314
+ function getRunningQueryThunk(endpointName: string, queryArgs: any) {
315
+ return (dispatch: Dispatch) => {
316
+ const endpointDefinition = getEndpointDefinition(context, endpointName)
317
+ const queryCacheKey = serializeQueryArgs({
318
+ queryArgs,
319
+ endpointDefinition,
320
+ endpointName,
321
+ })
322
+ return getRunningQueries(dispatch)?.get(queryCacheKey) as
323
+ | QueryActionCreatorResult<never>
324
+ | InfiniteQueryActionCreatorResult<never>
325
+ | undefined
326
+ }
327
+ }
328
+
329
+ function getRunningMutationThunk(
330
+ /**
331
+ * this is only here to allow TS to infer the result type by input value
332
+ * we could use it to validate the result, but it's probably not necessary
333
+ */
334
+ _endpointName: string,
335
+ fixedCacheKeyOrRequestId: string,
336
+ ) {
337
+ return (dispatch: Dispatch) => {
338
+ return getRunningMutations(dispatch)?.get(fixedCacheKeyOrRequestId) as
339
+ | MutationActionCreatorResult<never>
340
+ | undefined
341
+ }
342
+ }
343
+
344
+ function getRunningQueriesThunk() {
345
+ return (dispatch: Dispatch) =>
346
+ filterNullishValues(getRunningQueries(dispatch))
347
+ }
348
+
349
+ function getRunningMutationsThunk() {
350
+ return (dispatch: Dispatch) =>
351
+ filterNullishValues(getRunningMutations(dispatch))
352
+ }
353
+
354
+ function middlewareWarning(dispatch: Dispatch) {
355
+ if (process.env.NODE_ENV !== 'production') {
356
+ if ((middlewareWarning as any).triggered) return
357
+ const returnedValue = dispatch(
358
+ api.internalActions.internal_getRTKQSubscriptions(),
359
+ )
360
+
361
+ ;(middlewareWarning as any).triggered = true
362
+
363
+ // The RTKQ middleware should return the internal state object,
364
+ // but it should _not_ be the action object.
365
+ if (
366
+ typeof returnedValue !== 'object' ||
367
+ typeof returnedValue?.type === 'string'
368
+ ) {
369
+ // Otherwise, must not have been added
370
+ throw new Error(
371
+ `Warning: Middleware for RTK-Query API at reducerPath "${api.reducerPath}" has not been added to the store.
372
+ You must add the middleware for RTK-Query to function correctly!`,
373
+ )
374
+ }
375
+ }
376
+ }
377
+
378
+ function buildInitiateAnyQuery<T extends 'query' | 'infiniteQuery'>(
379
+ endpointName: string,
380
+ endpointDefinition:
381
+ | QueryDefinition<any, any, any, any>
382
+ | InfiniteQueryDefinition<any, any, any, any, any>,
383
+ ) {
384
+ const queryAction: AnyQueryActionCreator<any> =
385
+ (
386
+ arg,
387
+ {
388
+ subscribe = true,
389
+ forceRefetch,
390
+ subscriptionOptions,
391
+ [forceQueryFnSymbol]: forceQueryFn,
392
+ ...rest
393
+ } = {},
394
+ ) =>
395
+ (dispatch, getState) => {
396
+ const queryCacheKey = serializeQueryArgs({
397
+ queryArgs: arg,
398
+ endpointDefinition,
399
+ endpointName,
400
+ })
401
+
402
+ let thunk: AsyncThunkAction<unknown, QueryThunkArg, ThunkApiMetaConfig>
403
+
404
+ const commonThunkArgs = {
405
+ ...rest,
406
+ type: ENDPOINT_QUERY as 'query',
407
+ subscribe,
408
+ forceRefetch: forceRefetch,
409
+ subscriptionOptions,
410
+ endpointName,
411
+ originalArgs: arg,
412
+ queryCacheKey,
413
+ [forceQueryFnSymbol]: forceQueryFn,
414
+ }
415
+
416
+ if (isQueryDefinition(endpointDefinition)) {
417
+ thunk = queryThunk(commonThunkArgs)
418
+ } else {
419
+ const { direction, initialPageParam, refetchCachedPages } =
420
+ rest as Pick<
421
+ InfiniteQueryThunkArg<any>,
422
+ 'direction' | 'initialPageParam' | 'refetchCachedPages'
423
+ >
424
+ thunk = infiniteQueryThunk({
425
+ ...(commonThunkArgs as InfiniteQueryThunkArg<any>),
426
+ // Supply these even if undefined. This helps with a field existence
427
+ // check over in `buildSlice.ts`
428
+ direction,
429
+ initialPageParam,
430
+ refetchCachedPages,
431
+ })
432
+ }
433
+
434
+ const selector = (
435
+ api.endpoints[endpointName] as ApiEndpointQuery<any, any>
436
+ ).select(arg)
437
+
438
+ const thunkResult = dispatch(thunk)
439
+ const stateAfter = selector(getState())
440
+
441
+ middlewareWarning(dispatch)
442
+
443
+ const { requestId, abort } = thunkResult
444
+
445
+ const skippedSynchronously = stateAfter.requestId !== requestId
446
+
447
+ const runningQuery = getRunningQueries(dispatch)?.get(queryCacheKey)
448
+ const selectFromState = () => selector(getState())
449
+
450
+ const statePromise: AnyActionCreatorResult = Object.assign(
451
+ (forceQueryFn
452
+ ? // a query has been forced (upsertQueryData)
453
+ // -> we want to resolve it once data has been written with the data that will be written
454
+ thunkResult.then(selectFromState)
455
+ : skippedSynchronously && !runningQuery
456
+ ? // a query has been skipped due to a condition and we do not have any currently running query
457
+ // -> we want to resolve it immediately with the current data
458
+ Promise.resolve(stateAfter)
459
+ : // query just started or one is already in flight
460
+ // -> wait for the running query, then resolve with data from after that
461
+ Promise.all([runningQuery, thunkResult]).then(
462
+ selectFromState,
463
+ )) as SafePromise<any>,
464
+ {
465
+ arg,
466
+ requestId,
467
+ subscriptionOptions,
468
+ queryCacheKey,
469
+ abort,
470
+ async unwrap() {
471
+ const result = await statePromise
472
+
473
+ if (result.isError) {
474
+ throw result.error
475
+ }
476
+
477
+ return result.data
478
+ },
479
+ refetch: (options?: RefetchOptions) =>
480
+ dispatch(
481
+ queryAction(arg, {
482
+ subscribe: false,
483
+ forceRefetch: true,
484
+ ...options,
485
+ }),
486
+ ),
487
+ unsubscribe() {
488
+ if (subscribe)
489
+ dispatch(
490
+ unsubscribeQueryResult({
491
+ queryCacheKey,
492
+ requestId,
493
+ }),
494
+ )
495
+ },
496
+ updateSubscriptionOptions(options: SubscriptionOptions) {
497
+ statePromise.subscriptionOptions = options
498
+ dispatch(
499
+ updateSubscriptionOptions({
500
+ endpointName,
501
+ requestId,
502
+ queryCacheKey,
503
+ options,
504
+ }),
505
+ )
506
+ },
507
+ },
508
+ )
509
+
510
+ if (!runningQuery && !skippedSynchronously && !forceQueryFn) {
511
+ const runningQueries = getRunningQueries(dispatch)!
512
+ runningQueries.set(queryCacheKey, statePromise)
513
+
514
+ statePromise.then(() => {
515
+ runningQueries.delete(queryCacheKey)
516
+ })
517
+ }
518
+
519
+ return statePromise
520
+ }
521
+ return queryAction
522
+ }
523
+
524
+ function buildInitiateQuery(
525
+ endpointName: string,
526
+ endpointDefinition: QueryDefinition<any, any, any, any>,
527
+ ) {
528
+ const queryAction: StartQueryActionCreator<any> = buildInitiateAnyQuery(
529
+ endpointName,
530
+ endpointDefinition,
531
+ )
532
+
533
+ return queryAction
534
+ }
535
+
536
+ function buildInitiateInfiniteQuery(
537
+ endpointName: string,
538
+ endpointDefinition: InfiniteQueryDefinition<any, any, any, any, any>,
539
+ ) {
540
+ const infiniteQueryAction: StartInfiniteQueryActionCreator<any> =
541
+ buildInitiateAnyQuery(endpointName, endpointDefinition)
542
+
543
+ return infiniteQueryAction
544
+ }
545
+
546
+ function buildInitiateMutation(
547
+ endpointName: string,
548
+ ): StartMutationActionCreator<any> {
549
+ return (arg, { track = true, fixedCacheKey } = {}) =>
550
+ (dispatch, getState) => {
551
+ const thunk = mutationThunk({
552
+ type: 'mutation',
553
+ endpointName,
554
+ originalArgs: arg,
555
+ track,
556
+ fixedCacheKey,
557
+ })
558
+ const thunkResult = dispatch(thunk)
559
+ middlewareWarning(dispatch)
560
+ const { requestId, abort, unwrap } = thunkResult
561
+ const returnValuePromise = asSafePromise(
562
+ thunkResult.unwrap().then((data) => ({ data })),
563
+ (error) => ({ error }),
564
+ )
565
+
566
+ const reset = () => {
567
+ dispatch(removeMutationResult({ requestId, fixedCacheKey }))
568
+ }
569
+
570
+ const ret = Object.assign(returnValuePromise, {
571
+ arg: thunkResult.arg,
572
+ requestId,
573
+ abort,
574
+ unwrap,
575
+ reset,
576
+ })
577
+
578
+ const runningMutations = getRunningMutations(dispatch)!
579
+
580
+ runningMutations.set(requestId, ret)
581
+ ret.then(() => {
582
+ runningMutations.delete(requestId)
583
+ })
584
+ if (fixedCacheKey) {
585
+ runningMutations.set(fixedCacheKey, ret)
586
+ ret.then(() => {
587
+ if (runningMutations.get(fixedCacheKey) === ret) {
588
+ runningMutations.delete(fixedCacheKey)
589
+ }
590
+ })
591
+ }
592
+
593
+ return ret
594
+ }
595
+ }
596
+ }