hono 1.4.7 → 1.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md
CHANGED
|
@@ -556,9 +556,9 @@ import { RegExpRouter } from 'hono/router/reg-exp-router'
|
|
|
556
556
|
const app = new Hono({ router: new RegExpRouter() })
|
|
557
557
|
```
|
|
558
558
|
|
|
559
|
-
## Routing
|
|
559
|
+
## Routing priority
|
|
560
560
|
|
|
561
|
-
|
|
561
|
+
Handlers or middleware will be executed in registration order.
|
|
562
562
|
|
|
563
563
|
```ts
|
|
564
564
|
app.get('/book/a', (c) => c.text('a')) // a
|
|
@@ -566,27 +566,37 @@ app.get('/book/:slug', (c) => c.text('common')) // common
|
|
|
566
566
|
```
|
|
567
567
|
|
|
568
568
|
```http
|
|
569
|
-
GET /book/a ---> `a`
|
|
570
|
-
GET /book/b ---> `common`
|
|
569
|
+
GET /book/a ---> `a`
|
|
570
|
+
GET /book/b ---> `common`
|
|
571
571
|
```
|
|
572
572
|
|
|
573
|
-
|
|
573
|
+
When a handler is executed, the process will be stopped.
|
|
574
574
|
|
|
575
575
|
```ts
|
|
576
|
-
app.get('
|
|
577
|
-
app.get('/
|
|
578
|
-
app.get('/api/posts/:id', 'e') // score 3.3
|
|
579
|
-
app.get('/api/posts/123', 'f') // score 3.4
|
|
580
|
-
app.get('/*/*/:id', 'g') // score 3.5
|
|
581
|
-
app.get('/api/posts/*/comment', 'h') // score 4.6 - not match
|
|
582
|
-
app.get('*', 'a') // score 0.7
|
|
583
|
-
app.get('*', 'b') // score 0.8
|
|
576
|
+
app.get('*', (c) => c.text('common')) // common
|
|
577
|
+
app.get('/foo', (c) => c.text('foo')) // foo
|
|
584
578
|
```
|
|
585
579
|
|
|
586
|
-
```
|
|
587
|
-
GET /
|
|
588
|
-
|
|
589
|
-
|
|
580
|
+
```http
|
|
581
|
+
GET /foo ---> `common` // foo will not be dispatched
|
|
582
|
+
```
|
|
583
|
+
|
|
584
|
+
If you have the middleware that you want to execute, write the code above the handler.
|
|
585
|
+
|
|
586
|
+
```ts
|
|
587
|
+
app.use('*', logger())
|
|
588
|
+
app.get('/foo', (c) => c.text('foo'))
|
|
589
|
+
```
|
|
590
|
+
|
|
591
|
+
If you want a "_fallback_" handler, write the code below the other handler.
|
|
592
|
+
|
|
593
|
+
```ts
|
|
594
|
+
app.get('/foo', (c) => c.text('foo')) // foo
|
|
595
|
+
app.get('*', (c) => c.text('fallback')) // fallback
|
|
596
|
+
```
|
|
597
|
+
|
|
598
|
+
```http
|
|
599
|
+
GET /bar ---> `fallback`
|
|
590
600
|
```
|
|
591
601
|
|
|
592
602
|
## Cloudflare Workers with Hono
|
|
@@ -65,9 +65,7 @@ function compareRoute(a, b) {
|
|
|
65
65
|
return i === b.hint.regExpComponents.length || a.hint.endWithWildcard ? 1 : 0;
|
|
66
66
|
}
|
|
67
67
|
function compareHandler(a, b) {
|
|
68
|
-
return a.
|
|
69
|
-
? a.componentsLength - b.componentsLength
|
|
70
|
-
: a.index - b.index;
|
|
68
|
+
return a.index - b.index;
|
|
71
69
|
}
|
|
72
70
|
function getSortedHandlers(handlers) {
|
|
73
71
|
return [...handlers].sort(compareHandler).map((h) => h.handler);
|
|
@@ -158,7 +156,6 @@ class RegExpRouter {
|
|
|
158
156
|
const handlerWithSortIndex = {
|
|
159
157
|
index,
|
|
160
158
|
handler,
|
|
161
|
-
componentsLength: hint.components.length || 1,
|
|
162
159
|
};
|
|
163
160
|
for (let i = 0, len = routes.length; i < len; i++) {
|
|
164
161
|
if (routes[i].method === method && routes[i].path === path) {
|
|
@@ -318,9 +315,7 @@ class RegExpRouter {
|
|
|
318
315
|
}
|
|
319
316
|
}
|
|
320
317
|
if (routes[j].hint.components.length < routes[i].hint.components.length) {
|
|
321
|
-
const componentsLength = routes[j].hint.components.length || 1;
|
|
322
318
|
routes[j].middleware.push(...routes[i].handlers.map((h) => ({
|
|
323
|
-
componentsLength,
|
|
324
319
|
index: h.index,
|
|
325
320
|
handler: h.handler,
|
|
326
321
|
})));
|
|
@@ -66,18 +66,11 @@ class Node {
|
|
|
66
66
|
parentPatterns.push(...curNode.patterns);
|
|
67
67
|
curNode = curNode.children[p];
|
|
68
68
|
}
|
|
69
|
-
let score = 1;
|
|
70
|
-
if (path === '*') {
|
|
71
|
-
score = score + this.order * 0.01;
|
|
72
|
-
}
|
|
73
|
-
else {
|
|
74
|
-
score = parts.length + this.order * 0.01;
|
|
75
|
-
}
|
|
76
69
|
if (!curNode.methods.length) {
|
|
77
70
|
curNode.methods = [];
|
|
78
71
|
}
|
|
79
72
|
const m = {};
|
|
80
|
-
const handlerSet = { handler: handler, name: this.name, score:
|
|
73
|
+
const handlerSet = { handler: handler, name: this.name, score: this.order };
|
|
81
74
|
m[method] = handlerSet;
|
|
82
75
|
curNode.methods.push(m);
|
|
83
76
|
return curNode;
|
|
@@ -90,9 +83,6 @@ class Node {
|
|
|
90
83
|
const handlerSet = m[method] || m[router_1.METHOD_NAME_ALL];
|
|
91
84
|
if (handlerSet !== undefined) {
|
|
92
85
|
const hs = { ...handlerSet };
|
|
93
|
-
if (wildcard) {
|
|
94
|
-
hs.score = handlerSet.score - 1;
|
|
95
|
-
}
|
|
96
86
|
handlerSets.push(hs);
|
|
97
87
|
return;
|
|
98
88
|
}
|