RealityShiftR
tRPC3y ago
5 replies
RealityShift

Generic Zod as Input Type

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
    })
}


I'm not sure what should be in the
input
section to make this work?
Was this page helpful?