UNPKG

@vtex/api

Version:
49 lines (48 loc) 2.67 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createEventHandler = void 0; const tracingMiddlewares_1 = require("../../../tracing/tracingMiddlewares"); const clients_1 = require("../http/middlewares/clients"); const rateLimit_1 = require("../http/middlewares/rateLimit"); const settings_1 = require("../http/middlewares/settings"); const timings_1 = require("../http/middlewares/timings"); const compose_1 = require("../utils/compose"); const toArray_1 = require("../utils/toArray"); const body_1 = require("./middlewares/body"); const context_1 = require("./middlewares/context"); const createEventHandler = (clientsConfig, eventId, handler, serviceEvent, globalLimiter) => { var _a, _b; const { implementation, options } = clientsConfig; const middlewares = (0, toArray_1.toArray)(handler); const pipeline = [ (0, tracingMiddlewares_1.nameSpanOperationMiddleware)('event-handler', eventId), context_1.eventContextMiddleware, body_1.parseBodyMiddleware, (0, clients_1.clients)(implementation, options), ...((serviceEvent === null || serviceEvent === void 0 ? void 0 : serviceEvent.settingsType) === 'workspace' || (serviceEvent === null || serviceEvent === void 0 ? void 0 : serviceEvent.settingsType) === 'userAndWorkspace' ? [(0, settings_1.getServiceSettings)()] : []), timings_1.timings, (0, rateLimit_1.concurrentRateLimiter)((_a = serviceEvent === null || serviceEvent === void 0 ? void 0 : serviceEvent.rateLimitPerReplica) === null || _a === void 0 ? void 0 : _a.concurrent), (0, rateLimit_1.perMinuteRateLimiter)((_b = serviceEvent === null || serviceEvent === void 0 ? void 0 : serviceEvent.rateLimitPerReplica) === null || _b === void 0 ? void 0 : _b.perMinute, globalLimiter), (0, tracingMiddlewares_1.traceUserLandRemainingPipelineMiddleware)(), contextAdapter(middlewares), ]; return (0, compose_1.compose)(pipeline); }; exports.createEventHandler = createEventHandler; function contextAdapter(middlewares) { return async function middlewareCascade(ctx) { const ctxEvent = { body: ctx.state.body, clients: ctx.clients, key: ctx.vtex.eventInfo ? ctx.vtex.eventInfo.key : '', metrics: ctx.metrics, sender: ctx.vtex.eventInfo ? ctx.vtex.eventInfo.sender : '', state: ctx.state, subject: ctx.vtex.eventInfo ? ctx.vtex.eventInfo.subject : '', timings: ctx.timings, vtex: ctx.vtex, }; await (0, compose_1.composeForEvents)(middlewares)(ctxEvent); ctx.status = 204; }; }