From 276e33d7da8cd09364aab3080cb0e0ec2d43aa0e Mon Sep 17 00:00:00 2001 From: LordAndreasOtten <49192522+LordAndreasOtten@users.noreply.github.com> Date: Tue, 17 Dec 2024 11:50:58 +0100 Subject: [PATCH] Update index.ts Subscribe to existing topic, otherwise create --- src/index.ts | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/index.ts b/src/index.ts index a01ae5d..6072bdc 100644 --- a/src/index.ts +++ b/src/index.ts @@ -346,8 +346,15 @@ class ServerlessOfflineSns { ); } - this.log(`Creating topic: "${topicName}" for fn "${fnName}"`); - const data = await this.snsAdapter.createTopic(topicName); + const { Topics } = await this.snsAdapter.listTopics(); + + let data = Topics.find((sub) => sub.TopicArn.split(":").pop() === topicName); + + if(!data || !data.TopicArn){ + this.log(`Creating topic: "${topicName}" for fn "${fnName}"`); + data = await this.snsAdapter.createTopic(topicName); + } + this.debug("topic: " + JSON.stringify(data)); const handler = await this.createHandler(fnName, fn, lambdasLocation); await this.snsAdapter.subscribe( @@ -566,4 +573,4 @@ class ServerlessOfflineSns { } } -export default ServerlessOfflineSns; \ No newline at end of file +export default ServerlessOfflineSns;