relude-reason-react
relude-reason-react copied to clipboard
Route helper for parsing/creating routes
A lot of web frameworks have a concept of "routes" which allows you to define a set of URLs that your app understands, and generates the code to parse the routes in a router, and code to generate the routes from their parameters, for use in in-app navigation/URL generation.
It would be cool to have the ability to configure routes, and have the route parsing/generation functions fall out of that. This could either be a data structure with functions that know how to operate on that structure, or it could be a macro. GADTs feel like they could be relevant for tagging route data types somehow, but not sure if that's a good idea.
Hey @andywhite37, we have a fully working router functor, using the Relude Parse library with a rather simple API:
module Routes = Router.Make({
type t = Home | Login | ...;
let encode = fun
| Home => "/"
| Login => "/login"
| ...;
let encode = Router.parse >=> fun
| {pathName: []} => Some(Home)
| {pathName: ["login"], query, fragment} => Some(Login)
| _ => None;
});
// ... later
let (currentRoute, setRoute) = Routes.use();
currentRoute :> Routes.t
setRoute(Login);
setRoute(~replace=true, Login);
It partly mimics the Elm Url lib (https://package.elm-lang.org/packages/elm/url/latest/Url).
It's still pretty rough on the edges, but it works perfecly for us so far, please let me know if you're interested in a PR 👍
Yeah, that looks great! A PR would be awesome, thanks!