An open API service indexing awesome lists of open source software.

https://github.com/funkia/rudolph

A pure and functional router using classic FRP. Written in TypeScript.
https://github.com/funkia/rudolph

Last synced: about 1 year ago
JSON representation

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

Awesome Lists containing this project

README

          

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

[![Build Status](https://travis-ci.org/funkia/rudolph.svg?branch=master)](https://travis-ci.org/funkia/rudolph)
[![codecov](https://codecov.io/gh/funkia/rudolph/branch/master/graph/badge.svg)](https://codecov.io/gh/funkia/rudolph)

## Install
```
npm install --save @funkia/rudolph @funkia/hareactive
```

## API

### Router

```ts
type Router = {
prefixPath: string;
path: Behavior;
useHash: boolean;
};
```

### createRouter

Takes a configuration Object describing how to handle the routing:

* `useHash: boolean` - whether to use hash-routing
* `path: Behavior` - 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:

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

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

### navigate

```ts
navigate(router: Router, pathStream: Stream): Now>
```

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

Usage:

```ts
const navs: Stream = userIds
.map(prefix("/user/"))
.combine(on.homeClicks.mapTo("/"));

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

### routePath

`routePath(routes: Routes, router: Router): Behavior`

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:

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

Usage:

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

### Routes

```ts
type Routes
= Record>
```

Example:

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

### RouteHandler

```ts
type RouteHandler
= (
router: Router,
params: Record
) => A;
```

### locationHashB

`locationHashB: Behavior` represents the current values of the URL hash.

### locationB

`locationHashB: Behavior` represents the current values of the URL pathname.

### navigateHashIO

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

### navigateIO

`navigateIO: (path: string) => IO` 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.