lambda-api-helper
v0.0.15
Published
Simplify lambda API creation with middleware support.
Maintainers
Readme
Simplify lambda API creation with middleware support.
Installation
npm install lambda-api-helperUsage
Quick start
import { lambda } from "lambda-api-helper";
export const hello = lambda.extend(
async (request, response) => {
return { hello: "world" };
}
);More Options
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";
}
);Common configurations
It also allows configuring common initialization and middlewares globally.
lambda.commons.configure({
initialize() {
// some global initializations
},
middlewares: [
// some global middlewares
],
})Sending Response
There are 4 ways for sending a response.
returnthe 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
contentbyresponse.body(content), it will be collected and send after all middlewares ran - Call original
callback()provided by AWS. It is located atresponse.callback()
Test
npm run testContributing
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.
