Skip to content

Commit

Permalink
feat: allow getter inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
wobsoriano committed Feb 6, 2024
1 parent c9485f8 commit 9603998
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
6 changes: 3 additions & 3 deletions src/client/decorationProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ 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, ref, isRef, toRaw } from '#imports'
import { getCurrentInstance, onScopeDispose, useAsyncData, toValue, ref, isRef, toRaw } from '#imports'
import { getQueryKeyInternal } from './getQueryKey'

function createAbortController(trpc: any) {
Expand Down Expand Up @@ -45,11 +45,11 @@ export function createNuxtProxyDecoration<TRouter extends AnyRouter> (name: stri

const controller = createAbortController(trpc);

const queryKey = customQueryKey || getQueryKeyInternal(path, unref(input))
const queryKey = customQueryKey || getQueryKeyInternal(path, toValue(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), {
return useAsyncData(queryKey, () => (client as any)[path].query(toValue(input), {
signal: controller?.signal,
...trpc
}), {
Expand Down
8 changes: 3 additions & 5 deletions src/client/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import type {
import { type inferObservableValue, type Unsubscribable } from '@trpc/server/observable'
import { inferTransformedProcedureOutput } from '@trpc/server/shared'
import type { AsyncData, AsyncDataOptions } from 'nuxt/app'
import type { Ref, UnwrapRef } from 'vue'
import type { MaybeRefOrGetter, UnwrapRef } from 'vue'

type PickFrom<T, K extends Array<string>> = T extends Array<any> ? T : T extends Record<string, any> ? keyof T extends K[number] ? T : K[number] extends never ? T : Pick<T, K[number]> : T;
type KeysOf<T> = Array<T extends T ? keyof T extends string ? keyof T : never : never>;
Expand Down Expand Up @@ -50,8 +50,6 @@ type SubscriptionResolver<
]
) => Unsubscribable

type MaybeRef<T> = T | Ref<T>

export type DecorateProcedure<
TProcedure extends AnyProcedure,
TRouter extends AnyRouter,
Expand All @@ -63,7 +61,7 @@ export type DecorateProcedure<
DataT = ResT,
PickKeys extends KeysOf<DataT> = KeysOf<DataT>,
>(
input: MaybeRef<inferProcedureInput<TProcedure>>,
input: MaybeRefOrGetter<inferProcedureInput<TProcedure>>,
opts?: AsyncDataOptions<ResT, DataT, PickKeys> & {
trpc?: TRPCRequestOptions
/**
Expand All @@ -79,7 +77,7 @@ export type DecorateProcedure<
DataT = ResT,
PickKeys extends KeysOf<DataT> = KeysOf<DataT>,
>(
input: MaybeRef<inferProcedureInput<TProcedure>>,
input: MaybeRefOrGetter<inferProcedureInput<TProcedure>>,
opts?: Omit<AsyncDataOptions<ResT, DataT, PickKeys>, 'lazy'> & {
trpc?: TRPCRequestOptions
/**
Expand Down

0 comments on commit 9603998

Please sign in to comment.