mirror of
https://github.com/ArthurDanjou/trpc-nuxt.git
synced 2026-02-01 20:57:54 +01:00
import useAsyncData from #app
This commit is contained in:
@@ -7,6 +7,8 @@ import type {
|
|||||||
import { createFlatProxy, createRecursiveProxy } from '@trpc/server/shared'
|
import { createFlatProxy, createRecursiveProxy } from '@trpc/server/shared'
|
||||||
import { hash } from 'ohash'
|
import { hash } from 'ohash'
|
||||||
import type { DecoratedProcedureRecord } from './types'
|
import type { DecoratedProcedureRecord } from './types'
|
||||||
|
// @ts-expect-error: Nuxt internal
|
||||||
|
import { useAsyncData } from '#app'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Calculates the key used for `useAsyncData` call
|
* Calculates the key used for `useAsyncData` call
|
||||||
@@ -18,7 +20,10 @@ export function getQueryKey(
|
|||||||
return input === undefined ? path : `${path}-${hash(input || '')}`
|
return input === undefined ? path : `${path}-${hash(input || '')}`
|
||||||
}
|
}
|
||||||
|
|
||||||
function createNuxtProxyDecoration<TRouter extends AnyRouter>(name: string, client: inferRouterProxyClient<TRouter>) {
|
/**
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
export function createNuxtProxyDecoration<TRouter extends AnyRouter>(name: string, client: inferRouterProxyClient<TRouter>) {
|
||||||
return createRecursiveProxy((opts) => {
|
return createRecursiveProxy((opts) => {
|
||||||
const args = opts.args
|
const args = opts.args
|
||||||
|
|
||||||
@@ -35,14 +40,12 @@ function createNuxtProxyDecoration<TRouter extends AnyRouter>(name: string, clie
|
|||||||
const queryKey = getQueryKey(path, input)
|
const queryKey = getQueryKey(path, input)
|
||||||
|
|
||||||
if (lastArg === 'mutate') {
|
if (lastArg === 'mutate') {
|
||||||
// @ts-expect-error: Nuxt internal
|
|
||||||
return useAsyncData(queryKey, () => (client as any)[path][lastArg](input), {
|
return useAsyncData(queryKey, () => (client as any)[path][lastArg](input), {
|
||||||
...asyncDataOptions as Record<string, any>,
|
...asyncDataOptions as Record<string, any>,
|
||||||
immediate: false,
|
immediate: false,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// @ts-expect-error: Nuxt internal
|
|
||||||
return useAsyncData(queryKey, () => (client as any)[path][lastArg](input), asyncDataOptions as Record<string, any>)
|
return useAsyncData(queryKey, () => (client as any)[path][lastArg](input), asyncDataOptions as Record<string, any>)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user