Skip to content

Commit 054fad9

Browse files
committed
feat: add reactive inputs
1 parent e15a62a commit 054fad9

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

playground/pages/reactive.vue

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<script setup lang="ts">
2+
const route = useRoute()
3+
const { $client } = useNuxtApp()
4+
const id = ref(1)
5+
// const { data: todo, pending, error } = await useAsyncData(() => $client.todo.getTodo.query(id.value), {
6+
// watch: [id]
7+
// })
8+
9+
const { data: todo, pending, error, refresh } = await $client.todo.getTodo.useQuery(id, {
10+
watch: [id],
11+
})
12+
</script>
13+
14+
<template>
15+
<div v-if="pending">
16+
Loading...
17+
</div>
18+
<div v-else-if="error?.data?.code">
19+
Error: {{ error.data.code }}
20+
</div>
21+
<div v-else>
22+
ID: {{ todo?.id }} <br>
23+
Title: {{ todo?.title }} <br>
24+
Completed: {{ todo?.completed }}
25+
</div>
26+
<button @click="id++">
27+
Next Todo
28+
</button>
29+
</template>

src/client/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { createFlatProxy, createRecursiveProxy } from '@trpc/server/shared'
44
import { hash } from 'ohash'
55
import { type DecoratedProcedureRecord } from './types'
66
// @ts-expect-error: Nuxt auto-imports
7-
import { getCurrentInstance, onScopeDispose, useAsyncData } from '#imports'
7+
import { getCurrentInstance, onScopeDispose, useAsyncData, unref } from '#imports'
88

99
/**
1010
* Calculates the key used for `useAsyncData` call.
@@ -54,8 +54,8 @@ export function createNuxtProxyDecoration<TRouter extends AnyRouter> (name: stri
5454
controller = typeof AbortController !== 'undefined' ? new AbortController() : {} as AbortController
5555
}
5656

57-
const queryKey = getQueryKey(path, input)
58-
return useAsyncData(queryKey, () => (client as any)[path].query(input, {
57+
const queryKey = getQueryKey(path, unref(input))
58+
return useAsyncData(queryKey, () => (client as any)[path].query(unref(input), {
5959
signal: controller?.signal,
6060
...trpc
6161
}), asyncDataOptions)

src/client/types.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import type {
1919
KeysOf,
2020
PickFrom,
2121
} from 'nuxt/dist/app/composables/asyncData'
22+
import type { Ref } from 'vue'
2223

2324
interface TRPCRequestOptions extends _TRPCRequestOptions {
2425
abortOnUnmount?: boolean
@@ -44,6 +45,8 @@ type SubscriptionResolver<
4445
]
4546
) => Unsubscribable
4647

48+
type MaybeRef<T> = T | Ref<T>
49+
4750
type DecorateProcedure<
4851
TProcedure extends AnyProcedure,
4952
TRouter extends AnyRouter,
@@ -55,7 +58,7 @@ type DecorateProcedure<
5558
DataT = ResT,
5659
PickKeys extends KeysOf<DataT> = KeysOf<DataT>,
5760
>(
58-
input: inferProcedureInput<TProcedure>,
61+
input: MaybeRef<inferProcedureInput<TProcedure>>,
5962
opts?: AsyncDataOptions<ResT, DataT, PickKeys> & { trpc?: TRPCRequestOptions },
6063
) => AsyncData<PickFrom<DataT, PickKeys>, DataE>,
6164
query: Resolver<TProcedure>

0 commit comments

Comments
 (0)