index overview
Added in v0.4.0
Table of contents
formatters
Formatter (class)
Signature
export declare class Formatter<A> {
constructor(readonly run: (r: Route, a: A) => Route)
}
Added in v0.4.0
contramap (method)
Signature
contramap<B>(f: (b: B) => A): Formatter<B>
Added in v0.4.0
then (method)
Signature
then<B>(that: Formatter<B> & Formatter<RowLacks<B, keyof A>>): Formatter<A & B>
Added in v0.4.0
_A (property)
Signature
readonly _A: A
Added in v0.4.0
contramap
Signature
export declare const contramap: <A, B>(f: (b: B) => A) => (fa: Formatter<A>) => Formatter<B>
Added in v0.5.1
format
Signature
export declare function format<A>(formatter: Formatter<A>, a: A, encode: boolean = true): string
Added in v0.4.0
formatter
Signature
export declare const formatter: Contravariant1<'fp-ts-routing/Formatter'>
Added in v0.5.1
matchers
Match (class)
Signature
export declare class Match<A> {
constructor(readonly parser: Parser<A>, readonly formatter: Formatter<A>)
}
Added in v0.4.0
imap (method)
Signature
imap<B>(f: (a: A) => B, g: (b: B) => A): Match<B>
Added in v0.4.0
then (method)
Signature
then<B>(that: Match<B> & Match<RowLacks<B, keyof A>>): Match<A & B>
Added in v0.4.0
_A (property)
Signature
readonly _A: A
Added in v0.4.0
end
end matches the end of a route
Signature
export declare const end: Match<object>
Added in v0.4.0
imap
Signature
export declare function imap<A, B>(f: (a: A) => B, g: (b: B) => A): (ma: Match<A>) => Match<B>
Added in v0.5.1
int
int matches any integer path component
Signature
export declare function int<K extends string>(k: K): Match<{ [_ in K]: number }>
Example
import { int, Route } from '@rjdellecese/fp-ts-routing'
import { some, none } from 'fp-ts/lib/Option'
assert.deepStrictEqual(int('id').parser.run(Route.parse('/1')), some([{ id: 1 }, new Route([], {})]))
assert.deepStrictEqual(int('id').parser.run(Route.parse('/a')), none)
Added in v0.4.0
lit
lit(x) will match exactly the path component x
Signature
export declare function lit(literal: string): Match<object>
Example
import { lit, Route } from '@rjdellecese/fp-ts-routing'
import { some, none } from 'fp-ts/lib/Option'
assert.deepStrictEqual(lit('subview').parser.run(Route.parse('/subview/')), some([{}, new Route([], {})]))
assert.deepStrictEqual(lit('subview').parser.run(Route.parse('/')), none)
Added in v0.4.0
query
Will match a querystring.
Note. Use io-ts’s strict instead of type otherwise excess properties won’t be removed.
Signature
export declare function query<A>(type: Type<A, Record<string, QueryValues>>): Match<A>
Example
import * as t from 'io-ts'
import { lit, str, query, Route } from '@rjdellecese/fp-ts-routing'
const route = lit('accounts')
.then(str('accountId'))
.then(lit('files'))
.then(query(t.strict({ pathparam: t.string })))
.formatter.run(Route.empty, { accountId: 'testId', pathparam: '123' })
.toString()
assert.strictEqual(route, '/accounts/testId/files?pathparam=123')
Added in v0.4.0
str
str matches any string path component
Signature
export declare function str<K extends string>(k: K): Match<{ [_ in K]: string }>
Example
import { str, Route } from '@rjdellecese/fp-ts-routing'
import { some, none } from 'fp-ts/lib/Option'
assert.deepStrictEqual(str('id').parser.run(Route.parse('/abc')), some([{ id: 'abc' }, new Route([], {})]))
assert.deepStrictEqual(str('id').parser.run(Route.parse('/')), none)
Added in v0.4.0
succeed
succeed matches everything but consumes nothing
Signature
export declare function succeed<A>(a: A): Match<A>
Added in v0.4.0
then
Signature
export declare function then<B>(mb: Match<B>): <A>(ma: Match<A> & Match<RowLacks<A, keyof B>>) => Match<A & B>
Added in v0.5.1
type
type matches any io-ts type path component
Signature
export declare function type<K extends string, A>(k: K, type: Type<A, string>): Match<{ [_ in K]: A }>
Example
import * as t from 'io-ts'
import { lit, type, Route } from '@rjdellecese/fp-ts-routing'
import { some, none } from 'fp-ts/lib/Option'
const T = t.keyof({
a: null,
b: null,
})
const match = lit('search').then(type('topic', T))
assert.deepStrictEqual(match.parser.run(Route.parse('/search/a')), some([{ topic: 'a' }, Route.empty]))
assert.deepStrictEqual(match.parser.run(Route.parse('/search/b')), some([{ topic: 'b' }, Route.empty]))
assert.deepStrictEqual(match.parser.run(Route.parse('/search/')), none)
Added in v0.4.0
parsers
Parser (class)
Signature
export declare class Parser<A> {
constructor(readonly run: (r: Route) => Option<[A, Route]>)
}
Added in v0.4.0
of (static method)
Signature
static of<A>(a: A): Parser<A>
Added in v0.4.0
map (method)
Signature
map<B>(f: (a: A) => B): Parser<B>
Added in v0.4.0
ap (method)
Signature
ap<B>(fab: Parser<(a: A) => B>): Parser<B>
Added in v0.4.0
chain (method)
Signature
chain<B>(f: (a: A) => Parser<B>): Parser<B>
Added in v0.4.0
alt (method)
Signature
alt(that: Parser<A>): Parser<A>
Added in v0.4.0
then (method)
Signature
then<B>(that: Parser<RowLacks<B, keyof A>>): Parser<A & B>
Added in v0.4.0
_A (property)
Signature
readonly _A: A
Added in v0.4.0
alt
Signature
export declare const alt: <A>(that: Lazy<Parser<A>>) => (fa: Parser<A>) => Parser<A>
Added in v0.5.1
ap
Signature
export declare const ap: <A>(fa: Parser<A>) => <B>(fab: Parser<(a: A) => B>) => Parser<B>
Added in v0.5.1
apFirst
Signature
export declare const apFirst: <B>(second: Parser<B>) => <A>(first: Parser<A>) => Parser<A>
Added in v0.5.1
apSecond
Signature
export declare const apSecond: <B>(second: Parser<B>) => <A>(first: Parser<A>) => Parser<B>
Added in v0.5.1
chain
Signature
export declare const chain: <A, B>(f: (a: A) => Parser<B>) => (fa: Parser<A>) => Parser<B>
Added in v0.5.1
chainFirst
Signature
export declare const chainFirst: <A, B>(f: (a: A) => Parser<B>) => (first: Parser<A>) => Parser<A>
Added in v0.5.1
flatten
Signature
export declare const flatten: Monad1<'fp-ts-routing/Parser'> & Alternative1<'fp-ts-routing/Parser'>
Added in v0.5.1
getParserMonoid
Signature
export declare const getParserMonoid: <A>() => Monoid<Parser<A>>
Added in v0.5.1
map
Signature
export declare const map: <A, B>(f: (a: A) => B) => (fa: Parser<A>) => Parser<B>
Added in v0.5.1
parse
Signature
export declare function parse<A>(parser: Parser<A>, r: Route, a: A): A
Added in v0.4.0
parser
Signature
export declare const parser: Monad1<'fp-ts-routing/Parser'> & Alternative1<'fp-ts-routing/Parser'>
Added in v0.5.1
zero
Signature
export declare function zero<A>(): Parser<A>
Added in v0.4.0
routes
Query (interface)
Signature
export interface Query {
[key: string]: QueryValues
}
Added in v0.4.0
QueryValues (type alias)
Signature
export type QueryValues = string | Array<string> | undefined
Added in v0.4.0
Route (class)
Signature
export declare class Route {
constructor(readonly parts: Array<string>, readonly query: Query)
}
Added in v0.4.0
isEmpty (static method)
Signature
static isEmpty(r: Route): boolean
Added in v0.4.0
parse (static method)
Signature
static parse(s: string, decode: boolean = true): Route
Added in v0.4.0
toString (method)
Signature
toString(encode: boolean = true): string
Added in v0.4.0
utils
RowLacks (type alias)
Encodes the constraint that a given object O does not contain specific keys K
Signature
export type RowLacks<O, K extends string | number | symbol> = O & Record<Extract<keyof O, K>, never>
Added in v0.4.0