Comparing version 0.0.13 to 0.0.14
@@ -42,3 +42,4 @@ /// <reference types="@cloudflare/workers-types" /> | ||
fire(): void; | ||
onError(err: any): Response; | ||
notFound(): Response; | ||
} |
@@ -125,6 +125,10 @@ "use strict"; | ||
async handleEvent(event) { | ||
return this.dispatch(event.request, {}, event); | ||
return this.dispatch(event.request, {}, event).catch((err) => { | ||
return this.onError(err); | ||
}); | ||
} | ||
async fetch(request, env, event) { | ||
return this.dispatch(request, env, event); | ||
return this.dispatch(request, env, event).catch((err) => { | ||
return this.onError(err); | ||
}); | ||
} | ||
@@ -136,2 +140,6 @@ fire() { | ||
} | ||
onError(err) { | ||
console.error(err); | ||
return new Response('Internal Server Error', { status: 500 }); | ||
} | ||
notFound() { | ||
@@ -138,0 +146,0 @@ return new Response('Not Found', { status: 404 }); |
@@ -11,3 +11,7 @@ "use strict"; | ||
await next(); | ||
if (c.res.body) { | ||
const buff = await c.res.clone().arrayBuffer(); | ||
c.res.headers.append('Content-Length', buff.byteLength.toString()); | ||
} | ||
}; | ||
exports.defaultMiddleware = defaultMiddleware; |
{ | ||
"name": "hono", | ||
"version": "0.0.13", | ||
"description": "Ultrafast web framework for Cloudflare Workers.", | ||
"version": "0.0.14", | ||
"description": "[炎] Ultrafast web framework for Cloudflare Workers.", | ||
"main": "dist/index.js", | ||
@@ -6,0 +6,0 @@ "types": "dist/index.d.ts", |
@@ -16,3 +16,3 @@ # Hono | ||
- **Ultra Fast** - the router is implemented with Trie-Tree structure. | ||
- **Ultra fast** - the router is implemented with Trie-Tree structure. | ||
- **Zero dependencies** - using only Web standard API. | ||
@@ -74,8 +74,8 @@ - **Middleware** - builtin middleware, and you can make your own middleware. | ||
// HTTP Methods | ||
app.get('/', () => new Response('GET /')) | ||
app.post('/', () => new Response('POST /')) | ||
app.get('/', (c) => c.text('GET /')) | ||
app.post('/', (c) => c.text('POST /')) | ||
// Wildcard | ||
app.get('/wild/*/card', () => { | ||
return new Response('GET /wild/*/card') | ||
app.get('/wild/*/card', (c) => { | ||
return c.text('GET /wild/*/card') | ||
}) | ||
@@ -88,3 +88,3 @@ ``` | ||
// Any HTTP methods | ||
app.all('/hello', () => new Response('ALL Method /hello')) | ||
app.all('/hello', (c) => c.text('Any Method /hello')) | ||
``` | ||
@@ -123,5 +123,5 @@ | ||
```js | ||
app.get('/fetch-url', async () => { | ||
app.get('/fetch-url', async (c) => { | ||
const response = await fetch('https://example.com/') | ||
return new Response(`Status is ${response.status}`) | ||
return c.text(`Status is ${response.status}`) | ||
}) | ||
@@ -169,3 +169,3 @@ ``` | ||
app.get('/message/hello', () => 'Hello Middleware!') | ||
app.get('/message/hello', (c) => c.text('Hello Middleware!')) | ||
``` | ||
@@ -186,2 +186,15 @@ | ||
### Handling Error | ||
```js | ||
app.use('*', async (c, next) => { | ||
try { | ||
await next() | ||
} catch (err) { | ||
console.error(`${err}`) | ||
c.res = new Response('Custom Error Message', { status: 500 }) | ||
} | ||
}) | ||
``` | ||
### Complex Pattern | ||
@@ -248,3 +261,3 @@ | ||
```js | ||
// FetchEvent objest | ||
// FetchEvent object | ||
app.use('*', async (c, next) => { | ||
@@ -349,3 +362,3 @@ c.event.waitUntil( | ||
mkdir hono-example | ||
ch hono-example | ||
cd hono-example | ||
npm init -y | ||
@@ -352,0 +365,0 @@ ``` |
35340
737
434