Throwback is a lightweight JavaScript/TypeScript library that implements a simple asynchronous middleware pattern, designed to compose promise-returning functions. It is currently in its stable version 4.1.0 and maintains an active release cadence, frequently introducing breaking changes in major versions to refine its API. Key differentiators include its strict focus on a single `ctx` argument for improved TypeScript inference (since v3.0.0), its reliance solely on native Promises (since v2.0.0), and its inspiration from Koa's middleware composition, aiming for a minimalistic yet powerful approach for processing pipelines. It is commonly used in server-side HTTP applications (e.g., Servie) and client-side request libraries (e.g., Popsicle), providing development-time debugging aids that are automatically excluded from production builds.
npm install throwbackVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to compose asynchronous middleware functions and execute a stack with a context object and a final callback, showing the order of execution.
Remove any explicit `debugMode` configurations. Ensure your `NODE_ENV` is correctly set for development or production environments.
Instead of `await next(modifiedCtx);`, call `await next();` and modify `ctx` directly. For deep changes, consider a wrapper like `(fn) => (ctx, next) => fn(ctx.clone(), next)` if your context supports cloning.
Update your middleware functions to accept a single `ctx` argument. The `next` function is still accessible within the composed context's scope or by wrapping. Note: The `next()` function *can* accept an optional `ctx` argument to replace the context downstream since v3.0.0, but this was removed in v4.0.0.
Ensure your runtime environment (Node.js or browser) provides a native `Promise` implementation. If targeting older environments, you must provide your own Promise polyfill globally before `throwback` is loaded.
Pay attention to development-time errors to ensure correct middleware behavior. Do not rely on these error checks being present in production.
Ensure your middleware signature correctly receives `next` and that you are not passing an argument to `next()` if using version 4.0.0 or higher. For versions < 4.0.0, `next(ctx)` was allowed.
Ensure all functions in the middleware array are `async` functions or explicitly return a `Promise`. Example: `async function myMiddleware(ctx, next) { /* ... */ await next(); }`Review your middleware logic. Each middleware function should call `await next()` at most once. If you need to short-circuit, simply omit calling `next()`.
The `ctx` object is user-defined. If you need cloning functionality, you must implement it within your custom context object. The suggestion in the `throwback` documentation is a pattern recommendation, not an implicit feature of `throwback` itself.
No dependency data recorded yet.