Skip to content

Latest commit

 

History

History
53 lines (40 loc) · 2.69 KB

README.md

File metadata and controls

53 lines (40 loc) · 2.69 KB

fp-ts-routing-adt

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

Usage

Installation

yarn add fp-ts-routing-adt

Notes

  • 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
 */

TODO:

  • prevent duplicate routes at the type level?
    • implementation using this only supports up to 15 routes (example)
    • if a stable solution is possible, PRs are welcome!