Skip to content

Commit 2257705

Browse files
test: add ContextMiddleware, and update Middleware.
1 parent cb8c68e commit 2257705

File tree

2 files changed

+63
-7
lines changed

2 files changed

+63
-7
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { ContextMiddleware } from "../lib";
2+
3+
const contextMiddleware = new ContextMiddleware<{ req: string; res?: string }>(
4+
(context, next: () => void) => {
5+
console.log('Middleware 0 executed with args:', context);
6+
next();
7+
},
8+
(context, next: () => void) => {
9+
console.log('Middleware 00 executed with args:', context);
10+
next();
11+
}
12+
);
13+
14+
contextMiddleware.use((context, next) => {
15+
console.log('Middleware 1 executed with args:', context);
16+
context.req = 'newValue';
17+
next();
18+
});
19+
20+
21+
contextMiddleware.use((context, next) => {
22+
console.log('Middleware 2 executed with args:', context);
23+
next();
24+
});
25+
26+
contextMiddleware.execute({ req: 'value' });
27+
28+
// Async
29+
contextMiddleware.use(async (context, next) => {
30+
console.log('Async middleware 1 start with args:', context);
31+
context.req = 'newValue';
32+
await new Promise(resolve => setTimeout(resolve, 2000));
33+
console.log('Async middleware end');
34+
next();
35+
});
36+
37+
contextMiddleware.use(async (args, next) => {
38+
console.log('Async middleware 2 start with args:', args);
39+
next();
40+
});
41+
42+
contextMiddleware.onComplete((args) => {
43+
console.log('All middlewares completed with args:', args);
44+
});
45+
46+
contextMiddleware.executeAsync({ req: 'value' });

src/test/middleware.spec.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
import { Middleware } from "../lib";
22

3-
const middleware = new Middleware();
4-
5-
middleware.use((args, next) => {
3+
// <{ key: string; newKey?: string }>
4+
const middleware = new Middleware(
5+
(args: ({ key: string; newKey?: string })[], next: () => void) => {
6+
console.log('Middleware 0 executed with args:', args);
7+
next();
8+
},
9+
(args: { key: string; anyKey?: string }[], next: () => void) => {
10+
console.log('Middleware 00 executed with args:', args);
11+
next();
12+
}
13+
);
14+
15+
middleware.use(([args], next) => {
616
console.log('Middleware 1 executed with args:', args);
7-
args[0].newKey = 'newValue';
17+
args.newKey = 'newValue';
818
next();
919
});
1020

@@ -14,12 +24,12 @@ middleware.use((args, next) => {
1424
next();
1525
});
1626

17-
middleware.execute({ key: 'value' });
27+
middleware.execute({ key: 'value' }, 2);
1828

1929
// Async
20-
middleware.use(async (args, next) => {
30+
middleware.use(async ([args], next) => {
2131
console.log('Async middleware 1 start with args:', args);
22-
args[0].newKey = 'newValue';
32+
args.newKey = 'newValue';
2333
await new Promise(resolve => setTimeout(resolve, 2000));
2434
console.log('Async middleware end');
2535
next();

0 commit comments

Comments
 (0)