Skip to content

Commit

Permalink
Add support for message attributes (SamVerschueren#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
bartcallant authored and SamVerschueren committed Apr 24, 2019
1 parent 27b1cf2 commit f64c7da
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 0 deletions.
28 changes: 28 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,30 @@ const snsPublish = pify(sns.publish.bind(sns));

const isValidTopicName = input => /^[\w-]{1,255}$/.test(input);

const convertObjectToMessageAttributes = input => {
const result = {};

for (const key of Object.keys(input)) {
const value = input[key];
let parsedValue = `${value}`;
let dataType = 'String';

if (Array.isArray(value)) {
dataType = 'String.Array';
parsedValue = JSON.stringify(value);
} else if (typeof value === 'number') {
dataType = 'Number';
}

result[key] = {
DataType: dataType,
StringValue: parsedValue
};
}

return result;
};

module.exports = (message, opts) => {
opts = Object.assign({
region: process.env.AWS_REGION,
Expand Down Expand Up @@ -60,5 +84,9 @@ module.exports = (message, opts) => {
params.Subject = opts.subject;
}

if (opts.attributes) {
params.MessageAttributes = convertObjectToMessageAttributes(opts.attributes);
}

return snsPublish(params).then(data => data.MessageId);
};
10 changes: 10 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ snsPublish('SMS Message', {phone: '+14155552671'}).then(messageId => {
console.log(messageId);
//=> '6014fe16-26c1-11e7-93ae-92361f002671'
});

snsPublish('Hello World', {arn: 'arn:aws:sns:us-west-2:111122223333:MyTopic', messageAttributes: {hello: 'world'}}).then(messageId => {
console.log(messageId);
//=> 'ef5835d5-8a4b-4e8b-beff-6ccc314d2f6d'
});
```


Expand Down Expand Up @@ -101,6 +106,11 @@ Default: `process.env.AWS_ACCOUNT_ID`

AWS Account Id used when constructing the topic ARN when `name` is being used.

#### attributes

Type: `Object`

Key-value map defining the message attributes of the SNS message.

## License

Expand Down
90 changes: 90 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,93 @@ test.serial('phone, topic and subject', async t => {
Subject: 'MySubject'
});
});

test.serial('message attributes - string', async t => {
await m(JSON.stringify({foo: 'foo'}), {arn: 'arn:aws:sns:us-west-2:111122223333:MyTopic', json: true, attributes: {bar: 'bar'}});

t.deepEqual(sns.publish.lastCall.args[0], {
Message: '{"foo":"foo"}',
TopicArn: 'arn:aws:sns:us-west-2:111122223333:MyTopic',
MessageAttributes: {
bar: {
DataType: 'String',
StringValue: 'bar'
}
}
});
});

test.serial('message attributes - string array', async t => {
await m(JSON.stringify({foo: 'bar'}), {arn: 'arn:aws:sns:us-west-2:111122223333:MyTopic', json: true, attributes: {bar: ['bar']}});

t.deepEqual(sns.publish.lastCall.args[0], {
Message: '{"foo":"bar"}',
TopicArn: 'arn:aws:sns:us-west-2:111122223333:MyTopic',
MessageAttributes: {
bar: {
DataType: 'String.Array',
StringValue: '["bar"]'
}
}
});
});

test.serial('message attributes - number', async t => {
await m(JSON.stringify({foo: 'bar'}), {arn: 'arn:aws:sns:us-west-2:111122223333:MyTopic', json: true, attributes: {bar: 0}});

t.deepEqual(sns.publish.lastCall.args[0], {
Message: '{"foo":"bar"}',
TopicArn: 'arn:aws:sns:us-west-2:111122223333:MyTopic',
MessageAttributes: {
bar: {
DataType: 'Number',
StringValue: '0'
}
}
});
});

test.serial('message attributes - number array', async t => {
await m(JSON.stringify({foo: 'bar'}), {arn: 'arn:aws:sns:us-west-2:111122223333:MyTopic', json: true, attributes: {bar: [0]}});

t.deepEqual(sns.publish.lastCall.args[0], {
Message: '{"foo":"bar"}',
TopicArn: 'arn:aws:sns:us-west-2:111122223333:MyTopic',
MessageAttributes: {
bar: {
DataType: 'String.Array',
StringValue: '[0]'
}
}
});
});

test.serial('message attributes - boolean', async t => {
await m(JSON.stringify({foo: 'bar'}), {arn: 'arn:aws:sns:us-west-2:111122223333:MyTopic', json: true, attributes: {bar: true}});

t.deepEqual(sns.publish.lastCall.args[0], {
Message: '{"foo":"bar"}',
TopicArn: 'arn:aws:sns:us-west-2:111122223333:MyTopic',
MessageAttributes: {
bar: {
DataType: 'String',
StringValue: 'true'
}
}
});
});

test.serial('message attributes - boolean array', async t => {
await m(JSON.stringify({foo: 'bar'}), {arn: 'arn:aws:sns:us-west-2:111122223333:MyTopic', json: true, attributes: {bar: [true]}});

t.deepEqual(sns.publish.lastCall.args[0], {
Message: '{"foo":"bar"}',
TopicArn: 'arn:aws:sns:us-west-2:111122223333:MyTopic',
MessageAttributes: {
bar: {
DataType: 'String.Array',
StringValue: '[true]'
}
}
});
});

0 comments on commit f64c7da

Please sign in to comment.