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

awslambdaproxyresponse

v1.1.3

Published

Generates response payloads for AWS Lambda functions behind API Gateway 'Lambda proxy' integrations.

Downloads

225

Readme

AWS Lambda proxy response

A Node.js module which generates response payloads for API Gateway fronted Lambda functions integrated via the Lambda proxy method.

NPM

The response structure takes the following form:

{
	statusCode: httpStatusCode,
	headers: { headerName: 'headerValue' },
	body: '...'
}

Methods

AWSLambdaProxyResponse([statusCode])

  • Creates new AWSLambdaProxyResponse instance.
  • Optional statusCode sets the HTTP status code for the response, otherwise defaults to 200 / OK.
  • Collection of valid HTTP codes defined at AWSLambdaProxyResponse.HTTP_STATUS.
  • Constructor will throw an exception if given statusCode is not within this collection.

Example:

const AWSLambdaProxyResponse = require('awslambdaproxyresponse');

const resp = new AWSLambdaProxyResponse(
	AWSLambdaProxyResponse.HTTP_STATUS.FOUND
);

AWSLambdaProxyResponse.setStatusCode(statusCode)

  • Sets the HTTP statusCode for a response.
  • Throws an exception if given statusCode is not within the AWSLambdaProxyResponse.HTTP_STATUS collection.
  • Returns AWSLambdaProxyResponse instance.

AWSLambdaProxyResponse.addHeader(name[,value])

  • Adds HTTP headers to the Lambda proxy response.
  • Single HTTP header can be added by providing a name / value pair.
  • Multiple headers can be added by providing an object collection as name only.
  • Throws an exception if header names don't match the regular expression pattern /^[A-Za-z-]+$/.
  • Returns AWSLambdaProxyResponse instance.

Example:

const AWSLambdaProxyResponse = require('awslambdaproxyresponse');
const resp = new AWSLambdaProxyResponse();

// lets add a single header
resp.addHeader('Content-Type','text/html');

// add several others
resp.addHeader({
	'x-custom-header': 'value',
	'x-user-auth': 'Donald Duck'
});

AWSLambdaProxyResponse.setBody(body)

  • Sets the response body payload.
  • If body is not of type string, will be automatically serialized via JSON.stringify().
  • Returns AWSLambdaProxyResponse instance.

AWSLambdaProxyResponse.getPayload()

Returns a valid Lambda proxy response structure object.

Constants

AWSLambdaProxyResponse.HTTP_STATUS

A collection of valid HTTP status codes for use with the AWSLambdaProxyResponse() constructor or setStatusCode(statusCode) method:

const AWSLambdaProxyResponse = require('awslambdaproxyresponse');
console.dir(AWSLambdaProxyResponse.HTTP_STATUS);

/*
{
	OK: 200,
	MOVED: 301,
	FOUND: 302,
	BAD_REQUEST: 400,
	UNAUTHORIZED: 401,
	FORBIDDEN: 403,
	NOT_FOUND: 404,
	SERVER_ERROR: 500,
	NOT_IMPLEMENTED: 501,
	BAD_GATEWAY: 502,
	SERVICE_UNAVAILABLE: 503,
	GATEWAY_TIMEOUT: 504
}
*/

Example usage

Within the context of a Lambda function:

const AWSLambdaProxyResponse = require('awslambdaproxyresponse');

exports.myHandler = (event,context,callback) => {

	// create our response
	const resp = new AWSLambdaProxyResponse();
	resp.setBody('Hello world');

	// return from Lambda
	callback(null,resp.getPayload());

	/*
	console.dir(resp.getPayload());
	{
		statusCode: 200,
		headers: {},
		body: 'Hello world'
	}
	*/
};

A Lambda response that results in a redirect:

const AWSLambdaProxyResponse = require('awslambdaproxyresponse');

exports.myHandler = (event,context,callback) => {

	// create our response
	const resp = new AWSLambdaProxyResponse();

	resp.setStatusCode(AWSLambdaProxyResponse.HTTP_STATUS.MOVED);
	resp.addHeader('Location','https://my.new.domain.com/');

	// return from Lambda
	callback(null,resp.getPayload());

	/*
	console.dir(resp.getPayload());
	{
		statusCode: 301,
		headers: { Location: 'https://my.new.domain.com/' },
		body: ''
	}
	*/
};

Reference

  • http://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-handler.html
  • http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-set-up-simple-proxy.html#api-gateway-simple-proxy-for-lambda-output-format