hook-engine
Version:
Production-grade webhook engine with comprehensive adapter support, security, reliability, structured logging, and CLI tools.
31 lines (30 loc) • 1.11 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.receiveWebhook = receiveWebhook;
const body_1 = require("../utils/body");
const adapters_1 = require("../adapters");
async function receiveWebhook(req, config) {
const rawBody = await (0, body_1.normalizeRequestBody)(req);
const adapter = (0, adapters_1.getAdapter)(config.source);
if (!adapter) {
throw new Error(`Unsupported webhook source: ${config.source}`);
}
const signature = adapter.getSignature(req);
if (!signature) {
throw new Error(`Missing signature header for source: ${config.source}`);
}
const isValid = adapter.verifySignature(rawBody, signature, config.secret);
if (!isValid) {
throw new Error("Webhook signature verification failed");
}
const parsed = adapter.parsePayload(rawBody);
const normalized = adapter.normalize(parsed);
return {
id: normalized.id,
type: normalized.type,
timestamp: normalized.timestamp,
source: config.source,
payload: normalized.payload,
raw: parsed,
};
}