Skip to content

Commit

Permalink
Merge pull request #18 from afgallo/feature/sns-endpoint
Browse files Browse the repository at this point in the history
Add Endpoint Option for Local Development
  • Loading branch information
afgallo authored Jun 2, 2024
2 parents 3e78777 + 3ba8b49 commit fd7ac82
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
13 changes: 12 additions & 1 deletion lib/adapter/sns-adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,23 @@ module.exports = exports = class SNSAdapter {

constructor(options = {}) {
let credentials = null
let endpoint = null
options.region = options.region || 'us-east-1'

if (options.awsAccessKey && options.awsSecretKey) {
credentials = { accessKeyId: options.awsAccessKey, secretAccessKey: options.awsSecretKey }
}

this.#snsClient = options.snsClient || new SNSClient({ region: options.region || 'us-east-1', credentials })
if (options.endpoint) {
endpoint = options.endpoint // Only used in local development
}

this.#snsClient = options.snsClient || new SNSClient({ region: options.region, credentials, endpoint })
}

// Getter for snsClient - mostly for unit testing
get getSNSClient() {
return this.#snsClient
}

async publish(topicArn, message, options = {}) {
Expand Down
15 changes: 15 additions & 0 deletions test/sns-adapter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,19 @@ describe('SNSAdapter', () => {

consoleErrorStub.restore()
})

it('creates a new SNSAdapter with a custom endpoint', async () => {
const endpoint = 'http://localhost:4575' // LocalStack SNS endpoint for local testing
const customSNSAdapter = new SNSAdapter({ endpoint })

expect(customSNSAdapter).to.exist()
expect(customSNSAdapter).to.be.an.instanceof(SNSAdapter)

// Check that the SNSClient was created with the correct endpoint
const client = customSNSAdapter.getSNSClient
const actualEndpoint = await client.config.endpoint()

expect(actualEndpoint.hostname).to.equal('localhost')
expect(actualEndpoint.port).to.equal(4575)
})
})

0 comments on commit fd7ac82

Please sign in to comment.