-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathgroup.ts
129 lines (118 loc) · 3.36 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import type { HandlerFunc, MiddlewareFunc } from "./types.ts";
import type { Application } from "./app.ts";
import { join } from "./vendor/https/deno.land/std/path/mod.ts";
export class Group {
prefix: string;
middleware: MiddlewareFunc[];
app: Application;
#willBeAdded: Array<
[method: string, path: string, h: HandlerFunc, m: MiddlewareFunc[]]
>;
constructor(opts: { app: Application; prefix: string }) {
this.prefix = opts.prefix || "";
this.app = opts.app || ({} as Application);
this.middleware = [];
this.#willBeAdded = [];
}
use(...m: MiddlewareFunc[]): Group {
this.middleware.push(...m);
return this;
}
connect(path: string, h: HandlerFunc, ...m: MiddlewareFunc[]): Group {
this.#willBeAdded.push(["CONNECT", path, h, m]);
return this;
}
delete(path: string, h: HandlerFunc, ...m: MiddlewareFunc[]): Group {
this.#willBeAdded.push(["DELETE", path, h, m]);
return this;
}
get(path: string, h: HandlerFunc, ...m: MiddlewareFunc[]): Group {
this.#willBeAdded.push(["GET", path, h, m]);
return this;
}
head(path: string, h: HandlerFunc, ...m: MiddlewareFunc[]): Group {
this.#willBeAdded.push(["HEAD", path, h, m]);
return this;
}
options(path: string, h: HandlerFunc, ...m: MiddlewareFunc[]): Group {
this.#willBeAdded.push(["OPTIONS", path, h, m]);
return this;
}
patch(path: string, h: HandlerFunc, ...m: MiddlewareFunc[]): Group {
this.#willBeAdded.push(["PATCH", path, h, m]);
return this;
}
post(path: string, h: HandlerFunc, ...m: MiddlewareFunc[]): Group {
this.#willBeAdded.push(["POST", path, h, m]);
return this;
}
put(path: string, h: HandlerFunc, ...m: MiddlewareFunc[]): Group {
this.#willBeAdded.push(["PUT", path, h, m]);
return this;
}
trace(path: string, h: HandlerFunc, ...m: MiddlewareFunc[]): Group {
this.#willBeAdded.push(["TRACE", path, h, m]);
return this;
}
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.#willBeAdded.push([method, path, h, m]);
}
return this;
}
match(
methods: string[],
path: string,
h: HandlerFunc,
...m: MiddlewareFunc[]
): Group {
for (const method of methods) {
this.#willBeAdded.push([method, path, h, m]);
}
return this;
}
add(
method: string,
path: string,
handler: HandlerFunc,
...middleware: MiddlewareFunc[]
): Group {
this.#willBeAdded.push([method, path, handler, 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;
}
θapplyMiddleware(): void {
for (const [method, path, handler, middleware] of this.#willBeAdded) {
this.app.add(
method,
this.prefix + path,
handler,
...this.middleware,
...middleware,
);
}
this.#willBeAdded = [];
}
}