Skip to content

Commit bd442c8

Browse files
[Refactor] Extract asWebhookSubscription helper in extension-instance
buildHandle and buildUIDFromStrategy each duplicated the same 'topic' in config && 'uri' in config guard followed by an unchecked cast to SingleWebhookSubscriptionType. Extract that detection and cast into a single helper returning the typed subscription or undefined, and use an early return in both dynamic branches to drop a level of nesting. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 1cf6e57 commit bd442c8

1 file changed

Lines changed: 16 additions & 15 deletions

File tree

‎packages/app/src/cli/models/extensions/extension-instance.ts‎

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ const DEFAULT_WATCH_IGNORE = [
4646

4747
export const DEFAULT_DEV_SESSION_UPDATE_MESSAGE = 'Configuration accepted'
4848

49+
function asWebhookSubscription(configuration: object): SingleWebhookSubscriptionType | undefined {
50+
if (!('topic' in configuration) || !('uri' in configuration)) return undefined
51+
return configuration as unknown as SingleWebhookSubscriptionType
52+
}
53+
4954
/**
5055
* Class that represents an instance of a local extension
5156
* Before creating this class we've validated that:
@@ -532,15 +537,13 @@ export class ExtensionInstance<TConfiguration extends BaseConfigType = BaseConfi
532537
return this.specification.identifier
533538
case 'uuid':
534539
return this.configuration.handle ?? slugify(this.name ?? '')
535-
case 'dynamic':
540+
case 'dynamic': {
536541
// Hardcoded temporal solution for webhooks
537-
if ('topic' in this.configuration && 'uri' in this.configuration) {
538-
const subscription = this.configuration as unknown as SingleWebhookSubscriptionType
539-
const handle = `${subscription.topic}${subscription.uri}${subscription.filter}`
540-
return hashString(handle).substring(0, MAX_EXTENSION_HANDLE_LENGTH)
541-
} else {
542-
return nonRandomUUID(JSON.stringify(this.configuration))
543-
}
542+
const subscription = asWebhookSubscription(this.configuration)
543+
if (!subscription) return nonRandomUUID(JSON.stringify(this.configuration))
544+
const handle = `${subscription.topic}${subscription.uri}${subscription.filter}`
545+
return hashString(handle).substring(0, MAX_EXTENSION_HANDLE_LENGTH)
546+
}
544547
}
545548
}
546549

@@ -550,20 +553,18 @@ export class ExtensionInstance<TConfiguration extends BaseConfigType = BaseConfi
550553
return this.specification.identifier
551554
case 'uuid':
552555
return this.configuration.uid ?? nonRandomUUID(this.handle)
553-
case 'dynamic':
556+
case 'dynamic': {
554557
// NOTE: This is a temporary special case for webhook subscriptions.
555558
// We're directly checking for webhook properties and casting the configuration
556559
// instead of using a proper dynamic strategy implementation.
557560
// To remove this special case:
558561
// 1. Implement a proper dynamic UID strategy for webhooks in the server-side specification
559562
// 2. Update the CLI to use that strategy instead of this hardcoded logic
560563
// Related issues: PR #559094 in old Core repo
561-
if ('topic' in this.configuration && 'uri' in this.configuration) {
562-
const subscription = this.configuration as unknown as SingleWebhookSubscriptionType
563-
return `${subscription.topic}::${subscription.filter ?? ''}::${subscription.uri}`.substring(0, MAX_UID_LENGTH)
564-
} else {
565-
return nonRandomUUID(JSON.stringify(this.configuration))
566-
}
564+
const subscription = asWebhookSubscription(this.configuration)
565+
if (!subscription) return nonRandomUUID(JSON.stringify(this.configuration))
566+
return `${subscription.topic}::${subscription.filter ?? ''}::${subscription.uri}`.substring(0, MAX_UID_LENGTH)
567+
}
567568
}
568569
}
569570
}

0 commit comments

Comments
 (0)