UNPKG

@typed/fp

Version:

Data Structures and Resources for fp-ts

60 lines 1.65 kB
/** * This DSL represents all of the standard syntax for path-to-regexp (e.g. used in express.js and many other libs) * but does not cover the fancier regex validations that are techincally possible. * @since 0.13.0 */ import { pipe } from 'fp-ts/function'; import * as O from 'fp-ts/Option'; import ptr from 'path-to-regexp'; import { optional, param, pathJoin, prefix, queryParam, queryParams, unnamed, } from './Path'; import { altAll } from './ReaderOption'; /** * @category Constructor * @since 0.13.0 */ export function make(path) { const parse = ptr.match(path); const createPath = ptr.compile(path); return { path, match: (path) => { const match = parse(path); return !match ? O.none : O.some(match.params); }, createPath: createPath, }; } /** * @category Combinator * @since 0.13.0 */ export function map(f) { return (route) => { return { ...route, match: (r) => pipe(r, route.match, O.map(f)), }; }; } /* End Region: Route */ /** * @category Combinator * @since 0.13.0 */ export function oneOf(...[first, ...rest]) { return (path) => { const f = first.match; const rs = rest.map((r) => r.match); const all = pipe(rs, altAll(f)); return all(path); }; } // ** Type-level Tests ** // Should always be dead-code eliminated const query = queryParams(queryParam('d', optional(param('foo'))), queryParam('e', unnamed)); const path = pathJoin('test', param('bar'), optional(prefix('~', param('baz'))), query); check(); check(); check(); check(); //# sourceMappingURL=Route.js.map