@izara_project/izara-core-library-sns 1.0.7 → 1.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js CHANGED
@@ -15,8 +15,6 @@ You should have received a copy of the GNU Affero General Public License
15
15
  along with this program. If not, see <http://www.gnu.org/licenses/>.
16
16
  */
17
17
 
18
- 'use strict';
19
-
20
18
  // Import the SnS shared library functions
21
19
  import snsSharedLib from './src/SnsSharedLib.js';
22
20
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@izara_project/izara-core-library-sns",
3
- "version": "1.0.7",
3
+ "version": "1.0.8",
4
4
  "description": "Connecting with AWS SNS Resource",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -15,52 +15,76 @@ You should have received a copy of the GNU Affero General Public License
15
15
  along with this program. If not, see <http://www.gnu.org/licenses/>.
16
16
  */
17
17
 
18
- 'use strict';
19
-
20
18
  /**
21
- * Generate sns topic name
22
- * @param {Object} _izContext
23
- * @param {string} topicName
24
- * @param {string} serviceTag
19
+ * Build an SNS topic name for a given logical topic.
25
20
  *
26
- * @returns {string} deployed SNS resource name
21
+ * The final topic name is constructed by concatenating service tag, stage,
22
+ * and the provided `topicName`. When `serviceTag` is omitted the current
23
+ * service (`process.env.iz_serviceTag`) is used.
24
+ *
25
+ * @param {Object} _izContext - Izara context object (kept for parity with other helpers).
26
+ * @param {string} topicName - Logical topic name (e.g. function/event name).
27
+ * @param {string} [serviceTag=null] - Optional service tag for external services.
28
+ * @param {string} [stage=null] - Optional stage override (defaults to `process.env.iz_stage`).
29
+ * @returns {string} The resolved SNS topic name (no ARN).
27
30
  */
28
- function snsTopicName(_izContext, topicName, serviceTag = null) {
31
+ function snsTopicName(_izContext, topicName, serviceTag = null, stage = null) {
29
32
  // concatenate with env stage setting
30
33
  // return serviceName + process.env.iz_stage + topicName
31
34
 
32
- if (!serviceTag) { // for owner service
33
- return process.env.iz_serviceTag + process.env.iz_stage + topicName;
34
- } else { // create table name for external service
35
- return serviceTag + process.env.iz_stage + topicName;
36
- }
37
- }
35
+ const stageSuffix = stage ? stage : process.env.iz_stage;
36
+ const serviceTagResolved = serviceTag
37
+ ? serviceTag
38
+ : process.env.iz_serviceTag;
38
39
 
40
+ return serviceTagResolved + stageSuffix + topicName;
41
+ }
39
42
 
40
43
  /**
41
- * Generate sns topic name
42
- * @param {Object} _izContext
43
- * @param {string} topicName
44
- * @param {string} serviceTag
44
+ * Build the SNS Topic ARN for a given logical topic name.
45
45
  *
46
- * @returns {string} deployed SNS resource name
46
+ * The function uses `snsTopicName` to resolve the topic name and then
47
+ * builds an ARN in the form `arn:aws:sns:{region}:{accountId}:{topicName}`.
48
+ *
49
+ * @param {Object} _izContext - Izara context object.
50
+ * @param {string} topicName - Logical topic name.
51
+ * @param {string} [serviceTag=null] - Optional service tag for external service topics.
52
+ * @param {string} [region=null] - Optional AWS region override (defaults to `process.env.iz_region`).
53
+ * @param {string} [accountId=null] - Optional account override (defaults to `process.env.iz_accountId`).
54
+ * @param {string} [stage=null] - Optional stage override (defaults to `process.env.iz_stage`).
55
+ * @returns {string} The SNS topic ARN.
47
56
  */
