-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathmethod.ts
123 lines (119 loc) · 3.27 KB
/
method.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
// Copyright 2020 Liam Tan. All rights reserved. MIT license.
import { HttpMethod, ControllerMetadata } from "./types.ts";
import { getControllerMeta, defaultMetadata, setControllerMeta } from "./metadata.ts";
/**
* Responsible for producing function decorators for all given HttpMethods.
* Uses a curried function to return the function decorator.
*/
export const defineRouteDecorator = (path: string = "/", requestMethod: HttpMethod): MethodDecorator => (
target: any,
propertyKey: string | Symbol
): void => {
const meta: ControllerMetadata = getControllerMeta(target) ?? defaultMetadata();
meta.routes.set(propertyKey, {
requestMethod,
path,
methodName: propertyKey,
});
setControllerMeta(target, meta);
};
/**
* Method decorator function for mapping Get requests.
*
* `path` will be the routed path with the prefix of the
* parent controller prefix, E.g.
*
* ```ts
* @Controller('api')
* class DefaultController {
* @Get('/:id')
* public controllerAction(): any {}
* }
* ```
*
* The above action `controllerAction` will then be mapped
* to `GET` requests that match pattern `api/:id`
*/
export function Get(path?: string): MethodDecorator {
return defineRouteDecorator(path, HttpMethod.GET);
}
/**
* Method decorator function for mapping Put requests.
*
* `path` will be the routed path with the prefix of the
* parent controller prefix, E.g.
*
* ```ts
* @Controller('api')
* class DefaultController {
* @Put('/:id')
* public controllerAction(): any {}
* }
* ```
*
* The above action `controllerAction` will then be mapped
* to `PUT` requests that match pattern `api/:id`
*/
export function Put(path?: string): MethodDecorator {
return defineRouteDecorator(path, HttpMethod.PUT);
}
/**
* Method decorator function for mapping Post requests.
*
* `path` will be the routed path with the prefix of the
* parent controller prefix, E.g.
*
* ```ts
* @Controller('api')
* class DefaultController {
* @Post('/:id')
* public controllerAction(): any {}
* }
* ```
*
* The above action `controllerAction` will then be mapped
* to `POST` requests that match pattern `api/:id`
*/
export function Post(path?: string): MethodDecorator {
return defineRouteDecorator(path, HttpMethod.POST);
}
/**
* Method decorator function for mapping Patch requests.
*
* `path` will be the routed path with the prefix of the
* parent controller prefix, E.g.
*
* ```ts
* @Controller('api')
* class DefaultController {
* @Patch('/:id')
* public controllerAction(): any {}
* }
* ```
*
* The above action `controllerAction` will then be mapped
* to `PATCH` requests that match pattern `api/:id`
*/
export function Patch(path?: string): MethodDecorator {
return defineRouteDecorator(path, HttpMethod.PATCH);
}
/**
* Method decorator function for mapping Delete requests.
*
* `path` will be the routed path with the prefix of the
* parent controller prefix, E.g.
*
* ```ts
* @Controller('api')
* class DefaultController {
* @Delete('/:id')
* public controllerAction(): any {}
* }
* ```
*
* The above action `controllerAction` will then be mapped
* to `DELETE` requests that match pattern `api/:id`
*/
export function Delete(path?: string): MethodDecorator {
return defineRouteDecorator(path, HttpMethod.DELETE);
}