4.5.1 • Published 6 years ago

lambda-lib v4.5.1

Weekly downloads
2
License
MIT
Repository
github
Last release
6 years ago

Build Status License NPM codecov

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

import { HandlerController, ApiGateway } from 'lambda-lib'

const errorMap = [
  {
    error: ReferenceError,
    status: 400
  },
  {
    error: Error,
    status: 404
  }
]

@HandlerController
class SampleLambdaHandler {
  @ApiGateway({ statusCode: 200, cors: true })
  helloHandler (event) {
    return Promise.resolve({ hello: world })
  }

  @ApiGateway({ statusCode: 200, errorMap: errorMap })
  failedHandler (event) {
    return Promise.reject(new ReferenceError('I am a reference error'))
  }
}

const handler = new SampleLambdaHandler()

export default handler.getHandlers()

Sample Response

HelloHandler Response

HTTP/1.1 200 OK
Access-Control-Allow-Credentials: true
Access-Control-Allow-Methods: POST, GET, PUT, PATCH, DELETE, OPTIONS
Access-Control-Allow-Origin: *
Connection: keep-alive
Content-Type: application/json
Date: Tue, 29 Aug 2017 17:07:04 GMT
accept-ranges: bytes
cache-control: no-cache
content-length: 39
vary: origin,accept-encoding

{
  "hello": "world"
}

FailedHandler Response

HTTP/1.1 400 Bad Request
Connection: keep-alive
Content-Type: application/json
Date: Tue, 29 Aug 2017 17:08:00 GMT
cache-control: no-cache
content-length: 398
vary: accept-encoding

{
  "error": {
    "message": "I am a reference error",
    "name": "ReferenceError",
    "_stackTrace": [
      "ReferenceError: I am a reference error",
      "at SampleLambdaHandler.failedHandler (/.../src/resources/example/index.js:223:15)",
      "at /.../node_modules/lambda-lib/lib/api-gateway.js:93:19",
      "at process._tickDomainCallback (internal/process/next_tick.js:135:7)"
    ]
  }
}

Specifying a custom error response

import { Plugins } from 'lambda-lib'

// Registering a custom error response plugin. This is applied globally.
ApiGateway.registerPlugin(new Plugins.ErrorResponse(err => {
  return {
    test: 'This is the error response body for all errors',
    error: err.message
  }
}))

Sample response with custom error

HTTP/1.1 400 Bad Request
Connection: keep-alive
Content-Type: application/json
Date: Tue, 29 Aug 2017 17:08:00 GMT
cache-control: no-cache
content-length: 398
vary: accept-encoding

{
  "test": "This is the error response body for all errors",
  "error": "I am a reference error"
}

Included Plugins

PluginDescription
corsWhen 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
statusCodeDefault status code for the response. Any object resolved via a Promise in the handler, will get this status code.
errorMapMapping of error types to response codes for rejected promises.
errorResponseFormat the response of an error.

Plugin Hooks

HookDescription
INITIALIZEInitialize is executed right at the beginning of the request, before any default plugins have been executed
PRE_EXECUTEThe pre execute hook is run right before the execution of handler code.
POST_EXECUTEThis hook, post execute, is run after the execution of the handler code.
ON_ERRORWhen ever there is an error which results in a rejected promise, this hook is executed.
FINALLYFinal 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

4.5.1

6 years ago

4.5.0

6 years ago

4.4.0

6 years ago

4.3.0

6 years ago

4.2.0

6 years ago

4.1.1

6 years ago

4.1.0

6 years ago

4.0.0

6 years ago

3.0.3

6 years ago

3.0.2

6 years ago

3.0.1

6 years ago

3.0.0

6 years ago

2.2.2

6 years ago

2.2.1

6 years ago

2.2.0

6 years ago

2.1.3

6 years ago

2.1.2

6 years ago

2.1.1

6 years ago

2.1.0

6 years ago

2.0.2

6 years ago

2.0.1

6 years ago

2.0.0

6 years ago

2.0.0-beta.9

6 years ago

2.0.0-beta.8

6 years ago

2.0.0-beta.7

6 years ago

2.0.0-beta.6

6 years ago

2.0.0-beta.5

6 years ago

2.0.0-beta.4

6 years ago

2.0.0-beta.3

6 years ago

2.0.0-beta.2

7 years ago

2.0.0-beta.1

7 years ago

1.0.5

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.1.0-alpha9

7 years ago

1.1.0-alpha8

7 years ago

1.1.0-alpha7

7 years ago

1.1.0-alpha6

7 years ago

1.1.0-alpha5

7 years ago

1.1.0-alpha4

7 years ago

1.1.0-alpha3

7 years ago

1.0.2

7 years ago

1.1.0-alpha2

7 years ago

1.1.0-alpha1

7 years ago

1.0.1

7 years ago

0.1.2

7 years ago

0.1.1

7 years ago

0.1.0

7 years ago

0.0.6

7 years ago

0.0.5

7 years ago

0.0.4

7 years ago

0.0.1

7 years ago

0.0.3

7 years ago

0.0.2

7 years ago

0.0.1-dev

7 years ago

1.0.0-alpha.0

7 years ago