https://github.com/youpy/nihonbashi
A type-safe routing utility
https://github.com/youpy/nihonbashi
routing typescript utility
Last synced: about 2 months ago
JSON representation
A type-safe routing utility
- Host: GitHub
- URL: https://github.com/youpy/nihonbashi
- Owner: youpy
- License: mit
- Created: 2022-02-09T13:38:01.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2023-05-30T13:12:46.000Z (about 3 years ago)
- Last Synced: 2026-02-13T08:51:02.436Z (5 months ago)
- Topics: routing, typescript, utility
- Language: JavaScript
- Homepage:
- Size: 180 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# nihonbashi
A type-safe routing utility for Typescript
## install
```
$ yarn add nihonbashi
```
## Usage
```typescript
import { RouteGen } from 'nihonbashi'
type Param = string & { readonly __opaque__: 'param' }
const gen = new RouteGen<{ param1: Param }>()
const r = gen.route('/foo/:param1/bar/:param2')
r({ param1: 'xxx' as Param, param2: 'yyy' }) // => '/foo/xxx/bar/yyy'
r({ param1: 'xxx' as Param }) // => '/foo/xxx/bar/:param2'
r({ param1: 'xxx' }) // => error: Type 'string' is not assignable to type 'Param'.
// currying
gen.route(r({ param1: 'xxx' as Param }))({ param2: 'yyy' }) // => '/foo/xxx/bar/yyy'
```