From d16cf2d00e0b58650efe44e9e08c35049d54e24e Mon Sep 17 00:00:00 2001 From: ElisePatrikainen Date: Sun, 31 Mar 2024 15:52:43 +0200 Subject: [PATCH] refactor(mutation): remove passing mutation context to mutation key getter --- playground/src/pages/ecom/item/[id].vue | 2 +- src/use-mutation.test-d.ts | 3 +-- src/use-mutation.ts | 6 +++--- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/playground/src/pages/ecom/item/[id].vue b/playground/src/pages/ecom/item/[id].vue index f39acb5b..180ee319 100644 --- a/playground/src/pages/ecom/item/[id].vue +++ b/playground/src/pages/ecom/item/[id].vue @@ -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) diff --git a/src/use-mutation.test-d.ts b/src/use-mutation.test-d.ts index 3784cb6f..ba8b90b7 100644 --- a/src/use-mutation.test-d.ts +++ b/src/use-mutation.test-d.ts @@ -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'] }, }) diff --git a/src/use-mutation.ts b/src/use-mutation.ts index d801c226..b4a1277b 100644 --- a/src/use-mutation.ts +++ b/src/use-mutation.ts @@ -5,9 +5,9 @@ import type { EntryKey } from './entry-options' import type { ErrorDefault } from './types-extension' import { type _Awaitable, noop } from './utils' -type _MutationKey = +type _MutationKey = | EntryKey - | ((data: TResult, vars: TVars) => EntryKey) + | ((vars: TVars) => EntryKey) // TODO: move to a plugin /** @@ -73,7 +73,7 @@ export interface UseMutationOptions< */ mutation: (vars: TVars, context: NoInfer) => Promise - key?: _MutationKey + key?: _MutationKey // TODO: move this to a plugin that calls invalidateEntry() /**