lambda-proxy-response
A wrap around response for aws lambda integration proxy
Installation
npm install lambda-proxy-response --save
Response creation
- Response can be created using
response
method
const lambdaProxy = require('lambda-proxy-reponse');
exports.handler = (event, context, callback) => {
const response = lambdaProxy.response(200, { 'X-header': 'Your headers value' }, { 'bodyData': 'yourBodyData' });
callback(null, response);
lambdaProxy.response(200, { 'X-header': 'Your headers value' }, { 'bodyData': 'yourBodyData' }, callback);
};
- Some others shorthand method, useful for common http status
lambdaProxy.ok = lambdaProxy.response(200, ...)
lambdaProxy.created = lambdaProxy.response(201, ...)
lambdaProxy.badRequest = lambdaProxy.response(400, ...)
lambdaProxy.unAuthorized = lambdaProxy.response(401, ...)
lambdaProxy.forbidden = lambdaProxy.response(403, ...)
lambdaProxy.notFound = lambdaProxy.response(404, ...)
lambdaProxy.serverError = lambdaProxy.response(500, ...)
Options
From version 2.0, new config option has been introduced to set default values for response.
body
Default body for every response. Will be overriden if you support new body in the call
lambdaProxy.config({ body: { bodyData: 'yourDefaultBodyData' } });
let response = lambdaProxy.response(200, { 'X-header': 'Your headers value' });
response = lambdaProxy.response(200, { 'X-header': 'Your headers value' }, { 'bodyData': 'yourBodyData' });
Default headers for every response. By default, it will extend whatever headers you provide inside
your response. Can be turned off by setting extendHeader
in options.
lambdaProxy.config({ header: { 'X-Default-Header': 'Your default header' } });
let response = lambdaProxy.response(200, null, { bodyData: 'yourBodyData' });
response = lambdaProxy.response(200, { 'X-header': 'Your headers value' }, { 'bodyData': 'yourBodyData' });
lambdaProxy.config({ header: { 'X-Default-Header': 'Your default header' }, extendHeader: false });
response = lambdaProxy.response(200, { 'X-header': 'Your headers value' }, { 'bodyData': 'yourBodyData' });
status
Default status code.
lambdaProxy.config({ status: 200 });
response = lambdaProxy.response();
Note
This response template is only for used with newly aws lambda proxy integration.
You can read more about it here