Open History to see the last commit
1import type { DefaultError, WithRequired } from '@tanstack/query-core'
2import type { CreateMutationOptions } from './types.js'
3​
4/**
5 * You can generally pass everything to `mutationOptions` that you can also pass to `createMutation`. This
6 * overload requires `mutationKey`, so the resulting options can be looked up elsewhere (e.g. with
7 * `useMutationState`).
8 *
9 * @see {@link createMutation} to run a mutation with these options.
10 * @param options - The options to use — everything you can pass to `createMutation`, with `mutationKey` set.
11 * @returns The same options object.
12 *
13 * @example
14 * Looking the mutation up elsewhere via its `mutationKey`, e.g. for a global "saving…" indicator:
15 * ```svelte
16 * <script lang="ts">
17 * import { mutationOptions, useMutationState } from '@tanstack/svelte-query'
18 *
19 * const createPostOptions = mutationOptions({
20 * mutationKey: ['posts', 'create'],
21 * mutationFn: createPost,
22 * })
23 *
24 * const pending = useMutationState({
25 * filters: { mutationKey: createPostOptions.mutationKey, status: 'pending' },
26 * })
27 * </script>
28 *
29 * {#if pending.length > 0}
30 * <span>Saving…</span>
31 * {/if}
32 * ```
33 */
34export function mutationOptions<
35 TData = unknown,
36 TError = DefaultError,
37 TVariables = void,
38 TOnMutateResult = unknown,
39>(
40 options: WithRequired<
41 CreateMutationOptions<TData, TError, TVariables, TOnMutateResult>,
42 'mutationKey'
43 >,
44): WithRequired<
45 CreateMutationOptions<TData, TError, TVariables, TOnMutateResult>,
46 'mutationKey'
47>
48​
49/**
50 * You can generally pass everything to `mutationOptions` that you can also pass to `createMutation`.
51 *
52 * @see {@link createMutation} to run a mutation with these options.
53 * @param options - The options to use — everything you can pass to `createMutation`.
54 * @returns The same options object.
55 *
56 * @example
57 * ```svelte
58 * <script lang="ts">
59 * import { mutationOptions, createMutation } from '@tanstack/svelte-query'
60 *
61 * const createPostOptions = mutationOptions({
62 * mutationFn: createPost,
63 * })
64 *
65 * const mutation = createMutation(() => createPostOptions)
66 * </script>
67 *
68 * <button onclick={() => mutation.mutate({ title: 'Hello' })}>Create</button>
69 * ```
70 */
71export function mutationOptions<
72 TData = unknown,
73 TError = DefaultError,
74 TVariables = void,
75 TOnMutateResult = unknown,
76>(
77 options: Omit<
78 CreateMutationOptions<TData, TError, TVariables, TOnMutateResult>,
79 'mutationKey'
80 >,
81): Omit<
82 CreateMutationOptions<TData, TError, TVariables, TOnMutateResult>,
83 'mutationKey'
84>
85export function mutationOptions<
86 TData = unknown,
87 TError = DefaultError,
88 TVariables = void,
89 TOnMutateResult = unknown,
90>(
91 options: CreateMutationOptions<TData, TError, TVariables, TOnMutateResult>,
92): CreateMutationOptions<TData, TError, TVariables, TOnMutateResult> {
93 return options
94}