All Projects β†’ dbartholomae β†’ Lambda Middleware

dbartholomae / Lambda Middleware

Licence: mit
A collection of middleware for AWS lambda functions.

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Lambda Middleware

Lambcycle
πŸ‘πŸ›΅ A declarative lambda middleware with life cycle hooks πŸ‘πŸ›΅
Stars: ✭ 88 (+11.39%)
Mutual labels:  aws, lambda, middleware
Micro Aws Lambda
A 7KB and 0 dependencies AWS Lambda library which supports middleware and easy debug.
Stars: ✭ 181 (+129.11%)
Mutual labels:  aws, lambda, middleware
Grant
OAuth Proxy
Stars: ✭ 3,509 (+4341.77%)
Mutual labels:  aws, lambda, middleware
Seoul Bike
μ„œμšΈμ‹œ μžμ „κ±° 따릉이λ₯Ό μœ„ν•œ λŒ€μ—¬μ†Œ μ°ΎκΈ° μ„œλΉ„μŠ€ μž…λ‹ˆλ‹€.
Stars: ✭ 78 (-1.27%)
Mutual labels:  aws, lambda
Appsync Resolvers Example
Example project for AppSync, GraphQL, and AWS Lambda resolvers using Go.
Stars: ✭ 50 (-36.71%)
Mutual labels:  aws, lambda
Serverless Application Model
AWS Serverless Application Model (SAM) is an open-source framework for building serverless applications
Stars: ✭ 8,305 (+10412.66%)
Mutual labels:  aws, lambda
Aws Iot Button
Stars: ✭ 37 (-53.16%)
Mutual labels:  aws, lambda
Lambda Refarch Webapp
The Web Application reference architecture is a general-purpose, event-driven, web application back-end that uses AWS Lambda, Amazon API Gateway for its business logic. It also uses Amazon DynamoDB as its database and Amazon Cognito for user management. All static content is hosted using AWS Amplify Console.
Stars: ✭ 1,208 (+1429.11%)
Mutual labels:  aws, lambda
Lambda Audio
Run Sound eXchange (SoX), the Swiss Army knife of audio manipulation, with Lame on AWS Lambda
Stars: ✭ 53 (-32.91%)
Mutual labels:  aws, lambda
Up
Up focuses on deploying "vanilla" HTTP servers so there's nothing new to learn, just develop with your favorite existing frameworks such as Express, Koa, Django, Golang net/http or others.
Stars: ✭ 8,439 (+10582.28%)
Mutual labels:  aws, lambda
Vapor Aws Lambda Runtime
Run your Vapor api server on AWS Lambda using the official Swift Server runtime.
Stars: ✭ 65 (-17.72%)
Mutual labels:  aws, lambda
Ridge
AWS Lambda HTTP Proxy integration event bridge to Go net/http.
Stars: ✭ 45 (-43.04%)
Mutual labels:  aws, lambda
Chalice
Python Serverless Microframework for AWS
Stars: ✭ 8,513 (+10675.95%)
Mutual labels:  aws, lambda
Serverless Es Logs
A Serverless plugin to transport logs to ElasticSearch
Stars: ✭ 51 (-35.44%)
Mutual labels:  aws, lambda
Terraform Nextjs Plugin
A plugin to generate terraform configuration for Nextjs 8 and 9
Stars: ✭ 41 (-48.1%)
Mutual labels:  aws, lambda
Apex
Old apex/apex
Stars: ✭ 20 (-74.68%)
Mutual labels:  aws, lambda
Binaryalert
BinaryAlert: Serverless, Real-time & Retroactive Malware Detection.
Stars: ✭ 1,125 (+1324.05%)
Mutual labels:  aws, lambda
Ask Around Me
The Ask Around Me example serverless web application. See the Compute Blog series and video series for more information. Contact @jbesw for info.
Stars: ✭ 67 (-15.19%)
Mutual labels:  aws, lambda
Serverless Node Simple Image Resize
Simple image resize AWS lambda function
Stars: ✭ 74 (-6.33%)
Mutual labels:  aws, lambda
Serverless Plugin Stackstorm
Plugin for serverless framework to run ready to use actions from StackStorm Exchange as AWS Lambda.
Stars: ✭ 28 (-64.56%)
Mutual labels:  aws, lambda

