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

https://github.com/localvoid/routekit

:twisted_rightwards_arrows: RouteKit is a set of tools for generating efficient routers.
https://github.com/localvoid/routekit

javascript perfmatters routekit router typescript

Last synced: 12 months ago
JSON representation

:twisted_rightwards_arrows: RouteKit is a set of tools for generating efficient routers.

Awesome Lists containing this project

README

          

# [RouteKit](https://github.com/localvoid/routekit) · [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/localvoid/routekit/blob/master/LICENSE) [![codecov](https://codecov.io/gh/localvoid/routekit/branch/master/graph/badge.svg)](https://codecov.io/gh/localvoid/routekit) [![CircleCI Status](https://circleci.com/gh/localvoid/routekit.svg?style=shield&circle-token=:circle-token)](https://circleci.com/gh/localvoid/routekit) [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](https://github.com/localvoid/routekit)

RouteKit is a set of tools for generating lightweight and efficient routers.

|packages |NPM version |
|-----------------|---------------------------------------------------------------------------------------------------------------------|
|routekit |[![npm version](https://img.shields.io/npm/v/routekit.svg)](https://www.npmjs.com/package/routekit) |
|routekit-resolver|[![npm version](https://img.shields.io/npm/v/routekit-resolver.svg)](https://www.npmjs.com/package/routekit-resolver)|

## Installation

```sh
$ npm install -D routekit
```

## Usage Example

Create a routes file: `routes.build.js`

```ts
#!/usr/bin/env node

import { r, emitter } from "routekit";

const enum Location { Home, UserView, UserEdit }

process.stdout.write(emitter()(
r("/", Location.Home),
r("/user/:id", Location.UserView),
r("/user/:id/edit", Location.UserEdit),
));
```

Run `routes.build.js` file

```sh
$ node routes.build.js > routes.js
```

And it will generate `routes.js` file with a compact flattened trie.

```js
const ROUTES = {
f: [35, 38, 33, 7],
p: ["user/", "/edit"],
s: [0, 1, 2],
};
```

## Emmiter

Emitter generates flattened tries that can be used by `routekit-resolver` for matching urls.

```ts
export function emitter(name = "ROUTES"): (...routes: Route[]) => string;
export function injector(src: string): (...routes: Route[]) => string;
```

`inject` function injects code blocks into existing code using [incode](https://github.com/localvoid/incode) package.
This function is using `routekit` prefix to detect injectable regions.

## Resolver

### Installation

NPM package `routekit-resolver` provides a commonjs, es2015 modules and TypeScript typings.

```sh
npm install routekit-resolver
```

### Usage Example

```js
import { resolve } from "routekit-resolver";

// routekit:emit("routes")
const ROUTES = {
f: [35, 38, 33, 7],
p: ["user/", "/edit"],
s: [0, 1, 2],
};
// routekit:end

const match = (path) => resolve(ROUTES, path);

match("/user/123");
// {
// state: 1,
// vars: ["123"],
// }
```

### API

```ts
export interface ResolveResult {
readonly state: T;
readonly vars: string[];
}

export function resolve(map: RouteMap, path: string): ResolveResult | null;
```

`resolve()` function has 2 parameters:

- `map` is a flattened trie generated by routekit.
- `path` is a path that should be resolved.

When resolve function returns `null` value it means that no match was found.