1
1
/* eslint-disable @typescript-eslint/no-explicit-any */
2
- import { useMutation , UseMutationOptions , useQuery , useQueryClient , UseQueryOptions } from '@tanstack/react-query' ;
2
+ import {
3
+ useMutation ,
4
+ useQuery ,
5
+ useQueryClient ,
6
+ type MutateFunction ,
7
+ type QueryClient ,
8
+ type UseMutationOptions ,
9
+ type UseQueryOptions ,
10
+ } from '@tanstack/react-query' ;
3
11
import { marshal , registerSerializers } from '../serialization-utils' ;
4
12
import { fetcher , makeUrl } from './utils' ;
5
13
@@ -43,19 +51,17 @@ export function postMutation<T, R = any>(
43
51
invalidateQueries = true
44
52
) {
45
53
const queryClient = useQueryClient ( ) ;
46
- const mutation = useMutation < R , unknown , T > ( {
47
- mutationFn : ( data : any ) =>
48
- fetcher < R > ( url , {
49
- method : 'POST' ,
50
- headers : {
51
- 'content-type' : 'application/json' ,
52
- } ,
53
- body : marshal ( data ) ,
54
- } ) ,
55
- ...options ,
56
- onSuccess : invalidateQueries ? ( ) => queryClient . invalidateQueries ( [ QUERY_KEY_PREFIX + model ] ) : undefined ,
57
- } ) ;
54
+ const mutationFn = ( data : any ) =>
55
+ fetcher < R > ( url , {
56
+ method : 'POST' ,
57
+ headers : {
58
+ 'content-type' : 'application/json' ,
59
+ } ,
60
+ body : marshal ( data ) ,
61
+ } ) ;
58
62
63
+ const finalOptions = mergeOptions < T , R > ( model , options , invalidateQueries , mutationFn , queryClient ) ;
64
+ const mutation = useMutation < R , unknown , T > ( finalOptions ) ;
59
65
return mutation ;
60
66
}
61
67
@@ -75,19 +81,17 @@ export function putMutation<T, R = any>(
75
81
invalidateQueries = true
76
82
) {
77
83
const queryClient = useQueryClient ( ) ;
78
- const mutation = useMutation < R , unknown , T > ( {
79
- mutationFn : ( data : any ) =>
80
- fetcher < R > ( url , {
81
- method : 'PUT' ,
82
- headers : {
83
- 'content-type' : 'application/json' ,
84
- } ,
85
- body : marshal ( data ) ,
86
- } ) ,
87
- ...options ,
88
- onSuccess : invalidateQueries ? ( ) => queryClient . invalidateQueries ( [ QUERY_KEY_PREFIX + model ] ) : undefined ,
89
- } ) ;
84
+ const mutationFn = ( data : any ) =>
85
+ fetcher < R > ( url , {
86
+ method : 'PUT' ,
87
+ headers : {
88
+ 'content-type' : 'application/json' ,
89
+ } ,
90
+ body : marshal ( data ) ,
91
+ } ) ;
90
92
93
+ const finalOptions = mergeOptions < T , R > ( model , options , invalidateQueries , mutationFn , queryClient ) ;
94
+ const mutation = useMutation < R , unknown , T > ( finalOptions ) ;
91
95
return mutation ;
92
96
}
93
97
@@ -107,14 +111,31 @@ export function deleteMutation<T, R = any>(
107
111
invalidateQueries = true
108
112
) {
109
113
const queryClient = useQueryClient ( ) ;
110
- const mutation = useMutation < R , unknown , T > ( {
111
- mutationFn : ( data : any ) =>
112
- fetcher < R > ( makeUrl ( url , data ) , {
113
- method : 'DELETE' ,
114
- } ) ,
115
- ...options ,
116
- onSuccess : invalidateQueries ? ( ) => queryClient . invalidateQueries ( [ QUERY_KEY_PREFIX + model ] ) : undefined ,
117
- } ) ;
114
+ const mutationFn = ( data : any ) =>
115
+ fetcher < R > ( makeUrl ( url , data ) , {
116
+ method : 'DELETE' ,
117
+ } ) ;
118
118
119
+ const finalOptions = mergeOptions < T , R > ( model , options , invalidateQueries , mutationFn , queryClient ) ;
120
+ const mutation = useMutation < R , unknown , T > ( finalOptions ) ;
119
121
return mutation ;
120
122
}
123
+
124
+ function mergeOptions < T , R = any > (
125
+ model : string ,
126
+ options : Omit < UseMutationOptions < R , unknown , T , unknown > , 'mutationFn' > | undefined ,
127
+ invalidateQueries : boolean ,
128
+ mutationFn : MutateFunction < R , unknown , T > ,
129
+ queryClient : QueryClient
130
+ ) : UseMutationOptions < R , unknown , T , unknown > {
131
+ const result = { ...options , mutationFn } ;
132
+ if ( options ?. onSuccess || invalidateQueries ) {
133
+ result . onSuccess = ( ...args ) => {
134
+ if ( invalidateQueries ) {
135
+ queryClient . invalidateQueries ( [ QUERY_KEY_PREFIX + model ] ) ;
136
+ }
137
+ return options ?. onSuccess ?.( ...args ) ;
138
+ } ;
139
+ }
140
+ return result ;
141
+ }
0 commit comments