fix: headers missing

This commit is contained in:
Robert Soriano
2022-05-23 10:04:06 -07:00
parent 6dcb4ce8a6
commit 77325a6699
5 changed files with 72 additions and 14 deletions

View File

@@ -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
View 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,
},
}
})