@lambda-middleware

open issues debug build status codecov CLA assistant

This monorepo is a collection of middleware for AWS lambda functions.

Middlewares

Other packages

Furthermore there is utility collection available at @lambda-middleware/utils.downloads

Usage

Each middleware is a higher-order function that can be wrapped around the handler function.

export const handler = someMiddleware()(() => {
  return {
    body: '',
    statusCode: 200
  }
})

Each middleware is build as

(options) => (handler) => (event, context) => Response

This means that middleware can be composed and piped like any other function with only one parameter (the handler). This library contains a helper for composing, but any other implementation should work as well.

export const handler = compose(
  someMiddleware(),
  someOtherMiddleware(),
  aThirdMiddleware()
)(() => {
  return {
    body: '',
    statusCode: 200
  }
})

There's a known issue with TypeScript that pipe and compose functions cannot infer types correctly if the innermost function is generic (in this case the last argument to compose). If you use TypeScript in strict mode, you can instead use the composeHandler function exported from @lambda-middleware/compose:

export const handler = composeHandler(
  someMiddleware(),
  someOtherMiddleware(),
  aThirdMiddleware(),
  () => {
    return {
      body: '',
      statusCode: 200
    }
  }
)

Composing middleware is equivalent to calling it nested:

export const handler =
  someMiddleware()(
    someOtherMiddleware()(
      aThirdMiddleware()(() => {
        return {
          body: '',
          statusCode: 200
        }
      })
    )
  )

The order of composition can be relevant. When using a helper to do the composition, check, in which order the functions are applied. Most of the time TypeScript should be able to warn you, if the order is wrong.

Imagine middleware as an onion around your function: The outermost middleware will get called first before the handler starts, and last after the handler finishes or throws an error. In our example above the order in which middleware gets executed therefore would be:

someMiddleware
  someOtherMiddleware
    aThirdMiddleware
      the handler
    aThirdMiddleware
  someOtherMiddleware
someMiddleware

This means that middleware which transforms the input for the handler will be executed top to bottom, while middleware that transforms the response will be called bottom to top.

Writing your own middleware

If you want to write your own middleware, check the existing examples and feel free to borrow some of the tests for inspiration. The general idea for a middleware is the following:

const myMiddleware = (optionsForMyMiddleware) => (handler) => async (event, context) => {
  try {
    const modifiedEvent = doSomethingBeforeCallingTheHandler(event)
    const response = await handler(modifiedEvent, context)
    const modifiedResponse = doSomethingAfterCallingTheHandler(response)
    return modifiedResponse
  } catch (error) {
    const modifiedError = doSomethingInCaseOfAnError(error)
    throw modifiedError
  }
}

Usually the same middleware should not need to do something before the handler, after the handler and on error. Creating separated middlewares for these cases keeps them more versatile. But cases that require multiple steps are supported as well.

Since the middlewares only uses function composition, TypeScript can offer extensive typing support to let you know how the middleware changed. When adding your own middleware it is recommended to use generics to avoid losing type information.

Instead of

const bodyParser = () =>
  (handler: PromiseHandler<Omit<APIGatewayProxyEvent, body> & { body: object}, APIGatewayProxyResult>): PromiseHandler<APIGatewayProxyEvent, APIGatewayProxyResult> =>
  async (event: E, context: Context) => {
  return handler({ ...event, body: JSON.parse(event.body) }, context)
}

use

const bodyParser = () =>
  <E extends APIGatewayProxyEvent>(handler: PromiseHandler<Omit<E, body> & { body: object}, APIGatewayProxyResult>): PromiseHandler<E, APIGatewayProxyResult> =>
  async (event: E, context: Context) => {
  return handler({ ...event, body: JSON.parse(event.body) }, context)
}

so that if multiple middlewares change the event, the resulting type will have all changes and not just the latest.

Contributing

If you want to contribute to the project, please read our contributing guidelines first.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].