import { SNS } from "aws-sdk";
import * as middy from "middy";

interface ISNSMiddlewareOptions {
  topic: string;
}

const sns = new SNS();

export const DispatchSnsAfter = (options: ISNSMiddlewareOptions) => {
  return {
    after: async (handler) => {
      const { response } = handler;
      await sns
        .publish({
          TopicArn: options.topic,
          Message: JSON.stringify(response),
        })
        .promise();
    },
  } as middy.IMiddyMiddlewareObject;
};
