epicmau5time
epicmau5time17mo ago

Inference on routes & procedures

I have some routes that all have the same methods/procedures, but the input and output is specific to them due to prisma. I made it this far in making a generic hook but the return type of the data is always "any". Any help would be appreciated.
// server routes
const routeOne = router({
getList: t.procedure.input(routeOnePrismaScmeha).query(async ()=>({
return await prisma.routeOne.findMany({})
}),
getItem: t.procedure.input(routeOnePrismaScmeha).query(async ()=>({
return await prisma.routeOne.findFirst({})
}),
/* some more similary named procedures*/
})

const routeTwo = router({
getList: t.procedure.input(routeTwoPrismaScmeha).query(async ()=>({
return await prisma.routeTwo.findMany({})
}),
getItem: t.procedure.input(routeTwoPrismaScmeha).query(async ()=>({
return await prisma.routeTwo.findFirst({})
}),
/* some more similary named procedures*/
})

const routeThree = router({
getList: t.procedure.input(routeThreePrismaScmeha).query(async ()=>({
return await prisma.routeThree.findMany({})
}),
getItem: t.procedure.input(routeThreePrismaScmeha).query(async ()=>({
return await prisma.routeThree.findFirst({})
}),
/* some more similary named procedures*/
})

/* etc */
// server routes
const routeOne = router({
getList: t.procedure.input(routeOnePrismaScmeha).query(async ()=>({
return await prisma.routeOne.findMany({})
}),
getItem: t.procedure.input(routeOnePrismaScmeha).query(async ()=>({
return await prisma.routeOne.findFirst({})
}),
/* some more similary named procedures*/
})

const routeTwo = router({
getList: t.procedure.input(routeTwoPrismaScmeha).query(async ()=>({
return await prisma.routeTwo.findMany({})
}),
getItem: t.procedure.input(routeTwoPrismaScmeha).query(async ()=>({
return await prisma.routeTwo.findFirst({})
}),
/* some more similary named procedures*/
})

const routeThree = router({
getList: t.procedure.input(routeThreePrismaScmeha).query(async ()=>({
return await prisma.routeThree.findMany({})
}),
getItem: t.procedure.input(routeThreePrismaScmeha).query(async ()=>({
return await prisma.routeThree.findFirst({})
}),
/* some more similary named procedures*/
})

/* etc */
// client
type GenericInference<T extends AnyRouter> = RouterLike<T> extends {
getList: QueryLike<infer Items>;
getItem: QueryLike<infer Item>;
}
? {
getList: QueryLike<Items>;
getItem: QueryLike<Item>;
}
: never;

function useGeneric<T extends AnyRouter>(router: GenericInference<T>) {
return router.getList.useQuery(/*Common Options*/);
}

const c = useGeneric<AppRouter["routeOne"]>(trpc.routeOne);

const { data } = c //data is always "any"
// client
type GenericInference<T extends AnyRouter> = RouterLike<T> extends {
getList: QueryLike<infer Items>;
getItem: QueryLike<infer Item>;
}
? {
getList: QueryLike<Items>;
getItem: QueryLike<Item>;
}
: never;

function useGeneric<T extends AnyRouter>(router: GenericInference<T>) {
return router.getList.useQuery(/*Common Options*/);
}

const c = useGeneric<AppRouter["routeOne"]>(trpc.routeOne);

const { data } = c //data is always "any"
0 Replies
No replies yetBe the first to reply to this messageJoin