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:
-
isBase64Encoded (optional) -
true
orfalse
to enable or disable base64 encoding. Default value isfalse
.
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:
Example usage
A common usage would be inside an AWS Lambda function, similar to this:
'use strict' const httpResponse = const parseApiEvent = // A function that parses an eventconst businessLogic = // A function that handles a logic for your Lambda function { const request = try const body = await return catcherr return } exportshandler = 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