@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
package/src/nanoid.ts ADDED
@@ -0,0 +1,20 @@
1
+ // Borrowed from https://github.com/ai/nanoid/blob/3.0.2/non-secure/index.js
2
+ // This alphabet uses `A-Za-z0-9_-` symbols. A genetic algorithm helped
3
+ // optimize the gzip compression for this alphabet.
4
+ let urlAlphabet =
5
+ 'ModuleSymbhasOwnPr-0123456789ABCDEFGHNRVfgctiUvz_KqYTJkLxpZXIjQW'
6
+
7
+ /**
8
+ *
9
+ * @public
10
+ */
11
+ export let nanoid = (size = 21) => {
12
+ let id = ''
13
+ // A compact alternative for `for (var i = 0; i < step; i++)`.
14
+ let i = size
15
+ while (i--) {
16
+ // `| 0` is more compact and faster than `Math.floor()`.
17
+ id += urlAlphabet[(Math.random() * 64) | 0]
18
+ }
19
+ return id
20
+ }
@@ -0,0 +1,6 @@
1
+ export class HandledError {
2
+ constructor(
3
+ public readonly value: any,
4
+ public readonly meta: any = undefined,
5
+ ) {}
6
+ }
@@ -0,0 +1,120 @@
1
+ import type { UnknownAction } from '@reduxjs/toolkit'
2
+ import type { BaseQueryFn } from './baseQueryTypes'
3
+ import type { CombinedState, CoreModule } from './core/index'
4
+ import type { ApiModules } from './core/module'
5
+ import type { CreateApiOptions } from './createApi'
6
+ import type {
7
+ EndpointBuilder,
8
+ EndpointDefinition,
9
+ EndpointDefinitions,
10
+ UpdateDefinitions,
11
+ } from './endpointDefinitions'
12
+ import type { UnionToIntersection, WithRequiredProp } from './tsHelpers'
13
+
14
+ export type ModuleName = keyof ApiModules<any, any, any, any>
15
+
16
+ export type Module<Name extends ModuleName> = {
17
+ name: Name
18
+ init<
19
+ BaseQuery extends BaseQueryFn,
20
+ Definitions extends EndpointDefinitions,
21
+ ReducerPath extends string,
22
+ TagTypes extends string,
23
+ >(
24
+ api: Api<BaseQuery, EndpointDefinitions, ReducerPath, TagTypes, ModuleName>,
25
+ options: WithRequiredProp<
26
+ CreateApiOptions<BaseQuery, Definitions, ReducerPath, TagTypes>,
27
+ | 'reducerPath'
28
+ | 'serializeQueryArgs'
29
+ | 'keepUnusedDataFor'
30
+ | 'refetchOnMountOrArgChange'
31
+ | 'refetchOnFocus'
32
+ | 'refetchOnReconnect'
33
+ | 'invalidationBehavior'
34
+ | 'tagTypes'
35
+ >,
36
+ context: ApiContext<Definitions>,
37
+ ): {
38
+ injectEndpoint(
39
+ endpointName: string,
40
+ definition: EndpointDefinition<any, any, any, any>,
41
+ ): void
42
+ }
43
+ }
44
+
45
+ export interface ApiContext<Definitions extends EndpointDefinitions> {
46
+ apiUid: string
47
+ endpointDefinitions: Definitions
48
+ batch(cb: () => void): void
49
+ extractRehydrationInfo: (
50
+ action: UnknownAction,
51
+ ) => CombinedState<any, any, any> | undefined
52
+ hasRehydrationInfo: (action: UnknownAction) => boolean
53
+ }
54
+
55
+ export const getEndpointDefinition = <
56
+ Definitions extends EndpointDefinitions,
57
+ EndpointName extends keyof Definitions,
58
+ >(
59
+ context: ApiContext<Definitions>,
60
+ endpointName: EndpointName,
61
+ ) => context.endpointDefinitions[endpointName]
62
+
63
+ export type Api<
64
+ BaseQuery extends BaseQueryFn,
65
+ Definitions extends EndpointDefinitions,
66
+ ReducerPath extends string,
67
+ TagTypes extends string,
68
+ Enhancers extends ModuleName = CoreModule,
69
+ > = UnionToIntersection<
70
+ ApiModules<BaseQuery, Definitions, ReducerPath, TagTypes>[Enhancers]
71
+ > & {
72
+ /**
73
+ * A function to inject the endpoints into the original API, but also give you that same API with correct types for these endpoints back. Useful with code-splitting.
74
+ */
75
+ injectEndpoints<NewDefinitions extends EndpointDefinitions>(_: {
76
+ endpoints: (
77
+ build: EndpointBuilder<BaseQuery, TagTypes, ReducerPath>,
78
+ ) => NewDefinitions
79
+ /**
80
+ * Optionally allows endpoints to be overridden if defined by multiple `injectEndpoints` calls.
81
+ *
82
+ * If set to `true`, will override existing endpoints with the new definition.
83
+ * If set to `'throw'`, will throw an error if an endpoint is redefined with a different definition.
84
+ * If set to `false` (or unset), will not override existing endpoints with the new definition, and log a warning in development.
85
+ */
86
+ overrideExisting?: boolean | 'throw'
87
+ }): Api<
88
+ BaseQuery,
89
+ Definitions & NewDefinitions,
90
+ ReducerPath,
91
+ TagTypes,
92
+ Enhancers
93
+ >
94
+ /**
95
+ *A function to enhance a generated API with additional information. Useful with code-generation.
96
+ */
97
+ enhanceEndpoints<
98
+ NewTagTypes extends string = never,
99
+ NewDefinitions extends EndpointDefinitions = never,
100
+ >(_: {
101
+ addTagTypes?: readonly NewTagTypes[]
102
+ endpoints?: UpdateDefinitions<
103
+ Definitions,
104
+ TagTypes | NoInfer<NewTagTypes>,
105
+ NewDefinitions
106
+ > extends infer NewDefinitions
107
+ ? {
108
+ [K in keyof NewDefinitions]?:
109
+ | Partial<NewDefinitions[K]>
110
+ | ((definition: NewDefinitions[K]) => void)
111
+ }
112
+ : never
113
+ }): Api<
114
+ BaseQuery,
115
+ UpdateDefinitions<Definitions, TagTypes | NewTagTypes, NewDefinitions>,
116
+ ReducerPath,
117
+ TagTypes | NewTagTypes,
118
+ Enhancers
119
+ >
120
+ }
@@ -0,0 +1,101 @@
1
+ import type { ThunkDispatch } from '@reduxjs/toolkit'
2
+ import type { MaybePromise, UnwrapPromise } from './tsHelpers'
3
+
4
+ export interface BaseQueryApi {
5
+ signal: AbortSignal
6
+ abort: (reason?: string) => void
7
+ dispatch: ThunkDispatch<any, any, any>
8
+ getState: () => unknown
9
+ extra: unknown
10
+ endpoint: string
11
+ type: 'query' | 'mutation'
12
+ /**
13
+ * Only available for queries: indicates if a query has been forced,
14
+ * i.e. it would have been fetched even if there would already be a cache entry
15
+ * (this does not mean that there is already a cache entry though!)
16
+ *
17
+ * This can be used to for example add a `Cache-Control: no-cache` header for
18
+ * invalidated queries.
19
+ */
20
+ forced?: boolean
21
+ /**
22
+ * Only available for queries: the cache key that was used to store the query result
23
+ */
24
+ queryCacheKey?: string
25
+ }
26
+
27
+ export type QueryReturnValue<T = unknown, E = unknown, M = unknown> =
28
+ | {
29
+ error: E
30
+ data?: undefined
31
+ meta?: M
32
+ }
33
+ | {
34
+ error?: undefined
35
+ data: T
36
+ meta?: M
37
+ }
38
+
39
+ export type BaseQueryFn<
40
+ Args = any,
41
+ Result = unknown,
42
+ Error = unknown,
43
+ DefinitionExtraOptions = {},
44
+ Meta = {},
45
+ > = (
46
+ args: Args,
47
+ api: BaseQueryApi,
48
+ extraOptions: DefinitionExtraOptions,
49
+ ) => MaybePromise<QueryReturnValue<Result, Error, Meta>>
50
+
51
+ export type BaseQueryEnhancer<
52
+ AdditionalArgs = unknown,
53
+ AdditionalDefinitionExtraOptions = unknown,
54
+ Config = void,
55
+ > = <BaseQuery extends BaseQueryFn>(
56
+ baseQuery: BaseQuery,
57
+ config: Config,
58
+ ) => BaseQueryFn<
59
+ BaseQueryArg<BaseQuery> & AdditionalArgs,
60
+ BaseQueryResult<BaseQuery>,
61
+ BaseQueryError<BaseQuery>,
62
+ BaseQueryExtraOptions<BaseQuery> & AdditionalDefinitionExtraOptions,
63
+ NonNullable<BaseQueryMeta<BaseQuery>>
64
+ >
65
+
66
+ /**
67
+ * @public
68
+ */
69
+ export type BaseQueryResult<BaseQuery extends BaseQueryFn> =
70
+ UnwrapPromise<ReturnType<BaseQuery>> extends infer Unwrapped
71
+ ? Unwrapped extends { data: any }
72
+ ? Unwrapped['data']
73
+ : never
74
+ : never
75
+
76
+ /**
77
+ * @public
78
+ */
79
+ export type BaseQueryMeta<BaseQuery extends BaseQueryFn> = UnwrapPromise<
80
+ ReturnType<BaseQuery>
81
+ >['meta']
82
+
83
+ /**
84
+ * @public
85
+ */
86
+ export type BaseQueryError<BaseQuery extends BaseQueryFn> = Exclude<
87
+ UnwrapPromise<ReturnType<BaseQuery>>,
88
+ { error?: undefined }
89
+ >['error']
90
+
91
+ /**
92
+ * @public
93
+ */
94
+ export type BaseQueryArg<T extends (arg: any, ...args: any[]) => any> =
95
+ T extends (arg: infer A, ...args: any[]) => any ? A : any
96
+
97
+ /**
98
+ * @public
99
+ */
100
+ export type BaseQueryExtraOptions<BaseQuery extends BaseQueryFn> =
101
+ Parameters<BaseQuery>[2]
@@ -0,0 +1,377 @@
1
+ import type { SerializedError } from '@reduxjs/toolkit'
2
+ import type { BaseQueryError } from '../baseQueryTypes'
3
+ import type {
4
+ BaseEndpointDefinition,
5
+ EndpointDefinitions,
6
+ FullTagDescription,
7
+ InfiniteQueryDefinition,
8
+ MutationDefinition,
9
+ PageParamFrom,
10
+ QueryArgFromAnyQuery,
11
+ QueryDefinition,
12
+ ResultTypeFrom,
13
+ } from '../endpointDefinitions'
14
+ import type { Id, WithRequiredProp } from '../tsHelpers'
15
+
16
+ export type QueryCacheKey = string & { _type: 'queryCacheKey' }
17
+ export type QuerySubstateIdentifier = { queryCacheKey: QueryCacheKey }
18
+ export type MutationSubstateIdentifier =
19
+ | { requestId: string; fixedCacheKey?: string }
20
+ | { requestId?: string; fixedCacheKey: string }
21
+
22
+ export type RefetchConfigOptions = {
23
+ refetchOnMountOrArgChange: boolean | number
24
+ refetchOnReconnect: boolean
25
+ refetchOnFocus: boolean
26
+ }
27
+
28
+ export type InfiniteQueryConfigOptions<DataType, PageParam, QueryArg> = {
29
+ /**
30
+ * The initial page parameter to use for the first page fetch.
31
+ */
32
+ initialPageParam: PageParam
33
+ /**
34
+ * This function is required to automatically get the next cursor for infinite queries.
35
+ * The result will also be used to determine the value of `hasNextPage`.
36
+ */
37
+ getNextPageParam: (
38
+ lastPage: DataType,
39
+ allPages: Array<DataType>,
40
+ lastPageParam: PageParam,
41
+ allPageParams: Array<PageParam>,
42
+ queryArg: QueryArg,
43
+ ) => PageParam | undefined | null
44
+ /**
45
+ * This function can be set to automatically get the previous cursor for infinite queries.
46
+ * The result will also be used to determine the value of `hasPreviousPage`.
47
+ */
48
+ getPreviousPageParam?: (
49
+ firstPage: DataType,
50
+ allPages: Array<DataType>,
51
+ firstPageParam: PageParam,
52
+ allPageParams: Array<PageParam>,
53
+ queryArg: QueryArg,
54
+ ) => PageParam | undefined | null
55
+ /**
56
+ * If specified, only keep this many pages in cache at once.
57
+ * If additional pages are fetched, older pages in the other
58
+ * direction will be dropped from the cache.
59
+ */
60
+ maxPages?: number
61
+ /**
62
+ * Defaults to `true`. When this is `true` and an infinite query endpoint is refetched
63
+ * (due to tag invalidation, polling, arg change configuration, or manual refetching),
64
+ * RTK Query will try to sequentially refetch all pages currently in the cache.
65
+ * When `false` only the first page will be refetched.
66
+ */
67
+ refetchCachedPages?: boolean
68
+ }
69
+
70
+ export type InfiniteData<DataType, PageParam> = {
71
+ pages: Array<DataType>
72
+ pageParams: Array<PageParam>
73
+ }
74
+
75
+ // NOTE: DO NOT import and use this for runtime comparisons internally,
76
+ // except in the RTKQ React package. Use the string versions just below this.
77
+ // ESBuild auto-inlines TS enums, which bloats our bundle with many repeated
78
+ // constants like "initialized":
79
+ // https://github.com/evanw/esbuild/releases/tag/v0.14.7
80
+ // We still have to use this in the React package since we don't publicly export
81
+ // the string constants below.
82
+ /**
83
+ * Strings describing the query state at any given time.
84
+ */
85
+ export enum QueryStatus {
86
+ uninitialized = 'uninitialized',
87
+ pending = 'pending',
88
+ fulfilled = 'fulfilled',
89
+ rejected = 'rejected',
90
+ }
91
+
92
+ // Use these string constants for runtime comparisons internally
93
+ export const STATUS_UNINITIALIZED = QueryStatus.uninitialized
94
+ export const STATUS_PENDING = QueryStatus.pending
95
+ export const STATUS_FULFILLED = QueryStatus.fulfilled
96
+ export const STATUS_REJECTED = QueryStatus.rejected
97
+
98
+ export type RequestStatusFlags =
99
+ | {
100
+ status: QueryStatus.uninitialized
101
+ isUninitialized: true
102
+ isLoading: false
103
+ isSuccess: false
104
+ isError: false
105
+ }
106
+ | {
107
+ status: QueryStatus.pending
108
+ isUninitialized: false
109
+ isLoading: true
110
+ isSuccess: false
111
+ isError: false
112
+ }
113
+ | {
114
+ status: QueryStatus.fulfilled
115
+ isUninitialized: false
116
+ isLoading: false
117
+ isSuccess: true
118
+ isError: false
119
+ }
120
+ | {
121
+ status: QueryStatus.rejected
122
+ isUninitialized: false
123
+ isLoading: false
124
+ isSuccess: false
125
+ isError: true
126
+ }
127
+
128
+ export function getRequestStatusFlags(status: QueryStatus): RequestStatusFlags {
129
+ return {
130
+ status,
131
+ isUninitialized: status === STATUS_UNINITIALIZED,
132
+ isLoading: status === STATUS_PENDING,
133
+ isSuccess: status === STATUS_FULFILLED,
134
+ isError: status === STATUS_REJECTED,
135
+ } as any
136
+ }
137
+
138
+ /**
139
+ * @public
140
+ */
141
+ export type SubscriptionOptions = {
142
+ /**
143
+ * How frequently to automatically re-fetch data (in milliseconds). Defaults to `0` (off).
144
+ */
145
+ pollingInterval?: number
146
+ /**
147
+ * Defaults to 'false'. This setting allows you to control whether RTK Query will continue polling if the window is not focused.
148
+ *
149
+ * If pollingInterval is not set or set to 0, this **will not be evaluated** until pollingInterval is greater than 0.
150
+ *
151
+ * Note: requires [`setupListeners`](./setupListeners) to have been called.
152
+ */
153
+ skipPollingIfUnfocused?: boolean
154
+ /**
155
+ * Defaults to `false`. This setting allows you to control whether RTK Query will try to refetch all subscribed queries after regaining a network connection.
156
+ *
157
+ * If you specify this option alongside `skip: true`, this **will not be evaluated** until `skip` is false.
158
+ *
159
+ * Note: requires [`setupListeners`](./setupListeners) to have been called.
160
+ */
161
+ refetchOnReconnect?: boolean
162
+ /**
163
+ * Defaults to `false`. This setting allows you to control whether RTK Query will try to refetch all subscribed queries after the application window regains focus.
164
+ *
165
+ * If you specify this option alongside `skip: true`, this **will not be evaluated** until `skip` is false.
166
+ *
167
+ * Note: requires [`setupListeners`](./setupListeners) to have been called.
168
+ */
169
+ refetchOnFocus?: boolean
170
+ }
171
+ export type SubscribersInternal = Map<string, SubscriptionOptions>
172
+ export type Subscribers = { [requestId: string]: SubscriptionOptions }
173
+ export type QueryKeys<Definitions extends EndpointDefinitions> = {
174
+ [K in keyof Definitions]: Definitions[K] extends QueryDefinition<
175
+ any,
176
+ any,
177
+ any,
178
+ any
179
+ >
180
+ ? K
181
+ : never
182
+ }[keyof Definitions]
183
+
184
+ export type InfiniteQueryKeys<Definitions extends EndpointDefinitions> = {
185
+ [K in keyof Definitions]: Definitions[K] extends InfiniteQueryDefinition<
186
+ any,
187
+ any,
188
+ any,
189
+ any,
190
+ any
191
+ >
192
+ ? K
193
+ : never
194
+ }[keyof Definitions]
195
+
196
+ export type MutationKeys<Definitions extends EndpointDefinitions> = {
197
+ [K in keyof Definitions]: Definitions[K] extends MutationDefinition<
198
+ any,
199
+ any,
200
+ any,
201
+ any
202
+ >
203
+ ? K
204
+ : never
205
+ }[keyof Definitions]
206
+
207
+ type BaseQuerySubState<
208
+ D extends BaseEndpointDefinition<any, any, any, any>,
209
+ DataType = ResultTypeFrom<D>,
210
+ > = {
211
+ /**
212
+ * The argument originally passed into the hook or `initiate` action call
213
+ */
214
+ originalArgs: QueryArgFromAnyQuery<D>
215
+ /**
216
+ * A unique ID associated with the request
217
+ */
218
+ requestId: string
219
+ /**
220
+ * The received data from the query
221
+ */
222
+ data?: DataType
223
+ /**
224
+ * The received error if applicable
225
+ */
226
+ error?:
227
+ | SerializedError
228
+ | (D extends QueryDefinition<any, infer BaseQuery, any, any>
229
+ ? BaseQueryError<BaseQuery>
230
+ : never)
231
+ /**
232
+ * The name of the endpoint associated with the query
233
+ */
234
+ endpointName: string
235
+ /**
236
+ * Time that the latest query started
237
+ */
238
+ startedTimeStamp: number
239
+ /**
240
+ * Time that the latest query was fulfilled
241
+ */
242
+ fulfilledTimeStamp?: number
243
+ }
244
+
245
+ export type QuerySubState<
246
+ D extends BaseEndpointDefinition<any, any, any, any>,
247
+ DataType = ResultTypeFrom<D>,
248
+ > = Id<
249
+ | ({ status: QueryStatus.fulfilled } & WithRequiredProp<
250
+ BaseQuerySubState<D, DataType>,
251
+ 'data' | 'fulfilledTimeStamp'
252
+ > & { error: undefined })
253
+ | ({ status: QueryStatus.pending } & BaseQuerySubState<D, DataType>)
254
+ | ({ status: QueryStatus.rejected } & WithRequiredProp<
255
+ BaseQuerySubState<D, DataType>,
256
+ 'error'
257
+ >)
258
+ | {
259
+ status: QueryStatus.uninitialized
260
+ originalArgs?: undefined
261
+ data?: undefined
262
+ error?: undefined
263
+ requestId?: undefined
264
+ endpointName?: string
265
+ startedTimeStamp?: undefined
266
+ fulfilledTimeStamp?: undefined
267
+ }
268
+ >
269
+
270
+ export type InfiniteQueryDirection = 'forward' | 'backward'
271
+
272
+ export type InfiniteQuerySubState<
273
+ D extends BaseEndpointDefinition<any, any, any, any>,
274
+ > =
275
+ D extends InfiniteQueryDefinition<any, any, any, any, any>
276
+ ? QuerySubState<D, InfiniteData<ResultTypeFrom<D>, PageParamFrom<D>>> & {
277
+ direction?: InfiniteQueryDirection
278
+ }
279
+ : never
280
+
281
+ type BaseMutationSubState<
282
+ D extends BaseEndpointDefinition<any, any, any, any>,
283
+ > = {
284
+ requestId: string
285
+ data?: ResultTypeFrom<D>
286
+ error?:
287
+ | SerializedError
288
+ | (D extends MutationDefinition<any, infer BaseQuery, any, any>
289
+ ? BaseQueryError<BaseQuery>
290
+ : never)
291
+ endpointName: string
292
+ startedTimeStamp: number
293
+ fulfilledTimeStamp?: number
294
+ }
295
+
296
+ export type MutationSubState<
297
+ D extends BaseEndpointDefinition<any, any, any, any>,
298
+ > =
299
+ | (({
300
+ status: QueryStatus.fulfilled
301
+ } & WithRequiredProp<
302
+ BaseMutationSubState<D>,
303
+ 'data' | 'fulfilledTimeStamp'
304
+ >) & { error: undefined })
305
+ | (({ status: QueryStatus.pending } & BaseMutationSubState<D>) & {
306
+ data?: undefined
307
+ })
308
+ | ({ status: QueryStatus.rejected } & WithRequiredProp<
309
+ BaseMutationSubState<D>,
310
+ 'error'
311
+ >)
312
+ | {
313
+ requestId?: undefined
314
+ status: QueryStatus.uninitialized
315
+ data?: undefined
316
+ error?: undefined
317
+ endpointName?: string
318
+ startedTimeStamp?: undefined
319
+ fulfilledTimeStamp?: undefined
320
+ }
321
+
322
+ export type CombinedState<
323
+ D extends EndpointDefinitions,
324
+ E extends string,
325
+ ReducerPath extends string,
326
+ > = {
327
+ queries: QueryState<D>
328
+ mutations: MutationState<D>
329
+ provided: InvalidationState<E>
330
+ subscriptions: SubscriptionState
331
+ config: ConfigState<ReducerPath>
332
+ }
333
+
334
+ export type InvalidationState<TagTypes extends string> = {
335
+ tags: {
336
+ [_ in TagTypes]: {
337
+ [id: string]: Array<QueryCacheKey>
338
+ [id: number]: Array<QueryCacheKey>
339
+ }
340
+ }
341
+ keys: Record<QueryCacheKey, Array<FullTagDescription<any>>>
342
+ }
343
+
344
+ export type QueryState<D extends EndpointDefinitions> = {
345
+ [queryCacheKey: string]:
346
+ | QuerySubState<D[string]>
347
+ | InfiniteQuerySubState<D[string]>
348
+ | undefined
349
+ }
350
+
351
+ export type SubscriptionInternalState = Map<string, SubscribersInternal>
352
+
353
+ export type SubscriptionState = {
354
+ [queryCacheKey: string]: Subscribers | undefined
355
+ }
356
+
357
+ export type ConfigState<ReducerPath> = RefetchConfigOptions & {
358
+ reducerPath: ReducerPath
359
+ online: boolean
360
+ focused: boolean
361
+ middlewareRegistered: boolean | 'conflict'
362
+ } & ModifiableConfigState
363
+
364
+ export type ModifiableConfigState = {
365
+ keepUnusedDataFor: number
366
+ invalidationBehavior: 'delayed' | 'immediately'
367
+ } & RefetchConfigOptions
368
+
369
+ export type MutationState<D extends EndpointDefinitions> = {
370
+ [requestId: string]: MutationSubState<D[string]> | undefined
371
+ }
372
+
373
+ export type RootState<
374
+ Definitions extends EndpointDefinitions,
375
+ TagTypes extends string,
376
+ ReducerPath extends string,
377
+ > = { [P in ReducerPath]: CombinedState<Definitions, TagTypes, P> }