A small lambda wrapper that lets you write cleaner and maintainable lambda function
Version 3.x focuses on removing stuffs that does not make sense and providing a little bit "lambda"-ish familiarity.
You can start by installing this library using the command below:
npm i --save @serverless-guy/lambda
In the example below, the handler would log the event
first, then context
. Afterwards, it will return the event
as response.
import { wrapper } from "@serverless-guy/lambda";
export const handler = wrapper(function someHandler(event, context, response) {
console.log(event);
console.log(context);
return response(event);
});
import { wrapper } from "@serverless-guy/lambda";
export const handler = wrapper(function someHandler(event, context, response) {
console.log(event);
console.log(context);
return response(event);
});
handler.setResponseFunction(customResponseTemplate);
function customResponseTemplate(data, statusCode = 200, headers = {}) {
// do something
data.returnedOn = new Date();
return {
body: JSON.stringify(data),
headers: {
"Access-Control-Allow-Origin": "*",
...headers
},
statusCode
};
}
import { wrapper } from "@serverless-guy/lambda";
export const handler = wrapper(function someHandler(event, context, response) {
console.log(event);
console.log(context);
return response(event);
});
handler.setCatchFunction(customCatchResponseTemplate);
function customCatchResponseTemplate(error, event, context, responseFunction) {
const errorResponseObject = {
errorCode: error.name,
errorMessage: error.message
};
return response(errorResponseObject, 418); /** I'm a f***ing teapot */
}
Check out our documentation page to see more examples.