48
- function snsTopicArn(_izContext, topicName, serviceTag = null) {
49
- const snsTopicArn = snsTopicName(_izContext, topicName, serviceTag)
50
- // concatenate with env stage setting
57
+ function snsTopicArn(
58
+ _izContext,
59
+ topicName,
60
+ serviceTag = null,
61
+ region = null,
62
+ accountId = null,
63
+ stage = null
64
+ ) {
65
+ const topic = snsTopicName(_izContext, topicName, serviceTag, stage);
66
+
67
+ const actualRegion = region ? region : process.env.iz_region;
68
+ const actualAccountId = accountId ? accountId : process.env.iz_accountId;
69
+
51
70
  // arn:aws:sns:us-east-1:468568093265:GraphHandlerDevInChangeRelationshipType
52
- return `arn:aws:sns:${process.env.iz_region}:${process.env.iz_accountId}:${snsTopicArn}`
71
+ return `arn:aws:sns:${actualRegion}:${actualAccountId}:${topic}`;
53
72
  }
54
73
 
55
74
  /**
56
- * Generate sns topic name
57
- * @param {string} topicArn
75
+ * Extract the topic name from an SNS topic ARN.
58
76
  *
59
- * @returns {string} deployed SNS resource name
77
+ * Example: `arn:aws:sns:us-east-1:123456789012:MyTopic` -> `MyTopic`.
78
+ * The function returns `null` if the ARN is not in the expected format.
79
+ *
80
+ * @param {string} topicArn - Fully-qualified SNS topic ARN.
81
+ * @returns {string|null} The extracted topic name or `null` if invalid.
60
82
  */
61
83
  function extractTopicName(topicArn) {
62
- let topicName = topicArn.split(":")[5];
63
- return topicName
84
+ if (typeof topicArn !== 'string') return null;
85
+ const parts = topicArn.split(':');
86
+ if (parts.length < 6) return null;
87
+ return parts[5] || null;
64
88
  }
65
89
 
66
90
  /**
@@ -69,12 +93,16 @@ function extractTopicName(topicArn) {
69
93
  * @param {string} topicName
70
94
  * @param {string} serviceTag
71
95
  */
72
- function snsTopicNameByFlowSchema(_izContext, topicName, serviceTag = null) {
73
- if (!serviceTag) { // for owner service
74
- return process.env.iz_serviceTag + "_" + process.env.iz_stage + "_" + topicName;
75
- } else { // create table name for external service
76
- return serviceTag + "_" + process.env.iz_stage + "_" + topicName;
77
- }
96
+ function snsTopicNameByFlowSchema(
97
+ _izContext,
98
+ topicName,
99
+ serviceTag = null,
100
+ stage = null
101
+ ) {
102
+ const stageSuffix = stage ? stage : process.env.iz_stage;
103
+ const serviceTagSuffix = serviceTag ? serviceTag : process.env.iz_serviceTag;
104
+
105
+ return `${serviceTagSuffix}_${stageSuffix}_${topicName}`;
78
106
  }
79
107
 
80
108
  /**
@@ -85,11 +113,27 @@ function snsTopicNameByFlowSchema(_izContext, topicName, serviceTag = null) {
85
113
  *
86
114
  * @returns {string} deployed SNS resource name
87
115
  */
88
- function snsTopicArnByFlowSchema(_izContext, topicName, serviceTag = null) {
89
- const snsTopicArn = snsTopicNameByFlowSchema(_izContext, topicName, serviceTag)
116
+ function snsTopicArnByFlowSchema(
117
+ _izContext,
118
+ topicName,
119
+ serviceTag = null,
120
+ region = null,
121
+ accountId = null,
122
+ stage = null
123
+ ) {
124
+ const snsTopicArn = snsTopicNameByFlowSchema(
125
+ _izContext,
126
+ topicName,
127
+ serviceTag,
128
+ stage
129
+ );
130
+
131
+ const actualRegion = region ? region : process.env.iz_region;
132
+ const actualAccountId = accountId ? accountId : process.env.iz_accountId;
133
+
90
134
  // concatenate with env stage setting
91
135
  // arn:aws:sns:us-east-1:468568093265:GraphHandlerDevInChangeRelationshipType
92
- return `arn:aws:sns:${process.env.iz_region}:${process.env.iz_accountId}:${snsTopicArn}`
136
+ return `arn:aws:sns:${actualRegion}:${actualAccountId}:${snsTopicArn}`;
93
137
  }
94
138
 
95
139
  export default {
@@ -98,4 +142,4 @@ export default {
98
142
  extractTopicName,
99
143
  snsTopicNameByFlowSchema,
100
144
  snsTopicArnByFlowSchema
101
- }
145
+ };