This library provides a simple implementation of a middleware pattern that can
be used to wrap or extend functions with additional functionality without
changing their original implementation.
Installation
npm install @mellkam/middleware
Usage
import { type Middleware, wrap } from "@mellkam/middleware";
const timer: Middleware = (args, next) => {
console.time("timer");
const result = next(...args);
console.timeEnd("timer");
return result;
};
console.log(wrap(fibonacci).use(timer).run(40));
Execution order (LIFO)
Middlewares are executed in LIFO order ("Last In, First Out").
Everytime you push a new middleware to the stack, it is added as a new onion
layer on top of all existing ones.
import { wrap } from "@mellkam/middleware";
const log = wrap(console.log)
.use((args, next) => {
console.log("Middleware 1: Before");
next(...args);
console.log("Middleware 1: After");
})
.use((args, next) => {
console.log("Middleware 2: Before");
next(...args);
console.log("Middleware 2: After");
}).run;
log("Actual func call");
Logs:
Middleware 2: Before
Middleware 1: Before
Actual func call
Middleware 1: After
Middleware 2: After
Before/After concept
const mw: Middleware = (args, next) => {
next(...args);
};
Check out more examples here