Skip to content

Commit

Permalink
refactor(mutation): remove passing mutation context to mutation key g…
Browse files Browse the repository at this point in the history
…etter
  • Loading branch information
ElisePatrikainen committed Apr 5, 2024
1 parent bc8a47f commit d16cf2d
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 6 deletions.
2 changes: 1 addition & 1 deletion playground/src/pages/ecom/item/[id].vue
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const itemAvailability = ref()
watch(() => item.value?.availability, (value) => itemAvailability.value = value)
const { mutate: bookProduct } = useMutation({
key: ['book-item'],
key: (product) => ['book-item', product.id],
keys: (product) => [['items'], ['items', product.id]],
mutation: async (product: ProductListItem) => {
await delay(Math.random() * 1000 + 200)
Expand Down
3 changes: 1 addition & 2 deletions src/use-mutation.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import { useMutation } from './use-mutation'
it('types the parameters for the key', () => {
useMutation({
mutation: (_one: string) => Promise.resolve({ name: 'foo' }),
key(result, one) {
key(one) {
expectTypeOf(one).toBeString()
expectTypeOf(result).toEqualTypeOf<{ name: string }>()
return ['foo']
},
})
Expand Down
6 changes: 3 additions & 3 deletions src/use-mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import type { EntryKey } from './entry-options'
import type { ErrorDefault } from './types-extension'
import { type _Awaitable, noop } from './utils'

type _MutationKey<TVars, TResult> =
type _MutationKey<TVars> =
| EntryKey
| ((data: TResult, vars: TVars) => EntryKey)
| ((vars: TVars) => EntryKey)

// TODO: move to a plugin
/**
Expand Down Expand Up @@ -73,7 +73,7 @@ export interface UseMutationOptions<
*/
mutation: (vars: TVars, context: NoInfer<TContext>) => Promise<TResult>

key?: _MutationKey<TVars, TResult>
key?: _MutationKey<TVars>

// TODO: move this to a plugin that calls invalidateEntry()
/**
Expand Down

0 comments on commit d16cf2d

Please sign in to comment.