/** * 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 { A } from 'ts-toolbelt' import { Interpolate, optional, param, ParamsOf, pathJoin, prefix, queryParam, queryParams, QueryParamsOf, unnamed, } from './Path' import { altAll, ReaderOption } from './ReaderOption' /* End Region: Interpolations */ /* Start Region: Route */ /** * @category Model * @since 0.13.0 */ export interface Route
> {
readonly path: P
readonly match: ReaderOption
}
/**
* @category Type-level
* @since 0.13.0
*/
export type PathOf = [A] extends [Route (path: P): Route {
const parse = ptr.match(path)
const createPath = ptr.compile(path)
return {
path,
match: (path: string) => {
const match = parse(path)
return !match ? O.none : O.some(match.params as ParamsOf )
},
createPath: createPath as Route