Skip to content

Commit e7d818a

Browse files
feat: add context (object) based middleware for req, res.
1 parent 9e7a252 commit e7d818a

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Abstract.
2+
import { MiddlewareBase } from './middleware-base.abstract';
3+
// Type.
4+
import { ContextMiddlewareFunction } from '../type';
5+
/**
6+
* @description
7+
* @export
8+
* @abstract
9+
* @class ContextMiddlewareBase
10+
* @template [T=any]
11+
* @template {Function} [U=ContextMiddlewareFunction<T>]
12+
* @extends {MiddlewareBase<T, T, U>}
13+
*/
14+
export abstract class ContextMiddlewareBase<
15+
T = any,
16+
U extends Function = ContextMiddlewareFunction<T>
17+
> extends MiddlewareBase<T, T, U> {
18+
/**
19+
* @description
20+
* @public
21+
* @param {T} context
22+
*/
23+
public override execute(context: T) {
24+
return super.execute(context), this;
25+
}
26+
27+
/**
28+
* @description
29+
* @public
30+
* @async
31+
* @param {T} context
32+
* @returns {unknown}
33+
*/
34+
public override async executeAsync(context: T): Promise<T> {
35+
return super.executeAsync(context);
36+
}
37+
38+
/**
39+
* @description
40+
* @public
41+
* @param {(context: T) => void} onComplete
42+
*/
43+
public override onComplete(onComplete: (context: T) => void): void {
44+
super.onComplete(() => onComplete);
45+
}
46+
47+
/**
48+
* @description
49+
* @public
50+
* @param {U} middleware
51+
* @returns {this}
52+
*/
53+
public override use(middleware: U): this {
54+
return super.use(middleware);
55+
}
56+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Abstract.
2+
import { ContextMiddlewareBase } from './context-middleware-base.abstract';
3+
// Type.
4+
import { ContextMiddlewareFunction } from '../type';
5+
/**
6+
* @description
7+
* @export
8+
* @class ContextMiddleware
9+
* @template [T=any]
10+
* @template {Function} [U=MiddlewareFunction<T>]
11+
* @extends {ContextMiddlewareBase<T, U>}
12+
*/
13+
export class ContextMiddleware<
14+
T = any,
15+
U extends Function = ContextMiddlewareFunction<T>
16+
> extends ContextMiddlewareBase<T, U> {}

0 commit comments

Comments
 (0)