Open History to see the last commit
1import type { DefaultError, WithRequired } from '@tanstack/query-core'
2import type { CreateMutationOptions } from './types'
3β
4/**
5 * Allows to share and re-use mutation options in a type-safe way.
6 *
7 * **Example**
8 *
9 * ```ts
10 * export class QueriesService {
11 * private http = inject(HttpClient);
12 *
13 * updatePost(id: number) {
14 * return mutationOptions({
15 * mutationFn: (post: Post) => Promise.resolve(post),
16 * mutationKey: ["updatePost", id],
17 * onSuccess: (newPost) => {
18 * // ^? newPost: Post
19 * this.queryClient.setQueryData(["posts", id], newPost);
20 * },
21 * });
22 * }
23 * }
24 *
25 * queries = inject(QueriesService)
26 * idSignal = new Signal(0);
27 * mutation = injectMutation(() => this.queries.updatePost(this.idSignal()))
28 *
29 * mutation.mutate({ title: 'New Title' })
30 * ```
31 * @param options - The mutation options.
32 * @returns Mutation options.
33 */
34export function mutationOptions<
35 TData = unknown,
36 TError = DefaultError,
37 TVariables = void,
38 TContext = unknown,
39>(
40 options: WithRequired<
41 CreateMutationOptions<TData, TError, TVariables, TContext>,
42 'mutationKey'
43 >,
44): WithRequired<
45 CreateMutationOptions<TData, TError, TVariables, TContext>,
46 'mutationKey'
47>
48export function mutationOptions<
49 TData = unknown,
50 TError = DefaultError,
51 TVariables = void,
52 TContext = unknown,
53>(
54 options: Omit<
55 CreateMutationOptions<TData, TError, TVariables, TContext>,
56 'mutationKey'
57 >,
58): Omit<
59 CreateMutationOptions<TData, TError, TVariables, TContext>,
60 'mutationKey'
61>
62β
63/**
64 * Allows to share and re-use mutation options in a type-safe way.
65 *
66 * **Example**
67 *
68 * ```ts
69 * export class QueriesService {
70 * private http = inject(HttpClient);
71 *
72 * updatePost(id: number) {
73 * return mutationOptions({
74 * mutationFn: (post: Post) => Promise.resolve(post),
75 * mutationKey: ["updatePost", id],
76 * onSuccess: (newPost) => {
77 * // ^? newPost: Post
78 * this.queryClient.setQueryData(["posts", id], newPost);
79 * },
80 * });
81 * }
82 * }
83 *
84 * queries = inject(QueriesService)
85 * idSignal = new Signal(0);
86 * mutation = injectMutation(() => this.queries.updatePost(this.idSignal()))
87 *
88 * mutation.mutate({ title: 'New Title' })
89 * ```
90 * @param options - The mutation options.
91 * @returns Mutation options.
92 */
93export function mutationOptions<
94 TData = unknown,
95 TError = DefaultError,
96 TVariables = void,
97 TContext = unknown,
98>(
99 options: CreateMutationOptions<TData, TError, TVariables, TContext>,
100): CreateMutationOptions<TData, TError, TVariables, TContext> {
101 return options
102}