Parses route strings to a sum type using Matches from fp-ts-routing. Tangentially inspired by purescript-routing-duplex. Simplified version of morphic-ts-routing
yarn add fp-ts-routing-adt
- Formatting the
NotFound
type will return '/'
import * as R from 'fp-ts-routing'
import { routingFromMatches } from 'fp-ts-routing-adt'
const landing = R.end // landing: R.Match<{}>
const show = R.lit('show').then(R.end) // show: R.Match<{}>
const id = R.int('id').then(R.end) // id: R.Match<{ id: number }>
const { parse, format } = routingFromMatches(
['Landing', landing],
['Show', show],
['Id', id],
);
type RouteADT = ReturnType<typeof parse>
/*
type RouteADT = {
type: "NotFound";
} | {
type: "Landing";
} | {
type: "Show";
} | {
type: "Id";
id: number;
}
const parse: (path: string) => RouteADT
const format: (adt: RouteADT) => string
*/