3.1.0 • Published 5 years ago

lambda-wrap v3.1.0

Weekly downloads
14
License
MIT
Repository
github
Last release
5 years ago

LambdaWrap

Simple async function wrapper for AWS lambda and serverless library

  • allows using common middlewares (before, catch)
  • allows to set common error response format
  • supports generator functions using co (optional)
const { lambdaWrap } = require('lambda-wrap');

const wrap = lambdaWrap({
    headers: {
        'Access-Control-Allow-Origin': '*',
        'Access-Control-Allow-Credentials': 'true',
    },
    callbackWaitsForEmptyEventLoop: false, // usefull for mongodb
    verboseError: true // include error stack in response (possible to )
    verboseLog: true // include headers and body in error log
});

wrap.before((event) => {
    if (event.body && `${event.headers['content-type']}`.match(/^application\/json/)) {
        event.body = JSON.parse(event.body);
    }
});

// or you can set custom logger
wrap.logger = console;

wrap.finally((error, response) => {
    // close connections or send logs
});

module.exports.myHandler = wrap(async (event) => {

    // return json body
    return {
        body: {
            objectAttribute: true
        }
    };
})

API

Classes

Functions

lambdaWrap

Kind: global class

new lambdaWrap(globalOptions)

lambdaWrap function. You can pass options to override or assign new attributes to event object. For example add custom headers:

It returns an instance of LambdaWrap - wrap object. This object can be used for specifying additional properties:

Finally, wrap object can be used as a function to wrap any generator function and thus create lambda handler:

Returns: function - - the wrap function

ParamTypeDescription
globalOptionsLambdaWrapOptionsUse to override or assign new attributes

error(message, code)

Return new error object.

Kind: global function

ParamTypeDescription
messagestringError message.
codeintegerError code.
3.1.0

5 years ago

3.0.1

6 years ago

3.0.0

6 years ago

3.0.0-alpha.2

6 years ago

3.0.0-alpha.1

6 years ago

2.0.1

6 years ago

2.0.0

6 years ago

2.0.0-alpha.3

6 years ago

2.0.0-alpha.2

6 years ago

2.0.0-alpha.1

6 years ago

1.1.0

6 years ago

1.0.3

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago