Skip to content

Parses route strings to a sum type using Matches from fp-ts-routing

Notifications You must be signed in to change notification settings

anthonyjoeseph/fp-ts-routing-adt

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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!

About

Parses route strings to a sum type using Matches from fp-ts-routing

Resources

Stars

Watchers

Forks

Packages

No packages published