import { SQS } from 'aws-sdk' ; import { buildSendMessageRequestForQueue } from '../sqs-utils' ; export class SQSPublisher { sqs : SQS ; queueUrl : string | undefined ; constructor ( queueUrl = undefined ) { this . sqs = new SQS ( { apiVersion : '2012-11-05' , region : process . env . AWS_REGION , } ) ; this . queueUrl = queueUrl ; } async publish ( message ) { if ( this . queueUrl !== undefined ) { const messageRequest = buildSendMessageRequestForQueue ( message , this . queueUrl ) ; const { QueueUrl , ... params } = messageRequest ; try { console . log ( 'queueing to' , QueueUrl , params ) ; await this . sqs . sendMessage ( messageRequest ) . promise ( ) ; } catch ( e ) { console . error ( { e } ) ; } } } } let sqsPublisher ; export class SQSPublisherFactory { public static getInstance ( queueUrl ) { if ( ! sqsPublisher ) { sqsPublisher = new SQSPublisher ( queueUrl ) ; } return sqsPublisher ; } }