What is the recommended approach for modularizing routes while retaining inference in Treaty? #667
Unanswered
is-jonreeves
asked this question in
Q&A
Replies: 2 comments 1 reply
-
5 dot separators are way too many and ubiquitous
P.S. dropping an HTTP verb (RPC style) might be not possible in Elysia, just saying that I find it redundant. |
Beta Was this translation helpful? Give feedback.
1 reply
-
Would be nice if we could use the openapi name of an endpoint instead of the path and method. But then you need the mapping which means codegen... |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I've only really used Elysia for very small projects where I was fine with chaining all the routes. I recently decided to use it for something larger and couldn't quite figure out how to split out the routes and maintain the Type inferences (I'm using Treaty on the Client-side).
I was hoping that there was maybe some kind of helper factories like
createRoute
or something that I could use to extend the main router but also influence its type. But didn't see anything like that.Research
I went through most of the examples and did notice this one, which seemed to suggest that I could build Sub Routers and attach them via
.use
. To be honest it felt a little clunky but I was able to get it working at least.I also read through the MVC pages in the documentation which suggested a different approach. Keeping all the routes chained but instead just having them call out to the appropriate functions or class methods.
After playing with both a little, I settled on an approach that makes use of separate Elysia instances as "SubRouters", attached to the main router via
.group
and.use
. This works very well and (at least to my preference) allows for better code-splitting / organization. This wasn't a pattern I've seen with Elysia elsewhere, so was a bit concerned that it was not a wise choice and may introduce some issues I'm not seeing just yet.Examples
Minimal
Here is a minimal example to illustrate the approach:
/index.ts
How the expected routes materialize:
How it appears via Treaty:
Extended (API Versions and Services with revisions)
Here is a bigger example that modularizes the code across various files and shows API versioning, using multiple services and those each having their own revisions.
For this kind of work, I personally like TS namespaces an organization mechanism, but you could also just use Classes or simply separate files/imports:
/index.ts
/api/v1.ts
- introduces 1st revisions of Echo and Ping services/api/v2.ts
- adds Math service and replaces Ping service with new revision/api/v3.ts
- retires Echo service and adds new revisions for Math and Ping services/services/echo.ts
/services/math.ts
/services/ping.ts
How the expected routes materialize:
How it appears via Treaty:
Any Recomendations/Alternatives?
While the above all works great and for me personally feels really well structured, I'm just not confident that it doesn't break some important principals of Elysia's recommended usages. I'd love some guidance/feedback/suggestions about the above approach and any alternatives.
I suspect at least one issue with use of Sub-Routers here, is that they can't have typed knowledge of the context of the main Router. So any middlewares/decorates/state that will be present when attached, aren't known in isolation. I haven't explored this yet, but suspect it will introduce some type annoyances when trying to tap into any of that context.
Beta Was this translation helpful? Give feedback.
All reactions