1 | import type { SerializedError } from '@reduxjs/toolkit'
|
2 | import type { BaseQueryError } from '../baseQueryTypes'
|
3 | import type {
|
4 | QueryDefinition,
|
5 | MutationDefinition,
|
6 | EndpointDefinitions,
|
7 | BaseEndpointDefinition,
|
8 | ResultTypeFrom,
|
9 | QueryArgFrom,
|
10 | } from '../endpointDefinitions'
|
11 | import type { Id, WithRequiredProp } from '../tsHelpers'
|
12 |
|
13 | export type QueryCacheKey = string & { _type: 'queryCacheKey' }
|
14 | export type QuerySubstateIdentifier = { queryCacheKey: QueryCacheKey }
|
15 | export type MutationSubstateIdentifier =
|
16 | | {
|
17 | requestId: string
|
18 | fixedCacheKey?: string
|
19 | }
|
20 | | {
|
21 | requestId?: string
|
22 | fixedCacheKey: string
|
23 | }
|
24 |
|
25 | export type RefetchConfigOptions = {
|
26 | refetchOnMountOrArgChange: boolean | number
|
27 | refetchOnReconnect: boolean
|
28 | refetchOnFocus: boolean
|
29 | }
|
30 |
|
31 |
|
32 |
|
33 |
|
34 | export enum QueryStatus {
|
35 | uninitialized = 'uninitialized',
|
36 | pending = 'pending',
|
37 | fulfilled = 'fulfilled',
|
38 | rejected = 'rejected',
|
39 | }
|
40 |
|
41 | export type RequestStatusFlags =
|
42 | | {
|
43 | status: QueryStatus.uninitialized
|
44 | isUninitialized: true
|
45 | isLoading: false
|
46 | isSuccess: false
|
47 | isError: false
|
48 | }
|
49 | | {
|
50 | status: QueryStatus.pending
|
51 | isUninitialized: false
|
52 | isLoading: true
|
53 | isSuccess: false
|
54 | isError: false
|
55 | }
|
56 | | {
|
57 | status: QueryStatus.fulfilled
|
58 | isUninitialized: false
|
59 | isLoading: false
|
60 | isSuccess: true
|
61 | isError: false
|
62 | }
|
63 | | {
|
64 | status: QueryStatus.rejected
|
65 | isUninitialized: false
|
66 | isLoading: false
|
67 | isSuccess: false
|
68 | isError: true
|
69 | }
|
70 |
|
71 | export function getRequestStatusFlags(status: QueryStatus): RequestStatusFlags {
|
72 | return {
|
73 | status,
|
74 | isUninitialized: status === QueryStatus.uninitialized,
|
75 | isLoading: status === QueryStatus.pending,
|
76 | isSuccess: status === QueryStatus.fulfilled,
|
77 | isError: status === QueryStatus.rejected,
|
78 | } as any
|
79 | }
|
80 |
|
81 | export type SubscriptionOptions = {
|
82 | |
83 |
|
84 |
|
85 | pollingInterval?: number
|
86 | |
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 | skipPollingIfUnfocused?: boolean
|
94 | |
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 | refetchOnReconnect?: boolean
|
102 | |
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 | refetchOnFocus?: boolean
|
110 | }
|
111 | export type Subscribers = { [requestId: string]: SubscriptionOptions }
|
112 | export type QueryKeys<Definitions extends EndpointDefinitions> = {
|
113 | [K in keyof Definitions]: Definitions[K] extends QueryDefinition<
|
114 | any,
|
115 | any,
|
116 | any,
|
117 | any
|
118 | >
|
119 | ? K
|
120 | : never
|
121 | }[keyof Definitions]
|
122 | export type MutationKeys<Definitions extends EndpointDefinitions> = {
|
123 | [K in keyof Definitions]: Definitions[K] extends MutationDefinition<
|
124 | any,
|
125 | any,
|
126 | any,
|
127 | any
|
128 | >
|
129 | ? K
|
130 | : never
|
131 | }[keyof Definitions]
|
132 |
|
133 | type BaseQuerySubState<D extends BaseEndpointDefinition<any, any, any>> = {
|
134 | |
135 |
|
136 |
|
137 | originalArgs: QueryArgFrom<D>
|
138 | |
139 |
|
140 |
|
141 | requestId: string
|
142 | |
143 |
|
144 |
|
145 | data?: ResultTypeFrom<D>
|
146 | |
147 |
|
148 |
|
149 | error?:
|
150 | | SerializedError
|
151 | | (D extends QueryDefinition<any, infer BaseQuery, any, any>
|
152 | ? BaseQueryError<BaseQuery>
|
153 | : never)
|
154 | |
155 |
|
156 |
|
157 | endpointName: string
|
158 | |
159 |
|
160 |
|
161 | startedTimeStamp: number
|
162 | |
163 |
|
164 |
|
165 | fulfilledTimeStamp?: number
|
166 | }
|
167 |
|
168 | export type QuerySubState<D extends BaseEndpointDefinition<any, any, any>> = Id<
|
169 | | ({
|
170 | status: QueryStatus.fulfilled
|
171 | } & WithRequiredProp<
|
172 | BaseQuerySubState<D>,
|
173 | 'data' | 'fulfilledTimeStamp'
|
174 | > & { error: undefined })
|
175 | | ({
|
176 | status: QueryStatus.pending
|
177 | } & BaseQuerySubState<D>)
|
178 | | ({
|
179 | status: QueryStatus.rejected
|
180 | } & WithRequiredProp<BaseQuerySubState<D>, 'error'>)
|
181 | | {
|
182 | status: QueryStatus.uninitialized
|
183 | originalArgs?: undefined
|
184 | data?: undefined
|
185 | error?: undefined
|
186 | requestId?: undefined
|
187 | endpointName?: string
|
188 | startedTimeStamp?: undefined
|
189 | fulfilledTimeStamp?: undefined
|
190 | }
|
191 | >
|
192 |
|
193 | type BaseMutationSubState<D extends BaseEndpointDefinition<any, any, any>> = {
|
194 | requestId: string
|
195 | data?: ResultTypeFrom<D>
|
196 | error?:
|
197 | | SerializedError
|
198 | | (D extends MutationDefinition<any, infer BaseQuery, any, any>
|
199 | ? BaseQueryError<BaseQuery>
|
200 | : never)
|
201 | endpointName: string
|
202 | startedTimeStamp: number
|
203 | fulfilledTimeStamp?: number
|
204 | }
|
205 |
|
206 | export type MutationSubState<D extends BaseEndpointDefinition<any, any, any>> =
|
207 | | (({
|
208 | status: QueryStatus.fulfilled
|
209 | } & WithRequiredProp<
|
210 | BaseMutationSubState<D>,
|
211 | 'data' | 'fulfilledTimeStamp'
|
212 | >) & { error: undefined })
|
213 | | (({
|
214 | status: QueryStatus.pending
|
215 | } & BaseMutationSubState<D>) & { data?: undefined })
|
216 | | ({
|
217 | status: QueryStatus.rejected
|
218 | } & WithRequiredProp<BaseMutationSubState<D>, 'error'>)
|
219 | | {
|
220 | requestId?: undefined
|
221 | status: QueryStatus.uninitialized
|
222 | data?: undefined
|
223 | error?: undefined
|
224 | endpointName?: string
|
225 | startedTimeStamp?: undefined
|
226 | fulfilledTimeStamp?: undefined
|
227 | }
|
228 |
|
229 | export type CombinedState<
|
230 | D extends EndpointDefinitions,
|
231 | E extends string,
|
232 | ReducerPath extends string,
|
233 | > = {
|
234 | queries: QueryState<D>
|
235 | mutations: MutationState<D>
|
236 | provided: InvalidationState<E>
|
237 | subscriptions: SubscriptionState
|
238 | config: ConfigState<ReducerPath>
|
239 | }
|
240 |
|
241 | export type InvalidationState<TagTypes extends string> = {
|
242 | [_ in TagTypes]: {
|
243 | [id: string]: Array<QueryCacheKey>
|
244 | [id: number]: Array<QueryCacheKey>
|
245 | }
|
246 | }
|
247 |
|
248 | export type QueryState<D extends EndpointDefinitions> = {
|
249 | [queryCacheKey: string]: QuerySubState<D[string]> | undefined
|
250 | }
|
251 |
|
252 | export type SubscriptionState = {
|
253 | [queryCacheKey: string]: Subscribers | undefined
|
254 | }
|
255 |
|
256 | export type ConfigState<ReducerPath> = RefetchConfigOptions & {
|
257 | reducerPath: ReducerPath
|
258 | online: boolean
|
259 | focused: boolean
|
260 | middlewareRegistered: boolean | 'conflict'
|
261 | } & ModifiableConfigState
|
262 |
|
263 | export type ModifiableConfigState = {
|
264 | keepUnusedDataFor: number
|
265 | invalidationBehavior: 'delayed' | 'immediately'
|
266 | } & RefetchConfigOptions
|
267 |
|
268 | export type MutationState<D extends EndpointDefinitions> = {
|
269 | [requestId: string]: MutationSubState<D[string]> | undefined
|
270 | }
|
271 |
|
272 | export type RootState<
|
273 | Definitions extends EndpointDefinitions,
|
274 | TagTypes extends string,
|
275 | ReducerPath extends string,
|
276 | > = {
|
277 | [P in ReducerPath]: CombinedState<Definitions, TagTypes, P>
|
278 | }
|