How to type an expressjs-like route handler based on the path pattern.
If I define an application like:
get('/path/:id?q=:query', handler)
is it possible to look at the structure of the string and infer the type of the parameters that are passed to the handler
function as { id: string; query: string;}
. Given the prevalence of this pattern, I'd expect that someone has attempted it before, but I can't seem to find it in the docs and/or discord.12 Replies
So do you need tge validation on top of that or not?
not familiar with the
tge
acronym, but just having the basic shape would be a step up.*the
ah. validation be an upgrade on top of an upgrade
Cuz if you do need you generally define props/queries as object, not inline
Cheat Sheet (Elysia by example) - ElysiaJS
Elysia's cheat sheet in summary and how it work with "Elysia by example"
That's not an "upgrade", that's totally opposite way to do stuff
Anyways
Do you use some non-obvious Express extensions?
I do have a TypedRouter impl where I try to do roughly what you have described
Not really no, and I'm actually not using express, but I'm using express syntax in order to be very familiar to developers, but I'm not locked into it.
If there is something with clear advantages, I might be able to integrate it.
Also you generally want to get all those available queries to be typed to frontend a la tRpc or Elysia Eden and that requires you to either combine routes (which means you have to chain all your route definition
app.get().get()
) or have a definition file with all the routes (a la openapi.d.ts)
I use Express because switching to whatever esle would require a complete rewrite
Node with Express is not Bun where you can just use router from one library inside a router of another library and it'll just work (╯°□°)╯︵ ┻━┻
@cowboyd so what you do need this for anyways?I'm writing a structurally concurrent web server using Effection https://github.com/thefrontside/revolution/ (very alpha at the moment).There are a lot of use-cases for typed routes in it, but my current one is that I need to generate a sitemap.xml, and so each route that extends the sitemap needs to "generate" a set of parameters that will in turn "generate" a set of static paths corresponding to those parameters. I'd like to strongy type that generation. In effect say "you need to give me a list of entries that have this shape, and I'll plug them back into the path pattern" to create a list of paths
GitHub
GitHub - thefrontside/revolution: After the years of reaction comes...
After the years of reaction comes revolution. Contribute to thefrontside/revolution development by creating an account on GitHub.
Check Elysia and maybe Hono
Elysia is the SOTA router afaik
*state-of-the-art
I can help with type implementation but first you need to make all the UX choices
Make an app in Elysia and see if it's fine or you want to change something
Thanks so much for your time. This was really helpful.