Version: 0.1.2
Last Updated: Unknown
Use this tempalate to A starter for node apigw
TODO: Update this section
| Variable | Data Type | Info |
|---|---|---|
type | string | The name of the Lift folder type |
pkg | string | The name of the package |
1import { Handler } from "aws-lambda"23const handleError = (err) => {4 const body = err.stack || JSON.stringify(err, null, 2)5 console.error(body)67 return {8 statusCode: 500,9 body: "Internal server error",10 }11}1213const handleGet = async (event, _context) => {14 try {15 console.log(event)1617 return {18 statusCode: 200,19 headers: {20 "content-type": "application/json",21 },22 body: JSON.stringify({23 message: "Hello, world!",24 }),25 }26 } catch (err) {27 return handleError(err)28 }29}3031const handlePost = async (event, _context) => {32 try {33 const { email } = event.body3435 if (!email) {36 return {37 statusCode: 406,38 body: "Missing required parameters",39 }40 }4142 // do something43 const res = {}4445 return {46 statusCode: 200,47 headers: {48 "content-type": "application/json",49 },50 body: JSON.stringify(res),51 }52 } catch (err) {53 return handleError(err)54 }55}5657const handler: Handler = async (event, context) => {58 try {59 switch (event.httpMethod) {60 case "GET":61 return await handleGet(event, context)62 case "POST":63 return await handlePost(event, context)64 default:65 return {66 statusCode: 405,67 body: "Not allowed",68 }69 }70 } catch (error) {71 var body = error.stack || JSON.stringify(error, null, 2)72 console.error(body)7374 return {75 statusCode: 500,76 headers: {},77 }78 }79}8081exports.handler = handler
1const lambdaLocal = require("lambda-local")2const { resolve } = require("path")34const jsonPayload = {5 body: {6 listId: "1uVqnhCYiWDCc9kD4z0eSO6G4Ei",7 email: "any@test.com",8 },9 httpMethod: "POST",10}1112async function main() {13 try {14 const res = await lambdaLocal.execute({15 event: jsonPayload,16 lambdaPath: resolve(__dirname, "dist/index.js"),17 timeoutMs: 15000,18 environment: process.env,19 })20 console.log(res)21 } catch (err) {22 console.log(err)23 }24}2526main()