morphic-ts-routing
TypeScript icon, indicating that this package has built-in type declarations

0.0.49 • Public • Published

morphic-ts-routing

Parses route strings to a sum type using Matches from fp-ts-routing. Tangentially inspired by purescript-routing-duplex

Usage

Note

Formatting the NotFound type will return '/'

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<typeof RouteADT>

/*
  type RouteADT = {
      type: "NotFound";
  } | {
      type: "Landing";
  } | {
      type: "Show";
  } | {
      type: "Id";
      id: number;
  }
  const RouteADT: ADT<RouteADT, "type">
  const parse: (path: string) => RouteADT
  const format: (adt: RouteADT) => string
 */

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!

Readme

Keywords

none

Package Sidebar

Install

npm i morphic-ts-routing

Weekly Downloads

1

Version

0.0.49

License

MIT

Unpacked Size

1.16 MB

Total Files

163

Last publish

Collaborators

  • anthonyjoeseph