Skip to content

Instantly share code, notes, and snippets.

@wrumsby
Created September 10, 2019 01:39
Show Gist options
  • Save wrumsby/2bb7b6eb5d43c63a5eb066f989b853f8 to your computer and use it in GitHub Desktop.
Save wrumsby/2bb7b6eb5d43c63a5eb066f989b853f8 to your computer and use it in GitHub Desktop.
import 'reflect-metadata';
import AWS from 'aws-sdk';
const metadataKey = Symbol('snsAttribute');
function snsAttribute(isSnsAttribute: boolean = true) {
return Reflect.metadata(metadataKey, isSnsAttribute);
}
class Alex {
public x: string;
@snsAttribute()
public y: string;
}
const a = new Alex();
a.x = 'x';
a.y = 'why';
function hasSnsAttribute(target: any, propertyKey: string) {
return Reflect.getMetadata(metadataKey, target, propertyKey);
}
const attrs = Object.entries(a).reduce(
(previousValue: AWS.SNS.MessageAttributeMap, currentValue: [string, any]) => {
const [key, value] = currentValue;
if (hasSnsAttribute(a, key)) {
const attribute: AWS.SNS.MessageAttributeValue = {
DataType: 'String',
StringValue: value,
};
return Object.assign(previousValue, {
[key]: attribute,
});
}
return previousValue;
},
{} as AWS.SNS.MessageAttributeMap,
);
console.dir(attrs);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment