deno.land / x / abc@v1.3.3 / 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
import type { HandlerFunc } from "./types.ts";import type { Context } from "./context.ts";
import { Node } from "./vendor/https/deno.land/x/router/mod.ts";import { hasTrailingSlash, NotFoundHandler } from "./util.ts";
export class Router { trees: Record<string, Node> = {};
add(method: string, path: string, h: HandlerFunc): void { if (path[0] !== "/") { path = `/${path}`; }
if (hasTrailingSlash(path)) { path = path.slice(0, path.length - 1); }
let root = this.trees[method]; if (!root) { root = new Node(); this.trees[method] = root; }
root.add(path, h); }
find(method: string, c: Context): HandlerFunc { const node = this.trees[method]; let path = c.path; if (hasTrailingSlash(path)) { path = path.slice(0, path.length - 1); } let h: HandlerFunc | undefined; if (node) { const [handle, params] = node.find(path); if (params) { for (const [k, v] of params) { c.params[k] = v; } }
if (handle) { h = handle as HandlerFunc; } }
return h ?? NotFoundHandler; }}
abc

Version Info

Tagged at
2 years ago