Skip to content

Class: Middleware<MiddlewareFn>

The Middleware class implements the chain of responsibility design pattern and allows executing handlers in series.

The middleware handlers can be represented as any value you wish, such as:

  • A function: middleware.add(function () { console.log('called') })
  • An object with a handle method: middleware.add({ name: 'auth', handle: authenticate })

Example

ts
const context = {};
type MiddlewareFn = (ctx: typeof context, next: NextFn) => void | Promise<void>;

const middleware = new Middleware<MiddlewareFn>();

middleware.add((ctx, next) => {
  console.log("executing middleware");
  await next();
});

await middleware.runner().run((fn, next) => fn(context, next));

Type Parameters

Type ParameterDescription
MiddlewareFn extends anyThe type of the middleware function/handler

Constructors

Constructor

ts
new Middleware<MiddlewareFn>(): Middleware<MiddlewareFn>;

Returns

Middleware<MiddlewareFn>

Methods

add()

ts
add(handler: MiddlewareFn): this;

Registers a new middleware handler to the pipeline.

Adding the same middleware handler multiple times will result in a no-op, as handlers are stored in a Set to prevent duplicates.

Parameters

ParameterTypeDescription
handlerMiddlewareFnThe middleware handler to register

Returns

this

The Middleware instance for method chaining

Throws

If the middleware stack is frozen


all()

ts
all(): Set<MiddlewareFn>;

Returns all registered middleware handlers.

Returns

Set<MiddlewareFn>

A Set containing all registered middleware handlers


clear()

ts
clear(): void;

Removes all registered middleware handlers from the pipeline.

Returns

void

Throws

If the middleware stack is frozen


freeze()

ts
freeze(): void;

Freezes the middleware stack to prevent further modifications.

Once frozen, the middleware array is cached and no new handlers can be added, removed, or modified. This method is automatically called when creating a runner.

Returns

void


has()

ts
has(handler: MiddlewareFn): boolean;

Checks if a specific handler has already been registered as middleware.

Parameters

ParameterTypeDescription
handlerMiddlewareFnThe middleware handler to check for

Returns

boolean

true if the handler is registered, false otherwise


merge()

ts
merge(hooks: Middleware<MiddlewareFn>): void;

Merges middleware handlers from another Middleware instance.

The middleware from the source instance are appended to the current instance.

Parameters

ParameterTypeDescription
hooksMiddleware<MiddlewareFn>The source Middleware instance to merge from

Returns

void

Throws

If the middleware stack is frozen


remove()

ts
remove(handler: MiddlewareFn): boolean;

Removes a specific middleware handler from the pipeline.

Parameters

ParameterTypeDescription
handlerMiddlewareFnThe middleware handler to remove

Returns

boolean

true if the handler was removed, false if it was not found

Throws

If the middleware stack is frozen


runner()

ts
runner(): Runner<MiddlewareFn>;

Creates and returns a Runner instance to execute the middleware pipeline.

This method automatically freezes the middleware stack to prevent modifications during execution.

Returns

Runner<MiddlewareFn>

A new Runner instance configured with the current middleware handlers