1
1
/* eslint-disable ts/no-unsafe-argument, ts/no-unsafe-assignment, ts/no-unsafe-return, ts/no-unsafe-member-access, ts/no-unsafe-call */
2
- import { type QueryKey , useMutation , useQuery } from '@tanstack/vue-query'
2
+ import { type QueryKey , useMutation , useQuery , useQueryClient } from '@tanstack/vue-query'
3
3
import {
4
4
type CreateTRPCClientOptions ,
5
5
createTRPCProxyClient ,
@@ -35,17 +35,17 @@ function createVueQueryProxyDecoration<TRouter extends AnyRouter>(
35
35
const lastProperty = path . pop ( ) !
36
36
37
37
const joinedPath = path . join ( '.' )
38
- const [ input , options ] = args
38
+ const [ firstParam , secondParam ] = args
39
39
40
40
if ( lastProperty === '_def' ) {
41
41
return { path }
42
42
}
43
43
44
44
if ( lastProperty === 'useQuery' ) {
45
- const { trpc, ...queryOptions } = options || ( { } as any )
45
+ const { trpc, ...queryOptions } = secondParam || ( { } as any )
46
46
47
47
return useQuery ( {
48
- queryKey : computed ( ( ) => getQueryKey ( path , toValue ( input ) ) ) ,
48
+ queryKey : computed ( ( ) => getQueryKey ( path , toValue ( firstParam ) ) ) ,
49
49
queryFn : ( { queryKey, signal } ) =>
50
50
( client as any ) [ joinedPath ] . query ( queryKey . at ( - 1 ) , {
51
51
signal,
@@ -55,7 +55,33 @@ function createVueQueryProxyDecoration<TRouter extends AnyRouter>(
55
55
} )
56
56
}
57
57
58
+ if ( lastProperty === 'invalidate' ) {
59
+ try {
60
+ const queryClient = useQueryClient ( )
61
+ return queryClient . invalidateQueries ( {
62
+ queryKey : getQueryKey ( path , toValue ( firstParam ) ) ,
63
+ } )
64
+ } catch ( error ) {
65
+ console . error ( error )
66
+ }
67
+ }
68
+
69
+ if ( lastProperty === 'setQueryData' ) {
70
+ try {
71
+ const queryClient = useQueryClient ( )
72
+ return queryClient . setQueryData ( getQueryKey ( path , toValue ( secondParam ) ) , firstParam )
73
+ } catch ( error ) {
74
+ console . error ( error )
75
+ }
76
+ }
77
+
78
+ if ( lastProperty === 'key' ) {
79
+ return getQueryKey ( path , toValue ( firstParam ) )
80
+ }
81
+
58
82
if ( lastProperty === 'useMutation' ) {
83
+ const { trpc, ...mutationOptions } = firstParam || ( { } as any )
84
+
59
85
const vueApp = getCurrentInstance ( ) ?. appContext . app
60
86
const mutationOptionsWithContext = Object . fromEntries (
61
87
Object . entries ( mutationOptions ) . map ( ( [ key , value ] ) => {
0 commit comments