https://github.com/youpy/nihonbashi
A type-safe routing utility
https://github.com/youpy/nihonbashi
routing typescript utility
Last synced: 5 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 (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2023-05-30T03:44:10.000Z (over 2 years ago)
- Last Synced: 2023-05-30T04:24:09.999Z (over 2 years ago)
- Topics: routing, typescript, utility
- Language: JavaScript
- Homepage:
- Size: 151 KB
- Stars: 1
- Watchers: 2
- 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'
```