Tagged Routes
Routing using tagged unions
npm install tagged-routes
Example
const Route getRouteForURL getURLForRoute = console/* => <AppMain ({ routeParams: { appId: 'example' }, queryParams: { tab: 'hosting' }})> */ console/* => <Settings ({ routeParams: {}, queryParams: {}, routeSplat: 'can-be-anything'})> */ console/* => <NotFound ({ routeParams: {}, queryParams: {}, routePath: '/bad/route-bad'})> */ const exemplarRoute = Route console// => /apps/exemplar?tab=billing
Documentation
createRoutes(routeTable, catchAllRouteKind: string, options?: object)
Accepts a routeTable
object with route kinds (types of Route) as keys and valid path-to-regexp
paths as values along with a catch-all route kind catchAllRouteKind
. The options
are passed to path-to-regexp
and options.encode
is passed to path-to-regexp.compile
URL builders to override the encodeURIComponent
default.
Returns an object containing the below Route
union and functions.
Route
A tagmeme
union, has a .match
method and constructors for each route kind.
getRouteForURL(url: string): Route
Returns the route that matches the url
, or the catch-all route if none match. The shape of route data is:
getURLForRoute(route: Route): string
Returns the URL that matches the route
.
Throws in development if:
- a route requiring certain
routeParams
values is missing values. - the catch-all route does not have the
routePath
specified.