UNPKG

@gnosticdev/highlevel-sdk

Version:
108 lines (107 loc) 3.43 kB
import { t as HighLevelSDKError } from "./errors-DX_S3nKC.js"; //#region src/v2/webhooks/public-key.ts /** * The public key used to verify the webhook signature, for convenience. * * @see {@link https://marketplace.gohighlevel.com/docs/webhook/WebhookIntegrationGuide/index.html#security-verifying-webhook-authenticity} */ const GHL_WEBHOOK_PUBLIC_KEY_PEM = `-----BEGIN PUBLIC KEY----- MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAokvo/r9tVgcfZ5DysOSC Frm602qYV0MaAiNnX9O8KxMbiyRKWeL9JpCpVpt4XHIcBOK4u3cLSqJGOLaPuXw6 dO0t6Q/ZVdAV5Phz+ZtzPL16iCGeK9po6D6JHBpbi989mmzMryUnQJezlYJ3DVfB csedpinheNnyYeFXolrJvcsjDtfAeRx5ByHQmTnSdFUzuAnC9/GepgLT9SM4nCpv uxmZMxrJt5Rw+VUaQ9B8JSvbMPpez4peKaJPZHBbU3OdeCVx5klVXXZQGNHOs8gF 3kvoV5rTnXV0IknLBXlcKKAQLZcY/Q9rG6Ifi9c+5vqlvHPCUJFT5XUGG5RKgOKU J062fRtN+rLYZUV+BjafxQauvC8wSWeYja63VSUruvmNj8xkx2zE/Juc+yjLjTXp IocmaiFeAO6fUtNjDeFVkhf5LNb59vECyrHD2SQIrhgXpO4Q3dVNA5rw576PwTzN h/AMfHKIjE4xQA1SZuYJmNnmVZLIZBlQAF9Ntd03rfadZ+yDiOXCCs9FkHibELhC HULgCsnuDJHcrGNd5/Ddm5hxGQ0ASitgHeMZ0kcIOwKDOzOU53lDza6/Y09T7sYJ PQe7z0cvj7aE4B+Ax1ZoZGPzpJlZtGXCsu9aTEGEnKzmsFqwcSsnw3JB31IGKAyk T1hhTiaCeIY/OwwwNUY2yvcCAwEAAQ== -----END PUBLIC KEY-----`; //#endregion //#region src/v2/webhooks/webhooks-client.ts var HandlerMap = class extends Map { get(event) { return super.get(event); } set(event, handler) { super.set(event, handler); return this; } }; /** * Client for handling HighLevel webhook events * * **Note** You will need to decrypt the raw body of the payload response from HighLevel to get the actual payload. * @see {@link https://marketplace.gohighlevel.com/docs/webhook/WebhookIntegrationGuide} */ var WebhooksClient = class { handlers = new HandlerMap(); /** * The public key used to verify the webhook signature, for convenience. */ WEBHOOK_PUBLIC_KEY_PEM = GHL_WEBHOOK_PUBLIC_KEY_PEM; /** * Register a handler for a specific webhook event * * @example * ```ts * const webhooks = new WebhooksClient() * * webhooks.on('ContactCreate', async (payload) => { * // payload is fully typed as ContactCreate schema * const { firstName, lastName, email } = payload * await db.contacts.create({ firstName, lastName, email }) * }) * ``` */ on(event, handler) { this.handlers.set(event, handler); return this; } /** * Validate and handle an incoming webhook payload * * @example * ```ts * app.post('/webhooks/:event', async (ctx) => { * try { * await webhooks.handle(ctx.params.event, ctx.request.body) * return ctx.status(200).end() * } catch (error) { * return ctx.status(400).json({ error: error.message }) * } * }) * ``` */ async handle(event, payload) { const handler = this.handlers.get(event); if (!handler) throw new HighLevelSDKError("NO_HANDLER_REGISTERED", { event }); if (!payload || typeof payload !== "object") throw new HighLevelSDKError("INVALID_WEBHOOK_PAYLOAD"); await handler(payload); } /** * Get the TypeScript type for a webhook payload * Useful for type checking in your IDE * * @example * ```ts * const webhooks = new WebhooksClient() * * const contactType = webhooks.type<'ContactCreate'>() * // contactType is fully typed as ContactCreate schema * ``` */ $typeOf() { return {}; } }; /** * Create a new webhooks client for handling HighLevel webhook events */ function createWebhooksClient() { return new WebhooksClient(); } //#endregion export { WebhooksClient, createWebhooksClient };