UNPKG

hook-engine

Version:

Production-grade webhook engine with comprehensive adapter support, security, reliability, structured logging, and CLI tools.

94 lines (93 loc) 3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createWebhookMiddleware = createWebhookMiddleware; exports.createWebhookMiddlewareWithRetry = createWebhookMiddlewareWithRetry; exports.webhookErrorHandler = webhookErrorHandler; exports.createWebhookRoute = createWebhookRoute; const engine_1 = require("../core/engine"); /** * Express middleware factory for webhook handling */ function createWebhookMiddleware(config) { const engine = new engine_1.HookEngine(config); return async (req, res, next) => { try { const result = await engine.processWebhook(req); if (result.success) { res.status(200).json({ success: true, eventId: result.event?.id, message: result.message, duration: result.duration }); } else { res.status(400).json({ success: false, error: result.error, message: result.message, duration: result.duration }); } } catch (error) { next(error); } }; } /** * Express middleware factory for webhook handling with custom business logic */ function createWebhookMiddlewareWithRetry(config, businessLogic) { const engine = new engine_1.HookEngine(config); return async (req, res, next) => { try { const result = await engine.processWebhookWithRetry(req, businessLogic); if (result.success) { res.status(200).json({ success: true, eventId: result.event?.id, message: result.message, duration: result.duration, attempts: result.attempts }); } else { res.status(400).json({ success: false, error: result.error, message: result.message, duration: result.duration, attempts: result.attempts }); } } catch (error) { next(error); } }; } /** * Express error handler for webhook errors */ function webhookErrorHandler(error, req, res, next) { console.error('Webhook error:', error); // Don't expose internal errors to clients const isDevelopment = process.env.NODE_ENV === 'development'; res.status(500).json({ success: false, error: 'Internal server error', ...(isDevelopment && { details: error.message, stack: error.stack }) }); } /** * Express route factory for webhook endpoints */ function createWebhookRoute(path, config) { return { path, method: 'POST', middleware: [createWebhookMiddleware(config)], errorHandler: webhookErrorHandler }; }