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

https://github.com/lagunoff/typescript-invertible-router

Type-safe bidirectional routing for typescript
https://github.com/lagunoff/typescript-invertible-router

react router routing typescript

Last synced: 3 months ago
JSON representation

Type-safe bidirectional routing for typescript

Awesome Lists containing this project

README

        

This library helps to implement routing in web-applications. It
provides DSL for declaring routes, declarations then can be used for
both matching urls and printing links to any location inside
application.

```ts
import * as r from 'typescript-invertible-router';

const parser = r.oneOf(
r.tag('Shop').path('/shop'),
r.tag('Category').path('/category').segment('slug', r.nestring).params({ page: r.nat.withDefault(1) }),
r.tag('Item').path('/item').segment('id', r.nestring),
r.tag('Page404').path('/404'),
);

console.log(parser.parse('/non-existing-url')); // => null
console.log(parser.parse('/shop')); // => { tag: 'Shop' }
console.log(parser.parse('/category/groceries')); // => { tag: 'Category', slug: 'groceries', page: 1 }
console.log(parser.parse('/item/42')); // => { tag: 'Item', id: '42' }

console.log(parser.print({ tag: 'Shop' })); // => "/shop"
console.log(parser.print({ tag: 'Category', slug: 'groceries', page: 2 })); // => "/category/groceries?page=2"
console.log(parser.print({ tag: 'Item', id: '1' })); // => "/item/1"
```

## Installation

```sh
$ npm install typescript-invertible-router
```

## Examples



Simple example

source |
demo



Complete SPA, that uses public api https://restcountries.eu/

source |
demo



Breadcrumbs

source |
demo


## Documentation

[API reference](docs/index.md)