turbo-remote-cache-construct
Version:
A Turborepo Remote Cache implementation using AWS API Gateway, Lambda, S3, and DynamoDB.
2 lines (1 loc) • 920 B
JavaScript
import{DynamoDBClient as c,PutItemCommand as m}from"@aws-sdk/client-dynamodb";import{marshall as f}from"@aws-sdk/util-dynamodb";var u=new c({}),l=process.env.EVENTS_TABLE_NAME,p=async(r,g)=>{if(!r.body)return{statusCode:400,body:JSON.stringify({message:"No body provided"})};let t=JSON.parse(r.body);if(!Array.isArray(t))return{statusCode:400,body:JSON.stringify({message:"Invalid request body, expected an array of events"})};try{for(let e of t){let{sessionId:o,source:s,hash:n,event:a,duration:d}=e,i=new Date().toISOString(),y={hash:n,sessionId:o,timestamp:i,source:s,event:a,duration:d,ttl:Math.floor(Date.now()/1e3)+30*24*60*60};await u.send(new m({TableName:l,Item:f(y)}))}return{statusCode:200,body:JSON.stringify({message:"Events recorded successfully"})}}catch(e){return console.error("Error recording events:",e),{statusCode:500,body:JSON.stringify({message:"Error recording events"})}}};export{p as handler};