@alpha-lambda/handler
v1.3.0
Published
Middleware pipeline for AWS Lambda handlers
Downloads
2,027
Readme
@alpha-lambda/handler
Tiny wrapper that ensures that AWS Lambda function's callback is always called. In other words, from your handler you can return value, promise, throw exception, and this library will wrap your code into a promise while calling appropriate lambda-required callback. Your handler is composed of middleware, similar to Express or Koa.
Installation
$ npm install @alpha-lambda/handler
Usage
const handler = require('@alpha-lambda/handler');
const pino = require('pino');
module.exports.handler = handler()
.use(async (event, context, next) => {
// this is some middleware
// this will forward to next function in the chain
await next();
// you can do things, like logging, after the fact
// you can also override the return value
})
.with({
log: pino() // this will be accessible as context.log
})
.use((event, context) => {
context.log.info({ event }, 'incoming event');
return 'Hello, world!';
});
.use(middleware)
Adds middleware of the function(event, context, next) {}
form to the chain.
.with(extensions)
Adds extensions to the context. Extensions are grouped into sections. If context has a section already, new keys will be merged in, but will not override existing ones.
next()
The next()
function takes up to three arguments: (err, context, event)
.
If you specify err
, then execution is rejected with it. You can also (optionally) specify context/event to override those values for subsequent middleware.
Examples:
next(); // no err, no override for context/event
next(new Error('failure!')); // rejected with Error
next(null, {}); // context is overriden
next(null, null, {}); // event is overriden
next(null, {}, {}); // both context and event are overriden
Middleware
Use these middleware to extend functionality.
| Middleware | Author | |:-------|:------:| | Bunyan Logger Bunyan logger middleware for alpha-lambda | Anton Bazhal |
License
The MIT License (MIT)
Copyright (c) 2016-2019 Anton Bazhal
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.