-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
router.ts
42 lines (38 loc) · 1.3 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
import { HandlerArgument, PathArgument, Route, Router } from "./types.ts"
export function router(): Router {
const routes: Map<string, Route> = new Map()
const LOCALHOST = "localhost"
const instance = {
routes,
get: (path: PathArgument, ...handlers: HandlerArgument[]) => {
return createRoute("GET", path, handlers)
},
post: (path: PathArgument, ...handlers: HandlerArgument[]) => {
return createRoute("POST", path, handlers)
},
put: (path: PathArgument, ...handlers: HandlerArgument[]) => {
return createRoute("PUT", path, handlers)
},
delete: (path: PathArgument, ...handlers: HandlerArgument[]) => {
return createRoute("DELETE", path, handlers)
},
patch: (path: PathArgument, ...handlers: HandlerArgument[]) => {
return createRoute("PATCH", path, handlers)
},
head: (path: PathArgument, ...handlers: HandlerArgument[]) => {
return createRoute("HEAD", path, handlers)
},
options: (path: PathArgument, ...handlers: HandlerArgument[]) => {
return createRoute("OPTIONS", path, handlers)
},
}
function createRoute(
method: string,
path: PathArgument,
handlers: HandlerArgument[],
) {
routes.set(`${method}#${LOCALHOST}#${path}`, { method, path, handlers })
return instance
}
return instance
}