hono 0.5.9 → 1.1.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 +107 -45
- package/dist/context.d.ts +10 -11
- package/dist/context.js +51 -42
- package/dist/hono.d.ts +29 -31
- package/dist/hono.js +61 -55
- package/dist/index.d.ts +1 -1
- package/dist/middleware/basic-auth/{basic-auth.d.ts → index.d.ts} +2 -1
- package/dist/middleware/basic-auth/{basic-auth.js → index.js} +2 -2
- package/dist/middleware/body-parse/index.d.ts +8 -0
- package/dist/middleware/body-parse/{body-parse.js → index.js} +0 -0
- package/dist/middleware/cookie/{cookie.d.ts → index.d.ts} +4 -5
- package/dist/middleware/cookie/{cookie.js → index.js} +1 -1
- package/dist/middleware/cors/{cors.d.ts → index.d.ts} +2 -1
- package/dist/middleware/cors/{cors.js → index.js} +0 -0
- package/dist/middleware/etag/{etag.d.ts → index.d.ts} +2 -1
- package/dist/middleware/etag/{etag.js → index.js} +1 -1
- package/dist/middleware/graphql-server/{graphql-server.d.ts → index.d.ts} +3 -3
- package/dist/middleware/graphql-server/{graphql-server.js → index.js} +6 -4
- package/dist/middleware/graphql-server/parse-body.d.ts +1 -3
- package/dist/middleware/graphql-server/parse-body.js +3 -0
- package/dist/middleware/jwt/index.d.ts +6 -0
- package/dist/middleware/jwt/index.js +51 -0
- package/dist/middleware/logger/{logger.d.ts → index.d.ts} +2 -1
- package/dist/middleware/logger/{logger.js → index.js} +3 -5
- package/dist/middleware/mustache/{mustache.d.ts → index.d.ts} +2 -1
- package/dist/middleware/mustache/{mustache.js → index.js} +3 -9
- package/dist/middleware/powered-by/index.d.ts +3 -0
- package/dist/middleware/powered-by/{powered-by.js → index.js} +0 -1
- package/dist/middleware/pretty-json/index.d.ts +7 -0
- package/dist/middleware/pretty-json/index.js +11 -0
- package/dist/middleware/serve-static/{serve-static.d.ts → index.d.ts} +2 -1
- package/dist/middleware/serve-static/{serve-static.js → index.js} +1 -1
- 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/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.d.ts +1 -1
- package/dist/router/trie-router/node.js +1 -1
- package/dist/router/trie-router/router.d.ts +1 -1
- package/dist/router/trie-router/router.js +1 -1
- package/dist/utils/buffer.d.ts +1 -0
- package/dist/utils/buffer.js +10 -2
- package/dist/utils/crypto.d.ts +0 -2
- package/dist/utils/crypto.js +1 -45
- package/dist/utils/encode.d.ts +7 -0
- package/dist/utils/encode.js +105 -0
- package/dist/utils/http-status.d.ts +2 -1
- package/dist/utils/http-status.js +6 -2
- package/dist/utils/jwt/index.d.ts +1 -0
- package/dist/utils/jwt/index.js +27 -0
- package/dist/utils/jwt/jwt.d.ts +7 -0
- package/dist/utils/jwt/jwt.js +98 -0
- package/dist/utils/jwt/types.d.ts +20 -0
- package/dist/utils/jwt/types.js +44 -0
- package/package.json +58 -56
- package/dist/middleware/body-parse/body-parse.d.ts +0 -2
- package/dist/middleware/powered-by/powered-by.d.ts +0 -2
package/README.md
CHANGED
|
@@ -1,20 +1,26 @@
|
|
|
1
|
-
|
|
1
|
+
<div align="center">
|
|
2
|
+
<a href="https://github.com/honojs/hono">
|
|
3
|
+
<img src="https://raw.githubusercontent.com/honojs/hono/master/docs/images/hono-title.png" width="500" height="auto" alt="Hono"/>
|
|
4
|
+
</a>
|
|
5
|
+
</div>
|
|
6
|
+
|
|
7
|
+
<hr />
|
|
2
8
|
|
|
3
9
|
<p>
|
|
4
|
-
<a href="https://github.com/
|
|
10
|
+
<a href="https://github.com/honojs/hono/blob/master/README.md">English</a>
|
|
5
11
|
·
|
|
6
|
-
<a href="https://github.com/
|
|
12
|
+
<a href="https://github.com/honojs/hono/blob/master/docs/README.ja.md">日本語</a>
|
|
7
13
|
</p>
|
|
8
14
|
|
|
9
|
-
[](https://github.com/honojs/hono/actions)
|
|
16
|
+
[](https://github.com/honojs/hono/blob/master/LICENSE)
|
|
11
17
|
[](https://www.npmjs.com/package/hono)
|
|
12
18
|
[](https://www.npmjs.com/package/hono)
|
|
13
19
|
[](https://www.npmjs.com/package/hono)
|
|
14
|
-
[](https://github.com/honojs/hono/pulse)
|
|
21
|
+
[](https://github.com/honojs/hono/commits/master)
|
|
16
22
|
|
|
17
|
-
Hono
|
|
23
|
+
Hono - _**[炎] means flame🔥 in Japanese**_ - is a small, simple, and ultrafast web framework for Cloudflare Workers and Service Worker based serverless such as Fastly Compute@Edge.
|
|
18
24
|
|
|
19
25
|
```js
|
|
20
26
|
import { Hono } from 'hono'
|
|
@@ -30,7 +36,8 @@ app.fire()
|
|
|
30
36
|
- **Ultrafast** - the router does not use linear loops.
|
|
31
37
|
- **Zero-dependencies** - using only Service Worker and Web standard API.
|
|
32
38
|
- **Middleware** - built-in middleware and ability to extend with your own middleware.
|
|
33
|
-
- **
|
|
39
|
+
- **TypeScript** - first-class TypeScript support.
|
|
40
|
+
- **Optimized** - for Cloudflare Workers and Fastly Compute@Edge.
|
|
34
41
|
|
|
35
42
|
## Benchmark
|
|
36
43
|
|
|
@@ -51,7 +58,42 @@ A demonstration to create an application for Cloudflare Workers with Hono.
|
|
|
51
58
|
|
|
52
59
|

|
|
53
60
|
|
|
54
|
-
|
|
61
|
+
## Not only fast
|
|
62
|
+
|
|
63
|
+
Hono is fast. But not only fast.
|
|
64
|
+
|
|
65
|
+
### Write Less, do more
|
|
66
|
+
|
|
67
|
+
Built-in middleware make _"**Write Less, do more**"_ in reality. You can use a lot of middleware without writing code from scratch. Below are examples.
|
|
68
|
+
|
|
69
|
+
- [Basic Authentication](https://github.com/honojs/hono/tree/master/src/middleware/basic-auth/)
|
|
70
|
+
- [Cookie parsing / serializing](https://github.com/honojs/hono/tree/master/src/middleware/cookie/)
|
|
71
|
+
- [CORS](https://github.com/honojs/hono/tree/master/src/middleware/cors/)
|
|
72
|
+
- [ETag](https://github.com/honojs/hono/tree/master/src/middleware/etag/)
|
|
73
|
+
- [GraphQL Server](https://github.com/honojs/hono/tree/master/src/middleware/graphql-server/)
|
|
74
|
+
- [JWT Authentication](https://github.com/honojs/hono/tree/master/src/middleware/jwt/)
|
|
75
|
+
- [Logger](https://github.com/honojs/hono/tree/master/src/middleware/logger/)
|
|
76
|
+
- [Mustache template engine](https://github.com/honojs/hono/tree/master/src/middleware/mustache/) (Only for Cloudflare Workers)
|
|
77
|
+
- [JSON pretty printing](https://github.com/honojs/hono/tree/master/src/middleware/pretty-json/)
|
|
78
|
+
- [Serving static files](https://github.com/honojs/hono/tree/master/src/middleware/serve-static/) (Only for Cloudflare Workers)
|
|
79
|
+
|
|
80
|
+
You can enable logger and CORS middleware with just this code.
|
|
81
|
+
|
|
82
|
+
```js
|
|
83
|
+
import { Hono } from 'hono'
|
|
84
|
+
import { cors } from 'hono/cors'
|
|
85
|
+
import { logger } from 'hono/logger'
|
|
86
|
+
|
|
87
|
+
const app = new Hono()
|
|
88
|
+
app.use('*', cors()).use(logger())
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
### Developer Experience
|
|
92
|
+
|
|
93
|
+
And Hono provides fine _"**Developer Experience**"_. Easy access to Request/Response thanks to the `Context` object.
|
|
94
|
+
Above all, Hono is written in TypeScript. So, Hono has _"**Types**"_!
|
|
95
|
+
|
|
96
|
+
For example, the named path parameters will be literal types.
|
|
55
97
|
|
|
56
98
|

|
|
57
99
|
|
|
@@ -59,24 +101,24 @@ Now, the named path parameter has types.
|
|
|
59
101
|
|
|
60
102
|
You can install Hono from the npm registry.
|
|
61
103
|
|
|
62
|
-
```
|
|
63
|
-
|
|
104
|
+
```sh
|
|
105
|
+
yarn add hono
|
|
64
106
|
```
|
|
65
107
|
|
|
66
108
|
or
|
|
67
109
|
|
|
68
|
-
```
|
|
69
|
-
|
|
110
|
+
```sh
|
|
111
|
+
npm install hono
|
|
70
112
|
```
|
|
71
113
|
|
|
72
114
|
## Methods
|
|
73
115
|
|
|
74
116
|
An instance of `Hono` has these methods.
|
|
75
117
|
|
|
76
|
-
- app.**HTTP_METHOD**(path
|
|
77
|
-
- app.**all**(path
|
|
118
|
+
- app.**HTTP_METHOD**(\[path,\] handler)
|
|
119
|
+
- app.**all**(\[path,\] handler)
|
|
78
120
|
- app.**route**(path)
|
|
79
|
-
- app.**use**(path
|
|
121
|
+
- app.**use**(\[path,\] middleware)
|
|
80
122
|
- app.**notFound**(handler)
|
|
81
123
|
- app.**onError**(err, handler)
|
|
82
124
|
- app.**fire**()
|
|
@@ -120,6 +162,21 @@ app.get('/post/:date{[0-9]+}/:title{[a-z]+}', (c) => {
|
|
|
120
162
|
})
|
|
121
163
|
```
|
|
122
164
|
|
|
165
|
+
### Chained route
|
|
166
|
+
|
|
167
|
+
```js
|
|
168
|
+
app
|
|
169
|
+
.get('/endpoint', (c) => {
|
|
170
|
+
return c.text('GET /endpoint')
|
|
171
|
+
})
|
|
172
|
+
.post((c) => {
|
|
173
|
+
return c.text('POST /endpoint')
|
|
174
|
+
})
|
|
175
|
+
.delete((c) => {
|
|
176
|
+
return c.text('DELETE /endpoint')
|
|
177
|
+
})
|
|
178
|
+
```
|
|
179
|
+
|
|
123
180
|
### Nested route
|
|
124
181
|
|
|
125
182
|
```js
|
|
@@ -168,6 +225,9 @@ const app = new Hono()
|
|
|
168
225
|
|
|
169
226
|
app.use('*', poweredBy())
|
|
170
227
|
app.use('*', logger())
|
|
228
|
+
// Or you can write:
|
|
229
|
+
// app.use('*', poweredBy()).use(logger())
|
|
230
|
+
|
|
171
231
|
app.use(
|
|
172
232
|
'/auth/*',
|
|
173
233
|
basicAuth({
|
|
@@ -177,7 +237,7 @@ app.use(
|
|
|
177
237
|
)
|
|
178
238
|
```
|
|
179
239
|
|
|
180
|
-
Available built-in middleware is listed on [src/middleware](https://github.com/
|
|
240
|
+
Available built-in middleware is listed on [src/middleware](https://github.com/honojs/hono/tree/master/src/middleware).
|
|
181
241
|
|
|
182
242
|
### Custom Middleware
|
|
183
243
|
|
|
@@ -412,7 +472,7 @@ Let's write your first code for Cloudflare Workers with Hono.
|
|
|
412
472
|
**Wrangler 1.x** does not support importing middleware. We recommend two ways:
|
|
413
473
|
|
|
414
474
|
1. Use [Wragler 2.0 Beta](https://github.com/cloudflare/wrangler2).
|
|
415
|
-
2. Build without webpack 4.x. For example, you can use esbuild. See [the starter template](https://github.com/
|
|
475
|
+
2. Build without webpack 4.x. For example, you can use esbuild. See [the starter template](https://github.com/honojs/hono-minimal).
|
|
416
476
|
|
|
417
477
|
---
|
|
418
478
|
|
|
@@ -420,18 +480,18 @@ Let's write your first code for Cloudflare Workers with Hono.
|
|
|
420
480
|
|
|
421
481
|
Make a npm skeleton directory.
|
|
422
482
|
|
|
423
|
-
```
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
483
|
+
```sh
|
|
484
|
+
mkdir hono-example
|
|
485
|
+
cd hono-example
|
|
486
|
+
npm init -y
|
|
427
487
|
```
|
|
428
488
|
|
|
429
489
|
### 2. `wrangler init`
|
|
430
490
|
|
|
431
491
|
Initialize as a wrangler project.
|
|
432
492
|
|
|
433
|
-
```
|
|
434
|
-
|
|
493
|
+
```sh
|
|
494
|
+
npx wrangler@beta init
|
|
435
495
|
```
|
|
436
496
|
|
|
437
497
|
Answer the questions. If you want, you can answer `y`.
|
|
@@ -446,8 +506,8 @@ Would you like to create a Worker at src/index.js? (y/n) <--- n
|
|
|
446
506
|
|
|
447
507
|
Install `hono` from the npm registry.
|
|
448
508
|
|
|
449
|
-
```
|
|
450
|
-
|
|
509
|
+
```sh
|
|
510
|
+
npm i hono
|
|
451
511
|
```
|
|
452
512
|
|
|
453
513
|
### 4. Write your app
|
|
@@ -468,43 +528,47 @@ app.fire()
|
|
|
468
528
|
|
|
469
529
|
Run the development server locally. Then, access `http://127.0.0.1:8787/` in your Web browser.
|
|
470
530
|
|
|
471
|
-
```
|
|
472
|
-
|
|
531
|
+
```sh
|
|
532
|
+
npx wrangler@beta dev index.js
|
|
473
533
|
```
|
|
474
534
|
|
|
475
535
|
### 6. Publish
|
|
476
536
|
|
|
477
537
|
Deploy to Cloudflare. That's all!
|
|
478
538
|
|
|
479
|
-
```
|
|
480
|
-
|
|
539
|
+
```sh
|
|
540
|
+
npx wrangler@beta publish index.js
|
|
481
541
|
```
|
|
482
542
|
|
|
483
543
|
## Starter template
|
|
484
544
|
|
|
485
|
-
You can start making your Cloudflare Workers application with [the starter template](https://github.com/
|
|
545
|
+
You can start making your Cloudflare Workers application with [the starter template](https://github.com/honojs/hono-minimal). It is really minimal using TypeScript, esbuild, Miniflare, and Jest.
|
|
486
546
|
|
|
487
547
|
To generate a project skelton, run this command.
|
|
488
548
|
|
|
549
|
+
```sh
|
|
550
|
+
wrangler generate my-app https://github.com/honojs/hono-minimal
|
|
489
551
|
```
|
|
490
|
-
|
|
491
|
-
|
|
552
|
+
|
|
553
|
+
## Examples
|
|
554
|
+
|
|
555
|
+
- Hono Examples - <https://github.com/honojs/examples>
|
|
492
556
|
|
|
493
557
|
## Related projects
|
|
494
558
|
|
|
495
559
|
Implementation of the original router `TrieRouter` is inspired by [goblin](https://github.com/bmf-san/goblin). `RegExpRouter` is inspired by [Router::Boom](https://github.com/tokuhirom/Router-Boom). API design is inspired by [express](https://github.com/expressjs/express) and [koa](https://github.com/koajs/koa). [itty-router](https://github.com/kwhitley/itty-router), [Sunder](https://github.com/SunderJS/sunder), and [worktop](https://github.com/lukeed/worktop) are the other routers or frameworks for Cloudflare Workers.
|
|
496
560
|
|
|
497
|
-
- express <https://github.com/expressjs/express>
|
|
498
|
-
- koa <https://github.com/koajs/koa>
|
|
499
|
-
- itty-router <https://github.com/kwhitley/itty-router>
|
|
500
|
-
- Sunder <https://github.com/SunderJS/sunder>
|
|
501
|
-
- goblin <https://github.com/bmf-san/goblin>
|
|
502
|
-
- worktop <https://github.com/lukeed/worktop>
|
|
503
|
-
- Router::Boom <https://github.com/tokuhirom/Router-Boom>
|
|
561
|
+
- express - <https://github.com/expressjs/express>
|
|
562
|
+
- koa - <https://github.com/koajs/koa>
|
|
563
|
+
- itty-router - <https://github.com/kwhitley/itty-router>
|
|
564
|
+
- Sunder - <https://github.com/SunderJS/sunder>
|
|
565
|
+
- goblin - <https://github.com/bmf-san/goblin>
|
|
566
|
+
- worktop - <https://github.com/lukeed/worktop>
|
|
567
|
+
- Router::Boom - <https://github.com/tokuhirom/Router-Boom>
|
|
504
568
|
|
|
505
569
|
## Contributing
|
|
506
570
|
|
|
507
|
-
Contributions Welcome! You can contribute
|
|
571
|
+
Contributions Welcome! You can contribute in the following ways.
|
|
508
572
|
|
|
509
573
|
- Write or fix documents
|
|
510
574
|
- Write code of middleware
|
|
@@ -512,11 +576,9 @@ Contributions Welcome! You can contribute by the following way.
|
|
|
512
576
|
- Refactor the code
|
|
513
577
|
- etc.
|
|
514
578
|
|
|
515
|
-
Let's make Hono together!
|
|
516
|
-
|
|
517
579
|
## Contributors
|
|
518
580
|
|
|
519
|
-
Thanks to [all contributors](https://github.com/
|
|
581
|
+
Thanks to [all contributors](https://github.com/honojs/hono/graphs/contributors)!
|
|
520
582
|
|
|
521
583
|
## Author
|
|
522
584
|
|
package/dist/context.d.ts
CHANGED
|
@@ -1,17 +1,15 @@
|
|
|
1
1
|
/// <reference types="@cloudflare/workers-types" />
|
|
2
|
+
import type { StatusCode } from './utils/http-status';
|
|
2
3
|
declare type Headers = Record<string, string>;
|
|
3
4
|
declare type Data = string | ArrayBuffer | ReadableStream;
|
|
4
5
|
export interface Env {
|
|
5
6
|
}
|
|
6
7
|
export declare class Context<RequestParamKeyType = string> {
|
|
8
|
+
#private;
|
|
7
9
|
req: Request<RequestParamKeyType>;
|
|
8
10
|
res: Response;
|
|
9
11
|
env: Env;
|
|
10
12
|
event: FetchEvent;
|
|
11
|
-
private _headers;
|
|
12
|
-
private _status;
|
|
13
|
-
private _statusText;
|
|
14
|
-
private _pretty;
|
|
15
13
|
render: (template: string, params?: object, options?: object) => Promise<Response>;
|
|
16
14
|
notFound: () => Response | Promise<Response>;
|
|
17
15
|
constructor(req: Request<RequestParamKeyType>, opts?: {
|
|
@@ -19,14 +17,15 @@ export declare class Context<RequestParamKeyType = string> {
|
|
|
19
17
|
env: Env;
|
|
20
18
|
event: FetchEvent;
|
|
21
19
|
});
|
|
20
|
+
private initRequest;
|
|
22
21
|
header(name: string, value: string): void;
|
|
23
|
-
status(
|
|
24
|
-
pretty(prettyJSON: boolean): void;
|
|
22
|
+
status(status: StatusCode): void;
|
|
23
|
+
pretty(prettyJSON: boolean, space?: number): void;
|
|
25
24
|
newResponse(data: Data, init?: ResponseInit): Response;
|
|
26
|
-
body(data: Data, status?:
|
|
27
|
-
text(text: string, status?:
|
|
28
|
-
json(object: object, status?:
|
|
29
|
-
html(html: string, status?:
|
|
30
|
-
redirect(location: string, status?:
|
|
25
|
+
body(data: Data, status?: StatusCode, headers?: Headers): Response;
|
|
26
|
+
text(text: string, status?: StatusCode, headers?: Headers): Response;
|
|
27
|
+
json(object: object, status?: StatusCode, headers?: Headers): Response;
|
|
28
|
+
html(html: string, status?: StatusCode, headers?: Headers): Response;
|
|
29
|
+
redirect(location: string, status?: StatusCode): Response;
|
|
31
30
|
}
|
|
32
31
|
export {};
|
package/dist/context.js
CHANGED
|
@@ -1,82 +1,90 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
3
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
4
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
5
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
6
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
7
|
+
};
|
|
8
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
9
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
10
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
11
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
12
|
+
};
|
|
13
|
+
var _Context_headers, _Context_status, _Context_statusText, _Context_pretty, _Context_prettySpace;
|
|
2
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
15
|
exports.Context = void 0;
|
|
4
|
-
const url_1 = require("./utils/url");
|
|
5
16
|
const http_status_1 = require("./utils/http-status");
|
|
17
|
+
const url_1 = require("./utils/url");
|
|
6
18
|
class Context {
|
|
7
19
|
constructor(req, opts) {
|
|
8
|
-
this
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
this
|
|
20
|
+
_Context_headers.set(this, void 0);
|
|
21
|
+
_Context_status.set(this, void 0);
|
|
22
|
+
_Context_statusText.set(this, void 0);
|
|
23
|
+
_Context_pretty.set(this, void 0);
|
|
24
|
+
_Context_prettySpace.set(this, 2);
|
|
25
|
+
this.req = this.initRequest(req);
|
|
26
|
+
Object.assign(this, opts);
|
|
27
|
+
__classPrivateFieldSet(this, _Context_headers, {}, "f");
|
|
28
|
+
}
|
|
29
|
+
initRequest(req) {
|
|
30
|
+
req.header = (name) => {
|
|
31
|
+
return req.headers.get(name);
|
|
32
|
+
};
|
|
33
|
+
req.query = (key) => {
|
|
34
|
+
const url = new URL(req.url);
|
|
35
|
+
return url.searchParams.get(key);
|
|
36
|
+
};
|
|
37
|
+
return req;
|
|
15
38
|
}
|
|
16
39
|
header(name, value) {
|
|
17
|
-
/*
|
|
18
|
-
XXX:
|
|
19
|
-
app.use('*', (c, next) => {
|
|
20
|
-
next()
|
|
21
|
-
c.header('foo', 'bar') // => c.res.headers.set(...)
|
|
22
|
-
})
|
|
23
|
-
*/
|
|
24
40
|
if (this.res) {
|
|
25
41
|
this.res.headers.set(name, value);
|
|
26
42
|
}
|
|
27
|
-
this
|
|
43
|
+
__classPrivateFieldGet(this, _Context_headers, "f")[name] = value;
|
|
28
44
|
}
|
|
29
|
-
status(
|
|
45
|
+
status(status) {
|
|
30
46
|
if (this.res) {
|
|
31
47
|
console.warn('c.res.status is already set.');
|
|
32
48
|
return;
|
|
33
49
|
}
|
|
34
|
-
this
|
|
35
|
-
this
|
|
50
|
+
__classPrivateFieldSet(this, _Context_status, status, "f");
|
|
51
|
+
__classPrivateFieldSet(this, _Context_statusText, (0, http_status_1.getStatusText)(status), "f");
|
|
36
52
|
}
|
|
37
|
-
pretty(prettyJSON) {
|
|
38
|
-
this
|
|
53
|
+
pretty(prettyJSON, space = 2) {
|
|
54
|
+
__classPrivateFieldSet(this, _Context_pretty, prettyJSON, "f");
|
|
55
|
+
__classPrivateFieldSet(this, _Context_prettySpace, space, "f");
|
|
39
56
|
}
|
|
40
57
|
newResponse(data, init = {}) {
|
|
41
|
-
init.status = init.status || this
|
|
42
|
-
init.statusText =
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
let length = 0;
|
|
46
|
-
if (data) {
|
|
47
|
-
if (data instanceof ArrayBuffer) {
|
|
48
|
-
length = data.byteLength;
|
|
49
|
-
}
|
|
50
|
-
else if (typeof data == 'string') {
|
|
51
|
-
const Encoder = new TextEncoder();
|
|
52
|
-
length = Encoder.encode(data).byteLength || 0;
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
init.headers = Object.assign(Object.assign({}, init.headers), { 'Content-Length': length.toString() });
|
|
58
|
+
init.status = init.status || __classPrivateFieldGet(this, _Context_status, "f") || 200;
|
|
59
|
+
init.statusText =
|
|
60
|
+
init.statusText || __classPrivateFieldGet(this, _Context_statusText, "f") || (0, http_status_1.getStatusText)(init.status);
|
|
61
|
+
init.headers = Object.assign(Object.assign({}, __classPrivateFieldGet(this, _Context_headers, "f")), init.headers);
|
|
56
62
|
return new Response(data, init);
|
|
57
63
|
}
|
|
58
|
-
body(data, status = this
|
|
64
|
+
body(data, status = __classPrivateFieldGet(this, _Context_status, "f"), headers = __classPrivateFieldGet(this, _Context_headers, "f")) {
|
|
59
65
|
return this.newResponse(data, {
|
|
60
66
|
status: status,
|
|
61
67
|
headers: headers,
|
|
62
68
|
});
|
|
63
69
|
}
|
|
64
|
-
text(text, status = this
|
|
70
|
+
text(text, status = __classPrivateFieldGet(this, _Context_status, "f"), headers = {}) {
|
|
65
71
|
if (typeof text !== 'string') {
|
|
66
72
|
throw new TypeError('text method arg must be a string!');
|
|
67
73
|
}
|
|
68
74
|
headers['Content-Type'] || (headers['Content-Type'] = 'text/plain; charset=UTF-8');
|
|
69
75
|
return this.body(text, status, headers);
|
|
70
76
|
}
|
|
71
|
-
json(object, status = this
|
|
77
|
+
json(object, status = __classPrivateFieldGet(this, _Context_status, "f"), headers = {}) {
|
|
72
78
|
if (typeof object !== 'object') {
|
|
73
|
-
throw new TypeError('json method arg must be
|
|
79
|
+
throw new TypeError('json method arg must be an object!');
|
|
74
80
|
}
|
|
75
|
-
const body = this
|
|
81
|
+
const body = __classPrivateFieldGet(this, _Context_pretty, "f")
|
|
82
|
+
? JSON.stringify(object, null, __classPrivateFieldGet(this, _Context_prettySpace, "f"))
|
|
83
|
+
: JSON.stringify(object);
|
|
76
84
|
headers['Content-Type'] || (headers['Content-Type'] = 'application/json; charset=UTF-8');
|
|
77
85
|
return this.body(body, status, headers);
|
|
78
86
|
}
|
|
79
|
-
html(html, status = this
|
|
87
|
+
html(html, status = __classPrivateFieldGet(this, _Context_status, "f"), headers = {}) {
|
|
80
88
|
if (typeof html !== 'string') {
|
|
81
89
|
throw new TypeError('html method arg must be a string!');
|
|
82
90
|
}
|
|
@@ -101,3 +109,4 @@ class Context {
|
|
|
101
109
|
}
|
|
102
110
|
}
|
|
103
111
|
exports.Context = Context;
|
|
112
|
+
_Context_headers = new WeakMap(), _Context_status = new WeakMap(), _Context_statusText = new WeakMap(), _Context_pretty = new WeakMap(), _Context_prettySpace = new WeakMap();
|
package/dist/hono.d.ts
CHANGED
|
@@ -7,50 +7,48 @@ declare global {
|
|
|
7
7
|
param: (key: ParamKeyType) => string;
|
|
8
8
|
query: (key: string) => string;
|
|
9
9
|
header: (name: string) => string;
|
|
10
|
-
parsedBody: any;
|
|
11
10
|
}
|
|
12
11
|
}
|
|
13
|
-
export declare type Handler<RequestParamKeyType = string> = (c: Context<RequestParamKeyType>, next?:
|
|
14
|
-
export declare type MiddlewareHandler = (c: Context, next:
|
|
12
|
+
export declare type Handler<RequestParamKeyType = string> = (c: Context<RequestParamKeyType>, next?: Next) => Response | Promise<Response>;
|
|
13
|
+
export declare type MiddlewareHandler = (c: Context, next: Next) => Promise<void>;
|
|
15
14
|
export declare type NotFoundHandler = (c: Context) => Response | Promise<Response>;
|
|
16
15
|
export declare type ErrorHandler = (err: Error, c: Context) => Response;
|
|
16
|
+
export declare type Next = () => Promise<void>;
|
|
17
17
|
declare type ParamKeyName<NameWithPattern> = NameWithPattern extends `${infer Name}{${infer _Pattern}` ? Name : NameWithPattern;
|
|
18
18
|
declare type ParamKey<Component> = Component extends `:${infer NameWithPattern}` ? ParamKeyName<NameWithPattern> : never;
|
|
19
19
|
declare type ParamKeys<Path> = Path extends `${infer Component}/${infer Rest}` ? ParamKey<Component> | ParamKeys<Rest> : ParamKey<Path>;
|
|
20
|
-
|
|
21
|
-
|
|
20
|
+
interface HandlerInterface<T extends string> {
|
|
21
|
+
<Path extends string>(path: Path, handler: Handler<ParamKeys<Path>>): Hono<Path>;
|
|
22
|
+
<Path extends T>(handler: Handler<ParamKeys<T>>): Hono<Path>;
|
|
23
|
+
}
|
|
24
|
+
declare const Hono_base: new <T extends string>() => {
|
|
25
|
+
delete: HandlerInterface<T>;
|
|
26
|
+
get: HandlerInterface<T>;
|
|
27
|
+
post: HandlerInterface<T>;
|
|
28
|
+
put: HandlerInterface<T>;
|
|
29
|
+
head: HandlerInterface<T>;
|
|
30
|
+
options: HandlerInterface<T>;
|
|
31
|
+
patch: HandlerInterface<T>;
|
|
32
|
+
all: HandlerInterface<T>;
|
|
33
|
+
};
|
|
34
|
+
export declare class Hono<P extends string> extends Hono_base<P> {
|
|
35
|
+
#private;
|
|
36
|
+
readonly routerClass: {
|
|
22
37
|
new (): Router<any>;
|
|
23
38
|
};
|
|
24
|
-
strict: boolean;
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
tempPath: string;
|
|
28
|
-
constructor(init?: Partial<Pick<Hono, 'routerClass' | 'strict'>>);
|
|
39
|
+
readonly strict: boolean;
|
|
40
|
+
private path;
|
|
41
|
+
constructor(init?: Partial<Pick<Hono<P>, 'routerClass' | 'strict'>>);
|
|
29
42
|
private notFoundHandler;
|
|
30
43
|
private errorHandler;
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
put(path: string, handler: Handler<string>): Hono;
|
|
37
|
-
head<Path extends string>(path: Path, handler: Handler<ParamKeys<Path>>): Hono;
|
|
38
|
-
head(path: string, handler: Handler<string>): Hono;
|
|
39
|
-
delete<Path extends string>(path: Path, handler: Handler<ParamKeys<Path>>): Hono;
|
|
40
|
-
delete(path: string, handler: Handler<string>): Hono;
|
|
41
|
-
options<Path extends string>(path: Path, handler: Handler<ParamKeys<Path>>): Hono;
|
|
42
|
-
options(path: string, handler: Handler<string>): Hono;
|
|
43
|
-
patch<Path extends string>(path: Path, handler: Handler<ParamKeys<Path>>): Hono;
|
|
44
|
-
patch(path: string, handler: Handler<string>): Hono;
|
|
45
|
-
all<Path extends string>(path: Path, handler: Handler<ParamKeys<Path>>): Hono;
|
|
46
|
-
all(path: string, handler: Handler<string>): Hono;
|
|
47
|
-
route(path: string): Hono;
|
|
48
|
-
use(path: string, middleware: MiddlewareHandler): void;
|
|
49
|
-
onError(handler: ErrorHandler): Hono;
|
|
50
|
-
notFound(handler: NotFoundHandler): Hono;
|
|
44
|
+
route(path: string): Hono<P>;
|
|
45
|
+
use(path: string, middleware: MiddlewareHandler): Hono<P>;
|
|
46
|
+
use(middleware: MiddlewareHandler): Hono<P>;
|
|
47
|
+
onError(handler: ErrorHandler): Hono<P>;
|
|
48
|
+
notFound(handler: NotFoundHandler): Hono<P>;
|
|
51
49
|
private addRoute;
|
|
52
50
|
private matchRoute;
|
|
53
|
-
dispatch
|
|
51
|
+
private dispatch;
|
|
54
52
|
handleEvent(event: FetchEvent): Promise<Response>;
|
|
55
53
|
fetch(request: Request, env?: Env, event?: FetchEvent): Promise<Response>;
|
|
56
54
|
request(input: RequestInfo, requestInit?: RequestInit): Promise<Response>;
|