"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const flatten = (values) => [].concat(...values), noop = function noop() { return Promise.resolve(); }; function throwIfHasBeenCalled(fn) { if (fn.__appbuildercalled) { throw new Error('Cannot call next more than once'); } return (fn.__appbuildercalled = true); } function tryInvokeMiddleware(context, middleware, next = noop) { try { return Promise.resolve(middleware ? middleware(context, next) : context); } catch (error) { return Promise.reject(error); } } function functionList(list, ...args) { const arrayList = Symbol.iterator in list ? Array.from(list) : [list]; return arrayList.map((x) => { return (_, next) => Promise.resolve(x(...args)).then(next); }); } exports.functionList = functionList; /** * Create a function to invoke all passed middleware functions * with a single argument context * @param middleware */ function compose(...middleware) { return flatten(middleware) .filter((x) => { if ('function' !== typeof x) { throw new TypeError(`${x}, must be a middleware function accpeting (context, next) arguments`); } return x; }) .reduceRight((composed, mw) => { return function (context, nextFn) { const next = () => throwIfHasBeenCalled(next) && composed(context, nextFn); return tryInvokeMiddleware(context, mw, next); }; }, tryInvokeMiddleware); } exports.compose = compose; //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY29tcG9zZS5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3NyYy9jb21wb3NlLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7O0FBT0EsTUFBTSxPQUFPLEdBQUcsQ0FBSSxNQUEyQixFQUFFLEVBQUUsQ0FBRSxFQUFVLENBQUMsTUFBTSxDQUFDLEdBQUcsTUFBTSxDQUFRLEVBQ3RGLElBQUksR0FBRyxTQUFTLElBQUk7SUFDbEIsT0FBTyxPQUFPLENBQUMsT0FBTyxFQUFFLENBQUE7QUFDMUIsQ0FBQyxDQUFBO0FBRUgsU0FBUyxvQkFBb0IsQ0FBQyxFQUFPO0lBQ25DLElBQUksRUFBRSxDQUFDLGtCQUFrQixFQUFFO1FBQ3pCLE1BQU0sSUFBSSxLQUFLLENBQUMsaUNBQWlDLENBQUMsQ0FBQTtLQUNu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