clerk-auth-webhook-handler
Version:
A type-safe webhook handler for Clerk webhooks with support for Svix
49 lines • 1.88 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ClerkWebhookManager = void 0;
const express_1 = require("express");
const parse_1 = require("./parse");
class ClerkWebhookManager {
constructor(options) {
this.router = (0, express_1.Router)();
this.handlers = options.handlers;
this.secretKey = options.secretKey;
this.path = options.path || "/webhook";
this.setupRoutes();
}
setupRoutes() {
this.router.get(`${this.path}/health`, (req, res) => {
res.send("ok");
});
this.router.post(this.path, async (req, res) => {
var _a, _b;
try {
if (!this.secretKey) {
throw new Error("Secret key is not set");
}
const event = (0, parse_1.parseClerkEvent)({
requestBody: req.body,
requestHeaders: req.headers,
secret: this.secretKey,
});
if (event.type in this.handlers) {
await ((_b = (_a = this.handlers)[event.type]) === null || _b === void 0 ? void 0 : _b.call(_a, event));
return res
.status(200)
.json({ message: "Webhook processed successfully" });
}
console.warn(`Unhandled webhook type: ${event.type}`);
return res.status(200).json({ message: "Unhandled webhook type" });
}
catch (error) {
console.error("Webhook processing error:", error);
return res.status(400).json({ error: "Webhook processing failed" });
}
});
}
getRouter() {
return this.router;
}
}
exports.ClerkWebhookManager = ClerkWebhookManager;
//# sourceMappingURL=webhook.js.map