hono 0.0.13 → 0.0.14
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 +24 -11
- package/dist/hono.d.ts +1 -0
- package/dist/hono.js +10 -2
- package/dist/middleware/default.js +4 -0
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -13,7 +13,7 @@ app.fire()
|
|
|
13
13
|
|
|
14
14
|
## Features
|
|
15
15
|
|
|
16
|
-
- **Ultra
|
|
16
|
+
- **Ultra fast** - the router is implemented with Trie-Tree structure.
|
|
17
17
|
- **Zero dependencies** - using only Web standard API.
|
|
18
18
|
- **Middleware** - builtin middleware, and you can make your own middleware.
|
|
19
19
|
- **Optimized** - for Cloudflare Workers.
|
|
@@ -70,12 +70,12 @@ Instance of `Hono` has these methods:
|
|
|
70
70
|
|
|
71
71
|
```js
|
|
72
72
|
// HTTP Methods
|
|
73
|
-
app.get('/', () =>
|
|
74
|
-
app.post('/', () =>
|
|
73
|
+
app.get('/', (c) => c.text('GET /'))
|
|
74
|
+
app.post('/', (c) => c.text('POST /'))
|
|
75
75
|
|
|
76
76
|
// Wildcard
|
|
77
|
-
app.get('/wild/*/card', () => {
|
|
78
|
-
return
|
|
77
|
+
app.get('/wild/*/card', (c) => {
|
|
78
|
+
return c.text('GET /wild/*/card')
|
|
79
79
|
})
|
|
80
80
|
```
|
|
81
81
|
|
|
@@ -83,7 +83,7 @@ app.get('/wild/*/card', () => {
|
|
|
83
83
|
|
|
84
84
|
```js
|
|
85
85
|
// Any HTTP methods
|
|
86
|
-
app.all('/hello', () =>
|
|
86
|
+
app.all('/hello', (c) => c.text('Any Method /hello'))
|
|
87
87
|
```
|
|
88
88
|
|
|
89
89
|
### Named Parameter
|
|
@@ -117,9 +117,9 @@ app
|
|
|
117
117
|
## async/await
|
|
118
118
|
|
|
119
119
|
```js
|
|
120
|
-
app.get('/fetch-url', async () => {
|
|
120
|
+
app.get('/fetch-url', async (c) => {
|
|
121
121
|
const response = await fetch('https://example.com/')
|
|
122
|
-
return
|
|
122
|
+
return c.text(`Status is ${response.status}`)
|
|
123
123
|
})
|
|
124
124
|
```
|
|
125
125
|
|
|
@@ -162,7 +162,7 @@ app.use('/message/*', async (c, next) => {
|
|
|
162
162
|
await c.res.headers.add('x-message', 'This is middleware!')
|
|
163
163
|
})
|
|
164
164
|
|
|
165
|
-
app.get('/message/hello', () => 'Hello Middleware!')
|
|
165
|
+
app.get('/message/hello', (c) => c.text('Hello Middleware!'))
|
|
166
166
|
```
|
|
167
167
|
|
|
168
168
|
### Custom 404 Response
|
|
@@ -178,6 +178,19 @@ app.use('*', async (c, next) => {
|
|
|
178
178
|
})
|
|
179
179
|
```
|
|
180
180
|
|
|
181
|
+
### Handling Error
|
|
182
|
+
|
|
183
|
+
```js
|
|
184
|
+
app.use('*', async (c, next) => {
|
|
185
|
+
try {
|
|
186
|
+
await next()
|
|
187
|
+
} catch (err) {
|
|
188
|
+
console.error(`${err}`)
|
|
189
|
+
c.res = new Response('Custom Error Message', { status: 500 })
|
|
190
|
+
}
|
|
191
|
+
})
|
|
192
|
+
```
|
|
193
|
+
|
|
181
194
|
### Complex Pattern
|
|
182
195
|
|
|
183
196
|
You can also do this:
|
|
@@ -239,7 +252,7 @@ app.use('/', (c, next) => {
|
|
|
239
252
|
### c.event
|
|
240
253
|
|
|
241
254
|
```js
|
|
242
|
-
// FetchEvent
|
|
255
|
+
// FetchEvent object
|
|
243
256
|
app.use('*', async (c, next) => {
|
|
244
257
|
c.event.waitUntil(
|
|
245
258
|
...
|
|
@@ -339,7 +352,7 @@ Make npm skeleton directory.
|
|
|
339
352
|
|
|
340
353
|
```sh
|
|
341
354
|
mkdir hono-example
|
|
342
|
-
|
|
355
|
+
cd hono-example
|
|
343
356
|
npm init -y
|
|
344
357
|
```
|
|
345
358
|
|
package/dist/hono.d.ts
CHANGED
package/dist/hono.js
CHANGED
|
@@ -122,16 +122,24 @@ class Hono {
|
|
|
122
122
|
return c.res;
|
|
123
123
|
}
|
|
124
124
|
async handleEvent(event) {
|
|
125
|
-
return this.dispatch(event.request, {}, event)
|
|
125
|
+
return this.dispatch(event.request, {}, event).catch((err) => {
|
|
126
|
+
return this.onError(err);
|
|
127
|
+
});
|
|
126
128
|
}
|
|
127
129
|
async fetch(request, env, event) {
|
|
128
|
-
return this.dispatch(request, env, event)
|
|
130
|
+
return this.dispatch(request, env, event).catch((err) => {
|
|
131
|
+
return this.onError(err);
|
|
132
|
+
});
|
|
129
133
|
}
|
|
130
134
|
fire() {
|
|
131
135
|
addEventListener('fetch', (event) => {
|
|
132
136
|
event.respondWith(this.handleEvent(event));
|
|
133
137
|
});
|
|
134
138
|
}
|
|
139
|
+
onError(err) {
|
|
140
|
+
console.error(err);
|
|
141
|
+
return new Response('Internal Server Error', { status: 500 });
|
|
142
|
+
}
|
|
135
143
|
notFound() {
|
|
136
144
|
return new Response('Not Found', { status: 404 });
|
|
137
145
|
}
|
|
@@ -8,5 +8,9 @@ const defaultMiddleware = async (c, next) => {
|
|
|
8
8
|
return url.searchParams.get(key);
|
|
9
9
|
};
|
|
10
10
|
await next();
|
|
11
|
+
if (c.res.body) {
|
|
12
|
+
const buff = await c.res.clone().arrayBuffer();
|
|
13
|
+
c.res.headers.append('Content-Length', buff.byteLength.toString());
|
|
14
|
+
}
|
|
11
15
|
};
|
|
12
16
|
exports.defaultMiddleware = defaultMiddleware;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hono",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"description": "Ultrafast web framework for Cloudflare Workers.",
|
|
3
|
+
"version": "0.0.14",
|
|
4
|
+
"description": "[炎] Ultrafast web framework for Cloudflare Workers.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
7
7
|
"files": [
|