A simple package for building standard responses in AWS Lambda callback functions to be handled by API Gateway.
This package was written before Lambda Proxy Integrations existed. If you are looking for a package to handle lambda proxy responses, check out lambda-proxy-response
$ npm install --save aws-lambda-response
API Gateway does not support objects as the error parameter in callbacks from Lambda, this package solves this by stringifying error and failure responses and assumes the JSON is parsed at API Gateway level.
import resp from 'aws-lambda-response';
function handler(event, context, callback) {
callback(null, resp.success(200, { hello: "World"}));
}
export { handler };
The following API is based on the imported module being named resp
, however it can be substituted for any name you want.
// ES6 modules
import resp from 'aws-lambda-response';
// commonJS modules
const resp = require('aws-lambda-response');
resp.success(statusCode, data)
Type: int
HTTP status code to be mapped in the API Response header
Type: object
Response payload.
Type: object
{
status: "success",
httpStatus: (int)statusCode,
data: (obj)data
}
resp.error(statusCode, message, data)
Type: int
HTTP status code to be mapped in the API Response header
Type: string
Error message
Type: object
Any additional response data.
Type: string
Stringified object to be parsed in API Gateway output mapping.
{
"status": "error",
"httpStatus": "(int)statusCode",
"message": "(string)message",
"data": "(object)data"
}
resp.fail(statusCode, data)
Type: int
HTTP status code to be mapped in the API Response header
Type: object
Response data that may help explain the issue. Can be be a string if the information is better presented as one.
Type: string
Stringified object to be parsed in API Gateway output mapping.
{
"status": "fail",
"httpStatus": "(int)statusCode",
"data": "(object)data"
}
MIT