Skip to content

@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

ClassDescription
MiddlewareThe Middleware class implements the chain of responsibility design pattern and allows executing handlers in series.
RunnerRunner executes an array of middleware functions in sequence.

Type Aliases

Type AliasDescription
ErrorHandlerError handler called when any middleware in the pipeline raises an exception.
ExecutorThe executor function responsible for invoking each middleware handler.
FinalHandlerFinal handler called when the entire middleware chain completes successfully.
NextFnThe next function used to advance the middleware pipeline.

Variables

VariableDescription
versionThe current version of the package.