UNPKG

@nodeswork/sbase

Version:

Basic REST api foundation from Nodeswork.

64 lines (62 loc) 2.09 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const _ = require("underscore"); const debug_1 = require("debug"); const d = debug_1.default('sbase:utils'); /** * Compose `middleware` returning a fully valid middleware comprised of all * those which are passed. */ function compose(middleware) { if (!Array.isArray(middleware)) { throw new TypeError('Middleware stack must be an array!'); } for (const fn of middleware) { if (typeof fn !== 'function') { throw new TypeError('Middleware must be composed of functions!'); } } /** * @param {Object} context * @return {Promise} * @api public */ return function (context, next) { // last called middleware # let index = -1; const rid = `${context.request.method} ${context.request.path} ${context.requestId}`; return dispatch.call(this, 0); function dispatch(i) { if (i <= index) { return Promise.reject(new Error('next() called multiple times')); } index = i; const fn = i === middleware.length ? next : middleware[i]; if (!fn) return Promise.resolve(); const start = _.now(); const name = fn.name || '<anonymous>'; try { if (name !== 'bound dispatch') { d('[%O] Begin of middleware %O', rid, name); } return Promise.resolve(fn.call(this, context, dispatch.bind(this, i + 1))); } catch (err) { return Promise.reject(err); } finally { const end = _.now(); if (name !== 'bound dispatch') { d('[%O] End of middleware %O, duration: %O', rid, name, end - start); } } } }; } exports.compose = compose; function isPromise(promise) { return promise && promise.then != null; } exports.isPromise = isPromise; //# sourceMappingURL=utils.js.map