Package Exports
- lambda-serverless-api
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (lambda-serverless-api) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Abstraction for Serverless API
This project abstracts the creation of a basic API and the most commonly desired features.
Provides support for:
- Rate Limiting using lambda-rate-limiter
- Logging of ApiErrors using lambda-rollbar
Install
$ npm install --save lambda-serverless-api
Getting Started
First we need to wrap our lambda endpoint. Inside the lambda function we can then use ApiError and JsonResponse as following:
const api = require("lambda-serverless-api")({
limiter: {},
rollbar: {}
});
module.exports = api.wrap(process.env.RATE_LIMIT_PER_IP, () => {
if (new Date().getHours() === 4) {
throw new api.ApiError("I am a teapot", 418);
}
return new api.JsonResponse({ message: "What's up?" });
});
where RATE_LIMIT_PER_IP allows to set different limits per endpoint. Rate limiting is explained below.
If you want to send plain text instead of json, you can use ApiResponse.
Rate Limiting
Rate limiting uses lambda-rate-limiter. Note that there are some serious restrictions because it does not use centralized storage!
To customize rate limiting, the package options are passed as limiter into the constructor.
Logging Api Errors / Exceptions
To monitor api errors and exceptions lambda-rollbar can be enabled. Options are passed by putting them as rollbar into the constructor.