T
tRPC

❓-help

Generic Zod as Input Type

RRealityShift10/25/2023
I'm not positive if this is a TRPC question, typescript in general, or failure to understand Zod in this usecase. Our API has a several endpoints that all work the same way just pass in different payloads. I'm trying to create a single function to assign to all my TRPC routes:
enum Endpoint {
"Person" = "/users/person",
"Company" = "/core/company",
}

update: createTRPCRouter({
person: updateGeneric(ZPersonUpdate, Endpoint.Person),
company: updateGeneric(ZCompanyUpdate, Endpoint.Company),
})

function createUpdateApiProcedure<T extends z.SomeZodObject>(schema: T, path: Endpoint) {
return protectedProcedure.input(
z.object({id: z.number()}).extend(schema)
).mutation(async ({ ctx, input }) => {
//call api with payload
})
}
enum Endpoint {
"Person" = "/users/person",
"Company" = "/core/company",
}

update: createTRPCRouter({
person: updateGeneric(ZPersonUpdate, Endpoint.Person),
company: updateGeneric(ZCompanyUpdate, Endpoint.Company),
})

function createUpdateApiProcedure<T extends z.SomeZodObject>(schema: T, path: Endpoint) {
return protectedProcedure.input(
z.object({id: z.number()}).extend(schema)
).mutation(async ({ ctx, input }) => {
//call api with payload
})
}
I'm not sure what should be in the input section to make this work?
Nnlucas10/25/2023
You want a Router Factory, and potentially the polymorphism utilities I have a dev.to post on it if you google for “trpc router factories” but it’s a more advanced topic so we don’t really push this in the docs Inputs themselves can’t be generic though, you need to have multiple routers
RRealityShift11/10/2023
Got it thanks Nick!

Looking for more? Join the community!

Recommended Posts
Incompatible Router TypesI am working on a trpc implementation for sveltekit. I attached a screenshot of the createContext nextjs app router, "fs", "os", "zlib-sync" and trpc experimental edge RouterEnvironement: Turborepo + pnpm What's wrong: when I tried to build nextjs, the error is like this TS4111: Property 'error' comes from an index signature, so it must be accessed with ['error'].I am trying to use trpc with AnalogJs, an Angular meta framework and Nx. We use the @nx/vite builderTypeError: Cannot read properties of null (reading '_def')As the title says, I get the following: TypeError: Cannot read properties of null (reading '_def') I wanna add a localStorage persister, but I'm getting errors for hydration:I wanna add a localStorage persister, but I'm getting errors for hydration: ```js const persister =error route always getting 500 from trpc error? (next13/approuter)I've got this condition for throwing in my trpc procedure: ```ts if (!userClerkProfile) { force-cache planetscale errortrying to use the trpc API from RSCs fails and gives the following error ```log result: { data:What is the pattern for unsubscribing from a subscription?Hi there! I'd like to prevent unnecessary connections to a websocket server across multiple renders.How to add a short delay between requestsEnvironment: nextjs 13, node 18, npm, trpc 10.9.0 I'm wondering if it's possible to add a short delGlobal metadata / filters: Re-validate all queriesHi all, Simple question, I'm looking for the most efficient way to add some global state to all queAttempted import error: 'hashQueryKey' is not exported from '@tanstack/react-query' issuei'm getting this error while trying trpc with latest next version , i tried downgrading but got anotTRPC cant handle ErrorHi, can someone please help me: https://stackoverflow.com/questions/77333318/trpc-handle-trpcerror