Katzius
Katzius
TtRPC
Created by santiago_28407 on 10/20/2023 in #❓-help
TRPC cant handle Error
maybe there you are catching the error
3 replies
TtRPC
Created by santiago_28407 on 10/20/2023 in #❓-help
TRPC cant handle Error
you didn't write how you init the trpc client
3 replies
TtRPC
Created by Katzius on 10/20/2023 in #❓-help
I want to create a wrapper for TRPC.init but I can't seem to get the context type correct.
was able to resolve it using type inference: type LocalContext<T> = inferAsyncReturnType<typeof createInternalContext<T>> export type Context<T> = T extends infer R ? LocalContext<R> : never
9 replies
TtRPC
Created by Katzius on 10/20/2023 in #❓-help
I want to create a wrapper for TRPC.init but I can't seem to get the context type correct.
thanks for the answer by th eway
9 replies
TtRPC
Created by Katzius on 10/20/2023 in #❓-help
I want to create a wrapper for TRPC.init but I can't seem to get the context type correct.
even without the Awaited, it results in the same output
9 replies
TtRPC
Created by Katzius on 10/20/2023 in #❓-help
I want to create a wrapper for TRPC.init but I can't seem to get the context type correct.
makeTRPC<T> always resolves to {}
9 replies
TtRPC
Created by Katzius on 10/20/2023 in #❓-help
I want to create a wrapper for TRPC.init but I can't seem to get the context type correct.
it works, the problem is with the generic type
9 replies
TtRPC
Created by Katzius on 10/20/2023 in #❓-help
I want to create a wrapper for TRPC.init but I can't seem to get the context type correct.
and I want to do:
function makeTRPC<T>() {
const t = initTRPC.context<Context<T>>().create({
errorFormatter({shape}) {
return shape
},
})
return t
}
function makeTRPC<T>() {
const t = initTRPC.context<Context<T>>().create({
errorFormatter({shape}) {
return shape
},
})
return t
}
but when I do:
const t = makeTRPC<{test: string}>()
const t = makeTRPC<{test: string}>()
t.context is of type {} when I do that 😦
9 replies
TtRPC
Created by Katzius on 10/20/2023 in #❓-help
I want to create a wrapper for TRPC.init but I can't seem to get the context type correct.
create-context.ts
import {inferAsyncReturnType} from '@trpc/server'
import type {
CreateFastifyContextOptions,
FastifyTRPCPluginOptions,
// eslint-disable-next-line
} from '@trpc/server/adapters/fastify'

/**
* Defines your inner context shape.
* Add fields here that the inner context brings.
*/
export interface CreateInnerContextOptions extends Partial<CreateFastifyContextOptions> {}

export function createContext(config: Partial<FastifyTRPCPluginOptions<any>>) {
return (outerOptions: CreateFastifyContextOptions) => {
return createInternalContext((innerOptions) => {
if (!outerOptions?.req || !outerOptions?.res) {
return innerOptions
}
return {
...innerOptions,
...config?.trpcOptions?.createContext?.({
req: outerOptions.req,
res: outerOptions.res,
}),
}
}, outerOptions)
}
}

/**
* Outer context. Used in the routers and will e.g. bring `req` & `res` to the context as "not `undefined`".
*
* @see https://trpc.io/docs/context#inner-and-outer-context
*/
async function createInternalContext<T>(
createContextInner: (opts?: CreateInnerContextOptions) => T,
opts: CreateFastifyContextOptions,
) {
const contextInner = createContextInner()
return {
...contextInner,
req: opts.req,
res: opts.res,
}
}

export type Context<T> = inferAsyncReturnType<(opts: T) => Awaited<typeof createContext>>
import {inferAsyncReturnType} from '@trpc/server'
import type {
CreateFastifyContextOptions,
FastifyTRPCPluginOptions,
// eslint-disable-next-line
} from '@trpc/server/adapters/fastify'

/**
* Defines your inner context shape.
* Add fields here that the inner context brings.
*/
export interface CreateInnerContextOptions extends Partial<CreateFastifyContextOptions> {}

export function createContext(config: Partial<FastifyTRPCPluginOptions<any>>) {
return (outerOptions: CreateFastifyContextOptions) => {
return createInternalContext((innerOptions) => {
if (!outerOptions?.req || !outerOptions?.res) {
return innerOptions
}
return {
...innerOptions,
...config?.trpcOptions?.createContext?.({
req: outerOptions.req,
res: outerOptions.res,
}),
}
}, outerOptions)
}
}

/**
* Outer context. Used in the routers and will e.g. bring `req` & `res` to the context as "not `undefined`".
*
* @see https://trpc.io/docs/context#inner-and-outer-context
*/
async function createInternalContext<T>(
createContextInner: (opts?: CreateInnerContextOptions) => T,
opts: CreateFastifyContextOptions,
) {
const contextInner = createContextInner()
return {
...contextInner,
req: opts.req,
res: opts.res,
}
}

export type Context<T> = inferAsyncReturnType<(opts: T) => Awaited<typeof createContext>>
9 replies