@@ -2,25 +2,40 @@ import { SerializedError } from "@reduxjs/toolkit";
22import { FetchBaseQueryError } from "@reduxjs/toolkit/dist/query" ;
33import { ReactElement } from "react" ;
44
5+ /** Result of a RTK useQuery hook */
56export type UseQueryResult < T > = {
67 // Base query state
7- originalArgs ?: unknown ; // Arguments passed to the query
8- data ?: T ; // The latest returned result regardless of hook arg, if present
9- currentData ?: T ; // The latest returned result for the current hook arg, if present
10- error ?: unknown ; // Error result if present
11- requestId ?: string ; // A string generated by RTK Query
12- endpointName ?: string ; // The name of the given endpoint for the query
13- startedTimeStamp ?: number ; // Timestamp for when the query was initiated
14- fulfilledTimeStamp ?: number ; // Timestamp for when the query was completed
8+ /** Arguments passed to the query */
9+ originalArgs ?: unknown ;
10+ /** The latest returned result regardless of hook arg, if present */
11+ data ?: T ;
12+ /** The latest returned result for the current hook arg, if present */
13+ currentData ?: T ;
14+ /** Error result if present */
15+ error ?: unknown ;
16+ /** A string generated by RTK Query */
17+ requestId ?: string ;
18+ /** The name of the given endpoint for the query */
19+ endpointName ?: string ;
20+ /** Timestamp for when the query was initiated */
21+ startedTimeStamp ?: number ;
22+ /** Timestamp for when the query was completed */
23+ fulfilledTimeStamp ?: number ;
1524
1625 // Derived request status booleans
17- isUninitialized : boolean ; // Query has not started yet.
18- isLoading : boolean ; // Query is currently loading for the first time. No data yet.
19- isFetching : boolean ; // Query is currently fetching, but might have data from an earlier request.
20- isSuccess : boolean ; // Query has data from a successful load.
21- isError : boolean ; // Query is currently in an "error" state.
22-
23- refetch : ( ) => void ; // A function to force refetch the query
26+ /** Query has not started yet */
27+ isUninitialized : boolean ;
28+ /** Query is currently loading for the first time. No data yet. */
29+ isLoading : boolean ;
30+ /** Query is currently fetching, but might have data from an earlier request. */
31+ isFetching : boolean ;
32+ /** Query has data from a successful load */
33+ isSuccess : boolean ;
34+ /** Query is currently in an "error" state */
35+ isError : boolean ;
36+
37+ /** A function to force refetch the query */
38+ refetch : ( ) => void ;
2439} ;
2540
2641export type MakeDataRequired <
@@ -30,39 +45,45 @@ export type MakeDataRequired<
3045 [ K in keyof T ] : T [ K ] & { data : NonNullable < T [ K ] [ "data" ] > } ;
3146} ;
3247
48+ /** Use: `(...args: OptionalGenericArg<T>) => void;`
49+ * Allows either `T` or `none` for the parameter
50+ */
51+ export type OptionalGenericArg < T > = T extends never ? [ ] : [ T ] ;
52+
3353export type LoaderTransformFunction <
3454 QRU extends readonly UseQueryResult < unknown > [ ] ,
3555 R extends unknown
3656> = ( queries : MakeDataRequired < QRU > ) => R ;
3757
38- export type OptionalGenericArg < T > = T extends never ? [ ] : [ T ] ;
39-
4058export type CreateUseLoaderArgs <
4159 QRU extends readonly UseQueryResult < unknown > [ ] ,
4260 R extends unknown ,
4361 A = never
4462> = {
63+ /** Should return a list of RTK useQuery results.
64+ * Example:
65+ * ```typescript
66+ * (args: Args) => [
67+ * useGetPokemonQuery(args.pokemonId),
68+ * useGetSomethingElse(args.someArg)
69+ * ] as const
70+ * ```
71+ */
4572 queries : ( ...args : OptionalGenericArg < A > ) => QRU ;
73+ /** Transforms the output of the queries */
4674 transform ?: LoaderTransformFunction < QRU , R > ;
4775} ;
4876
4977export type UseLoader < A , R > = (
5078 ...args : OptionalGenericArg < A >
5179) => UseQueryResult < R > ;
5280
53- export type CreateLoaderType = <
54- QRU extends readonly UseQueryResult < unknown > [ ] ,
55- R extends unknown = MakeDataRequired < QRU > ,
56- A = never
57- > (
58- createLoaderArgs : CreateUseLoaderArgs < QRU , R , A >
59- ) => UseLoader < A , R > ;
60-
6181export type ComponentWithLoaderData <
6282 P extends Record < string , any > ,
6383 R extends unknown
6484> = ( props : P , loaderData : R ) => ReactElement ;
6585
86+ /** Use: `InferLoaderData<typeof loader>`. Returns the return-value of the given loader's aggregated query. */
6687export type InferLoaderData < T > = T extends Loader <
6788 any ,
6889 infer X ,
@@ -79,28 +100,49 @@ export type Component<P extends Record<string, any>> = (
79100 props : P
80101) => ReactElement ;
81102
82- export type WithLoaderArgs <
83- P extends unknown ,
84- R extends unknown ,
85- A = never
86- > = Loader < P , R , A > ;
87-
88103export type WhileFetchingArgs <
89104 P extends unknown ,
90105 R extends unknown
91106> = {
107+ /** Will be prepended before the component while the query is fetching */
92108 prepend ?: ( props : P , data ?: R ) => ReactElement ;
109+ /** Will be appended after the component while the query is fetching */
93110 append ?: ( props : P , data ?: R ) => ReactElement ;
94111} ;
95112
113+ export type CustomLoaderProps < T = unknown > = {
114+ /** What the loader requests be rendered while fetching data */
115+ onFetching ?: React . ReactElement ;
116+ /** What the loader requests be rendered while fetching data */
117+ whileFetching ?: {
118+ /** Should be appended to the success result while fetching */
119+ append ?: React . ReactElement ;
120+ /** Should be prepended to the success result while fetching */
121+ prepend ?: React . ReactElement ;
122+ } ;
123+ /** What the loader requests be rendered when data is available */
124+ onSuccess : ( data : T ) => React . ReactElement ;
125+ /** What the loader requests be rendered when the query fails */
126+ onError ?: (
127+ error : SerializedError | FetchBaseQueryError
128+ ) => JSX . Element ;
129+ /** What the loader requests be rendered while loading data */
130+ onLoading ?: React . ReactElement ;
131+ /** The joined query for the loader */
132+ query : UseQueryResult < T > ;
133+ } ;
134+
96135export type CreateLoaderArgs <
97136 P extends unknown ,
98137 QRU extends readonly UseQueryResult < unknown > [ ] ,
99138 R extends unknown = MakeDataRequired < QRU > ,
100139 A = never
101140> = Partial < CreateUseLoaderArgs < QRU , R , A > > & {
141+ /** Generates an argument for the `queries` based on component props */
102142 queriesArg ?: ( props : P ) => A ;
143+ /** Determines what to render while loading (with no data to fallback on) */
103144 onLoading ?: ( props : P ) => ReactElement ;
145+ /** Determines what to render when query fails. */
104146 onError ?: (
105147 props : P ,
106148 error : FetchBaseQueryError | SerializedError ,
@@ -111,17 +153,24 @@ export type CreateLoaderArgs<
111153 props : P ,
112154 renderBody : ( ) => ReactElement
113155 ) => ReactElement ;
156+ /** Determines what to render besides success-result while query is fetching. */
114157 whileFetching ?: WhileFetchingArgs < P , R > ;
158+ /** The component to use to switch between rendering the different query states. */
159+ loaderComponent ?: Component < CustomLoaderProps > ;
115160} ;
116161
117162export type Loader <
118163 P extends unknown ,
119164 R extends unknown ,
120165 A = never
121166> = {
167+ /** A hook that runs all queries and returns aggregated result */
122168 useLoader : UseLoader < A , R > ;
169+ /** Generates an argument for the `queries` based on component props */
123170 queriesArg ?: ( props : P ) => A ;
171+ /** Determines what to render while loading (with no data to fallback on) */
124172 onLoading ?: ( props : P ) => ReactElement ;
173+ /** Determines what to render when query fails. */
125174 onError ?: (
126175 props : P ,
127176 error : SerializedError | FetchBaseQueryError ,
@@ -132,7 +181,9 @@ export type Loader<
132181 props : P ,
133182 renderBody : ( ) => ReactElement
134183 ) => ReactElement ;
184+ /** Determines what to render besides success-result while query is fetching. */
135185 whileFetching ?: WhileFetchingArgs < P , R > ;
186+ /** Returns a new `Loader` extended from this `Loader`, with given overrides. */
136187 extend : <
137188 QRUb extends readonly UseQueryResult < unknown > [ ] ,
138189 Pb extends unknown = P ,
@@ -143,4 +194,14 @@ export type Loader<
143194 > (
144195 newLoader : Partial < CreateLoaderArgs < Pb , QRUb , Rb , Ab > >
145196 ) => Loader < Pb , Rb , Ab > ;
197+ /** The component to use to switch between rendering the different query states. */
198+ LoaderComponent : Component < CustomLoaderProps > ;
146199} ;
200+
201+ /** Legacy/unused, for backwards compatibility */
202+
203+ export type WithLoaderArgs <
204+ P extends unknown ,
205+ R extends unknown ,
206+ A = never
207+ > = Loader < P , R , A > ;
0 commit comments