import { S3 } from "aws-sdk";

import { initCommonMiddlewares } from "../middlewares";

interface IEvent {
  id: string;
}

interface IResult {
  content: string;
  id: string;
}

const { ASSETS_BUCKET } = process.env;

const s3 = new S3();

async function awsHandler(event: IEvent): Promise<IResult> {
  const { id } = event;

  const { Body } = await s3
    .getObject({
      Bucket: ASSETS_BUCKET,
      Key: id,
    })
    .promise();

  return { content: Body.toString(), id };
}

export const handler = initCommonMiddlewares(awsHandler);
