1- import { onUnmounted , reactive , watchEffect } from "vue-demi" ;
2- import { MutateOptions , MutationObserver } from "react-query/core" ;
1+ import {
2+ onUnmounted ,
3+ reactive ,
4+ watchEffect ,
5+ readonly ,
6+ ToRefs ,
7+ toRefs ,
8+ } from "vue-demi" ;
9+ import {
10+ MutateOptions ,
11+ MutationObserver ,
12+ MutationObserverResult ,
13+ } from "react-query/core" ;
314import {
415 UseMutationOptions ,
5- UseMutationResult ,
616 MutationFunction ,
717 MutationKey ,
18+ UseMutateFunction ,
19+ UseMutateAsyncFunction ,
820} from "react-query/types" ;
921import { parseMutationArgs , updateState } from "./utils" ;
1022import { useQueryClient } from "./useQueryClient" ;
1123
24+ type MutationResult < TData , TError , TVariables , TContext > = Omit <
25+ MutationObserverResult < TData , TError , TVariables , TContext > ,
26+ "mutate"
27+ > ;
28+
29+ export type UseMutationReturnType <
30+ TData ,
31+ TError ,
32+ TVariables ,
33+ TContext ,
34+ Result = MutationResult < TData , TError , TVariables , TContext >
35+ > = ToRefs < Readonly < Result > > & {
36+ mutate : UseMutateFunction < TData , TError , TVariables , TContext > ;
37+ mutateAsync : UseMutateAsyncFunction < TData , TError , TVariables , TContext > ;
38+ } ;
39+
1240export function useMutation <
1341 TData = unknown ,
1442 TError = unknown ,
1543 TVariables = void ,
1644 TContext = unknown
1745> (
1846 options : UseMutationOptions < TData , TError , TVariables , TContext >
19- ) : UseMutationResult < TData , TError , TVariables , TContext > ;
47+ ) : UseMutationReturnType < TData , TError , TVariables , TContext > ;
2048export function useMutation <
2149 TData = unknown ,
2250 TError = unknown ,
@@ -25,7 +53,7 @@ export function useMutation<
2553> (
2654 mutationFn : MutationFunction < TData , TVariables > ,
2755 options ?: UseMutationOptions < TData , TError , TVariables , TContext >
28- ) : UseMutationResult < TData , TError , TVariables , TContext > ;
56+ ) : UseMutationReturnType < TData , TError , TVariables , TContext > ;
2957export function useMutation <
3058 TData = unknown ,
3159 TError = unknown ,
@@ -34,7 +62,7 @@ export function useMutation<
3462> (
3563 mutationKey : MutationKey ,
3664 options ?: UseMutationOptions < TData , TError , TVariables , TContext >
37- ) : UseMutationResult < TData , TError , TVariables , TContext > ;
65+ ) : UseMutationReturnType < TData , TError , TVariables , TContext > ;
3866export function useMutation <
3967 TData = unknown ,
4068 TError = unknown ,
@@ -44,7 +72,7 @@ export function useMutation<
4472 mutationKey : MutationKey ,
4573 mutationFn ?: MutationFunction < TData , TVariables > ,
4674 options ?: UseMutationOptions < TData , TError , TVariables , TContext >
47- ) : UseMutationResult < TData , TError , TVariables , TContext > ;
75+ ) : UseMutationReturnType < TData , TError , TVariables , TContext > ;
4876export function useMutation <
4977 TData = unknown ,
5078 TError = unknown ,
@@ -59,7 +87,7 @@ export function useMutation<
5987 | MutationFunction < TData , TVariables >
6088 | UseMutationOptions < TData , TError , TVariables , TContext > ,
6189 arg3 ?: UseMutationOptions < TData , TError , TVariables , TContext >
62- ) : UseMutationResult < TData , TError , TVariables , TContext > {
90+ ) : UseMutationReturnType < TData , TError , TVariables , TContext > {
6391 const options = parseMutationArgs ( arg1 , arg2 , arg3 ) ;
6492 const queryClient = useQueryClient ( ) ;
6593 const observer = new MutationObserver ( queryClient , options ) ;
@@ -75,7 +103,7 @@ export function useMutation<
75103
76104 const mutate = (
77105 variables : TVariables ,
78- mutateOptions : MutateOptions < TData , TError , TVariables , TContext >
106+ mutateOptions ? : MutateOptions < TData , TError , TVariables , TContext >
79107 ) => {
80108 // eslint-disable-next-line @typescript-eslint/no-empty-function
81109 observer . mutate ( variables , mutateOptions ) . catch ( ( ) => { } ) ;
@@ -89,8 +117,13 @@ export function useMutation<
89117 unsubscribe ( ) ;
90118 } ) ;
91119
92- return Object . assign ( state , {
120+ const resultRefs = toRefs ( readonly ( state ) ) as ToRefs <
121+ Readonly < MutationObserverResult < TData , TError , TVariables , TContext > >
122+ > ;
123+
124+ return {
125+ ...resultRefs ,
93126 mutate,
94127 mutateAsync : state . mutate ,
95- } ) as UseMutationResult < TData , TError , TVariables , TContext > ;
128+ } ;
96129}
0 commit comments