UNPKG

@zodash/onion

Version:
44 lines (43 loc) 1.13 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Onion = void 0; const compose_1 = require("@zodash/compose"); class Onion { constructor() { this.middlewares = []; } use(middleware) { this.middlewares.push(middleware); return this; } callback() { if (!this.handler) { // the core handler this.use(this.handle()); // const fn = compose_1.compose(...this.middlewares); this.handler = fn; } return async (input, output) => { const context = this.createContext(input, output); await this.handler(context); return context; }; } async execute(input) { if (!this._callback) { this._callback = this.callback(); } const output = {}; const context = await this._callback(input, output); return context.output; } createContext(input, output) { return { input, output, state: {}, }; } } exports.Onion = Onion;