Skip to content
/ rudolph Public

A pure and functional router using classic FRP. Written in TypeScript.

Notifications You must be signed in to change notification settings

funkia/rudolph

Repository files navigation

Rudolph

A pure and functional router using classic FRP. Written in TypeScript. Experimental.

Build Status codecov

Install

npm install --save @funkia/rudolph @funkia/hareactive

API

Router

type Router = {
  prefixPath: string;
  path: Behavior<string>;
  useHash: boolean;
};

createRouter

Takes a configuration Object describing how to handle the routing:

  • useHash: boolean - whether to use hash-routing
  • path: Behavior<string> - defaults to locationHashB or locationB

It errors if useHash = true but hash-routing is unsupported in that browser, or if there is no support for the history API.

The returned Router object is identical to its input, augmented with prefixPath: "", which is used to nest routers.

Usage:

const router = createRouter({
  useHash: false
});

runComponent("#mount", main({ router }));

navigate

navigate(router: Router, pathStream: Stream<string>): Now<Stream<any>>

navigate takes a stream of paths. Whenever the stream has an occurence, it is navigated to.

Usage:

const navs: Stream<string> = userIds
  .map(prefix("/user/"))
  .combine(on.homeClicks.mapTo("/"));

start(navigate(props.router, navs));

routePath

routePath<A>(routes: Routes<A>, router: Router): Behavior<A>

Takes a description of the routes and a router, and returns a behavior with the result of parsing the router's location according to the routes' pattern.

The first parameter, routes: Routes, is a description of the routes, in the form:

{"/route/:urlParam"; (restUrl, params) => result}

Usage:

E.section(
  routePath(
    {
      "/user/:userId": (_subrouter, { userId }) => user(userId),
      "/": () => home,
      "*": () => notFound,
    },
    props.router
  )
)

Routes

type Routes<A> = Record<string, RouteHandler<A>>

Example:

{
  "/user/:userId": (_subrouter, { userId }) => user(userId),
  "/": () => home,
  "*": () => notFound,
}

RouteHandler

type RouteHandler<A> = (
  router: Router,
  params: Record<string, string>
) => A;

locationHashB

locationHashB: Behavior<string> represents the current values of the URL hash.

locationB

locationHashB: Behavior<string> represents the current values of the URL pathname.

navigateHashIO

navigateHashIO: (path: string) => IO<void> is an IO effect that updates the URL hash to the supplied argument.

navigateIO

navigateIO: (path: string) => IO<void> is an IO effect that updates the URL pathname to the supplied argument.

warnNavigation

Takes a behavior of a boolean, if true the user will have to confirm before unloading page.

About

A pure and functional router using classic FRP. Written in TypeScript.

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •