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