-
Notifications
You must be signed in to change notification settings - Fork 0
/
request-param.ts
46 lines (42 loc) · 1.73 KB
/
request-param.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
import RequestParameterMetadata from "../types/request-parameter-metadata";
/**
* Parameter decorator which defines a request body parameter to be provided to the Express route
*
* @param key - name of parameter in request body
* @param summary - description of the parameter for OpenAPI documentation
* @param exampleValue - example of expected data
* @param required - indicate if this parameter is required
* @param deprecated - indicate if this parameter is deprecated
* @typeParam T - Class method to be registered with the Express application as a route
* @example
* ```
* @Controller('/api', 'User', 'User Management Routes')
* class User {
*
* @HttpPut('/user/:userId', 'Update an existing user')
* async createUser(
* @UrlParam('userId') userId: number,
* @RequestParam('password') password: string,
* @Response() res: ExpressResponse
* ): Promise<void> {
* ...
* }
* }
* ```
*/
function RequestParam<T>(key: string, summary?: string, exampleValue?: string | number | boolean | undefined, required?: boolean): (target: T, propertyKey: string, parameterIndex: number) => void {
return function (target: T, propertyKey: string, parameterIndex: number): void {
const requestParams: RequestParameterMetadata[] = Reflect.getMetadata('etd:requestParams', target, propertyKey) || [];
const type = Reflect.getMetadata('design:paramtypes', target, propertyKey);
requestParams[parameterIndex] = {
key,
summary,
exampleValue,
type: type[parameterIndex].name,
required,
mode: 'request',
};
Reflect.defineMetadata('etd:requestParams', requestParams, target, propertyKey);
}
}
export default RequestParam;