Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/anthonyjoeseph/morphic-ts-routing
Parses route strings to a sum type using Matches from fp-ts-routing
https://github.com/anthonyjoeseph/morphic-ts-routing
Last synced: 12 days ago
JSON representation
Parses route strings to a sum type using Matches from fp-ts-routing
- Host: GitHub
- URL: https://github.com/anthonyjoeseph/morphic-ts-routing
- Owner: anthonyjoeseph
- Created: 2020-04-14T06:51:29.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2023-01-06T06:00:35.000Z (almost 2 years ago)
- Last Synced: 2024-10-12T01:18:07.327Z (about 1 month ago)
- Language: TypeScript
- Homepage:
- Size: 1.28 MB
- Stars: 9
- Watchers: 3
- Forks: 0
- Open Issues: 9
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# morphic-ts-routing
Parses route strings to a sum type using Matches from fp-ts-routing. Tangentially inspired by [purescript-routing-duplex](https://github.com/natefaubion/purescript-routing-duplex)
# Usage
## Note
Formatting the `NotFound` type will return '/'
```ts
import * as R from 'fp-ts-routing'
import { routingFromMatches3 } from 'morphic-ts-routing'
import { ADTType, ADT } from '@morphic-ts/adt'const landing: R.Match<{}> = R.end
const show: R.Match<{}> = R.lit('show').then(R.end)
const id: R.Match<{ id: number }> = R.int('id').then(R.end)const {
parse,
format,
adt: RouteADT
} = routingFromMatches3(
['Landing', landing],
['Show', show],
['Id', id],
);
type RouteADT = ADTType/*
type RouteADT = {
type: "NotFound";
} | {
type: "Landing";
} | {
type: "Show";
} | {
type: "Id";
id: number;
}
const RouteADT: ADT
const parse: (path: string) => RouteADT
const format: (adt: RouteADT) => string
*/```
# Package size
If this package is too big for your website, you can use `codegenWithNumRoutes` to generate a single file
```ts
import * as fs from 'fs'
import { codegenWithNumRoutes } from 'morphic-ts-routing'fs.writeFile(
`src/RoutingFromMatches100.ts`,
codegenWithNumRoutes(100),
(err) => {
// throws an error, you could also catch it here
if (err) throw err;// success case, the file was saved
console.log('RoutingFromMatches100.ts saved!');
}
);
```# Limitation
The library supports up to sixteen (16) routes. It cannot support more than that - if you try, you get this error:
```
Expression produces a union type that is too complex to represent.
```I'm uncertain what to do about this. Please make a PR if you have any ideas. Thanks!