import assert = require("assert"); import * as _ from "lodash"; import { APIs } from "../helpers"; import {IChassisMiddleware, IOperation } from "../interfaces"; /** * Emit a local Event * ------------------ * */ export default class Emit implements IChassisMiddleware { name = "api.emit"; title = "Emit a local event"; /** * feature to send an event on local event bus * * @param context * @param options * @returns {TargetEndpoint} */ fn(op: IOperation, _options: any): Function { let options = _.extend( {}, _options ); assert(options.event, "missing emit.event"); return (req, _res, next) => { let body = _.isObject(req.body)?req.body:options.body; let meta = APIs.getOperationContext(op, req, options.meta); op.context.bus.emit(options.event, { data: body, meta: meta } ); op.context.log( { code: "emit:event", message: options.event, meta: meta, body: body }); next(); } }; }