@@ -2,16 +2,13 @@ import {
22 QueryFunction ,
33 QueryFunctionContext ,
44 QueryKey ,
5+ UseInfiniteQueryOptions as TanStackUseInfiniteQueryOptions ,
56 useInfiniteQuery ,
6- UseInfiniteQueryOptions ,
7- UseInfiniteQueryResult ,
7+ UseMutationOptions as TanStackUseMutationOptions ,
88 useMutation ,
9- UseMutationOptions ,
10- UseMutationResult ,
9+ UseQueryOptions as TanStackUseQueryOptions ,
1110 useQueries ,
1211 useQuery ,
13- UseQueryOptions ,
14- UseQueryResult ,
1512} from '@tanstack/react-query' ;
1613import {
1714 AppRoute ,
@@ -21,20 +18,23 @@ import {
2118 AppRouter ,
2219 AreAllPropertiesOptional ,
2320 ClientArgs ,
24- ExtractExtraParametersFromClientArgs ,
2521 fetchApi ,
2622 getCompleteUrl ,
2723 getRouteQuery ,
28- HTTPStatusCode ,
2924 isAppRoute ,
30- OptionalIfAllOptional ,
31- PathParamsFromUrl ,
3225 Prettify ,
33- SuccessfulHttpStatusCode ,
3426 Without ,
3527 ZodInferOrType ,
3628} from '@ts-rest/core' ;
37- import { z , ZodTypeAny } from 'zod' ;
29+ import {
30+ DataReturnArgs ,
31+ UseInfiniteQueryOptions ,
32+ UseInfiniteQueryResult ,
33+ UseMutationOptions ,
34+ UseMutationResult ,
35+ UseQueryOptions ,
36+ UseQueryResult ,
37+ } from './types' ;
3838
3939type RecursiveProxyObj < T extends AppRouter , TClientArgs extends ClientArgs > = {
4040 [ TKey in keyof T ] : T [ TKey ] extends AppRoute
@@ -44,8 +44,6 @@ type RecursiveProxyObj<T extends AppRouter, TClientArgs extends ClientArgs> = {
4444 : never ;
4545} ;
4646
47- type AppRouteMutationType < T > = T extends ZodTypeAny ? z . input < T > : T ;
48-
4947type UseQueryArgs <
5048 TAppRoute extends AppRoute ,
5149 TClientArgs extends ClientArgs
@@ -70,65 +68,6 @@ type UseQueryArgs<
7068 : never ;
7169} ;
7270
73- type DataReturnArgsBase <
74- TRoute extends AppRoute ,
75- TClientArgs extends ClientArgs
76- > = {
77- body : TRoute extends AppRouteMutation
78- ? AppRouteMutationType < TRoute [ 'body' ] > extends null
79- ? never
80- : AppRouteMutationType < TRoute [ 'body' ] >
81- : never ;
82- params : PathParamsFromUrl < TRoute > ;
83- query : 'query' extends keyof TRoute
84- ? AppRouteMutationType < TRoute [ 'query' ] > extends null
85- ? never
86- : AppRouteMutationType < TRoute [ 'query' ] >
87- : never ;
88- /**
89- * Additional headers to send with the request, merged over baseHeaders,
90- *
91- * Unset a header by setting it to undefined
92- */
93- headers ?: Record < string , string | undefined > ;
94- } & ExtractExtraParametersFromClientArgs < TClientArgs > ;
95-
96- type DataReturnArgs <
97- TRoute extends AppRoute ,
98- TClientArgs extends ClientArgs
99- > = OptionalIfAllOptional < DataReturnArgsBase < TRoute , TClientArgs > > ;
100-
101- /**
102- * Split up the data and error to support react-query style
103- * useQuery and useMutation error handling
104- */
105- type SuccessResponseMapper < T > = {
106- [ K in keyof T ] : K extends SuccessfulHttpStatusCode
107- ? { status : K ; body : ZodInferOrType < T [ K ] > }
108- : never ;
109- } [ keyof T ] ;
110-
111- /**
112- * Returns any handled errors, or any unhandled non success errors
113- */
114- type ErrorResponseMapper < T > =
115- | {
116- [ K in keyof T ] : K extends SuccessfulHttpStatusCode
117- ? never
118- : { status : K ; body : ZodInferOrType < T [ K ] > } ;
119- } [ keyof T ]
120- // If the response isn't one of our typed ones. Return "unknown"
121- | {
122- status : Exclude < HTTPStatusCode , keyof T | SuccessfulHttpStatusCode > ;
123- body : unknown ;
124- } ;
125-
126- // Data response if it's a 2XX
127- type DataResponse < T extends AppRoute > = SuccessResponseMapper < T [ 'responses' ] > ;
128-
129- // Error response if it's not a 2XX
130- type ErrorResponse < T extends AppRoute > = ErrorResponseMapper < T [ 'responses' ] > ;
131-
13271// Used on X.useQuery
13372type DataReturnQuery <
13473 TAppRoute extends AppRoute ,
@@ -138,25 +77,19 @@ type DataReturnQuery<
13877 ? (
13978 queryKey : QueryKey ,
14079 args ?: TArgs ,
141- options ?: UseQueryOptions <
142- DataResponse < TAppRoute > ,
143- ErrorResponse < TAppRoute >
144- >
145- ) => UseQueryResult < DataResponse < TAppRoute > , ErrorResponse < TAppRoute > >
80+ options ?: UseQueryOptions < TAppRoute >
81+ ) => UseQueryResult < TAppRoute >
14682 : (
14783 queryKey : QueryKey ,
14884 args : TArgs ,
149- options ?: UseQueryOptions <
150- DataResponse < TAppRoute > ,
151- ErrorResponse < TAppRoute >
152- >
153- ) => UseQueryResult < DataResponse < TAppRoute > , ErrorResponse < TAppRoute > > ;
85+ options ?: UseQueryOptions < TAppRoute >
86+ ) => UseQueryResult < TAppRoute > ;
15487
15588type DataReturnQueriesOptions <
15689 TAppRoute extends AppRoute ,
15790 TClientArgs extends ClientArgs
15891> = Without < DataReturnArgs < TAppRoute , TClientArgs > , never > &
159- Omit < UseQueryOptions < TAppRoute [ 'responses' ] > , 'queryFn' > & {
92+ Omit < UseQueryOptions < TAppRoute > , 'queryFn' > & {
16093 queryKey : QueryKey ;
16194 } ;
16295
@@ -166,8 +99,8 @@ type DataReturnQueries<
16699 TQueries = readonly DataReturnQueriesOptions < TAppRoute , TClientArgs > [ ]
167100> = ( args : {
168101 queries : TQueries ;
169- context ?: UseQueryOptions [ 'context' ] ;
170- } ) => UseQueryResult < DataResponse < TAppRoute > , ErrorResponse < TAppRoute > > [ ] ;
102+ context ?: UseQueryOptions < TAppRoute > [ 'context' ] ;
103+ } ) => UseQueryResult < TAppRoute > [ ] ;
171104
172105// Used on X.useInfiniteQuery
173106type DataReturnInfiniteQuery <
@@ -181,45 +114,23 @@ type DataReturnInfiniteQuery<
181114 args ?: (
182115 context : QueryFunctionContext < QueryKey >
183116 ) => Without < DataReturnArgs < TAppRoute , TClientArgs > , never > ,
184- options ?: UseInfiniteQueryOptions <
185- DataResponse < TAppRoute > ,
186- ErrorResponse < TAppRoute >
187- >
188- ) => UseInfiniteQueryResult <
189- DataResponse < TAppRoute > ,
190- ErrorResponse < TAppRoute >
191- >
117+ options ?: UseInfiniteQueryOptions < TAppRoute >
118+ ) => UseInfiniteQueryResult < TAppRoute >
192119 : (
193120 queryKey : QueryKey ,
194121 args : (
195122 context : QueryFunctionContext < QueryKey >
196123 ) => Without < DataReturnArgs < TAppRoute , TClientArgs > , never > ,
197- options ?: UseInfiniteQueryOptions <
198- DataResponse < TAppRoute > ,
199- ErrorResponse < TAppRoute >
200- >
201- ) => UseInfiniteQueryResult <
202- DataResponse < TAppRoute > ,
203- ErrorResponse < TAppRoute >
204- > ;
124+ options ?: UseInfiniteQueryOptions < TAppRoute >
125+ ) => UseInfiniteQueryResult < TAppRoute > ;
205126
206127// Used pn X.useMutation
207128type DataReturnMutation <
208129 TAppRoute extends AppRoute ,
209130 TClientArgs extends ClientArgs
210131> = (
211- options ?: UseMutationOptions <
212- DataResponse < TAppRoute > ,
213- ErrorResponse < TAppRoute > ,
214- Prettify < Without < DataReturnArgs < TAppRoute , TClientArgs > , never > > ,
215- unknown
216- >
217- ) => UseMutationResult <
218- DataResponse < TAppRoute > ,
219- ErrorResponse < TAppRoute > ,
220- Prettify < Without < DataReturnArgs < TAppRoute , TClientArgs > , never > > ,
221- unknown
222- > ;
132+ options ?: UseMutationOptions < TAppRoute , TClientArgs >
133+ ) => UseMutationResult < TAppRoute , TClientArgs > ;
223134
224135const getRouteUseQuery = <
225136 TAppRoute extends AppRoute ,
@@ -231,7 +142,7 @@ const getRouteUseQuery = <
231142 return (
232143 queryKey : QueryKey ,
233144 args ?: DataReturnArgs < any , ClientArgs > ,
234- options ?: UseQueryOptions < TAppRoute [ 'responses' ] >
145+ options ?: TanStackUseQueryOptions < TAppRoute [ 'responses' ] >
235146 ) => {
236147 const dataFn : QueryFunction < TAppRoute [ 'responses' ] > = async ( ) => {
237148 const { query, params, body, headers, ...extraInputArgs } = args || { } ;
@@ -331,7 +242,7 @@ const getRouteUseInfiniteQuery = <
331242 return (
332243 queryKey : QueryKey ,
333244 args : ( context : QueryFunctionContext ) => DataReturnArgs < any , ClientArgs > ,
334- options ?: UseInfiniteQueryOptions < TAppRoute [ 'responses' ] >
245+ options ?: TanStackUseInfiniteQueryOptions < TAppRoute [ 'responses' ] >
335246 ) => {
336247 const dataFn : QueryFunction < TAppRoute [ 'responses' ] > = async (
337248 infiniteQueryParams
@@ -377,7 +288,7 @@ const getRouteUseMutation = <
377288 route : TAppRoute ,
378289 clientArgs : TClientArgs
379290) => {
380- return ( options ?: UseMutationOptions < TAppRoute [ 'responses' ] > ) => {
291+ return ( options ?: TanStackUseMutationOptions < TAppRoute [ 'responses' ] > ) => {
381292 const mutationFunction = async ( args ?: DataReturnArgs < any , ClientArgs > ) => {
382293 const { query, params, body, headers, ...extraInputArgs } = args || { } ;
383294
0 commit comments