-
Notifications
You must be signed in to change notification settings - Fork 2
/
Router.ts
140 lines (128 loc) · 3.55 KB
/
Router.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
import { ServerRequest } from "https://deno.land/std/http/server.ts";
import { Endpoint, EndpointMap, RouteVariables } from "./types.ts";
export class Router {
private root: string = "/";
constructor(root = "/") {
this.root = root;
}
private _get: EndpointMap = new Map();
private _post: EndpointMap = new Map();
private _put: EndpointMap = new Map();
private _delete: EndpointMap = new Map();
private _patch: EndpointMap = new Map();
private _head: EndpointMap = new Map();
private _options: EndpointMap = new Map();
private process(
route: string,
func: (req: ServerRequest, vars: RouteVariables) => void,
target: EndpointMap,
) {
const withoutSlash = route === "/" ? "" : route.replace(/(\/\/)/g, "/");
const withSlash = withoutSlash + "/";
target.set(withoutSlash, func);
if (!(withoutSlash === "" && this.root === "/")) {
target.set(withSlash, func);
}
}
/**
* Register a GET endpoint
*
* @param route - URI to call this function
* @param func - The function that will be run when this endpoint is called.
*/
public get(
route: string,
func: (req: ServerRequest, vars: RouteVariables) => void,
) {
this.process(route, func, this._get);
}
/**
* Register a POST endpoint
*
* @param route - URI to call this function
* @param func - The function that will be run when this endpoint is called.
*/
public post(
route: string,
func: (req: ServerRequest, vars: RouteVariables) => void,
) {
this.process(route, func, this._post);
}
/**
* Register a PUT endpoint
*
* @param route - URI to call this function
* @param func - The function that will be run when this endpoint is called.
*/
public put(
route: string,
func: (req: ServerRequest, vars: RouteVariables) => void,
) {
this.process(route, func, this._put);
}
/**
* Register a DELETE endpoint
*
* @param route - URI to call this function
* @param func - The function that will be run when this endpoint is called.
*/
public delete(
route: string,
func: (req: ServerRequest, vars: RouteVariables) => void,
) {
this.process(route, func, this._delete);
}
/**
* Register a HEAD endpoint
*
* @param route - URI to call this function
* @param func - The function that will be run when this endpoint is called.
*/
public head(
route: string,
func: (req: ServerRequest, vars: RouteVariables) => void,
) {
this.process(route, func, this._head);
}
/**
* Register an OPTIONS endpoint
*
* @param route - URI to call this function
* @param func - The function that will be run when this endpoint is called.
*/
public options(
route: string,
func: (req: ServerRequest, vars: RouteVariables) => void,
) {
this.process(route, func, this._options);
}
/**
* Register a PATCH endpoint
*
* @param route - URI to call this function
* @param func - The function that will be run when this endpoint is called.
*/
public patch(
route: string,
func: (req: ServerRequest, vars: RouteVariables) => void,
) {
this.process(route, func, this._patch);
}
/**
* Getter used to register all endpoints in this router with the Server.
*/
public get routes(): Endpoint {
return {
uri: this.root,
routes: {
getRoutes: this._get,
postRoutes: this._post,
putRoutes: this._put,
deleteRoutes: this._delete,
optionsRoutes: this._options,
headRoutes: this._head,
patchRoutes: this._patch,
},
};
}
}