.nickman
.nickman13mo ago

Type helpers to match available paths

Given a router like this:
const appRouter = t.router({
post: t.router({
list: t.procedure
.query(() => {
// imaginary db call
return [{ id: 1, title: 'tRPC is the best!' }];
}),
byId: t.procedure
.input(z.string())
.query((opts) => {
// imaginary db call
return { id: 1, title: 'tRPC is the best!' };
}),
}),
});
const appRouter = t.router({
post: t.router({
list: t.procedure
.query(() => {
// imaginary db call
return [{ id: 1, title: 'tRPC is the best!' }];
}),
byId: t.procedure
.input(z.string())
.query((opts) => {
// imaginary db call
return { id: 1, title: 'tRPC is the best!' };
}),
}),
});
I would like to define a generic function executeTrpc(path, handler), where "path" has to be one of the available paths (post.list or post.byId), and handler is a callback function that asks for the typed input, based on the path that was given. The return type of the method should also get the typed output, based on the path that was given. Are there already Typescript helpers available to achieve something like this? I tried looking trough the library to find what I need, but can't seem to figure it out.
0 Replies
No replies yetBe the first to reply to this messageJoin