UNPKG

@typed/fp

Version:

Data Structures and Resources for fp-ts

67 lines 2.11 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.oneOf = exports.map = exports.make = void 0; const tslib_1 = require("tslib"); /** * 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 */ const function_1 = require("fp-ts/function"); const O = (0, tslib_1.__importStar)(require("fp-ts/Option")); const path_to_regexp_1 = (0, tslib_1.__importDefault)(require("path-to-regexp")); const Path_1 = require("./Path"); const ReaderOption_1 = require("./ReaderOption"); /** * @category Constructor * @since 0.13.0 */ function make(path) { const parse = path_to_regexp_1.default.match(path); const createPath = path_to_regexp_1.default.compile(path); return { path, match: (path) => { const match = parse(path); return !match ? O.none : O.some(match.params); }, createPath: createPath, }; } exports.make = make; /** * @category Combinator * @since 0.13.0 */ function map(f) { return (route) => { return { ...route, match: (r) => (0, function_1.pipe)(r, route.match, O.map(f)), }; }; } exports.map = map; /* End Region: Route */ /** * @category Combinator * @since 0.13.0 */ function oneOf(...[first, ...rest]) { return (path) => { const f = first.match; const rs = rest.map((r) => r.match); const all = (0, function_1.pipe)(rs, (0, ReaderOption_1.altAll)(f)); return all(path); }; } exports.oneOf = oneOf; // ** Type-level Tests ** // Should always be dead-code eliminated const query = (0, Path_1.queryParams)((0, Path_1.queryParam)('d', (0, Path_1.optional)((0, Path_1.param)('foo'))), (0, Path_1.queryParam)('e', Path_1.unnamed)); const path = (0, Path_1.pathJoin)('test', (0, Path_1.param)('bar'), (0, Path_1.optional)((0, Path_1.prefix)('~', (0, Path_1.param)('baz'))), query); check(); check(); check(); check(); //# sourceMappingURL=Route.js.map