@nhtio/middleware
Example
ts
import { Middleware } from "@nhtio/middleware";
import type { NextFn } from "@nhtio/middleware";
const context = {};
type MiddlewareFn = (ctx: typeof context, next: NextFn) => void | Promise<void>;
const middleware = new Middleware<MiddlewareFn>();
middleware.add((ctx, next) => {
console.log("executing fn1");
await next();
});
await middleware.runner().run((fn, next) => fn(context, next));Classes
| Class | Description |
|---|---|
| Middleware | The Middleware class implements the chain of responsibility design pattern and allows executing handlers in series. |
| Runner | Runner executes an array of middleware functions in sequence. |
Type Aliases
| Type Alias | Description |
|---|---|
| ErrorHandler | Error handler called when any middleware in the pipeline raises an exception. |
| Executor | The executor function responsible for invoking each middleware handler. |
| FinalHandler | Final handler called when the entire middleware chain completes successfully. |
| NextFn | The next function used to advance the middleware pipeline. |
Variables
| Variable | Description |
|---|---|
| version | The current version of the package. |