@clerk/express
Version:
Clerk server SDK for usage with Express
1 lines • 2.97 kB
Source Map (JSON)
{"version":3,"file":"webhooks.mjs","names":["verifyWebhookBase"],"sources":["../src/webhooks.ts"],"sourcesContent":["/* eslint-disable import/export */\nimport type { VerifyWebhookOptions } from '@clerk/backend/webhooks';\nimport { verifyWebhook as verifyWebhookBase } from '@clerk/backend/webhooks';\nimport type { Request as ExpressRequest } from 'express';\n\nimport { incomingMessageToRequest } from './utils';\n\n// Ordering of exports matter here since\n// we're overriding the base verifyWebhook\nexport * from '@clerk/backend/webhooks';\n\n/**\n * Verifies the authenticity of a webhook request using Svix.\n *\n * @param request - The incoming webhook Express Request object\n * @param options - Optional configuration object\n * @param options.signingSecret - Custom signing secret. If not provided, falls back to CLERK_WEBHOOK_SIGNING_SECRET env variable\n * @throws Will throw an error if the webhook signature verification fails\n * @returns A promise that resolves to the verified webhook event data\n *\n * @example\n * ```typescript\n * import { verifyWebhook } from '@clerk/express/webhooks';\n *\n * app.post('/api/webhooks', async (req, res) => {\n * try {\n * const evt = await verifyWebhook(req);\n * // handle event\n * res.send('Webhook received');\n * } catch (err) {\n * res.status(400).send('Webhook verification failed');\n * }\n * });\n * ```\n *\n * @see {@link https://clerk.com/docs/webhooks/sync-data} to learn more about syncing Clerk data to your application using webhooks\n */\nexport async function verifyWebhook(req: ExpressRequest, options?: VerifyWebhookOptions) {\n const webRequest = incomingMessageToRequest(req);\n // Cloning instead of implementing the body inside incomingMessageToRequest\n // to make it more predictable\n // we must pass in body as string not as an Object or Buffer\n let serializedBody: string;\n if (typeof req.body === 'string') {\n serializedBody = req.body;\n } else if (Buffer.isBuffer(req.body)) {\n serializedBody = req.body.toString('utf8');\n } else if (req.body === undefined || req.body === null) {\n serializedBody = '';\n } else {\n try {\n serializedBody = JSON.stringify(req.body);\n } catch (error) {\n throw new Error(`Failed to serialize request body: ${error instanceof Error ? error.message : 'Unknown error'}`);\n }\n }\n const clonedRequest = new Request(webRequest, {\n body: serializedBody,\n });\n return verifyWebhookBase(clonedRequest, options);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAqCA,eAAsB,cAAc,KAAqB,SAAgC;CACvF,MAAM,aAAa,yBAAyB,GAAG;CAI/C,IAAI;CACJ,IAAI,OAAO,IAAI,SAAS,UACtB,iBAAiB,IAAI;MAChB,IAAI,OAAO,SAAS,IAAI,IAAI,GACjC,iBAAiB,IAAI,KAAK,SAAS,MAAM;MACpC,IAAI,IAAI,SAAS,UAAa,IAAI,SAAS,MAChD,iBAAiB;MAEjB,IAAI;EACF,iBAAiB,KAAK,UAAU,IAAI,IAAI;CAC1C,SAAS,OAAO;EACd,MAAM,IAAI,MAAM,qCAAqC,iBAAiB,QAAQ,MAAM,UAAU,iBAAiB;CACjH;CAKF,OAAOA,gBAAkB,IAHC,QAAQ,YAAY,EAC5C,MAAM,eACR,CACqC,GAAG,OAAO;AACjD"}