UNPKG

7.91 kBPlain TextView Raw
1import type { SerializedError } from '@reduxjs/toolkit'
2import type { BaseQueryError } from '../baseQueryTypes'
3import type {
4 QueryDefinition,
5 MutationDefinition,
6 EndpointDefinitions,
7 BaseEndpointDefinition,
8 ResultTypeFrom,
9 QueryArgFrom,
10} from '../endpointDefinitions'
11import type { Id, WithRequiredProp } from '../tsHelpers'
12
13export type QueryCacheKey = string & { _type: 'queryCacheKey' }
14export type QuerySubstateIdentifier = { queryCacheKey: QueryCacheKey }
15export type MutationSubstateIdentifier =
16 | {
17 requestId: string
18 fixedCacheKey?: string
19 }
20 | {
21 requestId?: string
22 fixedCacheKey: string
23 }
24
25export type RefetchConfigOptions = {
26 refetchOnMountOrArgChange: boolean | number
27 refetchOnReconnect: boolean
28 refetchOnFocus: boolean
29}
30
31/**
32 * Strings describing the query state at any given time.
33 */
34export enum QueryStatus {
35 uninitialized = 'uninitialized',
36 pending = 'pending',
37 fulfilled = 'fulfilled',
38 rejected = 'rejected',
39}
40
41export 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
71export 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
81export type SubscriptionOptions = {
82 /**
83 * How frequently to automatically re-fetch data (in milliseconds). Defaults to `0` (off).
84 */
85 pollingInterval?: number
86 /**
87 * Defaults to 'false'. This setting allows you to control whether RTK Query will continue polling if the window is not focused.
88 *
89 * If pollingInterval is not set or set to 0, this **will not be evaluated** until pollingInterval is greater than 0.
90 *
91 * Note: requires [`setupListeners`](./setupListeners) to have been called.
92 */
93 skipPollingIfUnfocused?: boolean
94 /**
95 * Defaults to `false`. This setting allows you to control whether RTK Query will try to refetch all subscribed queries after regaining a network connection.
96 *
97 * If you specify this option alongside `skip: true`, this **will not be evaluated** until `skip` is false.
98 *
99 * Note: requires [`setupListeners`](./setupListeners) to have been called.
100 */
101 refetchOnReconnect?: boolean
102 /**
103 * 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.
104 *
105 * If you specify this option alongside `skip: true`, this **will not be evaluated** until `skip` is false.
106 *
107 * Note: requires [`setupListeners`](./setupListeners) to have been called.
108 */
109 refetchOnFocus?: boolean
110}
111export type Subscribers = { [requestId: string]: SubscriptionOptions }
112export 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]
122export 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
133type BaseQuerySubState<D extends BaseEndpointDefinition<any, any, any>> = {
134 /**
135 * The argument originally passed into the hook or `initiate` action call
136 */
137 originalArgs: QueryArgFrom<D>
138 /**
139 * A unique ID associated with the request
140 */
141 requestId: string
142 /**
143 * The received data from the query
144 */
145 data?: ResultTypeFrom<D>
146 /**
147 * The received error if applicable
148 */
149 error?:
150 | SerializedError
151 | (D extends QueryDefinition<any, infer BaseQuery, any, any>
152 ? BaseQueryError<BaseQuery>
153 : never)
154 /**
155 * The name of the endpoint associated with the query
156 */
157 endpointName: string
158 /**
159 * Time that the latest query started
160 */
161 startedTimeStamp: number
162 /**
163 * Time that the latest query was fulfilled
164 */
165 fulfilledTimeStamp?: number
166}
167
168export 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
193type 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
206export 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
229export 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
241export type InvalidationState<TagTypes extends string> = {
242 [_ in TagTypes]: {
243 [id: string]: Array<QueryCacheKey>
244 [id: number]: Array<QueryCacheKey>
245 }
246}
247
248export type QueryState<D extends EndpointDefinitions> = {
249 [queryCacheKey: string]: QuerySubState<D[string]> | undefined
250}
251
252export type SubscriptionState = {
253 [queryCacheKey: string]: Subscribers | undefined
254}
255
256export type ConfigState<ReducerPath> = RefetchConfigOptions & {
257 reducerPath: ReducerPath
258 online: boolean
259 focused: boolean
260 middlewareRegistered: boolean | 'conflict'
261} & ModifiableConfigState
262
263export type ModifiableConfigState = {
264 keepUnusedDataFor: number
265 invalidationBehavior: 'delayed' | 'immediately'
266} & RefetchConfigOptions
267
268export type MutationState<D extends EndpointDefinitions> = {
269 [requestId: string]: MutationSubState<D[string]> | undefined
270}
271
272export type RootState<
273 Definitions extends EndpointDefinitions,
274 TagTypes extends string,
275 ReducerPath extends string,
276> = {
277 [P in ReducerPath]: CombinedState<Definitions, TagTypes, P>
278}