Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Route helper for parsing/creating routes #18

Open
andywhite37 opened this issue Oct 22, 2019 · 2 comments
Open

Route helper for parsing/creating routes #18

andywhite37 opened this issue Oct 22, 2019 · 2 comments
Labels
enhancement New feature or request

Comments

@andywhite37
Copy link
Member

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.

@gaku-sei
Copy link

gaku-sei commented Feb 4, 2020

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 👍

@andywhite37
Copy link
Member Author

Yeah, that looks great! A PR would be awesome, thanks!

@andywhite37 andywhite37 added the enhancement New feature or request label Feb 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants