import { SNSClient, PublishCommand, PublishCommandOutput, PublishCommandInput, } from '@aws-sdk/client-sns' import sha1 from 'sha1' //reusable client /** * A variable that holds the connection to the SNS client for publishing messages. * @type {SNSClient | null} */ // eslint-disable-next-line no-var let PUBLISHER_CONN: SNSClient | null = null /** * The connection hash for the publisher. It is initially set to null. * @type {string | null} */ // eslint-disable-next-line no-var let PUBLISHER_CONN_HASH: string | null = null /** * Represents the configuration options for a publisher. * @typedef {Object} PublisherConfig * @property {string} [region] - The region where the publisher is located. */ export type PublisherConfig = { region?: string } /** * Represents a publisher that can publish messages to an SNS topic. */ export default class Publisher { /** * The region of the object or entity. * @private * @type {string} */ private region: string | undefined /** * Constructs a new instance of the Publisher class. * @param {PublisherConfig} [config] - The configuration object for the Publisher. * @returns None */ constructor(config?: PublisherConfig) { if (config && config.region) { this.region = config.region // console.debug(`Using region: ${this.region}`); } } /** * Publishes a message on a specified topic. * @param {any} messageObject - The message object to be published. * @param {string} topic - The ARN of the topic to publish the message to. * @param {object} [additionalProps] - Additional properties to include in the publish command. * @returns {Promise} - A promise that resolves to the response from the publish command. */ public async publishOnTopic( messageObject: any, topic: string, additionalProps?: object ): Promise { let resp: undefined | PublishCommandOutput = undefined try { this.connect() //Send to SNS const params: PublishCommandInput = { Message: JSON.stringify(messageObject), TopicArn: topic, ...(additionalProps ? additionalProps : {}), } resp = await PUBLISHER_CONN!.send(new PublishCommand(params)) } catch (e) { console.error(`Error while publishing into topic ${topic} with error: ${e}`) } console.debug('Publisher resp', resp) return resp } /** * Publishes a message on a specified topic. * @param {string} message - The SMS message to publish. * @param {string} phone - The phone number to publish the message to. * @returns {Promise} - A promise that resolves to the response from the publish command. */ public async publishSMS( message: string, phone: string ): Promise { let resp: undefined | PublishCommandOutput = undefined try { this.connect() //Send to SNS const params: PublishCommandInput = { Message: message, PhoneNumber: phone, } resp = await PUBLISHER_CONN!.send(new PublishCommand(params)) } catch (e) { console.error(`Error while publishing SMS ${message} with error: ${e}`) } console.debug('Publisher resp', resp) return resp } /** * Establishes a connection to the SNS client if it does not already exist or if the region has changed. * @returns None */ private connect(): void { if ((!PUBLISHER_CONN && !PUBLISHER_CONN_HASH) || PUBLISHER_CONN_HASH != sha1(this.region)) { PUBLISHER_CONN = new SNSClient({ apiVersion: '2010-03-31', region: this.region, }) PUBLISHER_CONN_HASH = sha1(this.region).toString() } } }