Lambda Kernel
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.
Install
npm i -S lambda-kernel
Basic Usage
// handler.js (the lambda entrypoint) const Kernel = ;// controller is your own business logicconst controller = ; // instantiate the kernel, global configuration// can be passed into the constructor hereconst kernel = ; moduleexports = exampleAction: kernel;
// ./lib/controller.js (your business logic) const Response = ; moduleexports = { return `this is an example in ` 200; };
Middleware
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.
Classes
- 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.
Transformer
ServerlessTransformer ⇐ 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) ⇒
RequestData
serverlessTransformer.parseFromEvent(event) ⇒ Kind: instance method of ServerlessTransformer
Param | Type | Description |
---|---|---|
event | object |
lambda integration event |
object
serverlessTransformer.parsePath(event) ⇒ Kind: instance abstract method of ServerlessTransformer
Overrides: parsePath
Param | Type | Description |
---|---|---|
event | object |
lambda integration event |
object
serverlessTransformer.parseMethod(event) ⇒ Kind: instance abstract method of ServerlessTransformer
Overrides: parseMethod
Param | Type | Description |
---|---|---|
event | object |
lambda integration event |
object
serverlessTransformer.parseHeaders(event) ⇒ Kind: instance abstract method of ServerlessTransformer
Overrides: parseHeaders
Param | Type | Description |
---|---|---|
event | object |
lambda integration event |
object
serverlessTransformer.parseQuery(event) ⇒ Kind: instance abstract method of ServerlessTransformer
Overrides: parseQuery
Param | Type | Description |
---|---|---|
event | object |
lambda integration event |
string
serverlessTransformer.getRawBody(event) ⇒ Return the raw body string from the event structure
Kind: instance abstract method of ServerlessTransformer
Overrides: getRawBody
Param | Type |
---|---|
event | object |
Transformer
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
new Transformer(bodyParser)
Param | Type |
---|---|
bodyParser | function |
RequestData
transformer.parseFromEvent(event) ⇒ Kind: instance method of Transformer
Param | Type | Description |
---|---|---|
event | object |
lambda integration event |
object
transformer.parsePath(event) ⇒ Kind: instance abstract method of Transformer
Param | Type | Description |
---|---|---|
event | object |
lambda integration event |
object
transformer.parseMethod(event) ⇒ Kind: instance abstract method of Transformer
Param | Type | Description |
---|---|---|
event | object |
lambda integration event |
object
transformer.parseHeaders(event) ⇒ Kind: instance abstract method of Transformer
Param | Type | Description |
---|---|---|
event | object |
lambda integration event |
object
transformer.parseQuery(event) ⇒ Kind: instance abstract method of Transformer
Param | Type | Description |
---|---|---|
event | object |
lambda integration event |
string
transformer.getRawBody(event) ⇒ Return the raw body string from the event structure
Kind: instance abstract method of Transformer
Param | Type |
---|---|
event | object |
DictAccessor
Simple Dictionary (Map) accessor for data.
Kind: global class
- DictAccessor
- .has(key) ⇒
boolean
- .get(key, defaultVal) ⇒
*
- .all() ⇒
Object
- .has(key) ⇒
boolean
dictAccessor.has(key) ⇒ Check whether the dictionary has the provided key.
Kind: instance method of DictAccessor
Param | Type | Description |
---|---|---|
key | string |
dictionary key to check |
*
dictAccessor.get(key, defaultVal) ⇒ 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 |
Object
dictAccessor.all() ⇒ Return a clone of this dictionary as a simple JavaScript Object.
Kind: instance method of DictAccessor
Kernel
The core class of Lambda-Kernel, use this at the root of your application to handle the request to response lifecycle.
Kind: global class
new Kernel([options])
Construct a new kernel with the provided options.
Param | Type |
---|---|
[options] | Object |
[options.transformer] | Transformer |
[options.middlewares] | Array.<Middleware> |
[options.forceEnd] | boolean |
kernel.dispatch(action, [middleware]) ⇒
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 |
Middleware
Abstract class that your Middleware must extend and implement the interface as below.
Kind: global abstract class
new Middleware()
The constructor is the opportune time to pass any config information which can be later used in the create() method.
middleware.create(req, env) ⇒
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 |
middleware.destroy()
Perform any cleanup required before the termination of the request.
Kind: instance abstract method of Middleware
Request
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 ⇒
string
request.path ⇒ The HTTP path requested
Kind: instance property of Request
string
request.method ⇒ The HTTP request method
Kind: instance property of Request
DictAccessor
request.query ⇒ The HTTP request path query params
Kind: instance property of Request
DictAccessor
request.headers ⇒ The HTTP request headers
Kind: instance property of Request
DictAccessor
request.body ⇒ The HTTP request body
Kind: instance property of Request
Response
You must return a Response from your action.
Kind: global class
new Response(body, [code], [headers])
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 |
Object
response.simplified() ⇒ Generate and return Lambda integration object for the response.
Kind: instance method of Response
© 2017 Matthew Webb