NeoBean
NeoBean
TtRPC
Created by NeoBean on 2/23/2024 in #❓-help
Client request headers with createTRPCProxyClient
Simple proxy client, following the documentation
import { createTRPCProxyClient, httpBatchLink } from '@trpc/client'
import SuperJSON from 'superjson'
// .. other imports

export const trpcServer = createTRPCProxyClient<TRPCRootRouter>({
transformer: SuperJSON,
links: [httpBatchLink({ url: envs.API_URL_ABSOLUTE })],
})
import { createTRPCProxyClient, httpBatchLink } from '@trpc/client'
import SuperJSON from 'superjson'
// .. other imports

export const trpcServer = createTRPCProxyClient<TRPCRootRouter>({
transformer: SuperJSON,
links: [httpBatchLink({ url: envs.API_URL_ABSOLUTE })],
})
I'm using this helper inside my sveltekit load function, which runs server side. I see no way of forwarding the headers which i get from browser.
import { trpcServer } from 'lib/trpc.server'

export const load = (async ({ request: { headers } }) => {
// no way to forward request.headers
await trpcServer.user.me.query()
})
import { trpcServer } from 'lib/trpc.server'

export const load = (async ({ request: { headers } }) => {
// no way to forward request.headers
await trpcServer.user.me.query()
})
2 replies
TtRPC
Created by NeoBean on 11/19/2023 in #❓-help
Pass headers when prefetching using helpers
I dont see a way to pass headers and cookies with either fetch/prefetch methods from the ssr helper. They accept a context option but it is typed as a Record<string.., so i'm not sure what it does.
export const getServerSideProps = async ( ) => {
await ssHelpers.treedata.getAll.fetch(undefined)
await ssHelpers.treedata.getAll.prefetch(undefined)

return { trpcState: ssHelpers.dehydrate() }
}
export const getServerSideProps = async ( ) => {
await ssHelpers.treedata.getAll.fetch(undefined)
await ssHelpers.treedata.getAll.prefetch(undefined)

return { trpcState: ssHelpers.dehydrate() }
}
I'm following the external routers. https://trpc.io/docs/client/nextjs/server-side-helpers#2-external-router
4 replies
TtRPC
Created by NeoBean on 8/20/2023 in #❓-help
Forward client headers with createTRPCProxyClient & Server-Side Helpers
With createTRPCNext i had the option to tap into the context.req object nextjs passed in, but I cant achieve the same with createTRPCProxyClient. For example below code fails authentication, cause cookies arent forwarded.
export const getServerSideProps: GetServerSideProps = async ctx => {
ssHelpers.user.profile.prefetch(undefined, { context: ctx })
return { props: { trpcState: ssHelpers.dehydrate() } }
}
export const getServerSideProps: GetServerSideProps = async ctx => {
ssHelpers.user.profile.prefetch(undefined, { context: ctx })
return { props: { trpcState: ssHelpers.dehydrate() } }
}
8 replies
TtRPC
Created by NeoBean on 7/27/2023 in #❓-help
How to Type a Middleware factory?
No description
4 replies