hono 1.2.0 → 1.3.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 +92 -21
- package/dist/compose.test.d.ts +1 -0
- package/dist/compose.test.js +511 -0
- package/dist/context.d.ts +4 -1
- package/dist/context.js +31 -6
- package/dist/context.test.d.ts +1 -0
- package/dist/context.test.js +127 -0
- package/dist/hono.d.ts +21 -29
- package/dist/hono.js +27 -65
- package/dist/hono.test.d.ts +1 -0
- package/dist/hono.test.js +656 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -2
- package/dist/middleware/basic-auth/index.js +7 -8
- package/dist/middleware/basic-auth/index.test.d.ts +1 -0
- package/dist/middleware/basic-auth/index.test.js +119 -0
- package/dist/middleware/body-parse/index.test.d.ts +1 -0
- package/dist/middleware/body-parse/index.test.js +59 -0
- package/dist/middleware/cookie/index.d.ts +5 -2
- package/dist/middleware/cookie/index.js +9 -4
- package/dist/middleware/cookie/index.test.d.ts +1 -0
- package/dist/middleware/cookie/index.test.js +78 -0
- package/dist/middleware/cors/index.test.d.ts +1 -0
- package/dist/middleware/cors/index.test.js +59 -0
- package/dist/middleware/etag/index.test.d.ts +1 -0
- package/dist/middleware/etag/index.test.js +45 -0
- package/dist/middleware/graphql-server/index.js +1 -1
- package/dist/middleware/graphql-server/index.test.d.ts +1 -0
- package/dist/middleware/graphql-server/index.test.js +480 -0
- package/dist/middleware/graphql-server/parse-body.test.d.ts +1 -0
- package/dist/middleware/graphql-server/parse-body.test.js +57 -0
- package/dist/middleware/jwt/index.test.d.ts +1 -0
- package/dist/middleware/jwt/index.test.js +51 -0
- package/dist/middleware/logger/index.test.d.ts +1 -0
- package/dist/middleware/logger/index.test.js +49 -0
- package/dist/middleware/mustache/index.test.d.ts +1 -0
- package/dist/middleware/mustache/index.test.js +49 -0
- package/dist/middleware/powered-by/index.test.d.ts +1 -0
- package/dist/middleware/powered-by/index.test.js +15 -0
- package/dist/middleware/pretty-json/index.test.d.ts +1 -0
- package/dist/middleware/pretty-json/index.test.js +28 -0
- package/dist/middleware/serve-static/index.test.d.ts +1 -0
- package/dist/middleware/serve-static/index.test.js +58 -0
- package/dist/router/reg-exp-router/index.d.ts +1 -1
- package/dist/router/reg-exp-router/index.js +1 -1
- package/dist/router/reg-exp-router/router.js +1 -1
- package/dist/router/reg-exp-router/router.test.d.ts +1 -0
- package/dist/router/reg-exp-router/router.test.js +212 -0
- package/dist/router/reg-exp-router/trie.d.ts +3 -3
- package/dist/router/reg-exp-router/trie.js +1 -1
- package/dist/router/trie-router/index.d.ts +1 -1
- package/dist/router/trie-router/index.js +1 -1
- package/dist/router/trie-router/node.test.d.ts +1 -0
- package/dist/router/trie-router/node.test.js +351 -0
- package/dist/router/trie-router/router.d.ts +1 -1
- package/dist/router/trie-router/router.js +1 -1
- package/dist/router/trie-router/router.test.d.ts +1 -0
- package/dist/router/trie-router/router.test.js +98 -0
- package/dist/utils/body.test.d.ts +1 -0
- package/dist/utils/body.test.js +45 -0
- package/dist/utils/buffer.js +1 -1
- package/dist/utils/buffer.test.d.ts +1 -0
- package/dist/utils/buffer.test.js +36 -0
- package/dist/utils/cloudflare.test.d.ts +1 -0
- package/dist/utils/cloudflare.test.js +42 -0
- package/dist/utils/crypto.test.d.ts +1 -0
- package/dist/utils/crypto.test.js +15 -0
- package/dist/utils/encode.test.d.ts +1 -0
- package/dist/utils/encode.test.js +54 -0
- package/dist/utils/http-status.test.d.ts +1 -0
- package/dist/utils/http-status.test.js +8 -0
- package/dist/utils/jwt/jwt.test.d.ts +1 -0
- package/dist/utils/jwt/jwt.test.js +171 -0
- package/dist/utils/jwt/types.test.d.ts +1 -0
- package/dist/utils/jwt/types.test.js +12 -0
- package/dist/utils/mime.test.d.ts +1 -0
- package/dist/utils/mime.test.js +13 -0
- package/dist/utils/url.js +4 -0
- package/dist/utils/url.test.d.ts +1 -0
- package/dist/utils/url.test.js +96 -0
- package/package.json +3 -4
package/README.md
CHANGED
|
@@ -52,7 +52,7 @@ Fastest is hono - regexp-router
|
|
|
52
52
|
Routers used in Hono are really smart.
|
|
53
53
|
|
|
54
54
|
- **TrieRouter**(default) - Implemented with Trie tree structure.
|
|
55
|
-
- **RegExpRouter** - Match
|
|
55
|
+
- **RegExpRouter** - Match the route with using one big Regex made before dispatch.
|
|
56
56
|
|
|
57
57
|
## Hono in 1 minute
|
|
58
58
|
|
|
@@ -87,22 +87,20 @@ import { etag } from 'hono/etag'
|
|
|
87
87
|
import { logger } from 'hono/logger'
|
|
88
88
|
|
|
89
89
|
const app = new Hono()
|
|
90
|
-
app.use('*', etag(),
|
|
90
|
+
app.use('*', etag(), logger())
|
|
91
91
|
```
|
|
92
92
|
|
|
93
93
|
And, the routing of Hono is so flexible. It's easy to construct large web applications.
|
|
94
94
|
|
|
95
95
|
```ts
|
|
96
|
-
import { Hono
|
|
97
|
-
import {
|
|
98
|
-
|
|
99
|
-
const app = new Hono()
|
|
96
|
+
import { Hono } from 'hono'
|
|
97
|
+
import { basicAuth } from 'hono/basic-auth'
|
|
100
98
|
|
|
101
|
-
const v1 = new
|
|
99
|
+
const v1 = new Hono()
|
|
102
100
|
v1.get('/posts', (c) => {
|
|
103
101
|
return c.text('list pots')
|
|
104
102
|
})
|
|
105
|
-
.post(
|
|
103
|
+
.post(basicAuth({ username, password }), (c) => {
|
|
106
104
|
return c.text('created!', 201)
|
|
107
105
|
})
|
|
108
106
|
.get('/posts/:id', (c) => {
|
|
@@ -110,6 +108,7 @@ v1.get('/posts', (c) => {
|
|
|
110
108
|
return c.text(`your id is ${id}`)
|
|
111
109
|
})
|
|
112
110
|
|
|
111
|
+
const app = new Hono()
|
|
113
112
|
app.route('/v1', v1)
|
|
114
113
|
```
|
|
115
114
|
|
|
@@ -140,7 +139,7 @@ An instance of `Hono` has these methods.
|
|
|
140
139
|
|
|
141
140
|
- app.**HTTP_METHOD**(\[path,\] handler|middleware...)
|
|
142
141
|
- app.**all**(\[path,\] handler|middleware...)
|
|
143
|
-
- app.**route**(path, \[
|
|
142
|
+
- app.**route**(path, \[app\])
|
|
144
143
|
- app.**use**(\[path,\] middleware)
|
|
145
144
|
- app.**notFound**(handler)
|
|
146
145
|
- app.**onError**(err, handler)
|
|
@@ -156,6 +155,8 @@ An instance of `Hono` has these methods.
|
|
|
156
155
|
// HTTP Methods
|
|
157
156
|
app.get('/', (c) => c.text('GET /'))
|
|
158
157
|
app.post('/', (c) => c.text('POST /'))
|
|
158
|
+
app.put('/', (c) => c.text('PUT /'))
|
|
159
|
+
app.delete('/', (c) => c.text('DELETE /'))
|
|
159
160
|
|
|
160
161
|
// Wildcard
|
|
161
162
|
app.get('/wild/*/card', (c) => {
|
|
@@ -175,12 +176,20 @@ app.get('/user/:name', (c) => {
|
|
|
175
176
|
})
|
|
176
177
|
```
|
|
177
178
|
|
|
179
|
+
or all parameters at once:
|
|
180
|
+
|
|
181
|
+
```ts
|
|
182
|
+
app.get('/posts/:id/comment/:comment_id', (c) => {
|
|
183
|
+
const { id, comment_id } = c.req.param()
|
|
184
|
+
...
|
|
185
|
+
})
|
|
186
|
+
```
|
|
187
|
+
|
|
178
188
|
### Regexp
|
|
179
189
|
|
|
180
190
|
```ts
|
|
181
191
|
app.get('/post/:date{[0-9]+}/:title{[a-z]+}', (c) => {
|
|
182
|
-
const date = c.req.param(
|
|
183
|
-
const title = c.req.param('title')
|
|
192
|
+
const { date, title } = c.req.param()
|
|
184
193
|
...
|
|
185
194
|
})
|
|
186
195
|
```
|
|
@@ -219,12 +228,12 @@ app.get('/fetch-url', async (c) => {
|
|
|
219
228
|
})
|
|
220
229
|
```
|
|
221
230
|
|
|
222
|
-
##
|
|
231
|
+
## Grouping
|
|
223
232
|
|
|
224
|
-
`
|
|
233
|
+
Group the routes with `Hono` instance and add them to the main app with `route` method.
|
|
225
234
|
|
|
226
235
|
```ts
|
|
227
|
-
const book = new
|
|
236
|
+
const book = new Hono()
|
|
228
237
|
|
|
229
238
|
book.get('/', (c) => c.text('List Books')) // GET /book
|
|
230
239
|
book.get('/:id', (c) => {
|
|
@@ -234,6 +243,7 @@ book.get('/:id', (c) => {
|
|
|
234
243
|
})
|
|
235
244
|
book.post('/', (c) => c.text('Create Book')) // POST /book
|
|
236
245
|
|
|
246
|
+
const app = new Hono()
|
|
237
247
|
app.route('/book', book)
|
|
238
248
|
```
|
|
239
249
|
|
|
@@ -338,6 +348,12 @@ app.get('/search', (c) => {
|
|
|
338
348
|
...
|
|
339
349
|
})
|
|
340
350
|
|
|
351
|
+
// Get all params at once
|
|
352
|
+
app.get('/search', (c) => {
|
|
353
|
+
const { q, limit, offset } = c.req.query()
|
|
354
|
+
...
|
|
355
|
+
})
|
|
356
|
+
|
|
341
357
|
// Captured params
|
|
342
358
|
app.get('/entry/:id', (c) => {
|
|
343
359
|
const id = c.req.param('id')
|
|
@@ -427,8 +443,8 @@ app.get('/redirect-permanently', (c) => c.redirect('/', 301))
|
|
|
427
443
|
|
|
428
444
|
```ts
|
|
429
445
|
// Response object
|
|
430
|
-
app.use('/', (c, next) => {
|
|
431
|
-
next()
|
|
446
|
+
app.use('/', async (c, next) => {
|
|
447
|
+
await next()
|
|
432
448
|
c.res.headers.append('X-Debug', 'Debug message')
|
|
433
449
|
})
|
|
434
450
|
```
|
|
@@ -437,11 +453,11 @@ app.use('/', (c, next) => {
|
|
|
437
453
|
|
|
438
454
|
```ts
|
|
439
455
|
// FetchEvent object
|
|
440
|
-
app.
|
|
456
|
+
app.get('/foo', async (c) => {
|
|
441
457
|
c.event.waitUntil(
|
|
442
|
-
|
|
458
|
+
c.env.KV.put(key, data)
|
|
443
459
|
)
|
|
444
|
-
|
|
460
|
+
...
|
|
445
461
|
})
|
|
446
462
|
```
|
|
447
463
|
|
|
@@ -546,7 +562,7 @@ npx wrangler dev
|
|
|
546
562
|
Deploy to Cloudflare. That's all!
|
|
547
563
|
|
|
548
564
|
```
|
|
549
|
-
npx wrangler publish index.ts
|
|
565
|
+
npx wrangler publish ./src/index.ts
|
|
550
566
|
```
|
|
551
567
|
|
|
552
568
|
## Starter template
|
|
@@ -559,7 +575,62 @@ To generate a project skelton, run this command.
|
|
|
559
575
|
npx create-cloudflare my-app https://github.com/honojs/hono-minimal
|
|
560
576
|
```
|
|
561
577
|
|
|
562
|
-
##
|
|
578
|
+
## Practical Example
|
|
579
|
+
|
|
580
|
+
How about writing web API with Hono?
|
|
581
|
+
|
|
582
|
+
```ts
|
|
583
|
+
import { Hono } from 'hono'
|
|
584
|
+
import { cors } from 'hono/cors'
|
|
585
|
+
import { basicAuth } from 'hono/basic-auth'
|
|
586
|
+
import { prettyJSON } from 'hono/pretty-json'
|
|
587
|
+
import { getPosts, getPosts, createPost } from './model'
|
|
588
|
+
|
|
589
|
+
const app = new Hono()
|
|
590
|
+
app.get('/', (c) => c.text('Pretty Blog API'))
|
|
591
|
+
app.use('*', prettyJSON())
|
|
592
|
+
app.notFound((c) => c.json({ message: 'Not Found', ok: false }, 404))
|
|
593
|
+
|
|
594
|
+
export interface Bindings {
|
|
595
|
+
USERNAME: string
|
|
596
|
+
PASSWORD: string
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
const api = new Hono<Bindings>()
|
|
600
|
+
|
|
601
|
+
api.get('/posts', (c) => {
|
|
602
|
+
const { limit, offset } = c.req.query()
|
|
603
|
+
const posts = getPosts({ limit, offset })
|
|
604
|
+
return c.json({ posts })
|
|
605
|
+
})
|
|
606
|
+
|
|
607
|
+
api.get('/posts/:id', (c) => {
|
|
608
|
+
const id = c.req.param('id')
|
|
609
|
+
const post = getPost({ id })
|
|
610
|
+
return c.json({ post })
|
|
611
|
+
})
|
|
612
|
+
|
|
613
|
+
api.post(
|
|
614
|
+
'/posts',
|
|
615
|
+
async (c, next) => {
|
|
616
|
+
const auth = basicAuth({ username: c.env.USERNAME, password: c.env.PASSWORD })
|
|
617
|
+
await auth(c, next)
|
|
618
|
+
},
|
|
619
|
+
async (c) => {
|
|
620
|
+
const post = await c.req.json<POST>()
|
|
621
|
+
const ok = createPost({ post })
|
|
622
|
+
return c.json({ ok })
|
|
623
|
+
}
|
|
624
|
+
)
|
|
625
|
+
|
|
626
|
+
app.use('/posts/*', cors())
|
|
627
|
+
|
|
628
|
+
app.route('/api', api)
|
|
629
|
+
|
|
630
|
+
export default app
|
|
631
|
+
```
|
|
632
|
+
|
|
633
|
+
## Other Examples
|
|
563
634
|
|
|
564
635
|
- Hono Examples - <https://github.com/honojs/examples>
|
|
565
636
|
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|