mirror of
https://github.com/ArthurDanjou/trpc-nuxt.git
synced 2026-01-14 12:14:40 +01:00
feat: remove useMutation
This commit is contained in:
@@ -38,7 +38,6 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"h3": "^1.0.1",
|
||||
"nanoid": "^4.0.0",
|
||||
"ohash": "^1.0.0",
|
||||
"ufo": "^1.0.0"
|
||||
},
|
||||
|
||||
2
pnpm-lock.yaml
generated
2
pnpm-lock.yaml
generated
@@ -11,14 +11,12 @@ importers:
|
||||
concurrently: ^7.5.0
|
||||
eslint: ^8.25.0
|
||||
h3: ^1.0.1
|
||||
nanoid: ^4.0.0
|
||||
ohash: ^1.0.0
|
||||
tsup: 6.4.0
|
||||
typescript: ^4.7.4
|
||||
ufo: ^1.0.0
|
||||
dependencies:
|
||||
h3: 1.0.1
|
||||
nanoid: 4.0.0
|
||||
ohash: 1.0.0
|
||||
ufo: 1.0.0
|
||||
devDependencies:
|
||||
|
||||
@@ -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,6 +30,7 @@ export function createNuxtProxyDecoration<TRouter extends AnyRouter> (name: stri
|
||||
|
||||
const [input, otherOptions] = args
|
||||
|
||||
if (lastArg === 'useQuery') {
|
||||
const { trpc, ...asyncDataOptions } = otherOptions || {} as any
|
||||
|
||||
let controller: AbortController
|
||||
@@ -44,7 +44,6 @@ export function createNuxtProxyDecoration<TRouter extends AnyRouter> (name: stri
|
||||
controller = typeof AbortController !== 'undefined' ? new AbortController() : {} as AbortController
|
||||
}
|
||||
|
||||
if (lastArg === 'useQuery') {
|
||||
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)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -13,7 +13,6 @@ import type {
|
||||
} from '@trpc/server'
|
||||
import { type inferObservableValue, type Unsubscribable } from '@trpc/server/observable'
|
||||
import { inferTransformedProcedureOutput } from '@trpc/server/shared'
|
||||
// import { inferTransformedProcedureOutput } from '@trpc/server/shared'
|
||||
import type {
|
||||
AsyncData,
|
||||
AsyncDataOptions,
|
||||
@@ -22,10 +21,6 @@ import type {
|
||||
_Transform
|
||||
} from 'nuxt/dist/app/composables/asyncData'
|
||||
|
||||
// Modified @trpc/client and @trpc/react-query types
|
||||
// https://github.com/trpc/trpc/blob/next/packages/client/src/createTRPCClientProxy.ts
|
||||
// https://github.com/trpc/trpc/blob/next/packages/react-query/src/createTRPCReact.tsx
|
||||
|
||||
interface TRPCRequestOptions extends _TRPCRequestOptions {
|
||||
abortOnUnmount?: boolean
|
||||
}
|
||||
@@ -65,15 +60,6 @@ type DecorateProcedure<
|
||||
) => AsyncData<PickFrom<ReturnType<Transform>, PickKeys>, TRPCClientErrorLike<TProcedure>>,
|
||||
query: Resolver<TProcedure>
|
||||
} : TProcedure extends AnyMutationProcedure ? {
|
||||
useMutation: <
|
||||
TData = inferTransformedProcedureOutput<TProcedure>,
|
||||
Transform extends _Transform<TData> = _Transform<TData, TData>,
|
||||
PickKeys extends KeyOfRes<Transform> = KeyOfRes<Transform>,
|
||||
>(
|
||||
opts?: AsyncDataOptions<TData, Transform, PickKeys> & { trpc?: TRPCRequestOptions },
|
||||
) => AsyncData<PickFrom<ReturnType<Transform>, PickKeys>, TRPCClientErrorLike<TProcedure>> & {
|
||||
mutate: (input: inferProcedureInput<TProcedure>) => Promise<PickFrom<ReturnType<Transform>, PickKeys>>
|
||||
},
|
||||
mutate: Resolver<TProcedure>
|
||||
} : TProcedure extends AnySubscriptionProcedure ? {
|
||||
subscribe: SubscriptionResolver<TProcedure, TRouter>
|
||||
|
||||
Reference in New Issue
Block a user