-
Notifications
You must be signed in to change notification settings - Fork 504
/
middlewaresHierarchyController.ts
41 lines (32 loc) · 1.19 KB
/
middlewaresHierarchyController.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { Controller, Middlewares as GenericMiddlewares, Get, Route } from '@tsoa/runtime';
import type { NextFunction, Request, RequestHandler, Response } from 'express';
function Middlewares(...mws: Array<RequestHandler | (() => Promise<RequestHandler>)>) {
return GenericMiddlewares(...mws);
}
const middlewaresState: string[] = [];
export function state(): string[] {
return middlewaresState;
}
function testMiddleware(key: string) {
return async (req: Request, res: Response, next: NextFunction) => {
middlewaresState.push(key);
next();
};
}
// base class with some middleware
@Middlewares(testMiddleware('base'))
class BaseController extends Controller {}
// another one
@Middlewares(testMiddleware('intermediate'))
class IntermediateController extends BaseController {}
// intermediate controller class without middlewares
class NoopController extends IntermediateController {}
@GenericMiddlewares<RequestHandler>(testMiddleware('route'))
@Route('MiddlewareHierarchyTestExpress')
export class MiddlewareHierarchyTestExpress extends NoopController {
@Middlewares(testMiddleware('test1'), testMiddleware('test2'))
@Get('/test1')
public async test1(): Promise<void> {
return;
}
}