[go: up one dir, main page]

Skip to content

joelvh/lambda-stack

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

lambda-stack

AWS Lambda toolkit for JavaScript and Node.js

Install

yarn add lambda-stack

Publish

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

Features

  • 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 as event.nestedQueryStringParameters
  • Routing - uses custom-router for path matching and more

Usage

Use the default middy middleware we've setup, along with:

import { withDefaultMiddleware } from 'lambda-stack/middlewares'

export default withDefaultMiddleware(async (event, context) => {
  // do something
})

Router

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.

About

AWS Lambda toolkit for JavaScript and Node.js

Resources

Stars

Watchers

Forks

Packages

No packages published