@ikonintegration/ikapi 5.0.8 → 5.0.10

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ikonintegration/ikapi",
3
- "version": "5.0.8",
3
+ "version": "5.0.10",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -20,6 +20,7 @@
20
20
  "license": "ISC",
21
21
  "dependencies": {
22
22
  "@aws-sdk/client-dynamodb": "^3.650.0",
23
+ "@aws-sdk/client-eventbridge": "^3.916.0",
23
24
  "@aws-sdk/client-kms": "^3.650.0",
24
25
  "@aws-sdk/client-secrets-manager": "^3.650.0",
25
26
  "@aws-sdk/client-ses": "^3.650.0",
@@ -35,7 +36,7 @@
35
36
  "cors": "^2.8.5",
36
37
  "cuid": "^3.0.0",
37
38
  "dotenv": "^16.4.1",
38
- "email-templates": "^12.0.1",
39
+ "email-templates": "^12.0.3",
39
40
  "express": "^4.21.1",
40
41
  "json-stringify-safe": "^5.0.1",
41
42
  "kysely": "^0.27.2",
@@ -1,3 +1,9 @@
1
+ import {
2
+ EventBridgeClient,
3
+ PutEventsCommand,
4
+ PutEventsCommandInput,
5
+ PutEventsCommandOutput,
6
+ } from '@aws-sdk/client-eventbridge'
1
7
  import {
2
8
  SNSClient,
3
9
  PublishCommand,
@@ -5,7 +11,7 @@ import {
5
11
  PublishCommandInput,
6
12
  } from '@aws-sdk/client-sns'
7
13
  import sha1 from 'sha1'
8
- //reusable client
14
+ //reusable client - sns
9
15
  /**
10
16
  * A variable that holds the connection to the SNS client for publishing messages.
11
17
  * @type {SNSClient | null}
@@ -19,6 +25,20 @@ let PUBLISHER_CONN: SNSClient | null = null
19
25
  // eslint-disable-next-line no-var
20
26
  let PUBLISHER_CONN_HASH: string | null = null
21
27
 
28
+ //reusable client - event bridge
29
+ /**
30
+ * A variable that holds the connection to the EventBridge client for publishing events.
31
+ * @type {EventBridgeClient | null}
32
+ */
33
+ // eslint-disable-next-line no-var
34
+ let EVENT_BRIDGE_CONN: EventBridgeClient | null = null
35
+ /**
36
+ * The connection hash for the event bridge publisher. It is initially set to null.
37
+ * @type {string | null}
38
+ */
39
+ // eslint-disable-next-line no-var
40
+ let EVENT_BRIDGE_CONN_HASH: string | null = null
41
+
22
42
  /**
23
43
  * Represents the configuration options for a publisher.
24
44
  * @typedef {Object} PublisherConfig
@@ -93,4 +113,57 @@ export default class Publisher {
93
113
  PUBLISHER_CONN_HASH = sha1(this.region).toString()
94
114
  }
95
115
  }
116
+
117
+ /**
118
+ * Publishes a message on EventBridge.
119
+ * @param {any} messageObject - The message object to be published.
120
+ * @param {string} eventBusName - The name of the event bus to publish to.
121
+ * @param {string} source - The source of the event.
122
+ * @param {string} detailType - The detail-type of the event.
123
+ * @returns {Promise<PutEventsCommandOutput>} - A promise that resolves to the response from the publish command.
124
+ */
125
+ public async publishOnEventBridge(
126
+ messageObject: any,
127
+ eventBusName: string,
128
+ source: string,
129
+ detailType: string
130
+ ): Promise<PutEventsCommandOutput | undefined> {
131
+ let resp: undefined | PutEventsCommandOutput = undefined
132
+ try {
133
+ this.connectEventBridge()
134
+ const params: PutEventsCommandInput = {
135
+ Entries: [
136
+ {
137
+ Source: source,
138
+ EventBusName: eventBusName,
139
+ DetailType: detailType,
140
+ Detail: JSON.stringify(messageObject),
141
+ },
142
+ ],
143
+ }
144
+ resp = await EVENT_BRIDGE_CONN!.send(new PutEventsCommand(params))
145
+ } catch (e) {
146
+ console.error(
147
+ `Error while publishing into event bus ${eventBusName} - ${source} - ${detailType} with error: ${e}`
148
+ )
149
+ }
150
+ console.debug('Publisher event resp', resp)
151
+ return resp
152
+ }
153
+
154
+ /**
155
+ * Establishes a connection to the EventBridge client if it does not already exist or if the region has changed.
156
+ * @returns None
157
+ */
158
+ private connectEventBridge(): void {
159
+ if (
160
+ (!EVENT_BRIDGE_CONN && !EVENT_BRIDGE_CONN_HASH) ||
161
+ EVENT_BRIDGE_CONN_HASH != sha1(this.region)
162
+ ) {
163
+ EVENT_BRIDGE_CONN = new EventBridgeClient({
164
+ region: this.region,
165
+ })
166
+ EVENT_BRIDGE_CONN_HASH = sha1(this.region).toString()
167
+ }
168
+ }
96
169
  }