UNPKG

@spotable/attio-sdk

Version:
32 lines (31 loc) 1.57 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.generateWebhookEventFunction = generateWebhookEventFunction; exports.generateWebhookEventFunctionForList = generateWebhookEventFunctionForList; function generateWebhookEventFunction({ eventName, populatedType, customPrefix = "on", ...rest }) { const [, action] = eventName.split("."); if (!action) { throw new Error(`Invalid event name: ${eventName}. Expected format is "object.action".`); } const functionName = `${customPrefix}${action.charAt(0).toUpperCase() + action.slice(1)}`; const fetchDataCode = "fetchCode" in rest ? `const data = await (${rest.fetchCode});` : "idKey" in rest ? `const data = await this.getById(payload.id.${rest.idKey});` : `const data = undefined; // No idKey or fetchCode provided`; return ` ${functionName}(listener: AttioWebhookEventPopulatedListener<WebhookEventDataByType<"${eventName}">, ${eventName.includes("deleted") ? "undefined" : populatedType}>): void { this.client.webhooks.on("${eventName}", async (payload: WebhookEventDataByType<"${eventName}">) => { ${eventName.includes("deleted") ? "const data = undefined;" : fetchDataCode} await listener({ payload, data, }) }); } `.trim(); } function generateWebhookEventFunctionForList({ eventNames, ...options }) { return eventNames.map((eventName) => generateWebhookEventFunction({ ...options, eventName })).join("\n\n "); } //# sourceMappingURL=webhookEventFactory.js.map