Kernel to handle and provide structure to requests and responses for AWS Lambda and Serverless.
This module provides the necessary building blocks for you to build a structured Lambda function. Initially it is targeting Serverless v1, but easily used for vanilla Lambda.
npm i -S lambda-kernel
// handler.js (the lambda entrypoint)
const Kernel = require('lambda-kernel');
// controller is your own business logic
const controller = require('./lib/controller');
// instantiate the kernel, global configuration
// can be passed into the constructor here
const kernel = new Kernel();
module.exports = {
exampleAction: kernel.dispatch(controller.exampleAction)
};
// ./lib/controller.js (your business logic)
const Response = require('lambda-kernel/lib/Response');
module.exports = {
exampleAction: (req, env) => {
return new Response(`this is an example in ${env.stage}`, 200);
}
};
Middleware provides a clean and re-usable way to instantiate services needed for your actions. You can create middleware by extending the middleware class and passing your middleware instances in an array into the Kernel constructor to apply it globally or as a parameter of the dispatch method to apply it to that action only.
Middleware#create
is passed the same req
and env
parameters as controller methods.
The return value of the Middleware#create
method will then be provided to the controller method
in the order that the middleware was provided (after the req
, env
parameters).
Action-specific middleware is always provided in order after the Kernel middleware.
- ServerlessTransformer ⇐
Transformer
Transformer to handle Serverless v1's default Lambda integration event.
- Transformer
Abstract class that all transformers should extend and implement the interface as below
- DictAccessor
Simple Dictionary (Map) accessor for data.
- Kernel
The core class of Lambda-Kernel, use this at the root of your application to handle the request to response lifecycle.
- Middleware
Abstract class that your Middleware must extend and implement the interface as below.
- Request
Request object provided to your action by the dispatcher. You use this to access the event request data.
- Response
You must return a Response from your action.
ServerlessTransformer ⇐ Transformer
Transformer to handle Serverless v1's default Lambda integration event.
Kind: global class
Extends: Transformer
- ServerlessTransformer ⇐
Transformer
- .parseFromEvent(event) ⇒
RequestData
- .parsePath(event) ⇒
object
- .parseMethod(event) ⇒
object
- .parseHeaders(event) ⇒
object
- .parseQuery(event) ⇒
object
- .getRawBody(event) ⇒
string
- .parseFromEvent(event) ⇒
Kind: instance method of ServerlessTransformer
Param | Type | Description |
---|---|---|
event | object |
lambda integration event |
Kind: instance abstract method of ServerlessTransformer
Overrides: parsePath
Param | Type | Description |
---|---|---|
event | object |
lambda integration event |
Kind: instance abstract method of ServerlessTransformer
Overrides: parseMethod
Param | Type | Description |
---|---|---|
event | object |
lambda integration event |
Kind: instance abstract method of ServerlessTransformer
Overrides: parseHeaders
Param | Type | Description |
---|---|---|
event | object |
lambda integration event |
Kind: instance abstract method of ServerlessTransformer
Overrides: parseQuery
Param | Type | Description |
---|---|---|
event | object |
lambda integration event |
Return the raw body string from the event structure
Kind: instance abstract method of ServerlessTransformer
Overrides: getRawBody
Param | Type |
---|---|
event | object |
Abstract class that all transformers should extend and implement the interface as below
Kind: global abstract class
- Transformer
- new Transformer(bodyParser)
- .parseFromEvent(event) ⇒
RequestData
- .parsePath(event) ⇒
object
- .parseMethod(event) ⇒
object
- .parseHeaders(event) ⇒
object
- .parseQuery(event) ⇒
object
- .getRawBody(event) ⇒
string
Param | Type |
---|---|
bodyParser | function |
Kind: instance method of Transformer
Param | Type | Description |
---|---|---|
event | object |
lambda integration event |
Kind: instance abstract method of Transformer
Param | Type | Description |
---|---|---|
event | object |
lambda integration event |
Kind: instance abstract method of Transformer
Param | Type | Description |
---|---|---|
event | object |
lambda integration event |
Kind: instance abstract method of Transformer
Param | Type | Description |
---|---|---|
event | object |
lambda integration event |
Kind: instance abstract method of Transformer
Param | Type | Description |
---|---|---|
event | object |
lambda integration event |
Return the raw body string from the event structure
Kind: instance abstract method of Transformer
Param | Type |
---|---|
event | object |
Simple Dictionary (Map) accessor for data.
Kind: global class
- DictAccessor
- .has(key) ⇒
boolean
- .get(key, defaultVal) ⇒
*
- .all() ⇒
Object
- .has(key) ⇒
Check whether the dictionary has the provided key.
Kind: instance method of DictAccessor
Param | Type | Description |
---|---|---|
key | string |
dictionary key to check |
Ger the value for the provided key, or the provided default value if the key does not exist. Otherwise returns null.
Kind: instance method of DictAccessor
Param | Type | Description |
---|---|---|
key | string |
dictionary key to get |
defaultVal | default value if key doesn't exist |
Return a clone of this dictionary as a simple JavaScript Object.
Kind: instance method of DictAccessor
The core class of Lambda-Kernel, use this at the root of your application to handle the request to response lifecycle.
Kind: global class
Construct a new kernel with the provided options.
Param | Type |
---|---|
[options] | Object |
[options.transformer] | Transformer |
[options.middlewares] | Array.<Middleware> |
[options.forceEnd] | boolean |
Public dispatch method to interface with the Lambda integration layer.
Kind: instance method of Kernel
Returns: function
Access: public
Param | Type | Description |
---|---|---|
action | function |
function to invoke for the request |
[middleware] | Array.<Middleware> |
any additional middleware for this request |
Abstract class that your Middleware must extend and implement the interface as below.
Kind: global abstract class
The constructor is the opportune time to pass any config information which can be later used in the create() method.
Return the value which should be passed to the controller method.
Kind: instance abstract method of Middleware
Returns: any
Param | Type | Description |
---|---|---|
req | Request |
Request transformed from Lambda integration event |
env | Object |
Lambda context information |
Perform any cleanup required before the termination of the request.
Kind: instance abstract method of Middleware
Request object provided to your action by the dispatcher. You use this to access the event request data.
Kind: global class
- Request
- .path ⇒
string
- .method ⇒
string
- .query ⇒
DictAccessor
- .headers ⇒
DictAccessor
- .body ⇒
DictAccessor
- .path ⇒
The HTTP path requested
Kind: instance property of Request
The HTTP request method
Kind: instance property of Request
request.query ⇒ DictAccessor
The HTTP request path query params
Kind: instance property of Request
request.headers ⇒ DictAccessor
The HTTP request headers
Kind: instance property of Request
request.body ⇒ DictAccessor
The HTTP request body
Kind: instance property of Request
You must return a Response from your action.
Kind: global class
Construct a new response
Param | Type | Default | Description |
---|---|---|---|
body | object |
object to be serialized for HTTP body | |
[code] | number |
200 |
integer HTTP code |
[headers] | object |
key/value map of any HTTP headers |
Generate and return Lambda integration object for the response.
Kind: instance method of Response
© 2017 Matthew Webb