Simplify lambda API creation with middleware support.
npm install lambda-api-helper
import { lambda } from "lambda-api-helper";
export const hello = lambda.extend(
async (request, response) => {
return { hello: "world" };
}
);
It accepts first parameter for more options, which configure each route specifically.
export const hello = lambda.extend(
{
initialize() {
// some route-specific initialization here
},
middlewares: [
async (request, response, next) => {
// runs middleware before request
await next();
// runs middleware after response
},
],
},
async (request, response) => {
return "Hello World";
}
);
It also allows configuring common initialization and middlewares globally.
lambda.commons.configure({
initialize() {
// some global initializations
},
middlewares: [
// some global middlewares
],
})
There are 4 ways for sending a response.
-
return
the response in the handler. The content will be automatically stringify if it is object-like. - Call
response.send(content)
to immediately send your content - Set your
content
byresponse.body(content)
, it will be collected and send after all middlewares ran - Call original
callback()
provided by AWS. It is located atresponse.callback()
npm run test
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.