1
1
import {
2
- type UseMutationOptions ,
3
- type UseMutationResult ,
4
- type UseQueryOptions ,
5
- type UseQueryResult ,
6
- type UseSuspenseQueryOptions ,
7
- type UseSuspenseQueryResult ,
8
2
type QueryClient ,
9
3
type QueryFunctionContext ,
10
4
type SkipToken ,
5
+ useInfiniteQuery ,
6
+ type UseInfiniteQueryOptions ,
7
+ type UseInfiniteQueryResult ,
11
8
useMutation ,
9
+ type UseMutationOptions ,
10
+ type UseMutationResult ,
12
11
useQuery ,
12
+ type UseQueryOptions ,
13
+ type UseQueryResult ,
13
14
useSuspenseQuery ,
15
+ type UseSuspenseQueryOptions ,
16
+ type UseSuspenseQueryResult
14
17
} from "@tanstack/react-query" ;
15
- import type { ClientMethod , FetchResponse , MaybeOptionalInit , Client as FetchClient } from "openapi-fetch" ;
18
+ import type { ClientMethod , Client as FetchClient , FetchResponse , MaybeOptionalInit } from "openapi-fetch" ;
16
19
import type { HttpMethod , MediaType , PathsWithMethod , RequiredKeysOf } from "openapi-typescript-helpers" ;
17
20
18
21
type InitWithUnknowns < Init > = Init & { [ key : string ] : unknown } ;
@@ -84,6 +87,19 @@ export type UseSuspenseQueryMethod<Paths extends Record<string, Record<HttpMetho
84
87
: [ InitWithUnknowns < Init > , Options ?, QueryClient ?]
85
88
) => UseSuspenseQueryResult < Response [ "data" ] , Response [ "error" ] > ;
86
89
90
+ export type UseInfiniteQueryMethod < Paths extends Record < string , Record < HttpMethod , { } > > , Media extends MediaType > = <
91
+ Method extends HttpMethod ,
92
+ Path extends PathsWithMethod < Paths , Method > ,
93
+ Init extends MaybeOptionalInit < Paths [ Path ] , Method > ,
94
+ Response extends Required < FetchResponse < Paths [ Path ] [ Method ] , Init , Media > > ,
95
+ > (
96
+ method : Method ,
97
+ url : Path ,
98
+ ...[ init , options , queryClient ] : RequiredKeysOf < Init > extends never
99
+ ? [ InitWithUnknowns < Init > ?, Omit < UseInfiniteQueryOptions < Response [ "data" ] , Response [ "error" ] , Response [ "data" ] , number , QueryKey < Paths , Method , Path > > , "queryKey" | "queryFn" > ?, QueryClient ?]
100
+ : [ InitWithUnknowns < Init > , Omit < UseInfiniteQueryOptions < Response [ "data" ] , Response [ "error" ] , Response [ "data" ] , number , QueryKey < Paths , Method , Path > > , "queryKey" | "queryFn" > ?, QueryClient ?]
101
+ ) => UseInfiniteQueryResult < Response [ "data" ] , Response [ "error" ] > ;
102
+
87
103
export type UseMutationMethod < Paths extends Record < string , Record < HttpMethod , { } > > , Media extends MediaType > = <
88
104
Method extends HttpMethod ,
89
105
Path extends PathsWithMethod < Paths , Method > ,
@@ -101,6 +117,7 @@ export interface OpenapiQueryClient<Paths extends {}, Media extends MediaType =
101
117
queryOptions : QueryOptionsFunction < Paths , Media > ;
102
118
useQuery : UseQueryMethod < Paths , Media > ;
103
119
useSuspenseQuery : UseSuspenseQueryMethod < Paths , Media > ;
120
+ useInfiniteQuery : UseInfiniteQueryMethod < Paths , Media > ;
104
121
useMutation : UseMutationMethod < Paths , Media > ;
105
122
}
106
123
@@ -128,12 +145,38 @@ export default function createClient<Paths extends {}, Media extends MediaType =
128
145
...options ,
129
146
} ) ;
130
147
148
+
149
+ const infiniteQueryOptions = <
150
+ Method extends HttpMethod ,
151
+ Path extends PathsWithMethod < Paths , Method > ,
152
+ Init extends MaybeOptionalInit < Paths [ Path ] , Method & keyof Paths [ Path ] > ,
153
+ Response extends Required < FetchResponse < any , Init , Media > > ,
154
+ > (
155
+ method : Method ,
156
+ path : Path ,
157
+ init ?: InitWithUnknowns < Init > ,
158
+ options ?: Omit < UseInfiniteQueryOptions < Response [ "data" ] , Response [ "error" ] , Response [ "data" ] , number , QueryKey < Paths , Method , Path > > , "queryKey" | "queryFn" >
159
+ ) => ( {
160
+ queryKey : [ method , path , init ] as const ,
161
+ queryFn,
162
+ ...options ,
163
+ } ) ;
164
+
131
165
return {
132
166
queryOptions,
133
167
useQuery : ( method , path , ...[ init , options , queryClient ] ) =>
134
168
useQuery ( queryOptions ( method , path , init as InitWithUnknowns < typeof init > , options ) , queryClient ) ,
135
169
useSuspenseQuery : ( method , path , ...[ init , options , queryClient ] ) =>
136
170
useSuspenseQuery ( queryOptions ( method , path , init as InitWithUnknowns < typeof init > , options ) , queryClient ) ,
171
+ useInfiniteQuery : ( method , path , ...[ init , options , queryClient ] ) => {
172
+ const baseOptions = infiniteQueryOptions ( method , path , init as InitWithUnknowns < typeof init > , options as any ) ; // TODO: find a way to avoid as any
173
+ return useInfiniteQuery ( {
174
+ ...baseOptions ,
175
+ initialPageParam : 0 ,
176
+ getNextPageParam : ( lastPage : any , allPages : any [ ] , lastPageParam : number , allPageParams : number [ ] ) =>
177
+ options ?. getNextPageParam ?.( lastPage , allPages , lastPageParam , allPageParams ) ?? allPages . length ,
178
+ } as any , queryClient ) ;
179
+ } ,
137
180
useMutation : ( method , path , options , queryClient ) =>
138
181
useMutation (
139
182
{
0 commit comments