feat: add reactive inputs

This commit is contained in:
wobsoriano
2023-05-26 20:41:22 -07:00
parent e15a62a5b2
commit 054fad952a
3 changed files with 36 additions and 4 deletions

View File

@@ -4,7 +4,7 @@ import { createFlatProxy, createRecursiveProxy } from '@trpc/server/shared'
import { hash } from 'ohash'
import { type DecoratedProcedureRecord } from './types'
// @ts-expect-error: Nuxt auto-imports
import { getCurrentInstance, onScopeDispose, useAsyncData } from '#imports'
import { getCurrentInstance, onScopeDispose, useAsyncData, unref } from '#imports'
/**
* Calculates the key used for `useAsyncData` call.
@@ -54,8 +54,8 @@ export function createNuxtProxyDecoration<TRouter extends AnyRouter> (name: stri
controller = typeof AbortController !== 'undefined' ? new AbortController() : {} as AbortController
}
const queryKey = getQueryKey(path, input)
return useAsyncData(queryKey, () => (client as any)[path].query(input, {
const queryKey = getQueryKey(path, unref(input))
return useAsyncData(queryKey, () => (client as any)[path].query(unref(input), {
signal: controller?.signal,
...trpc
}), asyncDataOptions)

View File

@@ -19,6 +19,7 @@ import type {
KeysOf,
PickFrom,
} from 'nuxt/dist/app/composables/asyncData'
import type { Ref } from 'vue'
interface TRPCRequestOptions extends _TRPCRequestOptions {
abortOnUnmount?: boolean
@@ -44,6 +45,8 @@ type SubscriptionResolver<
]
) => Unsubscribable
type MaybeRef<T> = T | Ref<T>
type DecorateProcedure<
TProcedure extends AnyProcedure,
TRouter extends AnyRouter,
@@ -55,7 +58,7 @@ type DecorateProcedure<
DataT = ResT,
PickKeys extends KeysOf<DataT> = KeysOf<DataT>,
>(
input: inferProcedureInput<TProcedure>,
input: MaybeRef<inferProcedureInput<TProcedure>>,
opts?: AsyncDataOptions<ResT, DataT, PickKeys> & { trpc?: TRPCRequestOptions },
) => AsyncData<PickFrom<DataT, PickKeys>, DataE>,
query: Resolver<TProcedure>