@faceteer/cdk
Version:
CDK 2.0 constructs and helpers that make composing a Lambda powered service easier.
46 lines (45 loc) • 1.55 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.NotificationHandler = NotificationHandler;
const handler_1 = require("./handler");
function defaultValidator() {
return undefined;
}
function NotificationHandler(options, handler) {
const { validator = defaultValidator, ...definition } = options;
const wrappedHandler = async (event, context, callback) => {
const notificationEvent = {
Records: event.Records,
InvalidMessages: [],
ValidMessages: [],
};
try {
for (const record of event.Records) {
try {
const validRecord = validator(JSON.parse(record.Sns.Message));
notificationEvent.ValidMessages.push({
attempts: 0,
body: validRecord,
messageId: record.Sns.MessageId,
});
}
catch (error) {
notificationEvent.InvalidMessages.push({
attempts: 0,
body: record.Sns.Message,
error,
messageId: record.Sns.MessageId,
});
}
}
await handler(notificationEvent, context);
}
catch (error) {
callback(error);
}
};
return Object.assign(wrappedHandler, {
type: handler_1.HandlerTypes.Notification,
definition: definition,
});
}