@@ -3,10 +3,10 @@ import {
3
3
type UseMutationResult ,
4
4
type UseQueryOptions ,
5
5
type UseQueryResult ,
6
- type UseSuspenseQueryOptions ,
7
- type UseSuspenseQueryResult ,
8
6
type UseInfiniteQueryOptions ,
9
7
type UseInfiniteQueryResult ,
8
+ type UseSuspenseQueryOptions ,
9
+ type UseSuspenseQueryResult ,
10
10
type QueryClient ,
11
11
type QueryFunctionContext ,
12
12
type SkipToken ,
@@ -15,7 +15,7 @@ import {
15
15
useSuspenseQuery ,
16
16
useInfiniteQuery ,
17
17
} from "@tanstack/react-query" ;
18
- import type { ClientMethod , FetchResponse , MaybeOptionalInit , Client as FetchClient } from "openapi-fetch" ;
18
+ import type { ClientMethod , FetchResponse , MaybeOptionalInit , Client as FetchClient , DefaultParamsOption } from "openapi-fetch" ;
19
19
import type { HttpMethod , MediaType , PathsWithMethod , RequiredKeysOf } from "openapi-typescript-helpers" ;
20
20
21
21
// Helper type to dynamically infer the type from the `select` property
@@ -93,6 +93,45 @@ export type UseQueryMethod<Paths extends Record<string, Record<HttpMethod, {}>>,
93
93
: [ InitWithUnknowns < Init > , Options ?, QueryClient ?]
94
94
) => UseQueryResult < InferSelectReturnType < Response [ "data" ] , Options [ "select" ] > , Response [ "error" ] > ;
95
95
96
+ export type UseInfiniteQueryMethod < Paths extends Record < string , Record < HttpMethod , { } > > , Media extends MediaType > = <
97
+ Method extends HttpMethod ,
98
+ Path extends PathsWithMethod < Paths , Method > ,
99
+ Init extends MaybeOptionalInit < Paths [ Path ] , Method > ,
100
+ Response extends Required < FetchResponse < Paths [ Path ] [ Method ] , Init , Media > > ,
101
+ > (
102
+ method : Method ,
103
+ url : Path ,
104
+ ...[ init , options , queryClient ] : RequiredKeysOf < Init > extends never
105
+ ? [
106
+ InitWithUnknowns < Init > ?,
107
+ Omit <
108
+ UseInfiniteQueryOptions <
109
+ Response [ "data" ] ,
110
+ Response [ "error" ] ,
111
+ Response [ "data" ] ,
112
+ number ,
113
+ QueryKey < Paths , Method , Path >
114
+ > ,
115
+ "queryKey" | "queryFn"
116
+ > ?,
117
+ QueryClient ?,
118
+ ]
119
+ : [
120
+ InitWithUnknowns < Init > ,
121
+ Omit <
122
+ UseInfiniteQueryOptions <
123
+ Response [ "data" ] ,
124
+ Response [ "error" ] ,
125
+ Response [ "data" ] ,
126
+ number ,
127
+ QueryKey < Paths , Method , Path >
128
+ > ,
129
+ "queryKey" | "queryFn"
130
+ > ?,
131
+ QueryClient ?,
132
+ ]
133
+ ) => UseInfiniteQueryResult < Response [ "data" ] , Response [ "error" ] > ;
134
+
96
135
export type UseSuspenseQueryMethod < Paths extends Record < string , Record < HttpMethod , { } > > , Media extends MediaType > = <
97
136
Method extends HttpMethod ,
98
137
Path extends PathsWithMethod < Paths , Method > ,
@@ -115,45 +154,6 @@ export type UseSuspenseQueryMethod<Paths extends Record<string, Record<HttpMetho
115
154
: [ InitWithUnknowns < Init > , Options ?, QueryClient ?]
116
155
) => UseSuspenseQueryResult < InferSelectReturnType < Response [ "data" ] , Options [ "select" ] > , Response [ "error" ] > ;
117
156
118
- export type UseInfiniteQueryMethod < Paths extends Record < string , Record < HttpMethod , { } > > , Media extends MediaType > = <
119
- Method extends HttpMethod ,
120
- Path extends PathsWithMethod < Paths , Method > ,
121
- Init extends MaybeOptionalInit < Paths [ Path ] , Method > ,
122
- Response extends Required < FetchResponse < Paths [ Path ] [ Method ] , Init , Media > > ,
123
- > (
124
- method : Method ,
125
- url : Path ,
126
- ...[ init , options , queryClient ] : RequiredKeysOf < Init > extends never
127
- ? [
128
- InitWithUnknowns < Init > ?,
129
- Omit <
130
- UseInfiniteQueryOptions <
131
- Response [ "data" ] ,
132
- Response [ "error" ] ,
133
- Response [ "data" ] ,
134
- number ,
135
- QueryKey < Paths , Method , Path >
136
- > ,
137
- "queryKey" | "queryFn"
138
- > ?,
139
- QueryClient ?,
140
- ]
141
- : [
142
- InitWithUnknowns < Init > ,
143
- Omit <
144
- UseInfiniteQueryOptions <
145
- Response [ "data" ] ,
146
- Response [ "error" ] ,
147
- Response [ "data" ] ,
148
- number ,
149
- QueryKey < Paths , Method , Path >
150
- > ,
151
- "queryKey" | "queryFn"
152
- > ?,
153
- QueryClient ?,
154
- ]
155
- ) => UseInfiniteQueryResult < Response [ "data" ] , Response [ "error" ] > ;
156
-
157
157
export type UseMutationMethod < Paths extends Record < string , Record < HttpMethod , { } > > , Media extends MediaType > = <
158
158
Method extends HttpMethod ,
159
159
Path extends PathsWithMethod < Paths , Method > ,
@@ -199,30 +199,54 @@ export default function createClient<Paths extends {}, Media extends MediaType =
199
199
...options ,
200
200
} ) ;
201
201
202
- const infiniteQueryOptions = <
203
- Method extends HttpMethod ,
204
- Path extends PathsWithMethod < Paths , Method > ,
205
- Init extends MaybeOptionalInit < Paths [ Path ] , Method & keyof Paths [ Path ] > ,
206
- Response extends Required < FetchResponse < any , Init , Media > > ,
207
- > (
208
- method : Method ,
209
- path : Path ,
210
- init ?: InitWithUnknowns < Init > ,
211
- options ?: Omit <
212
- UseInfiniteQueryOptions <
213
- Response [ "data" ] ,
214
- Response [ "error" ] ,
215
- Response [ "data" ] ,
216
- number ,
217
- QueryKey < Paths , Method , Path >
218
- > ,
219
- "queryKey" | "queryFn"
220
- > ,
221
- ) => ( {
222
- queryKey : [ method , path , init ] as const ,
223
- queryFn,
224
- ...options ,
225
- } ) ;
202
+ const infiniteQueryOptions = <
203
+ Method extends HttpMethod ,
204
+ Path extends PathsWithMethod < Paths , Method > ,
205
+ Init extends MaybeOptionalInit < Paths [ Path ] , Method & keyof Paths [ Path ] > ,
206
+ Response extends Required < FetchResponse < any , Init , Media > > ,
207
+ > (
208
+ method : Method ,
209
+ path : Path ,
210
+ init ?: InitWithUnknowns < Init > ,
211
+ options ?: Omit <
212
+ UseInfiniteQueryOptions <
213
+ Response [ "data" ] ,
214
+ Response [ "error" ] ,
215
+ Response [ "data" ] ,
216
+ number ,
217
+ QueryKey < Paths , Method , Path >
218
+ > ,
219
+ "queryKey" | "queryFn"
220
+ > ,
221
+ ) => ( {
222
+ queryKey : [ method , path , init ] as const ,
223
+ queryFn : async ( {
224
+ queryKey : [ method , path , init ] ,
225
+ pageParam = 0 ,
226
+ signal,
227
+ } : QueryFunctionContext < QueryKey < Paths , Method , Path > , number > ) => {
228
+ const mth = method . toUpperCase ( ) as Uppercase < typeof method > ;
229
+ const fn = client [ mth ] as ClientMethod < Paths , typeof method , Media > ;
230
+ const mergedInit = {
231
+ ...init ,
232
+ signal,
233
+ params : {
234
+ ...( init ?. params || { } ) ,
235
+ query : {
236
+ ...( init ?. params as { query ?: DefaultParamsOption } ) ?. query ,
237
+ cursor : pageParam ,
238
+ } ,
239
+ } ,
240
+ } ;
241
+
242
+ const { data, error } = await fn ( path , mergedInit as any ) ;
243
+ if ( error ) {
244
+ throw error ;
245
+ }
246
+ return data ;
247
+ } ,
248
+ ...options ,
249
+ } ) ;
226
250
227
251
return {
228
252
queryOptions,
@@ -233,13 +257,12 @@ export default function createClient<Paths extends {}, Media extends MediaType =
233
257
useInfiniteQuery : ( method , path , ...[ init , options , queryClient ] ) => {
234
258
const baseOptions = infiniteQueryOptions ( method , path , init as InitWithUnknowns < typeof init > , options as any ) ; // TODO: find a way to avoid as any
235
259
return useInfiniteQuery (
236
- {
237
- ...baseOptions ,
238
- initialPageParam : 0 ,
239
- getNextPageParam : ( lastPage : any , allPages : any [ ] , lastPageParam : number , allPageParams : number [ ] ) =>
240
- options ?. getNextPageParam ?.( lastPage , allPages , lastPageParam , allPageParams ) ?? allPages . length ,
241
- } as any ,
242
- queryClient ,
260
+ {
261
+ ...baseOptions ,
262
+ getNextPageParam : ( lastPage : any , allPages : any [ ] , lastPageParam : number , allPageParams : number [ ] ) =>
263
+ options ?. getNextPageParam ?.( lastPage , allPages , lastPageParam , allPageParams ) ?? allPages . length ,
264
+ } as any ,
265
+ queryClient ,
243
266
) ;
244
267
} ,
245
268
useMutation : ( method , path , options , queryClient ) =>
0 commit comments