@izara_project/izara-core-library-sns 1.0.6 → 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,16 +15,8 @@ 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
- const SnsSharedLib = require('./src/SnsSharedLib');
19
+ import snsSharedLib from './src/SnsSharedLib.js';
22
20
 
23
21
  // Export all the functions
24
- module.exports = {
25
- snsTopicName: SnsSharedLib.snsTopicName,
26
- snsTopicArn: SnsSharedLib.snsTopicArn,
27
- extractTopicName: SnsSharedLib.extractTopicName,
28
- snsTopicNameByFlowSchema: SnsSharedLib.snsTopicNameByFlowSchema,
29
- snsTopicArnByFlowSchema: SnsSharedLib.snsTopicArnByFlowSchema
30
- };
22
+ export default snsSharedLib;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@izara_project/izara-core-library-sns",
3
- "version": "1.0.6",
3
+ "version": "1.0.8",
4
4
  "description": "Connecting with AWS SNS Resource",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -14,12 +14,11 @@
14
14
  "license": "AGPL-3.0-or-later",
15
15
  "homepage": "https://bitbucket.org/izara-core-libraries/izara-core-library-sns/src/master/README.md",
16
16
  "devDependencies": {
17
- "jest": "^29.7.0"
17
+ "jest": "^30.2.0"
18
18
  },
19
19
  "jest": {
20
20
  "testEnvironment": "node"
21
21
  },
22
- "dependencies": {
23
- "@izara_project/izara-core-library-service-schemas": "^1.0.49"
24
- }
22
+ "type": "module",
23
+ "dependencies": {}
25
24
  }
@@ -15,53 +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
- // const { getServiceNameWithCache } = require('@izara_project/izara-core-library-service-schemas/src/ServiceConfig')
21
18
  /**
22
- * Generate sns topic name
23
- * @param {Object} _izContext
24
- * @param {string} topicName
25
- * @param {string} serviceTag
19
+ * Build an SNS topic name for a given logical topic.
26
20
  *
27
- * @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).
28
30
  */
29
- module.exports.snsTopicName = async (_izContext, topicName, serviceTag = null) => {
31
+ function snsTopicName(_izContext, topicName, serviceTag = null, stage = null) {
30
32
  // concatenate with env stage setting
31
33
  // return serviceName + process.env.iz_stage + topicName
32
34
 
33
- if (!serviceTag) { // for owner service
34
- return process.env.iz_serviceTag + process.env.iz_stage + topicName;
35
- } else { // create table name for external service
36
- return serviceTag + process.env.iz_stage + topicName;
37
- }
38
- }
35
+ const stageSuffix = stage ? stage : process.env.iz_stage;
36
+ const serviceTagResolved = serviceTag
37
+ ? serviceTag
38
+ : process.env.iz_serviceTag;
39
39
 
40
+ return serviceTagResolved + stageSuffix + topicName;
41
+ }
40
42
 
41
43
  /**
42
- * Generate sns topic name
43
- * @param {Object} _izContext
44
- * @param {string} topicName
45
- * @param {string} serviceTag
44
+ * Build the SNS Topic ARN for a given logical topic name.
46
45
  *
47
- * @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.
48
56
  */
49
- module.exports.snsTopicArn = async (_izContext, topicName, serviceTag = null) => {
50
- const snsTopicArn = await this.snsTopicName(_izContext, topicName, serviceTag)
51
- // 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
+
52
70
  // arn:aws:sns:us-east-1:468568093265:GraphHandlerDevInChangeRelationshipType
53
- return await `arn:aws:sns:${process.env.iz_region}:${process.env.iz_accountId}:${snsTopicArn}`
71
+ return `arn:aws:sns:${actualRegion}:${actualAccountId}:${topic}`;
54
72
  }
55
73
 
56
74
  /**
57
- * Generate sns topic name
58
- * @param {string} topicArn
75
+ * Extract the topic name from an SNS topic ARN.
59
76
  *
60
- * @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.
61
82
  */
62
- module.exports.extractTopicName = (topicArn) => {
63
- let topicName = topicArn.split(":")[5];
64
- return topicName
83
+ function extractTopicName(topicArn) {
84
+ if (typeof topicArn !== 'string') return null;
85
+ const parts = topicArn.split(':');
86
+ if (parts.length < 6) return null;
87
+ return parts[5] || null;
65
88
  }
66
89
 
67
90
  /**
@@ -70,12 +93,16 @@ module.exports.extractTopicName = (topicArn) => {
70
93
  * @param {string} topicName
71
94
  * @param {string} serviceTag
72
95
  */
73
- module.exports.snsTopicNameByFlowSchema = (_izContext, topicName, serviceTag = null) => {
74
- if (!serviceTag) { // for owner service
75
- return process.env.iz_serviceTag + "_" + process.env.iz_stage + "_" + topicName;
76
- } else { // create table name for external service
77
- return serviceTag + "_" + process.env.iz_stage + "_" + topicName;
78
- }
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}`;
79
106
  }
80
107
 
81
108
  /**
@@ -86,9 +113,33 @@ module.exports.snsTopicNameByFlowSchema = (_izContext, topicName, serviceTag = n
86
113
  *
87
114
  * @returns {string} deployed SNS resource name
88
115
  */
89
- module.exports.snsTopicArnByFlowSchema = async (_izContext, topicName, serviceTag = null) => {
90
- const snsTopicArn = await this.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
+
91
134
  // concatenate with env stage setting
92
135
  // arn:aws:sns:us-east-1:468568093265:GraphHandlerDevInChangeRelationshipType
93
- return await `arn:aws:sns:${process.env.iz_region}:${process.env.iz_accountId}:${snsTopicArn}`
94
- }
136
+ return `arn:aws:sns:${actualRegion}:${actualAccountId}:${snsTopicArn}`;
137
+ }
138
+
139
+ export default {
140
+ snsTopicName,
141
+ snsTopicArn,
142
+ extractTopicName,
143
+ snsTopicNameByFlowSchema,
144
+ snsTopicArnByFlowSchema
145
+ };