11import type { SerializedError } from '@reduxjs/toolkit'
22import type { BaseQueryError } from '../baseQueryTypes'
33import type {
4- QueryDefinition ,
5- MutationDefinition ,
6- EndpointDefinitions ,
74 BaseEndpointDefinition ,
8- ResultTypeFrom ,
5+ EndpointDefinitions ,
6+ InfiniteQueryDefinition ,
7+ MutationDefinition ,
8+ PageParamFrom ,
99 QueryArgFrom ,
10+ QueryDefinition ,
11+ ResultTypeFrom ,
1012} from '../endpointDefinitions'
1113import type { Id , WithRequiredProp } from '../tsHelpers'
1214
@@ -28,6 +30,35 @@ export type RefetchConfigOptions = {
2830 refetchOnFocus : boolean
2931}
3032
33+ export type GetNextPageParamFunction < TPageParam , TQueryFnData > = (
34+ lastPage : TQueryFnData ,
35+ allPages : Array < TQueryFnData > ,
36+ lastPageParam : TPageParam ,
37+ allPageParams : Array < TPageParam > ,
38+ ) => TPageParam | undefined | null
39+
40+ export type GetPreviousPageParamFunction < TPageParam , TQueryFnData > = (
41+ firstPage : TQueryFnData ,
42+ allPages : Array < TQueryFnData > ,
43+ firstPageParam : TPageParam ,
44+ allPageParams : Array < TPageParam > ,
45+ ) => TPageParam | undefined | null
46+
47+ export type InfiniteQueryConfigOptions < TQueryFnData , TPageParam > = {
48+ initialPageParam : TPageParam
49+ /**
50+ * This function can be set to automatically get the previous cursor for infinite queries.
51+ * The result will also be used to determine the value of `hasPreviousPage`.
52+ */
53+ getPreviousPageParam ?: GetPreviousPageParamFunction < TPageParam , TQueryFnData >
54+ getNextPageParam : GetNextPageParamFunction < TPageParam , TQueryFnData >
55+ }
56+
57+ export interface InfiniteData < TData , TPageParam > {
58+ pages : Array < TData >
59+ pageParams : Array < TPageParam >
60+ }
61+
3162/**
3263 * Strings describing the query state at any given time.
3364 */
@@ -133,7 +164,10 @@ export type MutationKeys<Definitions extends EndpointDefinitions> = {
133164 : never
134165} [ keyof Definitions ]
135166
136- type BaseQuerySubState < D extends BaseEndpointDefinition < any , any , any > > = {
167+ type BaseQuerySubState <
168+ D extends BaseEndpointDefinition < any , any , any > ,
169+ DataType = ResultTypeFrom < D > ,
170+ > = {
137171 /**
138172 * The argument originally passed into the hook or `initiate` action call
139173 */
@@ -145,7 +179,7 @@ type BaseQuerySubState<D extends BaseEndpointDefinition<any, any, any>> = {
145179 /**
146180 * The received data from the query
147181 */
148- data ?: ResultTypeFrom < D >
182+ data ?: DataType
149183 /**
150184 * The received error if applicable
151185 */
@@ -166,21 +200,31 @@ type BaseQuerySubState<D extends BaseEndpointDefinition<any, any, any>> = {
166200 * Time that the latest query was fulfilled
167201 */
168202 fulfilledTimeStamp ?: number
203+ /**
204+ * Infinite Query Specific substate properties
205+ */
206+ hasNextPage ?: boolean
207+ hasPreviousPage ?: boolean
208+ direction ?: 'forward' | 'backward'
209+ param ?: QueryArgFrom < D >
169210}
170211
171- export type QuerySubState < D extends BaseEndpointDefinition < any , any , any > > = Id <
212+ export type QuerySubState <
213+ D extends BaseEndpointDefinition < any , any , any > ,
214+ DataType = ResultTypeFrom < D > ,
215+ > = Id <
172216 | ( {
173217 status : QueryStatus . fulfilled
174218 } & WithRequiredProp <
175- BaseQuerySubState < D > ,
219+ BaseQuerySubState < D , DataType > ,
176220 'data' | 'fulfilledTimeStamp'
177221 > & { error : undefined } )
178222 | ( {
179223 status : QueryStatus . pending
180- } & BaseQuerySubState < D > )
224+ } & BaseQuerySubState < D , DataType > )
181225 | ( {
182226 status : QueryStatus . rejected
183- } & WithRequiredProp < BaseQuerySubState < D > , 'error' > )
227+ } & WithRequiredProp < BaseQuerySubState < D , DataType > , 'error' > )
184228 | {
185229 status : QueryStatus . uninitialized
186230 originalArgs ?: undefined
@@ -193,6 +237,21 @@ export type QuerySubState<D extends BaseEndpointDefinition<any, any, any>> = Id<
193237 }
194238>
195239
240+ export type InfiniteQuerySubState <
241+ D extends BaseEndpointDefinition < any , any , any > ,
242+ > =
243+ D extends InfiniteQueryDefinition < any , any , any , any , any >
244+ ? QuerySubState < D , InfiniteData < ResultTypeFrom < D > , PageParamFrom < D > > > & {
245+ // TODO: These shouldn't be optional
246+ hasNextPage ?: boolean
247+ hasPreviousPage ?: boolean
248+ isFetchingNextPage ?: boolean
249+ isFetchingPreviousPage ?: boolean
250+ param ?: PageParamFrom < D >
251+ direction ?: 'forward' | 'backward'
252+ }
253+ : never
254+
196255type BaseMutationSubState < D extends BaseEndpointDefinition < any , any , any > > = {
197256 requestId : string
198257 data ?: ResultTypeFrom < D >
@@ -249,7 +308,10 @@ export type InvalidationState<TagTypes extends string> = {
249308}
250309
251310export type QueryState < D extends EndpointDefinitions > = {
252- [ queryCacheKey : string ] : QuerySubState < D [ string ] > | undefined
311+ [ queryCacheKey : string ] :
312+ | QuerySubState < D [ string ] >
313+ | InfiniteQuerySubState < D [ string ] >
314+ | undefined
253315}
254316
255317export type SubscriptionState = {
0 commit comments