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
- Host: GitHub
- URL: https://github.com/lagunoff/typescript-invertible-router
- Owner: lagunoff
- Created: 2018-01-05T23:51:38.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2019-01-10T14:07:18.000Z (over 6 years ago)
- Last Synced: 2025-02-16T05:03:07.718Z (3 months ago)
- Topics: react, router, routing, typescript
- Language: TypeScript
- Homepage:
- Size: 201 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
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)