# fp-ts-routing

> A type-safe routing library for TypeScript

Latest version **0.7.0** (published 2025-02-24) · MIT license · 0 weekly downloads

## Install

```sh
npm install fp-ts-routing
pnpm add fp-ts-routing
yarn add fp-ts-routing
bun add fp-ts-routing
```

## Health

**Score 40/100 (D)** — status: maintenance-mode.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads; pre 1.0.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.7.0 |
| Published | 2025-02-24 |
| First published | 2017-09-06 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 91.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 171 |
| Author | Giulio Canti |
| Maintainers | gcanti |
| Keywords | typescript, functional-programming, routing, applicative |

## Links

- npm: https://www.npmjs.com/package/fp-ts-routing
- Repository: https://github.com/gcanti/fp-ts-routing
- Issues: https://github.com/gcanti/fp-ts-routing/issues
- npm.io page: https://npm.io/package/fp-ts-routing

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 0.7.0 (latest) — 2025-02-24
- 0.1.0 (next) — 2017-10-24
- 0.6.0 — 2023-06-20
- 0.5.4 — 2020-06-07
- 0.5.3 — 2020-02-05
- 0.5.2 — 2020-01-09
- 0.5.1 — 2019-07-17
- 0.5.0 — 2019-07-06
- 0.4.4 — 2019-07-02
- 0.4.3 — 2019-06-21
- 0.4.2 — 2019-06-21
- 0.4.1 — 2019-01-08
- 0.4.0 — 2018-11-27
- 0.3.9 — 2018-11-21
- 0.3.8 — 2018-11-14
- … 11 more at https://npm.io/package/fp-ts-routing/versions

## README

# Installation

To install the stable version:

```sh
npm i fp-ts-routing
```

# TypeScript compatibility

| `fp-ts-routing` version | required `typescript` version                                  |
| ----------------------- | -------------------------------------------------------------- |
| 0.7.0+                  | 4.1+                                                           |
| <= 0.6.0                | tested against TypeScript 3.5.2 but should run with 3.2.2+ too |

# Usage

```ts
//
// locations
//

interface Home {
  readonly _tag: 'Home'
}

interface User {
  readonly _tag: 'User'
  readonly id: number
}

interface Invoice {
  readonly _tag: 'Invoice'
  readonly userId: number
  readonly invoiceId: number
}

interface NotFound {
  readonly _tag: 'NotFound'
}

type Location = Home | User | Invoice | NotFound

const home: Location = { _tag: 'Home' }

const user = (id: number): Location => ({ _tag: 'User', id })

const invoice = (userId: number, invoiceId: number): Location => ({ _tag: 'Invoice', userId, invoiceId })

const notFound: Location = { _tag: 'NotFound' }

// matches
const defaults = end
const homeMatch = lit('home').then(end)
const userIdMatch = lit('users').then(int('userId'))
const userMatch = userIdMatch.then(end)
const invoiceMatch = userIdMatch.then(lit('invoice')).then(int('invoiceId')).then(end)

// router
const router = zero<Location>()
  .alt(defaults.parser.map(() => home))
  .alt(homeMatch.parser.map(() => home))
  .alt(userMatch.parser.map(({ userId }) => user(userId)))
  .alt(invoiceMatch.parser.map(({ userId, invoiceId }) => invoice(userId, invoiceId)))

// helper
const parseLocation = (s: string): Location => parse(router, Route.parse(s), notFound)

import * as assert from 'assert'

//
// parsers
//

assert.strictEqual(parseLocation('/'), home)
assert.strictEqual(parseLocation('/home'), home)
assert.deepEqual(parseLocation('/users/1'), user(1))
assert.deepEqual(parseLocation('/users/1/invoice/2'), invoice(1, 2))
assert.strictEqual(parseLocation('/foo'), notFound)

//
// formatters
//

assert.strictEqual(format(userMatch.formatter, { userId: 1 }), '/users/1')
assert.strictEqual(format(invoiceMatch.formatter, { userId: 1, invoiceId: 2 }), '/users/1/invoice/2')
```

# Documentation

- [API Reference](https://gcanti.github.io/fp-ts-routing)

---
_Source: https://npm.io/package/fp-ts-routing · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
