{"size": 1154, "ext": "js", "max_stars_repo_path": "index.js", "max_stars_repo_name": "telemark/azure-queue-add-message", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "index.js", "max_issues_repo_name": "telemark/azure-queue-add-message", "max_issues_repo_issues_event_min_datetime": "2021-08-25T04:45:45.000Z", "max_issues_repo_issues_event_max_datetime": "2021-08-25T04:45:45.000Z", "max_forks_repo_path": "index.js", "max_forks_repo_name": "vtfk/azure-queue-add-message", "max_forks_repo_forks_event_min_datetime": "2019-03-08T12:23:11.000Z", "max_forks_repo_forks_event_max_datetime": "2019-03-08T12:23:11.000Z"}
{"max_stars_count": null, "max_issues_count": 1, "max_forks_count": 1, "avg_line_length": 30.3684210526, "max_line_length": 89, "alphanum_fraction": 0.6386481802}
const azure = require('azure-sb')

module.exports = options => {
  if (!options) {
    throw Error('Missing required input: options')
  }
  if (!options.connectionString) {
    throw Error('Missing required input: options.connectionString')
  }
  if (!options.topicName && !options.queueName) {
    throw Error('Missing required input: missing options.topicName or options.queueName')
  }

  const sbService = azure.createServiceBusService(options.connectionString)

  return (messageBody) => {
    return new Promise((resolve, reject) => {
      if (!messageBody) {
        throw Error('Missing required input: messageBody')
      }
      const message = {
        ...options.settings,
        body: JSON.stringify(messageBody),
        contentType: 'application/json'
      }
      if (options.queueName) {
        sbService.sendQueueMessage(options.queueName, message, error => {
          return error ? reject(error) : resolve(message)
        })
      } else if (options.topicName) {
        sbService.sendTopicMessage(options.topicName, message, error => {
          return error ? reject(error) : resolve(message)
        })
      }
    })
  }
}
