npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

api-gateway-http-response

v1.0.0

Published

A small library that generates API Gateway HTTP responses for Lambda proxy integration (ie. for CloudFormation and AWS SAM)

Downloads

4

Readme

API Gateway HTTP Response for AWS Lambda

A small library that generates API Gateway HTTP responses for Lambda proxy integration. It can be used with AWS CloudFormation and AWS SAM.

Usage

This package exports a function that accepts four params and returns an object.

Function Params

Function accepts following four params:

  • Response body (optional) - A string or an object that should be returned as a HTTP response from the API Gateway. Default value is an empty string.

  • Status code (optional) - A number that represents HTTP status code. Default values are:

    • 204, if response body is an empty string.
    • 200, if response body is a string or an object.
    • 400, if response body is an instance of JavaScript Error.
  • Headers (optional) - An object with headers that will be passed as response headers. Default value is:

    {
        "Access-Control-Allow-Headers": "Content-Type,Authorization,X-Amz-Date,X-Api-Key,X-Amz-Security-Token",
    	"Access-Control-Allow-Methods": "OPTIONS,POST,GET,PUT,DELETE",
    	"Access-Control-Allow-Origin": "*",
    	"Access-Control-Max-Age": "86400"
    }
  • isBase64Encoded (optional) - true or false to enable or disable base64 encoding. Default value is false.

Output

The output of this function is an object in a valid Lambda function proxy integration format, described here. For example, a valud output for httpResponse('hello world') would be the following object:

{
    "isBase64Encoded": false,
    "statusCode": 200,
    "body": "hello world",
    "headers": {
   		"Access-Control-Allow-Headers": "Content-Type,Authorization,X-Amz-Date,X-Api-Key,X-Amz-Security-Token",
		"Access-Control-Allow-Methods": "OPTIONS,POST,GET,PUT,DELETE",
		"Access-Control-Allow-Origin": "*",
		"Access-Control-Max-Age": "86400"
	}
}

Example usage

A common usage would be inside an AWS Lambda function, similar to this:

'use strict'

const httpResponse = require('api-gateway-http-response')
const parseApiEvent = require('./parse-event') // A function that parses an event
const businessLogic = require('./business-logic') // A function that handles a logic for your Lambda function

async function lambda(event) {
  const request = parseApiEvent(event)
  try {
    const body = await businessLogic(request)
    return httpResponse(body)
  } catch(err) {
    return httpResponse(err)
  }
}

exports.handler = lambda

CORS support

By default, this function will return headers that supports CORS from any origin. Supported HTTP methods are: OPTIONS, POST, GET, PUT and DELETE.

Motivation

I wrote similar thing many times, packing it into a small independent package is easier that searching through other projects.

License

MIT -- see LICENSE