{"library":"middleware-io","title":"Modern Promise-Based Middleware","description":"middleware-io is a lightweight, zero-dependency library for composing promise-based middleware in JavaScript and TypeScript applications. It provides a `compose` function similar to `koa-compose` and a `Composer` class for building middleware chains with various utility 'snippets' like lazy loading, forking, and concurrency control. The library is written in TypeScript, ensuring type safety and a robust development experience. Currently at version 2.8.1, its release cadence has been irregular since its active development phase around 2019-2021, with the latest significant changes related to `exports` in `package.json` for modern module resolution. Key differentiators include its self-sufficiency (zero dependencies), native ESM support, and a rich set of built-in middleware snippets.","language":"javascript","status":"maintenance","last_verified":"Wed Apr 22","install":{"commands":["npm install middleware-io"],"cli":null},"imports":["import { compose } from 'middleware-io'","import { Composer } from 'middleware-io'","import type { Middleware } from 'middleware-io'"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { compose } from 'middleware-io';\n\ninterface MyContext {\n  now?: number;\n  customData?: string;\n}\n\nconst composedMiddleware = compose<MyContext>([\n  async (context, next) => {\n    // This is the first middleware in the chain.\n    console.log('Step 1: Before next() in first middleware');\n\n    // Pass control to the next middleware in the stack.\n    await next();\n\n    // Control returns here after subsequent middleware complete.\n    console.log('Step 4: After next() in first middleware');\n\n    // Access data modified by later middleware.\n    console.log(`Context.now from next middleware: ${context.now}`);\n  },\n  async (context, next) => {\n    // This is the second middleware.\n    console.log('Step 2: Before next() in second middleware');\n\n    // Modify the context object, which is shared across the chain.\n    context.now = Date.now();\n    context.customData = 'Hello from second middleware!';\n\n    // Pass control to the next middleware (or the final handler if no more middleware).\n    await next();\n\n    // Control returns here after the next middleware (or final handler) completes.\n    console.log('Step 3: After next() in second middleware');\n  }\n]);\n\n// Execute the composed middleware with an initial context and a final handler.\ncomposedMiddleware({}, () => {\n  console.log('Final handler reached (no more middleware).');\n  return Promise.resolve(); // Ensure the final handler also returns a Promise\n})\n  .then(() => {\n    console.log('Middleware chain finished work successfully.');\n  })\n  .catch(console.error);\n","lang":"typescript","description":"This example demonstrates basic promise-based middleware composition, context modification, and asynchronous flow control using `compose`.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}