Wayfinder.Processor.GroupRoutes (Wayfinder Ex v0.1.4)

View Source

Handle %Phoenix.Router.Route{} entries — These are the steps in this module.

  1. Group routes by their controller and action.
  2. Routes are grouped by shared static path prefix. Ex.: /users/:id and users/:id/:name
  3. Within each group, the path with the most parameters becomes canonical. Ex.: /users/:id/:name
  4. Routes are sorted with the shortest path first.
  5. All methods across the group are merged.
  6. Argument handling is inferred based on the variation in path parameters arity.
  7. We get for each route a map of all their arguments and if they are optional or not.

Also this is my best shot to simulating Phoenix has built-in support for optional parameters in the router. This is not a feature of Phoenix but if 2 routes are declared with 2 paths like this

get "/users/:id", UserController, :show
get "/users/:id/:name", UserController, :show

This is the logic here: We assume :name is optional and :id is required.

We do all of this to generate a compresive TypeScript route definition

The final out is a list of %Wayfinder.Processor.Route{} structs

Summary

Types

indexed_variant()

@type indexed_variant() :: {variant(), non_neg_integer()}

variant()

@type variant() :: {{module(), atom(), String.t()}, [Phoenix.Router.Route.t()]}

Functions

call(routes, opts)

@spec call([Phoenix.Router.Route.t()], Wayfinder.Processor.Route.phoenix_route_opts()) ::
  [
    Wayfinder.Processor.Route.t()
  ]