Skip to content

Commit

Permalink
Extracted logic for getting topicArn. Simplified logic in sqsInitter,…
Browse files Browse the repository at this point in the history
… just pass the whole configuration.
  • Loading branch information
kamilwylegala committed Sep 23, 2024
1 parent ba1c2a1 commit 72fa4c2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 23 deletions.
15 changes: 5 additions & 10 deletions packages/sns/lib/utils/snsInitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export async function initSnsSqs(
creationConfig?: SNSCreationConfig & SQSCreationConfig,
subscriptionConfig?: SNSSubscriptionOptions,
extraParams?: ExtraParams,
) : Promise<{subscriptionArn: string, topicArn: string, queueUrl: string, queueName: string}> {
): Promise<{ subscriptionArn: string; topicArn: string; queueUrl: string; queueName: string }> {
if (!locatorConfig?.subscriptionArn) {
if (!creationConfig?.topic && !locatorConfig?.topicArn && !locatorConfig?.topicName) {
throw new Error(
Expand All @@ -51,15 +51,10 @@ export async function initSnsSqs(
)
}

const subscriptionTopicArn = locatorConfig
? (locatorConfig.topicArn ?? (await getTopicArnByName(snsClient, locatorConfig.topicName)))
: undefined

const topicResolutionOptions: TopicResolutionOptions = subscriptionTopicArn
? {
topicArn: subscriptionTopicArn,
}
: creationConfig.topic!
const topicResolutionOptions: TopicResolutionOptions = {
...locatorConfig,
...creationConfig.topic,
}

const { subscriptionArn, topicArn, queueUrl } = await subscribeToTopic(
sqsClient,
Expand Down
37 changes: 24 additions & 13 deletions packages/sns/lib/utils/snsSubscriber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,28 @@ export type SNSSubscriptionOptions = Omit<
'TopicArn' | 'Endpoint' | 'Protocol' | 'ReturnSubscriptionArn'
> & { updateAttributesIfExists: boolean }

async function resolveTopicArnToSubscribeTo(
topicConfiguration: TopicResolutionOptions,
snsClient: SNSClient,
extraParams: (ExtraSNSCreationParams & ExtraSQSCreationParams & ExtraParams) | undefined,
) {
//If topicArn is present, let's use it and return early.
if (isSNSTopicLocatorType(topicConfiguration) && topicConfiguration.topicArn) {
return topicConfiguration.topicArn
}

//If input configuration is capable of creating a topic, let's create it and return its ARN.
if (isCreateTopicCommand(topicConfiguration)) {
return await assertTopic(snsClient, topicConfiguration, {
queueUrlsWithSubscribePermissionsPrefix: extraParams?.queueUrlsWithSubscribePermissionsPrefix,
allowedSourceOwner: extraParams?.allowedSourceOwner,
})
}

//Last option: let's not create a topic but resolve a ARN based on the desired topic name.
return await getTopicArnByName(snsClient, topicConfiguration.topicName)
}

export async function subscribeToTopic(
sqsClient: SQSClient,
snsClient: SNSClient,
Expand All @@ -28,19 +50,8 @@ export async function subscribeToTopic(
subscriptionConfiguration: SNSSubscriptionOptions,
extraParams?: ExtraSNSCreationParams & ExtraSQSCreationParams & ExtraParams,
) {
let topicArn = isSNSTopicLocatorType(topicConfiguration) ? topicConfiguration.topicArn : undefined

if (!topicArn) {
if (isCreateTopicCommand(topicConfiguration)) {
topicArn = await assertTopic(snsClient, topicConfiguration, {
queueUrlsWithSubscribePermissionsPrefix:
extraParams?.queueUrlsWithSubscribePermissionsPrefix,
allowedSourceOwner: extraParams?.allowedSourceOwner,
})
} else {
topicArn = await getTopicArnByName(snsClient, topicConfiguration.topicName)
}
}
const topicArn = await resolveTopicArnToSubscribeTo(topicConfiguration, snsClient, extraParams)

const { queueUrl, queueArn } = await assertQueue(sqsClient, queueConfiguration, {
topicArnsWithPublishPermissionsPrefix: extraParams?.topicArnsWithPublishPermissionsPrefix,
updateAttributesIfExists: extraParams?.updateAttributesIfExists,
Expand Down

0 comments on commit 72fa4c2

Please sign in to comment.