@aws-lambda/http

1.0.1 • Public • Published

npm version build status Coverage Status

@aws-lambda/http

http is a utility for simplifying response handling when using aws lambda functions with the http event proxy.

Table of Contents

  1. Installation and Usage
  2. Examples

Installation and Usage

Prerequisites: Node.js >=8, npm version 6+.

You can install the http module using npm:

$ npm install @aws-lambda/http --save

Response Examples

AWS Lambda handler response with JSON body and header

const Response = require("@aws-lambda/http").Response;

module.exports.hello = async event => {
  return new Response()
                .status(200)
                .body({"message" : "hello world!"})
                .header("x-api-version", "1.0")
                .json();
};

Adding HTTP security headers to response (alternative to Lambda@Edge + CloudFront)

const HTTP = require("@aws-lambda/http");
const Response = HTTP.Response;

module.exports.hello = async event => {
  return new Response()
                .status(200)
                .body({"message" : "hello world!"})
                .header("x-api-version", "1.0")
                .headers(HTTP.SecurityHeaders)
                .json();
};

Adding cookie to response

const HTTP = require("@aws-lambda/http");
const Response = HTTP.Response;
const Cookie = HTTP.Cookie;

module.exports.hello = async event => {
  return new Response()
                .status(200)
                .body({"message" : "hello world!"})
                .header("x-api-version", "1.0")
                .headers(HTTP.SecurityHeaders)
                .cookie(new Cookie("SSID", "ubmHxGlgi71l8ayOMw9f6wknjGv2FwDKLt")))
                .json();
};

Adding secure cookie to response

const HTTP = require("@aws-lambda/http");
const Response = HTTP.Response;
const Cookie = HTTP.Cookie;

module.exports.hello = async event => {
  return new Response()
                .status(200)
                .body({"message" : "hello world!"})
                .header("x-api-version", "1.0")
                .headers(HTTP.SecurityHeaders)
                .cookie(Cookie.Secure("SSID", "ubmHxGlgi71l8ayOMw9f6wknjGv2FwDKLt"))
                .json();
};

Adding multiple cookies to response

const HTTP = require("@aws-lambda/http");
const Response = HTTP.Response;
const Cookie = HTTP.Cookie;

module.exports.hello = async event => {
  return new Response()
                .status(200)
                .body({"message" : "hello world!"})
                .header("x-api-version", "1.0")
                .headers(HTTP.SecurityHeaders)
                .cookies([
                  Cookie.Secure("SSID", "ubmHxGlgi71l8ayOMw9f6wknjGv2FwDKLt"),
                  Cookie.Secure("secret", "shhhhh").expireInMinutes(15),
                  new Cookie("lang", "en").path("/docs")
                ])
                .json();
};

Package Sidebar

Install

npm i @aws-lambda/http

Weekly Downloads

0

Version

1.0.1

License

MIT

Unpacked Size

10.6 kB

Total Files

8

Last publish

Collaborators

  • bravecloud