dispatch

Path-based dispatching for client- and server-side applications
IN THIS PACKAGE
Module Dispatch
type tag = [
| `Lit
| `Var
]

The type tag for a path component. `Lit indiciates that the component should match exactly, while `Var indicates that the component can be anything and should be associated with the variable name provided in the tuple.

type typ = [
| `Prefix
| `Exact
]

The type of match for the route. `Prefix indicates that the route does not need to match the entirety of the path, while `Exact indicates an exact match.

type assoc = (string * string) list

Type alias for an association list of string to string

type 'a route = (tag * string) list * typ * ( assoc -> string option -> 'a )

The type of a route. The first tuple element specifies the matching rules for the path components, with the tag indicating the type of match. The interpretation of the corresponding string depends on the tag provided: for (`Lit, lit), the path component must match the string lit exactly; for (`Var, name), the path component can be anything and will be associated with name in the event of a successful match.

The second component of the tuple indicates the type of match that this route support, which can be either a prefix match (indicated by the `Prefix variant), or an exact match (indicated by the `Exact variant).

The third and final component is the handler function. On a successful route match, the matching information will be passed to the handler to produce a value of tyoe 'a that will be returned.

type 'a t
val create : 'a route list -> 'a t
val dispatch : 'a t -> string -> 'a option
val dispatch_exn : 'a t -> string -> 'a

dispatch routes path iterates through routes and selects the first one that matches path. It then applies the route handler to any component mappings and trailing path components (in the case of a prefix match) and returns the result. If none of the routes matches path, it will return None.

dispatch_exn routes path behaves just like dispatch routes path except will raise an exception using failwith in the case of no matches.

val to_dsl : ((tag * string) list * typ) -> string
val of_dsl : string -> (tag * string) list * typ

to_dsl route_spec is a string in the routing DSL that will beahve in the exact same way as route_spec.

A good way to become comfortable with the DSL is to load up this library in the REPL of your choice and translate route patterns to the DSL using the function to_dsl.

module DSL : sig ... end

A module that implements the dispatch operations for a DLS represented as a string literal. A more familiar interface for the Web world.