#Type Safe Routing in Gleam

1 messages · Page 1 of 1 (latest)

turbid verge
pallid horizon
#

amazing commit messages, 10/10

hybrid mango
#

Never let them know your next step

rain elbow
#

I think the next step is "x"

turbid verge
#

😅

#

let's just say it took a while to get the interface right

whole ridge
#

Just to note this isn’t type safe! Using strings instead of some precise type means that there’s no guarantee that the string had the correct number of parameters

sharp orbit
#

I have been thinking often about this, as my side app grows which has lots of routes. i.e. some nice way to generate the routing from a minimal DSL.

Some thoughts:

A challenge for me is to have middleware applied at different level of the routing. E.g. when you hit some particular path and descendants you want to use some authentication middleware. This is easy with the current model of pattern matching. But hard with a DSL.

Another thing that I like is using an intermediate type, e.g. a Route type. The first step of routing matches the path and returns that type. Then a second step matches the types and calls the handlers. This gives me 1. A way to generate the paths in a type safe way and 2. Something I can pass to views and make decisions on what to show e.g. highlight the current route in the nav.

whole ridge
#

I think code generation from a schema is probably the only viable option for besting pattern matching

#

Any in-Gleam DSL is gunna be slower, and I’ve yet to see one that’s type safe

turbid verge
#

That's true @whole ridge ! There's a validate function that does exactly that, checking if there are too many or too few parameters available. But yeah technically that's not typesafe on its own

#

Wrote up a blog post before this project about how to do typesafe routing with a custom DSL and code-gen, which is going to be fully typesafe: https://www.kurz.net/posts/gleam-routing

But decided that something without code-gen will be nicer and can be a similar DX with some static analysis and the validate function

alpine compass
turbid verge
#

@alpine compass yeah however you like 😁 not on that list yet, will talk to my team about it!