mirror of
https://github.com/ArthurDanjou/trpc-nuxt.git
synced 2026-01-26 09:50:31 +01:00
fix: headers missing
This commit is contained in:
@@ -9,7 +9,6 @@ import type { ProcedureRecord, inferHandlerInput, inferProcedureInput, inferProc
|
||||
import type { TRPCClientErrorLike } from '@trpc/client'
|
||||
import { objectHash } from 'ohash'
|
||||
import { useAsyncData, useState } from '#app'
|
||||
import { useClient } from '#build/trpc-client'
|
||||
// @ts-expect-error: Resolved by Nuxt
|
||||
import type { router } from '~/server/trpc'
|
||||
|
||||
@@ -38,12 +37,12 @@ export async function useAsyncQuery<
|
||||
pathAndInput: [path: TPath, ...args: inferHandlerInput<TQueries[TPath]>],
|
||||
options: AsyncDataOptions<TOutput, Transform, PickKeys> = {},
|
||||
): Promise<AsyncData<PickFrom<ReturnType<Transform>, PickKeys>, TError>> {
|
||||
const client = useClient()
|
||||
const { $client } = useNuxtApp()
|
||||
const key = `${pathAndInput[0]}-${objectHash(pathAndInput[1] ? JSON.stringify(pathAndInput[1]) : '')}`
|
||||
const serverError = useState<TError | null>(`error-${key}`, () => null)
|
||||
const { error, data, ...rest } = await useAsyncData(
|
||||
key,
|
||||
() => client.query(...pathAndInput),
|
||||
() => $client.query(...pathAndInput),
|
||||
options,
|
||||
)
|
||||
|
||||
|
||||
18
src/runtime/plugin.ts
Normal file
18
src/runtime/plugin.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import * as trpc from '@trpc/client'
|
||||
import { defineNuxtPlugin, useRequestHeaders } from '#app'
|
||||
import type { router } from '~/server/trpc'
|
||||
|
||||
const options = JSON.parse('<%= JSON.stringify(options) %>')
|
||||
|
||||
export default defineNuxtPlugin(() => {
|
||||
const client = trpc.createTRPCClient<typeof router>({
|
||||
url: options.url as string,
|
||||
headers: useRequestHeaders(),
|
||||
})
|
||||
|
||||
return {
|
||||
provide: {
|
||||
client,
|
||||
},
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user