UNPKG

@ark7/router

Version:
119 lines (117 loc) 3.71 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TeePost = exports.Tee = exports._check = exports.Check = exports._when = exports.When = exports._if = exports.If = void 0; const decorators_1 = require("./decorators"); const utils_1 = require("./utils"); /** * A IF middleware that helps a branch checking. Usage: * * @If(predictor, ifClause) * - execute ifClause when predictor returns true. * - execute next middleware when predictor returns false. * * @If(predictor, ifClause, elseClause) * - execute ifClause when predictor returns true. * - execute elseClause when predictor returns false. * * @param predictor - Predict which clause to execute. * @param ifClause - Execute when predictor returns true. * @param elseClause - Execute when predictor returns false. By default, execute * else clause. */ function If(predictor, ifClause, elseClause) { return (0, decorators_1.Middleware)(_if(predictor, ifClause, elseClause)); } exports.If = If; function _if(predictor, ifClause, elseClause) { return async (ctx, next) => { const value = predictor(ctx); const boolValue = (0, utils_1.isPromise)(value) ? await value : value; if (boolValue) { await (0, utils_1.compose)(ifClause)(ctx, next); } else if (elseClause) { await (0, utils_1.compose)(elseClause)(ctx, next); } else { await next(); } }; } exports._if = _if; /** * A WHEN middleware that helps a branch checking, next middleware will always * be executed regardless what predictor returns. * * @param predictor - Predict when the when clause will be executed. * @param whenClause - Execute when predictor returns true. */ function When(predictor, whenClause) { return (0, decorators_1.Middleware)(_when(predictor, whenClause)); } exports.When = When; function _when(predictor, whenClause) { return async (ctx, next) => { const value = predictor(ctx); const boolValue = (0, utils_1.isPromise)(value) ? await value : value; if (boolValue && whenClause) { await (0, utils_1.compose)(whenClause)(ctx, () => Promise.resolve({})); } await next(); }; } exports._when = _when; /** * A CHECK middleware that helps determines if to execute next middleware. * Usage: * * @Check((ctx: Router.IRouterContext) => ctx.isAuthenticated()) * * @param predictor - Returns true to execute next middleware. */ function Check(predictor) { return (0, decorators_1.Middleware)(_check(predictor)); } exports.Check = Check; function _check(predictor) { return async (ctx, next) => { const value = predictor(ctx); const boolValue = (0, utils_1.isPromise)(value) ? await value : value; if (boolValue) { await next(); } }; } exports._check = _check; /** * A Tee middleware that helps execute a side function. * Usage: * * @Tee((ctx) => console.log(ctx.path)) * * @param fn - The side function to execute. * @param opts.post - specifies run tee after returned. */ function Tee(fn, opts = {}) { async function process(ctx) { const value = fn(ctx, () => { }); if ((0, utils_1.isPromise)(value)) { await value; } } return (0, decorators_1.Middleware)(async (ctx, next) => { if (!opts.post) { await process(ctx); } await next(); if (opts.post) { await process(ctx); } }); } exports.Tee = Tee; function TeePost(fn) { return Tee(fn, { post: true }); } exports.TeePost = TeePost; //# sourceMappingURL=flows.js.map