RPC, nested routing and types
Hi everyone, quick question given the attached example:
- How do you avoid losing RPC typing when doing nested routing with different routes? Example
/api/stores/:storeId/products. The storeId is not required in RPC because productsCollectionRoutes doesn't have the routing context of storesRoutes.
- It would be less verbose to declare routes with :id instead of :productId, but I guess that's wouldn't work in RPC as I would have two ids?
As you can see, I have different routes like:
GET /api/stores/:storeId --> get store
GET/POST /api/stores/:storeId/products --> get or create products for a store
GET/PATCH/DELETE /api/products/:productId --> get, update or delete a specific product
8 Replies
i'm not sure i understand your goal or the issue you're running into
My main issue is that as I have my productsCollectionRoutes declared separated from the storesRoutes, I don't get the type safety params for the :storeId.
I was wondering if there's a workaround to that.
i don't know the logic behind your app/api, so i'm still not sure i understand
you have routes like this
are you talking about handling a route like this?
Sorry, I was out. Yes, those are the routes.
are you talking about handling a route like this?No, my resource route is under the resource route:
GET api/products/:productId
The thing is that this route doesn't infer the type in RPC: GET /api/stores/:storeId/products
Just wondering if it was possible to infer it somehow by specifying some generic type in the new Hono<...> of the productsCollectionRoutes declarationthis is what i would expect from a RESTful perspective:
you might not need/have all of these routes
but if i understand correctly, you have a route
/api/products/:productId
where you're doing something with products for a specific store.
it would be more intuitive to do that at /api/stores/:storeId/products/:productId
then it's clear from the url what all the endpoint applies to
i think maybe i'm starting to understand your issue though
the problem is with how you've set up stores
i'm not sure why, but this is breaking the typing
instead, try this
or, if you really don't want the intermediate layer
tl;dr - hono should infer param types the way you want, but the way you've structured your routes is breaking the typing somehow
the issue arises specifically from this
i.e., routing to a path that has a dynamic parameter, and is followed by a static parameter. if the static paremeter precedes the dynamic paremeter, there's no problem (.e., app.route('/static/:dynamic') works fine)
does that make sense? has something to do with how hono's type system constructs the route typeokay, that makes sense. thanks a lot!
Hi there 👋
I'm facing the same issue, is there a way to do it now?
is there a way to do what now?
i don't think the type edge-case has been addressed, but you should be good w a restful architecture