-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy patharg.ts
124 lines (120 loc) · 3.44 KB
/
arg.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright 2020 Liam Tan. All rights reserved. MIT license.
import { ArgsType, ControllerMetadata } from "./types.ts";
import { setControllerMeta, getControllerMeta, defaultMetadata } from "./metadata.ts";
//import { Reflect } from "./lib/reflect.ts";
/**
* Curried function responsible for generating parameter decorators
* for controller actions.
*/
export const defineParameterDecorator = (
argType: ArgsType,
paramKey?: string
): ParameterDecorator => (
target: any,
propertyKey: string | Symbol,
parameterIndex: number
): void => {
const meta: ControllerMetadata = getControllerMeta(target) ?? defaultMetadata();
meta.args.push({
type: argType,
key: paramKey,
index: parameterIndex,
argFor: propertyKey,
});
setControllerMeta(target, meta);
};
/**
* Parameter decorator - maps `context.params` onto controller actions
* as an argument, e.g.
*
* ```ts
* public controllerAction(@Param('id') id: number): any {}
* ```
*
* Returns whole `context.params` object if no key specified
*/
export function Param(paramKey?: string): ParameterDecorator {
return defineParameterDecorator(ArgsType.PARAM, paramKey);
}
/**
* Parameter decorator - maps `context.request.body()` onto controller actions
* as an argument, e.g.
*
* ```ts
* public controllerAction(@Body('name') name: string): any { }
* ```
*
* Returns whole `context.request.body()` if no key specified
*/
export function Body(bodyKey?: string): ParameterDecorator {
return defineParameterDecorator(ArgsType.BODY, bodyKey);
}
/**
* Parameter decorator - maps `url.searchParams.entries()` onto controller actions
* as an argument, e.g.
*
* ```ts
* public controllerAction(@Query('orderBy') orderBy: string): any { }
* ```
*
* Returns whole `url.searchParams.entries()` if no key specified.
*/
export function Query(queryKey?: string): ParameterDecorator {
return defineParameterDecorator(ArgsType.QUERY, queryKey);
}
/**
* Parameter decorator - maps `context.request.headers` onto controller actions
* as an argument, e.g.
*
* ```ts
* public controllerAction(@Header('content-type') contentType: string): any { }
* ```
* Returns whole `context.request.headers` if no key specified
*/
export function Header(headerKey?: string): ParameterDecorator {
return defineParameterDecorator(ArgsType.HEADER, headerKey);
}
/**
* Parameter decorator - maps whole `context` onto controller actions
* as an argument, e.g.
*
* ```ts
* public controllerAction(@Context() ctx: RouterContext): any { }
* ```
*/
export function Context(): ParameterDecorator {
return defineParameterDecorator(ArgsType.CONTEXT);
}
/**
* Parameter decorator - maps `context.request` onto controller actions
* as an argument, e.g.
*
* ```ts
* public controllerAction(@Request() req: Request): any { }
* ```
*/
export function Request(): ParameterDecorator {
return defineParameterDecorator(ArgsType.REQUEST);
}
/**
* Parameter decorator - maps `context.response` onto controller actions
* as an argument, e.g.
*
* ```ts
* public controllerAction(@Response() res: Response): any { }
* ```
*/
export function Response(): ParameterDecorator {
return defineParameterDecorator(ArgsType.RESPONSE);
}
/**
* Parameter decorator - maps dependency by key from DI container
* to a method argument
*
* ```ts
* public controllerAction(@Inject() userService: UserService): any { }
* ```
*/
export function Inject(key: string): ParameterDecorator {
return defineParameterDecorator(ArgsType.INJECT, key);
}