-
Notifications
You must be signed in to change notification settings - Fork 0
/
decorators.ts
83 lines (80 loc) · 3.35 KB
/
decorators.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
import { THandlers, HttpRequest, HttpResponse, NextFunction, THandler } from "./types.ts";
import { findFns } from "./utils.ts";
function withMethodDecorator(method: string, path: string = "") {
return (_: any, __: any, des: PropertyDescriptor) => {
if (typeof des.value === "object") {
des.value = { method, path, handlers: des.value?.handlers };
} else {
des.value = { method, path, handlers: [des.value] };
}
return des;
}
}
export const Get = (path: string = "") => withMethodDecorator("GET", path);
export const Post = (path: string = "") => withMethodDecorator("POST", path);
export const Put = (path: string = "") => withMethodDecorator("PUT", path);
export const Delete = (path: string = "") => withMethodDecorator("DELETE", path);
export const Any = (path: string = "") => withMethodDecorator("ANY", path);
export const Options = (path: string = "") => withMethodDecorator("OPTIONS", path);
export const Head = (path: string = "") => withMethodDecorator("HEAD", path);
export const Trace = (path: string = "") => withMethodDecorator("TRACE", path);
export const Connect = (path: string = "") => withMethodDecorator("CONNECT", path);
export const Patch = (path: string = "") => withMethodDecorator("PATCH", path);
export function Wares<
Req extends HttpRequest = HttpRequest,
Res extends HttpResponse = HttpResponse
>(...handlers: THandlers<Req, Res>) {
let fns = findFns(handlers);
return (_: any, __: any, des: PropertyDescriptor) => {
if (typeof des.value === 'object') {
let obj = des.value;
obj.handlers = fns.concat(obj.handlers);
des.value = obj;
return des;
}
}
}
export function Status(status: number) {
return (_: any, __: any, des: PropertyDescriptor) => {
let obj = typeof des.value === "object" ? des.value : { handlers: [des.value] };
obj.handlers = [(req: HttpRequest, res: HttpResponse, next: NextFunction) => {
res.status(status);
next();
}].concat(obj.handlers);
des.value = obj;
return des;
}
}
export function Header(header: { [k: string]: any } | THandler) {
return (_: any, __: any, des: PropertyDescriptor) => {
let obj = typeof des.value === "object" ? des.value : { handlers: [des.value] };
obj.handlers = [(req: HttpRequest, res: HttpResponse, next: NextFunction) => {
let _headers = typeof header === 'function' ? header(req, res, next) : header;
res.header(_headers);
next();
}].concat(obj.handlers);
des.value = obj;
return des;
}
}
export function Controller(path: string = "") {
return (target: Function) => {
let c_routes = [] as any;
const protos = Object.getOwnPropertyNames(target.prototype);
for (const key of protos) {
let descriptor = Object.getOwnPropertyDescriptor(target.prototype, key);
if (descriptor) {
const el = descriptor.value;
if (el !== target && typeof el === 'object') {
c_routes.push({
method: el.method,
path: path + el.path,
handlers: el.handlers
});
}
}
}
target.prototype.c_routes = c_routes;
return void 0;
}
}