diff --git a/docs/content/1.get-started/5.tips/7.mutation.md b/docs/content/1.get-started/5.tips/7.mutation.md
index b2e04c5..910dc5f 100644
--- a/docs/content/1.get-started/5.tips/7.mutation.md
+++ b/docs/content/1.get-started/5.tips/7.mutation.md
@@ -10,17 +10,21 @@ The example below shows how you can use Nuxt's [`useNuxtData`](https://nuxt.com/
```
```vue [components/NewTodo.vue]
@@ -31,7 +36,7 @@ const { data: todos, pending, error, refresh } = await $client.todo.getTodos.use
-
export const todoRouter = router({
getTodos: publicProcedure
.query(() => {
- return $fetch(`${baseURL}/todos`)
+ return $fetch(`${baseURL}/todos?_limit=5`)
}),
getTodo: publicProcedure
.input(z.number())
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 88770b2..d2685ac 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -6,8 +6,8 @@ settings:
overrides:
nuxt: 3.8.0
- '@trpc/client': ^10.41.0
- '@trpc/server': ^10.41.0
+ '@trpc/client': ^10.44.1
+ '@trpc/server': ^10.44.1
importers:
@@ -27,10 +27,10 @@ importers:
specifier: ^0.2.0
version: 0.2.0(eslint@8.52.0)
'@trpc/client':
- specifier: ^10.41.0
+ specifier: ^10.44.1
version: 10.44.1(@trpc/server@10.44.1)
'@trpc/server':
- specifier: ^10.41.0
+ specifier: ^10.44.1
version: 10.44.1
changelogen:
specifier: ^0.5.5
@@ -63,10 +63,10 @@ importers:
playground:
dependencies:
'@trpc/client':
- specifier: ^10.41.0
+ specifier: ^10.44.1
version: 10.44.1(@trpc/server@10.44.1)
'@trpc/server':
- specifier: ^10.41.0
+ specifier: ^10.44.1
version: 10.44.1
superjson:
specifier: ^2.1.0
@@ -3026,7 +3026,7 @@ packages:
/@trpc/client@10.44.1(@trpc/server@10.44.1):
resolution: {integrity: sha512-vTWsykNcgz1LnwePVl2fKZnhvzP9N3GaaLYPkfGINo314ZOS0OBqe9x0ytB2LLUnRVTAAZ2WoONzARd8nHiqrA==}
peerDependencies:
- '@trpc/server': ^10.41.0
+ '@trpc/server': ^10.44.1
dependencies:
'@trpc/server': 10.44.1
diff --git a/src/client/decorationProxy.ts b/src/client/decorationProxy.ts
new file mode 100644
index 0000000..76e1099
--- /dev/null
+++ b/src/client/decorationProxy.ts
@@ -0,0 +1,58 @@
+import { type inferRouterProxyClient } from '@trpc/client'
+import { type AnyRouter } from '@trpc/server'
+import { createRecursiveProxy } from '@trpc/server/shared'
+// @ts-expect-error: Nuxt auto-imports
+import { getCurrentInstance, onScopeDispose, useAsyncData, unref, isRef } from '#imports'
+import { getQueryKeyInternal } from './getQueryKey'
+
+export function createNuxtProxyDecoration (name: string, client: inferRouterProxyClient) {
+ return createRecursiveProxy((opts) => {
+ const args = opts.args
+
+ const pathCopy = [name, ...opts.path]
+
+ // The last arg is for instance `.useMutation` or `.useQuery()`
+ const lastArg = pathCopy.pop()!
+
+ // The `path` ends up being something like `post.byId`
+ const path = pathCopy.join('.')
+
+ const [input, otherOptions] = args
+
+ if (lastArg === '_def') {
+ return {
+ path: pathCopy,
+ };
+ }
+
+ if (['useQuery', 'useLazyQuery'].includes(lastArg)) {
+ const { trpc, queryKey: customQueryKey, ...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 = customQueryKey || getQueryKeyInternal(path, unref(input))
+ const watch = isRef(input) ? [...(asyncDataOptions.watch || []), input] : asyncDataOptions.watch
+ const isLazy = lastArg === 'useLazyQuery' ? true : (asyncDataOptions.lazy || false)
+
+ return useAsyncData(queryKey, () => (client as any)[path].query(unref(input), {
+ signal: controller?.signal,
+ ...trpc
+ }), {
+ ...asyncDataOptions,
+ watch,
+ lazy: isLazy
+ })
+ }
+
+ return (client as any)[path][lastArg](...args)
+ })
+}
diff --git a/src/client/getQueryKey.ts b/src/client/getQueryKey.ts
new file mode 100644
index 0000000..a3bcb9a
--- /dev/null
+++ b/src/client/getQueryKey.ts
@@ -0,0 +1,57 @@
+import {
+ AnyQueryProcedure,
+ AnyRouter,
+ DeepPartial,
+ inferProcedureInput,
+} from '@trpc/server';
+import { hash } from 'ohash'
+import { DecorateProcedure } from './types';
+
+export type GetQueryParams<
+ TProcedureOrRouter extends AnyQueryProcedure,
+ TProcedureInput = inferProcedureInput,
+> = DeepPartial;
+
+type GetParams<
+ TProcedureOrRouter extends AnyQueryProcedure,
+> = [
+ procedureOrRouter: DecorateProcedure | string,
+ params: GetQueryParams,
+];
+
+type GetQueryKeyParams<
+ TProcedureOrRouter extends AnyQueryProcedure,
+> = GetParams;
+
+/**
+ * Method to extract the query key for a procedure
+ * @param procedure - procedure
+ * @param input - input to procedure
+ * @link https://trpc-nuxt.vercel.app/get-started/tips/mutation
+ */
+export function getQueryKey<
+ TProcedure extends AnyQueryProcedure,
+>(..._params: GetQueryKeyParams): string {
+ const [procedure, input] = _params;
+
+ if (typeof procedure === 'string') {
+ // TODO: Warn here if string is passed that it will be deprecated in the future.
+ return getQueryKeyInternal(procedure, input);
+ }
+
+ // @ts-expect-error: we don't expose _def on the type layer
+ const path = procedure._def().path as string[];
+ const dotPath = path.join('.');
+
+ return getQueryKeyInternal(dotPath, input)
+}
+
+/**
+ * @internal
+ */
+export function getQueryKeyInternal (
+ path: string,
+ input: unknown
+): string {
+ return input === undefined ? path : `${path}-${hash(input || '')}`
+}
diff --git a/src/client/index.ts b/src/client/index.ts
index 3e01ae6..8527a82 100644
--- a/src/client/index.ts
+++ b/src/client/index.ts
@@ -1,76 +1,11 @@
-import { type CreateTRPCClientOptions, type inferRouterProxyClient, createTRPCProxyClient } from '@trpc/client'
+import { type CreateTRPCClientOptions, createTRPCProxyClient } from '@trpc/client'
import { type AnyRouter } from '@trpc/server'
-import { createFlatProxy, createRecursiveProxy } from '@trpc/server/shared'
-import { hash } from 'ohash'
+import { createFlatProxy } from '@trpc/server/shared'
import { type DecoratedProcedureRecord } from './types'
-// @ts-expect-error: Nuxt auto-imports
-import { getCurrentInstance, onScopeDispose, useAsyncData, unref, isRef } from '#imports'
+import { getQueryKey } from './getQueryKey'
+import { createNuxtProxyDecoration } from './decorationProxy'
-/**
- * Calculates the key used for `useAsyncData` call.
- *
- * @example
- *
- * ```ts
- * import { getQueryKey } from 'trpc-nuxt/client'
- *
- * $client.todo.getTodo(1)
- *
- * const queryKey = getQueryKey('todo.getTodo', 1)
- * ```
- */
-export function getQueryKey (
- path: string,
- input: unknown
-): string {
- return input === undefined ? path : `${path}-${hash(input || '')}`
-}
-
-export function createNuxtProxyDecoration (name: string, client: inferRouterProxyClient) {
- return createRecursiveProxy((opts) => {
- const args = opts.args
-
- const pathCopy = [name, ...opts.path]
-
- // The last arg is for instance `.useMutation` or `.useQuery()`
- const lastArg = pathCopy.pop()!
-
- // The `path` ends up being something like `post.byId`
- const path = pathCopy.join('.')
-
- const [input, otherOptions] = args
-
- if (['useQuery', 'useLazyQuery'].includes(lastArg)) {
- const { trpc, queryKey: customQueryKey, ...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 = customQueryKey || getQueryKey(path, unref(input))
- const watch = isRef(input) ? [...(asyncDataOptions.watch || []), input] : asyncDataOptions.watch
- const isLazy = lastArg === 'useLazyQuery' ? true : (asyncDataOptions.lazy || false)
-
- return useAsyncData(queryKey, () => (client as any)[path].query(unref(input), {
- signal: controller?.signal,
- ...trpc
- }), {
- ...asyncDataOptions,
- watch,
- lazy: isLazy
- })
- }
-
- return (client as any)[path][lastArg](...args)
- })
-}
+export { getQueryKey }
export function createTRPCNuxtClient (opts: CreateTRPCClientOptions) {
const client = createTRPCProxyClient(opts)
diff --git a/src/client/types.ts b/src/client/types.ts
index abedebe..8383c66 100644
--- a/src/client/types.ts
+++ b/src/client/types.ts
@@ -47,7 +47,7 @@ type SubscriptionResolver<
type MaybeRef = T | Ref
-type DecorateProcedure<
+export type DecorateProcedure<
TProcedure extends AnyProcedure,
TRouter extends AnyRouter,
> = TProcedure extends AnyQueryProcedure