-
Notifications
You must be signed in to change notification settings - Fork 568
/
requestMapping.ts
148 lines (133 loc) 路 3.22 KB
/
requestMapping.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/**
* 'HEAD', 'OPTIONS', 'GET', 'PUT', 'PATCH', 'POST', 'DELETE' 灏佽
*/
import { attachClassMetadata, WEB_ROUTER_KEY } from '../';
import { MiddlewareParamArray } from '../../interface';
export interface RouterOption {
/**
* router path, like "/api"
*/
path?: string | RegExp;
/**
* http method, like "get", "post"
*/
requestMethod: string;
/**
* router alias name
*/
routerName?: string;
/**
* which method decorator attached
*/
method?: string;
/**
* middleware array in router
*/
middleware?: MiddlewareParamArray;
/**
* router summary, for swagger
* @deprecated
*/
summary?: string;
/**
* router description, for swagger
* @deprecated
*/
description?: string;
/**
* ignore global prefix
*/
ignoreGlobalPrefix?: boolean;
}
export const RequestMethod = {
GET: 'get',
POST: 'post',
PUT: 'put',
DELETE: 'delete',
PATCH: 'patch',
ALL: 'all',
OPTIONS: 'options',
HEAD: 'head',
};
const defaultMetadata = {
path: '/',
requestMethod: RequestMethod.GET,
routerName: null,
middleware: [],
};
export const RequestMapping = (
metadata: RouterOption = defaultMetadata
): MethodDecorator => {
const path = metadata.path || '/';
const requestMethod = metadata.requestMethod || RequestMethod.GET;
const routerName = metadata.routerName;
const middleware = metadata.middleware;
return (target, key, descriptor: PropertyDescriptor) => {
attachClassMetadata(
WEB_ROUTER_KEY,
{
path,
requestMethod,
routerName,
method: key,
middleware,
summary: metadata?.summary || '',
description: metadata?.description || '',
ignoreGlobalPrefix: metadata?.ignoreGlobalPrefix ?? false,
} as RouterOption,
target
);
return descriptor;
};
};
const createMappingDecorator =
(method: string) =>
(
path?: string | RegExp,
routerOptions: {
routerName?: string;
middleware?: MiddlewareParamArray;
summary?: string;
description?: string;
ignoreGlobalPrefix?: boolean;
} = { middleware: [] }
): MethodDecorator => {
return RequestMapping(
Object.assign(routerOptions, {
requestMethod: method,
path,
})
);
};
/**
* Routes HTTP POST requests to the specified path.
*/
export const Post = createMappingDecorator(RequestMethod.POST);
/**
* Routes HTTP GET requests to the specified path.
*/
export const Get = createMappingDecorator(RequestMethod.GET);
/**
* Routes HTTP DELETE requests to the specified path.
*/
export const Del = createMappingDecorator(RequestMethod.DELETE);
/**
* Routes HTTP PUT requests to the specified path.
*/
export const Put = createMappingDecorator(RequestMethod.PUT);
/**
* Routes HTTP PATCH requests to the specified path.
*/
export const Patch = createMappingDecorator(RequestMethod.PATCH);
/**
* Routes HTTP OPTIONS requests to the specified path.
*/
export const Options = createMappingDecorator(RequestMethod.OPTIONS);
/**
* Routes HTTP HEAD requests to the specified path.
*/
export const Head = createMappingDecorator(RequestMethod.HEAD);
/**
* Routes all HTTP requests to the specified path.
*/
export const All = createMappingDecorator(RequestMethod.ALL);