Introduction
This TypeScript library provides a simple implementation of middleware pattern
which can be used to wrap or augment functions with additional functionality
without modifying their original implementation.
Usage
- Creating a Middleware object:
const myFunction = (name: string, age: number) => {
console.log(`${name} is ${age} years old.`);
};
const myMiddleware = middleware(myFunction)
.use((args, next) => {
console.log(`Before function execution`);
return next(...args);
console.log(`After function execution`);
}).run;
myMiddleware("Alice", 25);
Before/after concept
const mv: MiddlewareFn = (args, next) => {
const result = next(args);
return result;
};
Execution order (LIFO)
Since everything is a middleware, the order of execution is important.
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.
Example
middleware((a, b) => {
console.log("Actual function logic");
return a + b;
})
.use((args, next) => {
console.log("A: Before logic");
const result = next(...args);
console.log("A: After logic");
return result;
})
.use((args, next) => {
console.log("B: Before logic");
const result = next(...args);
console.log("B: After logic");
return result;
})
.run(10, 5);
Execution order:
B
Before logicA
Before logic- Actual function logic
A
After logicB
After logic
Fetch example
const modifiedFetch = middleware(fetch).use(async (args, next) => {
const res = await next(...args);
if (res.ok) {
return res;
}
throw new Error(await res.text());
}).run;
console.log(await modifiedFetch("https://example.com"));