fp-ts-routing
Version:
A type-safe routing library for TypeScript
144 lines (143 loc) • 4.21 kB
TypeScript
import { Type } from 'io-ts';
import { Formatter } from './formatter';
import { RowLacks } from './helpers';
import { Parser } from './parser';
import { QueryValues } from './route';
/**
* @category matchers
* @since 0.4.0
*/
export declare class Match<A> {
readonly parser: Parser<A>;
readonly formatter: Formatter<A>;
/**
* @since 0.4.0
*/
readonly _A: A;
constructor(parser: Parser<A>, formatter: Formatter<A>);
/**
* @since 0.4.0
*/
imap<B>(f: (a: A) => B, g: (b: B) => A): Match<B>;
/**
* @since 0.4.0
*/
then<B>(that: Match<B> & Match<RowLacks<B, keyof A>>): Match<A & B>;
}
/**
* @category matchers
* @since 0.5.1
*/
export declare const imap: <A, B>(f: (a: A) => B, g: (b: B) => A) => (ma: Match<A>) => Match<B>;
/**
* @category matchers
* @since 0.5.1
*/
export declare const then: <B>(mb: Match<B>) => <A>(ma: Match<A> & Match<RowLacks<A, keyof B>>) => Match<A & B>;
/**
* `succeed` matches everything but consumes nothing
*
* @category matchers
* @since 0.4.0
*/
export declare const succeed: <A>(a: A) => Match<A>;
/**
* `end` matches the end of a route
*
* @category matchers
* @since 0.4.0
*/
export declare const end: Match<{}>;
/**
* `type` matches any io-ts type path component
*
* @example
* import * as t from 'io-ts'
* import { lit, type, Route } from '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)
*
* @category matchers
* @since 0.4.0
*/
export declare const type: <K extends string, A>(k: K, type: Type<A, string, unknown>) => Match<{ [_ in K]: A; }>;
/**
* `str` matches any string path component
*
* @example
* import { str, Route } from '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)
*
* @category matchers
* @since 0.4.0
*/
export declare const str: <K extends string>(k: K) => Match<{ [_ in K]: string; }>;
/**
* @category matchers
* @since 0.4.2
*/
export declare const IntegerFromString: Type<number, string, unknown>;
/**
* `int` matches any integer path component
*
* @example
* import { int, Route } from '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)
*
* @category matchers
* @since 0.4.0
*/
export declare const int: <K extends string>(k: K) => Match<{ [_ in K]: number; }>;
/**
* `lit(x)` will match exactly the path component `x`
*
* @example
* import { lit, Route } from '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)
*
* @category matchers
* @since 0.4.0
*/
export declare const lit: (literal: string) => Match<{}>;
/**
* Will match a querystring.
*
*
* **Note**. Use `io-ts`'s `strict` instead of `type` otherwise excess properties won't be removed.
*
* @example
* import * as t from 'io-ts'
* import { lit, str, query, Route } from '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')
*
* @category matchers
* @since 0.4.0
*/
export declare const query: <A>(type: Type<A, Record<string, QueryValues>, unknown>) => Match<A>;