mirror of
https://github.com/ArthurDanjou/trpc-nuxt.git
synced 2026-01-31 04:07:55 +01:00
feat: remove useMutation
This commit is contained in:
@@ -2,7 +2,6 @@ import { type CreateTRPCClientOptions, type inferRouterProxyClient, createTRPCPr
|
||||
import { type AnyRouter } from '@trpc/server'
|
||||
import { createFlatProxy, createRecursiveProxy } from '@trpc/server/shared'
|
||||
import { hash } from 'ohash'
|
||||
import { nanoid } from 'nanoid'
|
||||
import { type DecoratedProcedureRecord } from './types'
|
||||
// @ts-expect-error: Nuxt auto-imports
|
||||
import { getCurrentInstance, onScopeDispose, useAsyncData, ref, unref } from '#imports'
|
||||
@@ -31,20 +30,20 @@ export function createNuxtProxyDecoration<TRouter extends AnyRouter> (name: stri
|
||||
|
||||
const [input, otherOptions] = args
|
||||
|
||||
const { trpc, ...asyncDataOptions } = otherOptions || {} as any
|
||||
|
||||
let controller: AbortController
|
||||
|
||||
if (trpc?.abortOnUnmount) {
|
||||
if (getCurrentInstance()) {
|
||||
onScopeDispose(() => {
|
||||
controller?.abort?.()
|
||||
})
|
||||
}
|
||||
controller = typeof AbortController !== 'undefined' ? new AbortController() : {} as AbortController
|
||||
}
|
||||
|
||||
if (lastArg === 'useQuery') {
|
||||
const { trpc, ...asyncDataOptions } = otherOptions || {} as any
|
||||
|
||||
let controller: AbortController
|
||||
|
||||
if (trpc?.abortOnUnmount) {
|
||||
if (getCurrentInstance()) {
|
||||
onScopeDispose(() => {
|
||||
controller?.abort?.()
|
||||
})
|
||||
}
|
||||
controller = typeof AbortController !== 'undefined' ? new AbortController() : {} as AbortController
|
||||
}
|
||||
|
||||
const queryKey = getQueryKey(path, input)
|
||||
return useAsyncData(queryKey, () => (client as any)[path].query(input, {
|
||||
signal: controller?.signal,
|
||||
@@ -52,28 +51,6 @@ export function createNuxtProxyDecoration<TRouter extends AnyRouter> (name: stri
|
||||
}), asyncDataOptions)
|
||||
}
|
||||
|
||||
if (lastArg === 'useMutation') {
|
||||
const reactiveInput = ref(null)
|
||||
const { refresh, ...result } = useAsyncData(nanoid(), () => (client as any)[path].mutate(reactiveInput.value, {
|
||||
signal: controller?.signal,
|
||||
...trpc
|
||||
}), {
|
||||
...asyncDataOptions,
|
||||
immediate: false
|
||||
})
|
||||
|
||||
async function mutate (_input: any) {
|
||||
reactiveInput.value = _input
|
||||
await refresh()
|
||||
return unref(result.data)
|
||||
}
|
||||
|
||||
return {
|
||||
mutate,
|
||||
...result
|
||||
}
|
||||
}
|
||||
|
||||
return (client as any)[path][lastArg](input)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user