AWS Lambda toolkit for JavaScript and Node.js
yarn add lambda-stack
This package is published by first converting to ES5 modules in a dist
folder. The package.json
is copied to dist
folder and published from there to allow importing modules from lambda-stack/middlewares
instead of lambda-stack/dist/middlewares
.
yarn dist && npm publish dist
- Middlewares - uses middy
- serverless-offline middleware - replaces "offline" event values with local development (e.g. localhost) values for more accurate testing
- nestedQueryStringParameters middleware - combines single- and multi-value query string parameters into a nested object as necessary (similar to Rails) using
qs
- accessible asevent.nestedQueryStringParameters
- Routing - uses custom-router for path matching and more
Use the default middy middleware we've setup, along with:
import { withDefaultMiddleware } from 'lambda-stack/middlewares'
export default withDefaultMiddleware(async (event, context) => {
// do something
})
You can match paths to different handlers. Using the Serverless Framework, your serverless.yml
might have a wildcard path configured like this.
functions:
integrationsConnect:
handler: handlers.default
events:
- http:
path: connect/{path+}
method: any
You would then define a single handler function which will invoke different responses based on matching the request path. This handler would be in handlers.js
as the default
export, based on the serverless.yml
above.
import { createRouteHandler } from 'lambda-stack'
// Returns a Lambda handler, which matches the request path and invokes the proper response.
// Uses the default middleware.
export default createRouteHandler(router => {
// specify the request method
router.post(
// path pattern
'/connect/:integration/callback',
// path parameter constraints
{
constraints: {
integration: ['shop', 'gateway']
}
},
// response handler
async (_event, _context) => ({
status: 200,
body: 'Called back'
})
)
// default response
router.unknown(async (_event, _context) => ({
status: 404,
body: 'Not Found'
}))
})
See custom-router for more router options.