Noteworthy Programming Masterpiece
Have opinions about JavaScript? We want to hear them. Take the 2018 JavaScript Ecosystem Survey »

format-lambda-body

1.0.7 • Public • Published

format-lambda-body

Description

When using AWS Lambda functions with their Api Gateway service response bodies must contain specific properties, and some of them have to be written as JSON strings. I found this to be tedious when converting from more conventional api frameworks like Koa and Express, so I wrote a small helper package to handle the heavy lifting for me.

Installation

npm install format-lambda-body

Usage

First, import the package into your lambda function. Pass your status code and response into the function format in the callback of your lambda function:

import lambdaResponse from 'format-lambda-body';

exports.executeLambda = async (event, context, next) => {
  try {
    const body = { message: 'You've successfully logged in!' };    
    return next(null, lambdaResponse.format(200, body, true));
  } catch (err) {
    return next(null, lambdaResponse.format(401, body));
  }
};

Paramters

format(statusCode, body, cors)

statusCode (required)

A status code is required and must be a 'number' value.

body (required)

A body is required, but can be blank. The body must be a string, JSON string, object, or error object.

cors (optional)

Specifies if the response must include cors related headers. Must be a boolean value.

install

npm i format-lambda-body

Downloadsweekly downloads

8

version

1.0.7

license

MIT

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability