{"library":"serverless-compose","title":"Serverless Compose (Lambda Middleware)","description":"serverless-compose is a lightweight, functional middleware framework specifically designed for AWS Lambda functions, enabling the creation of composable, \"onion-style\" middleware stacks. Currently at version 2.4.0, its release cadence is not explicitly stated in the provided documentation, but it appears actively maintained. A key differentiator is its zero-dependency philosophy, ensuring a minimal bundle size and avoiding dependency conflicts. Unlike more opinionated frameworks, serverless-compose focuses on providing a thin `compose` function and flexible patterns like `recoveryMiddleware` and `timingLogMiddleware` without enforcing specific architectural designs, allowing developers to define their middleware stack with explicit, functional wrappers around their Lambda handlers. It aims to prevent the \"fossilization\" of bad middleware habits by promoting a clear, functional approach for event processing.","language":"javascript","status":"active","last_verified":"Thu Apr 23","install":{"commands":["npm install serverless-compose"],"cli":null},"imports":["import { compose } from 'serverless-compose'","import { recoveryMiddleware } from 'serverless-compose'","import { timingLogMiddleware } from 'serverless-compose'"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { compose, recoveryMiddleware, timingLogMiddleware } from 'serverless-compose';\n\n// Suppose this is a client that fetches the weather from some external API\nclass WeatherClient {\n  static async askBadAPIForWeather() {\n    // Simulate a failing API call 50% of the time\n    if (Math.random() > 0.5) {\n      throw new Error('Weather API is down!');\n    }\n    return { temperature: 25, unit: 'C' };\n  }\n}\n\n// Your actual handler code\nasync function getWeather(request, context) {\n  const response = await WeatherClient.askBadAPIForWeather();\n  return {\n    statusCode: 200,\n    body: JSON.stringify(response),\n  };\n}\n\n// Your own custom logging function\nasync function logDuration(duration) {\n  console.log(`It took: ${duration}ms to return the weather`);\n}\n\n// Your own custom error handler\nasync function sendError(error) {\n  console.error('Handler Error:', error);\n  return {\n    statusCode: 500,\n    body: JSON.stringify({\n      error: `${error.message}`,\n      message: 'The darn weather API failed me again!',\n    }),\n  };\n}\n\nconst TimingMiddleware = timingLogMiddleware(logDuration);\nconst RecoveryMiddleware = recoveryMiddleware(sendError);\nconst MyMiddlewareStack = compose(\n  TimingMiddleware,\n  RecoveryMiddleware,\n);\n\nexport const lambdaHandler = MyMiddlewareStack(getWeather);\n","lang":"typescript","description":"Demonstrates basic setup with `compose`, `timingLogMiddleware` for logging duration, and `recoveryMiddleware` for graceful error handling in an AWS Lambda handler.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}