AWS Lambda Lib
This library contains a set of decorators to apply to AWS Lambda function to help abstract the event source and eliminate boilerplate code
Example
const errorMap = error: ReferenceError status: 400 error: Error status: 404 @HandlerController @ { return Promise } @ { return Promise } const handler =
Sample Response
HelloHandler Response
HTTP/1.1 200 OKAccess-Control-Allow-Credentials: trueAccess-Control-Allow-Methods: POST, GET, PUT, PATCH, DELETE, OPTIONSAccess-Control-Allow-Origin: *Connection: keep-aliveContent-Type: application/jsonDate: Tue, 29 Aug 2017 17:07:04 GMTaccept-ranges: bytescache-control: no-cachecontent-length: 39vary: origin,accept-encoding
FailedHandler Response
HTTP/1.1 400 Bad RequestConnection: keep-aliveContent-Type: application/jsonDate: Tue, 29 Aug 2017 17:08:00 GMTcache-control: no-cachecontent-length: 398vary: accept-encoding
Specifying a custom error response
// Registering a custom error response plugin. This is applied globally.ApiGateway
Sample response with custom error
HTTP/1.1 400 Bad RequestConnection: keep-aliveContent-Type: application/jsonDate: Tue, 29 Aug 2017 17:08:00 GMTcache-control: no-cachecontent-length: 398vary: accept-encoding
Included Plugins
Plugin | Description |
---|---|
cors | When true, a set of default CORS headers are added to the response. Such as: Access-Control-Allow-Origin , Access-Control-Allow-Credentials , Access-Control-Allow-Methods |
statusCode | Default status code for the response. Any object resolved via a Promise in the handler, will get this status code. |
errorMap | Mapping of error types to response codes for rejected promises. |
errorResponse | Format the response of an error. |
Plugin Hooks
Hook | Description |
---|---|
INITIALIZE | Initialize is executed right at the beginning of the request, before any default plugins have been executed |
PRE_EXECUTE | The pre execute hook is run right before the execution of handler code. |
POST_EXECUTE | This hook, post execute, is run after the execution of the handler code. |
ON_ERROR | When ever there is an error which results in a rejected promise, this hook is executed. |
FINALLY | Final hook executed after the response has been sent to the client already. (Unable to manipulate response contents here) |
Writing your own plugin
See built-in plugins for samples