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
handlemethod:middleware.add({ name: 'auth', handle: authenticate })
Example
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 Parameter | Description |
|---|---|
MiddlewareFn extends any | The type of the middleware function/handler |
Constructors
Constructor
new Middleware<MiddlewareFn>(): Middleware<MiddlewareFn>;Returns
Middleware<MiddlewareFn>
Methods
add()
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
| Parameter | Type | Description |
|---|---|---|
handler | MiddlewareFn | The middleware handler to register |
Returns
this
The Middleware instance for method chaining
Throws
If the middleware stack is frozen
all()
all(): Set<MiddlewareFn>;Returns all registered middleware handlers.
Returns
Set<MiddlewareFn>
A Set containing all registered middleware handlers
clear()
clear(): void;Removes all registered middleware handlers from the pipeline.
Returns
void
Throws
If the middleware stack is frozen
freeze()
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()
has(handler: MiddlewareFn): boolean;Checks if a specific handler has already been registered as middleware.
Parameters
| Parameter | Type | Description |
|---|---|---|
handler | MiddlewareFn | The middleware handler to check for |
Returns
boolean
true if the handler is registered, false otherwise
merge()
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
| Parameter | Type | Description |
|---|---|---|
hooks | Middleware<MiddlewareFn> | The source Middleware instance to merge from |
Returns
void
Throws
If the middleware stack is frozen
remove()
remove(handler: MiddlewareFn): boolean;Removes a specific middleware handler from the pipeline.
Parameters
| Parameter | Type | Description |
|---|---|---|
handler | MiddlewareFn | The 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()
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