Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

mutationOptions.ts

97 lines2.8 KB

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