💬 A tiny utility for creating AWS Lambda response objects.
This package provides an easy way for you set default headers for your lambda functions. Helps reduce clutter and make your output more consistent in large scale applications.
npm install aws-lambda-reply
When developing large applications you often work across multiple repositories. I found myself following the same pattern of creating a factory in each repository every time I wanted set standards for a lambda's output. When you also have to add tests for this boilerplate it can quickly get annoying - this package intends to solve this problem by creating a first class, best version of this generic method, that is easily accessible as a module.
Set up aws-lambda-reply with your defaults:
import { LambdaReply } from 'aws-lambda-reply';
const reply = new LambdaReply({
headers: {
'x-powered-by': 'nodejs',
}
});
And then use inside your function:
async function handler(event, context) {
return reply.make(200, '{"message": "Hello world!"}');
}
Your headers will automatically be added to the response object, along with any additional options you pass.
// ES6
import { LambdaReply } from 'aws-lambda-reply';
// commonJS
const LambdaReply = require('aws-lambda-reply').LambdaReply;
const reply = new LambdaReply(defaults);
This API assumes that you name the instance
reply
, but you can call it anything you like.
Type: object
Defaults for all replies made using this object.
headers
default to { 'Content-Type': 'application/json' }
.
multiValueHeaders
default to {}
.
{
headers?: { [key: string]: string | boolean | number },
multiValueHeaders?: { [key: string]: string[] }
}
reply.make(statusCode, body, options);
Type: number
HTTP status code
Type: string
The response body to return.
Type: object
Add additional headers or set the response as base64 encoded.
{
headers?: { [key: string]: string | boolean | number },
multiValueHeaders?: { [key: string]: Array<string | boolean | number> },
isBase64Encoded?: boolean,
}
Type: object
{
statusCode: number,
body: string,
headers?: { [key: string]: string | boolean | number },
multiValueHeaders?: { [key: string]: Array<string | boolean | number> },
isBase64Encoded?: boolean
}