-
-
Notifications
You must be signed in to change notification settings - Fork 632
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Grouped routing breaks RPC types #1512
Comments
Got this issue too when working with the Zod-to-openapi RPC-Client and using |
Hi @sandervspl , @exsjabe This is not a bug, although it is hard to understand. You have to use the return value of const bookesRoutes = books.get("/", (c) => {
return c.jsonT({
books: true,
});
}); |
Right, that makes sense. I'd love to contribute to the docs but I'm not experienced enough yet to do so, but this would be really helpful to have in the docs! |
Okay! I will close this issue and opened honojs/website#110. Thanks. |
With this in mind, how should we structure big applications? All the routes in an endpoint should be defined in 1 constant for the type-safety to work. import { Hono } from "hono";
import { hc } from "hono/client";
const app = new Hono();
const booksRouter = new Hono();
const storesRouter = new Hono();
// How to split this into multiple files?
const booksRoutes = booksRouter.get("/", c => c.jsonT({ books: "get" })).post(c => c.jsonT({ books: "post" }));
const storesRoutes = storesRouter.get("/", c => c.jsonT({ stores: "get" })).post(c => c.jsonT({ stores: "post" }));
const routes = app.route("/books", booksRoutes).route("/stores", storesRoutes);
type Routes = typeof routes;
const client = hc<Routes>("http://localhost:8787/"); |
This works for me.
|
@martinnormark thanks! That's what I'm doing now as well. Yet the problem is that all the routes for an endpoint have to be defined in one variable for types to work properly. |
@MonsterDeveloper It's possible to export a union type that I believe emulates what hono builds for the single variable (at least I haven't run into any issues so far). I use this so I can define route associated items alongside the route functions, like schemas.
The client can then be created with |
What version of Hono are you using?
3.7.2
What runtime/platform is your app running on?
N/A
What steps can reproduce the bug?
Here is a reproduction. I have followed the RPC and grouped routing steps from the docs.
https://www.typescriptlang.org/play?#code/JYWwDg9gTgLgBAbzgCQgOwnAvnAZlCEOAIgAt0JiBuAKFElkTlIGNs8CiyKB6FgG2ABTNDGo0aLdAGd4AIwgQA1tLgBeOGiEB3FBQAUASlo0Fy6QDoA5kJj7iPYgBo4+lofUA+RDThwotgCuUGhwLBYAVtLoACr6CL5+cGYqAFxwMFCBQk6JWMY0+SZSaLJwAIZgYOqaOnoYRrQlZQSBMEKqGpVgFq3t9jwp0s7JiioFNDAAnmBCcACCVTEzcxrTsxC4-hBtHcUy8ALCojWsADyLYMuznvakMDBgqTw8-BAs5fzksqkAHADsAMcBSScBecAAegB+CTlaRTNBsXCBREwYDoOAgcrANBGHyg5rwALSSClVYVbTYw6CEQwCxDCwAEhsdhBSWaEH4Qgsbys+mJpOkQgKWAkWJxjRoQA
What is the expected behavior?
You should be able to write this without type errors. At least, there is nothing in the docs that says that this doesn't work.
What do you see instead?
You cannot write the above code without type errors
Additional information
No response
The text was updated successfully, but these errors were encountered: