@devries-cameron20 / composio Mirror · read-only / commits / 74646f0bf
Fork 0

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

Mirrored from https://github.com/Camo67/composio.git

Synced Nov 22, 12:00 UTC

feat(ts/core): simplify code

jkomyno committed Jan 29, 2026 at 04:21 UTC 74646f0bf89f2760602280a3a0e589f2c7a36a53
1 file changed +45 -48
ts/packages/core/src/models/Triggers.ts
+45 -48
@@ -450,30 +450,58 @@ export class Triggers<TProvider extends BaseComposioProvider<unknown, unknown, u
450 }
451
452 /**
453 - * Parses incoming Pusher payload, supporting V1, V2, V3, and legacy TriggerData formats.
454 - * This method reuses the same schemas and normalizers used for HTTP webhooks.
453 + * Tries to parse data as V1, V2, or V3 webhook payload format.
454 + * Returns the parsed result with version info, or null if no format matches.
455 * @private
456 */
457 - private parsePusherPayload(data: Record<string, unknown>): IncomingTriggerPayload {
458 - // Try V3 format first (has 'composio.trigger.message' type)
457 + private tryParseVersionedPayload(data: unknown): {
458 + version: WebhookVersion;
459 + rawPayload: WebhookPayload;
460 + normalizedPayload: IncomingTriggerPayload;
461 + } | null {
462 + // Try V3 first (has 'composio.trigger.message' type)
463 const v3Result = WebhookPayloadV3Schema.safeParse(data);
464 if (v3Result.success) {
461 - logger.debug('Parsed Pusher payload as V3 format');
462 - return this.normalizeV3Payload(v3Result.data);
465 + return {
466 + version: WebhookVersions.V3,
467 + rawPayload: v3Result.data,
468 + normalizedPayload: this.normalizeV3Payload(v3Result.data),
469 + };
470 }
471
465 - // Try V2 format (has 'type', 'timestamp', 'log_id', and 'data')
472 + // Try V2 (has 'type', 'timestamp', 'log_id', and 'data')
473 const v2Result = WebhookPayloadV2Schema.safeParse(data);
474 if (v2Result.success) {
468 - logger.debug('Parsed Pusher payload as V2 format');
469 - return this.normalizeV2Payload(v2Result.data);
475 + return {
476 + version: WebhookVersions.V2,
477 + rawPayload: v2Result.data,
478 + normalizedPayload: this.normalizeV2Payload(v2Result.data),
479 + };
480 }
481
472 - // Try V1 format (has 'trigger_name', 'connection_id', 'trigger_id', 'payload', 'log_id')
482 + // Try V1 (has 'trigger_name', 'connection_id', 'trigger_id', 'payload', 'log_id')
483 const v1Result = WebhookPayloadV1Schema.safeParse(data);
484 if (v1Result.success) {
475 - logger.debug('Parsed Pusher payload as V1 format');
476 - return this.normalizeV1Payload(v1Result.data);
485 + return {
486 + version: WebhookVersions.V1,
487 + rawPayload: v1Result.data,
488 + normalizedPayload: this.normalizeV1Payload(v1Result.data),
489 + };
490 + }
491 +
492 + return null;
493 + }
494 +
495 + /**
496 + * Parses incoming Pusher payload, supporting V1, V2, V3, and legacy TriggerData formats.
497 + * @private
498 + */
499 + private parsePusherPayload(data: Record<string, unknown>): IncomingTriggerPayload {
500 + // Try V1/V2/V3 formats
501 + const versionedResult = this.tryParseVersionedPayload(data);
502 + if (versionedResult) {
503 + logger.debug(`Parsed Pusher payload as ${versionedResult.version} format`);
504 + return versionedResult.normalizedPayload;
505 }
506
507 // Try legacy TriggerData format (for backwards compatibility)
@@ -654,47 +682,16 @@ export class Triggers<TProvider extends BaseComposioProvider<unknown, unknown, u
682 });
683 }
684
657 - // Try V3 first (has 'composio.trigger.message' type)
658 - const v3Result = WebhookPayloadV3Schema.safeParse(jsonPayload);
659 - if (v3Result.success) {
660 - return {
661 - version: WebhookVersions.V3,
662 - rawPayload: v3Result.data,
663 - normalizedPayload: this.normalizeV3Payload(v3Result.data),
664 - };
665 - }
666 -
667 - // Try V2 (has 'type', 'timestamp', 'log_id', and 'data' with specific fields)
668 - const v2Result = WebhookPayloadV2Schema.safeParse(jsonPayload);
669 - if (v2Result.success) {
670 - return {
671 - version: WebhookVersions.V2,
672 - rawPayload: v2Result.data,
673 - normalizedPayload: this.normalizeV2Payload(v2Result.data),
674 - };
675 - }
676 -
677 - // Try V1 (has 'trigger_name', 'connection_id', 'trigger_id', 'payload', 'log_id')
678 - const v1Result = WebhookPayloadV1Schema.safeParse(jsonPayload);
679 - if (v1Result.success) {
680 - return {
681 - version: WebhookVersions.V1,
682 - rawPayload: v1Result.data,
683 - normalizedPayload: this.normalizeV1Payload(v1Result.data),
684 - };
685 + // Try V1/V2/V3 formats using shared parsing logic
686 + const result = this.tryParseVersionedPayload(jsonPayload);
687 + if (result) {
688 + return result;
689 }
690
691 // None of the schemas matched
692 throw new ComposioWebhookPayloadError(
693 'Webhook payload does not match any known version (V1, V2, or V3). ' +
690 - 'Please ensure you are using a supported webhook payload format.',
691 - {
692 - cause: {
693 - v1Error: v1Result.error?.message,
694 - v2Error: v2Result.error?.message,
695 - v3Error: v3Result.error?.message,
696 - },
697 - }
694 + 'Please ensure you are using a supported webhook payload format.'
695 );
696 }
697