-
Notifications
You must be signed in to change notification settings - Fork 46
/
group.ts
112 lines (103 loc) · 2.92 KB
/
group.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
import type { MiddlewareFunc, HandlerFunc } from "./types.ts";
import type { Application } from "./app.ts";
import { join } from "./vendor/https/deno.land/std/path/mod.ts";
import { NotFoundHandler } from "./util.ts";
export class Group {
prefix: string;
middleware: MiddlewareFunc[];
app: Application;
constructor(opts: { app: Application; prefix: string }) {
this.prefix = opts.prefix || "";
this.app = opts.app || ({} as Application);
this.middleware = [];
}
use(...m: MiddlewareFunc[]): Group {
this.middleware.push(...m);
if (this.middleware.length !== 0) {
this.any("", NotFoundHandler);
}
return this;
}
connect(path: string, h: HandlerFunc, ...m: MiddlewareFunc[]): Group {
return this.add("CONNECT", path, h, ...m);
}
delete(path: string, h: HandlerFunc, ...m: MiddlewareFunc[]): Group {
return this.add("DELETE", path, h, ...m);
}
get(path: string, h: HandlerFunc, ...m: MiddlewareFunc[]): Group {
return this.add("GET", path, h, ...m);
}
head(path: string, h: HandlerFunc, ...m: MiddlewareFunc[]): Group {
return this.add("HEAD", path, h, ...m);
}
options(path: string, h: HandlerFunc, ...m: MiddlewareFunc[]): Group {
return this.add("OPTIONS", path, h, ...m);
}
patch(path: string, h: HandlerFunc, ...m: MiddlewareFunc[]): Group {
return this.add("PATCH", path, h, ...m);
}
post(path: string, h: HandlerFunc, ...m: MiddlewareFunc[]): Group {
return this.add("POST", path, h, ...m);
}
put(path: string, h: HandlerFunc, ...m: MiddlewareFunc[]): Group {
return this.add("PUT", path, h, ...m);
}
trace(path: string, h: HandlerFunc, ...m: MiddlewareFunc[]): Group {
return this.add("TRACE", path, h, ...m);
}
any(path: string, h: HandlerFunc, ...m: MiddlewareFunc[]): Group {
const methods = [
"CONNECT",
"DELETE",
"GET",
"HEAD",
"OPTIONS",
"PATCH",
"POST",
"PUT",
"TRACE",
];
for (const method of methods) {
this.add(method, path, h, ...m);
}
return this;
}
match(
methods: string[],
path: string,
h: HandlerFunc,
...m: MiddlewareFunc[]
): Group {
for (const method of methods) {
this.add(method, path, h, ...m);
}
return this;
}
add(
method: string,
path: string,
handler: HandlerFunc,
...middleware: MiddlewareFunc[]
): Group {
this.app.add(
method,
this.prefix + path,
handler,
...this.middleware,
...middleware,
);
return this;
}
static(prefix: string, root: string): Group {
this.app.static(join(this.prefix, prefix), root);
return this;
}
file(p: string, filepath: string, ...m: MiddlewareFunc[]): Group {
this.app.file(join(this.prefix, p), filepath, ...m);
return this;
}
group(prefix: string, ...m: MiddlewareFunc[]): Group {
const g = this.app.group(this.prefix + prefix, ...this.middleware, ...m);
return g;
}
}