-
-
Notifications
You must be signed in to change notification settings - Fork 281
/
useBefore.ts
38 lines (36 loc) · 1.17 KB
/
useBefore.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
import {DecoratorParameters, getDecoratorType, StoreMerge, UnsupportedDecoratorType} from "@tsed/core";
import {EndpointRegistry} from "../../registries/EndpointRegistry";
/**
* Mounts the specified middleware function or functions at the specified path: the middleware function is executed when
* the base of the requested path matches `path.
*
* ```typescript
* @Controller('/')
* @UseBefore(Middleware1) // called only one time before all endpoint
* export class Ctrl {
*
* @Get('/')
* @UseBefore(Middleware2)
* get() { }
* }
* ```
*
* @returns {function(any, any, any): *}
* @param args
* @decorator
* @endpoint
*/
export function UseBefore(...args: any[]): Function {
return <T>(...decoratorArgs: DecoratorParameters): TypedPropertyDescriptor<T> | void => {
switch (getDecoratorType(decoratorArgs, true)) {
case "method":
EndpointRegistry.useBefore(decoratorArgs[0], decoratorArgs[1]!, args);
return decoratorArgs[2] as any;
case "class":
StoreMerge("middlewares", {useBefore: args})(...decoratorArgs);
break;
default:
throw new UnsupportedDecoratorType(UseBefore, decoratorArgs);
}
};
}