-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmutation.types.ts
More file actions
156 lines (145 loc) · 4.17 KB
/
mutation.types.ts
File metadata and controls
156 lines (145 loc) · 4.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import type {
DefaultError,
InvalidateQueryFilters,
MutationFunctionContext as MutationFunctionContextCore,
MutationObserverOptions,
} from '@tanstack/query-core';
import type { Mutation } from './mutation.js';
import type { AnyQueryClient } from './query-client.types.js';
export interface MutationFeatures {
/**
* Invalidate queries by mutation key.
*
* - when `true`, invalidate all queries by mutation key (not exact)
* - when `object`, invalidate all queries by mutation key with this additional filters
*/
invalidateByKey?:
| boolean
| Omit<InvalidateQueryFilters, 'queryKey' | 'predicate'>;
/**
* Reset mutation when destroy or abort signal is called
*/
resetOnDestroy?: boolean;
/**
* **EXPERIMENTAL**
*
* Make all mutation reactions and subscriptions lazy.
* They exists only when mutation result is observed.
*/
lazy?: boolean;
transformError?: (error: any) => any;
}
export interface MutationInvalidateQueriesOptions
extends Omit<InvalidateQueryFilters, 'queryKey'> {
queryKey?: InvalidateQueryFilters['queryKey'];
queryKeys?: InvalidateQueryFilters['queryKey'][];
allQueryKeys?: true;
}
export type MutationFn<TData = unknown, TVariables = unknown> = (
variables: TVariables,
context: MutationFunctionContext,
) => Promise<TData>;
export type MutationSettledListener<
TData = unknown,
TError = DefaultError,
TVariables = void,
TContext = unknown,
> = (
data: TData | undefined,
error: TError | null,
variables: TVariables,
context: TContext | undefined,
) => void;
export type MutationErrorListener<
TError = DefaultError,
TVariables = void,
TContext = unknown,
> = (error: TError, payload: TVariables, context: TContext | undefined) => void;
export type MutationDoneListener<
TData = unknown,
TVariables = void,
TContext = unknown,
> = (data: TData, payload: TVariables, context: TContext | undefined) => void;
export interface MutationConfig<
TData = unknown,
TVariables = void,
TError = DefaultError,
TContext = unknown,
> extends Omit<
MutationObserverOptions<TData, TError, TVariables, TContext>,
'_defaulted' | 'mutationFn'
>,
MutationFeatures {
mutationFn?: MutationFn<TData, TVariables>;
queryClient: AnyQueryClient;
abortSignal?: AbortSignal;
invalidateQueries?:
| MutationInvalidateQueriesOptions
| ((
data: TData,
payload: TVariables,
) => MutationInvalidateQueriesOptions | null | undefined);
onInit?: (mutation: Mutation<TData, TVariables, TError, TContext>) => void;
}
export interface MutationFunctionContext extends MutationFunctionContextCore {
signal: AbortSignal;
}
export type MutationConfigFromFn<
T extends (...args: any[]) => any,
TError = DefaultError,
TContext = unknown,
> = MutationConfig<
ReturnType<T> extends Promise<infer TData> ? TData : ReturnType<T>,
Parameters<T>[0],
TError,
TContext
>;
export type InferMutation<
T extends MutationConfig | Mutation,
TInferValue extends
| 'data'
| 'variables'
| 'error'
| 'context'
| 'mutation'
| 'config',
> =
T extends MutationConfig<
infer TData,
infer TVariables,
infer TError,
infer TContext
>
? TInferValue extends 'config'
? T
: TInferValue extends 'data'
? TData
: TInferValue extends 'variables'
? TVariables
: TInferValue extends 'error'
? TError
: TInferValue extends 'context'
? TContext
: TInferValue extends 'mutation'
? Mutation<TData, TVariables, TError, TContext>
: never
: T extends Mutation<
infer TData,
infer TVariables,
infer TError,
infer TContext
>
? TInferValue extends 'config'
? MutationConfig<TData, TVariables, TError, TContext>
: TInferValue extends 'data'
? TData
: TInferValue extends 'variables'
? TVariables
: TInferValue extends 'error'
? TError
: TInferValue extends 'context'
? TContext
: TInferValue extends 'mutation'
? T
: never
: never;