hono 1.2.1 → 1.3.1
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 +91 -20
- package/dist/context.d.ts +1 -1
- package/dist/context.js +24 -6
- package/dist/hono.d.ts +21 -29
- package/dist/hono.js +27 -65
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -2
- package/dist/middleware/cookie/index.d.ts +5 -2
- package/dist/middleware/cookie/index.js +9 -4
- package/dist/middleware/graphql-server/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/router.d.ts +1 -1
- package/dist/router/trie-router/router.js +1 -1
- package/dist/utils/buffer.js +1 -1
- package/dist/utils/url.js +4 -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
|
|
|
@@ -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
|
|
package/dist/context.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ import type { StatusCode } from './utils/http-status';
|
|
|
3
3
|
declare type Headers = Record<string, string>;
|
|
4
4
|
declare type Data = string | ArrayBuffer | ReadableStream;
|
|
5
5
|
export declare type Env = Record<string, any>;
|
|
6
|
-
export declare class Context<RequestParamKeyType = string, E = Env> {
|
|
6
|
+
export declare class Context<RequestParamKeyType extends string = string, E = Env> {
|
|
7
7
|
req: Request<RequestParamKeyType>;
|
|
8
8
|
res: Response;
|
|
9
9
|
env: E;
|
package/dist/context.js
CHANGED
|
@@ -16,13 +16,31 @@ class Context {
|
|
|
16
16
|
Object.assign(this, opts);
|
|
17
17
|
}
|
|
18
18
|
initRequest(req) {
|
|
19
|
-
req.header = (name) => {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
19
|
+
req.header = ((name) => {
|
|
20
|
+
if (name) {
|
|
21
|
+
return req.headers.get(name);
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
const result = {};
|
|
25
|
+
for (const [key, value] of req.headers) {
|
|
26
|
+
result[key] = value;
|
|
27
|
+
}
|
|
28
|
+
return result;
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
req.query = ((key) => {
|
|
23
32
|
const url = new URL(req.url);
|
|
24
|
-
|
|
25
|
-
|
|
33
|
+
if (key) {
|
|
34
|
+
return url.searchParams.get(key);
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
const result = {};
|
|
38
|
+
for (const [key, value] of url.searchParams) {
|
|
39
|
+
result[key] = value;
|
|
40
|
+
}
|
|
41
|
+
return result;
|
|
42
|
+
}
|
|
43
|
+
});
|
|
26
44
|
return req;
|
|
27
45
|
}
|
|
28
46
|
header(name, value) {
|
package/dist/hono.d.ts
CHANGED
|
@@ -2,15 +2,23 @@
|
|
|
2
2
|
import { Context } from './context';
|
|
3
3
|
import type { Env } from './context';
|
|
4
4
|
import type { Router } from './router';
|
|
5
|
-
import { METHOD_NAME_ALL_LOWERCASE } from './router';
|
|
6
5
|
declare global {
|
|
7
|
-
interface Request<ParamKeyType = string> {
|
|
8
|
-
param:
|
|
9
|
-
|
|
10
|
-
|
|
6
|
+
interface Request<ParamKeyType extends string = string> {
|
|
7
|
+
param: {
|
|
8
|
+
(key: ParamKeyType): string;
|
|
9
|
+
(): Record<ParamKeyType, string>;
|
|
10
|
+
};
|
|
11
|
+
query: {
|
|
12
|
+
(key: string): string;
|
|
13
|
+
(): Record<string, string>;
|
|
14
|
+
};
|
|
15
|
+
header: {
|
|
16
|
+
(name: string): string;
|
|
17
|
+
(): Record<string, string>;
|
|
18
|
+
};
|
|
11
19
|
}
|
|
12
20
|
}
|
|
13
|
-
export declare type Handler<RequestParamKeyType = string, E = Env> = (c: Context<RequestParamKeyType, E>, next
|
|
21
|
+
export declare type Handler<RequestParamKeyType extends string = string, E = Env> = (c: Context<RequestParamKeyType, E>, next: Next) => Response | Promise<Response> | void | Promise<void>;
|
|
14
22
|
export declare type NotFoundHandler<E = Env> = (c: Context<string, E>) => Response;
|
|
15
23
|
export declare type ErrorHandler<E = Env> = (err: Error, c: Context<string, E>) => Response;
|
|
16
24
|
export declare type Next = () => Promise<void>;
|
|
@@ -23,29 +31,11 @@ interface HandlerInterface<T extends string, E = Env, U = Hono<E, T>> {
|
|
|
23
31
|
<Path extends string>(...handlers: Handler<ParamKeys<Path> extends never ? string : ParamKeys<Path>, E>[]): U;
|
|
24
32
|
(...handlers: Handler<string, E>[]): U;
|
|
25
33
|
}
|
|
26
|
-
|
|
27
|
-
declare type Methods = typeof methods[number] | typeof METHOD_NAME_ALL_LOWERCASE;
|
|
28
|
-
interface Routing<E extends Env> {
|
|
34
|
+
interface Route<E extends Env> {
|
|
29
35
|
path: string;
|
|
30
|
-
method:
|
|
36
|
+
method: string;
|
|
31
37
|
handler: Handler<string, E>;
|
|
32
38
|
}
|
|
33
|
-
declare const Route_base: new <E_1 extends Env, T extends string, U>() => {
|
|
34
|
-
delete: HandlerInterface<T, E_1, U>;
|
|
35
|
-
get: HandlerInterface<T, E_1, U>;
|
|
36
|
-
all: HandlerInterface<T, E_1, U>;
|
|
37
|
-
post: HandlerInterface<T, E_1, U>;
|
|
38
|
-
put: HandlerInterface<T, E_1, U>;
|
|
39
|
-
head: HandlerInterface<T, E_1, U>;
|
|
40
|
-
options: HandlerInterface<T, E_1, U>;
|
|
41
|
-
patch: HandlerInterface<T, E_1, U>;
|
|
42
|
-
};
|
|
43
|
-
export declare class Route<E = Env, P extends string = ''> extends Route_base<E, P, Route<E, P>> {
|
|
44
|
-
#private;
|
|
45
|
-
routes: Routing<E>[];
|
|
46
|
-
constructor();
|
|
47
|
-
private add;
|
|
48
|
-
}
|
|
49
39
|
declare const Hono_base: new <E_1 extends Env, T extends string, U>() => {
|
|
50
40
|
delete: HandlerInterface<T, E_1, U>;
|
|
51
41
|
get: HandlerInterface<T, E_1, U>;
|
|
@@ -56,17 +46,19 @@ declare const Hono_base: new <E_1 extends Env, T extends string, U>() => {
|
|
|
56
46
|
options: HandlerInterface<T, E_1, U>;
|
|
57
47
|
patch: HandlerInterface<T, E_1, U>;
|
|
58
48
|
};
|
|
59
|
-
export declare class Hono<E = Env, P extends string = ''> extends Hono_base<E, P, Hono<E, P>> {
|
|
60
|
-
#private;
|
|
49
|
+
export declare class Hono<E = Env, P extends string = '/'> extends Hono_base<E, P, Hono<E, P>> {
|
|
61
50
|
readonly routerClass: {
|
|
62
51
|
new (): Router<any>;
|
|
63
52
|
};
|
|
64
53
|
readonly strict: boolean;
|
|
54
|
+
private _router;
|
|
55
|
+
private _tempPath;
|
|
65
56
|
private path;
|
|
57
|
+
routes: Route<E>[];
|
|
66
58
|
constructor(init?: Partial<Pick<Hono, 'routerClass' | 'strict'>>);
|
|
67
59
|
private notFoundHandler;
|
|
68
60
|
private errorHandler;
|
|
69
|
-
route(path: string,
|
|
61
|
+
route(path: string, app?: Hono<any>): Hono<E, P>;
|
|
70
62
|
use(path: string, ...middleware: Handler<string, E>[]): Hono<E, P>;
|
|
71
63
|
use(...middleware: Handler<string, E>[]): Hono<E, P>;
|
|
72
64
|
onError(handler: ErrorHandler<E>): Hono<E, P>;
|
package/dist/hono.js
CHANGED
|
@@ -1,18 +1,6 @@
|
|
|
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 _Route_path, _Hono_router, _Hono_tempPath;
|
|
14
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
exports.Hono =
|
|
3
|
+
exports.Hono = void 0;
|
|
16
4
|
const compose_1 = require("./compose");
|
|
17
5
|
const context_1 = require("./context");
|
|
18
6
|
const router_1 = require("./router");
|
|
@@ -24,44 +12,13 @@ function defineDynamicClass() {
|
|
|
24
12
|
return class {
|
|
25
13
|
};
|
|
26
14
|
}
|
|
27
|
-
class Route extends defineDynamicClass() {
|
|
28
|
-
constructor() {
|
|
29
|
-
super();
|
|
30
|
-
this.routes = [];
|
|
31
|
-
_Route_path.set(this, '');
|
|
32
|
-
const allMethods = [...methods, router_2.METHOD_NAME_ALL_LOWERCASE];
|
|
33
|
-
allMethods.map((method) => {
|
|
34
|
-
this[method] = (args1, ...args) => {
|
|
35
|
-
if (typeof args1 === 'string') {
|
|
36
|
-
__classPrivateFieldSet(this, _Route_path, args1, "f");
|
|
37
|
-
}
|
|
38
|
-
else {
|
|
39
|
-
this.add(method, __classPrivateFieldGet(this, _Route_path, "f"), args1);
|
|
40
|
-
}
|
|
41
|
-
args.map((handler) => {
|
|
42
|
-
if (typeof handler !== 'string') {
|
|
43
|
-
this.add(method, __classPrivateFieldGet(this, _Route_path, "f"), handler);
|
|
44
|
-
}
|
|
45
|
-
});
|
|
46
|
-
return this;
|
|
47
|
-
};
|
|
48
|
-
});
|
|
49
|
-
}
|
|
50
|
-
add(method, path, handler) {
|
|
51
|
-
const r = { path: path, method: method, handler: handler };
|
|
52
|
-
this.routes.push(r);
|
|
53
|
-
return this;
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
exports.Route = Route;
|
|
57
|
-
_Route_path = new WeakMap();
|
|
58
15
|
class Hono extends defineDynamicClass() {
|
|
59
16
|
constructor(init = {}) {
|
|
60
17
|
super();
|
|
61
18
|
this.routerClass = trie_router_1.TrieRouter;
|
|
62
19
|
this.strict = true; // strict routing - default is true
|
|
63
|
-
|
|
64
|
-
|
|
20
|
+
this.path = '/';
|
|
21
|
+
this.routes = [];
|
|
65
22
|
this.notFoundHandler = (c) => {
|
|
66
23
|
const message = '404 Not Found';
|
|
67
24
|
return c.text(message, 404);
|
|
@@ -89,19 +46,17 @@ class Hono extends defineDynamicClass() {
|
|
|
89
46
|
};
|
|
90
47
|
});
|
|
91
48
|
Object.assign(this, init);
|
|
92
|
-
|
|
93
|
-
|
|
49
|
+
this._router = new this.routerClass();
|
|
50
|
+
this._tempPath = null;
|
|
94
51
|
}
|
|
95
|
-
route(path,
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
route.routes.map((r) => {
|
|
101
|
-
newHono.addRoute(r.method, r.path, r.handler);
|
|
52
|
+
route(path, app) {
|
|
53
|
+
this._tempPath = path;
|
|
54
|
+
if (app) {
|
|
55
|
+
app.routes.map((r) => {
|
|
56
|
+
this.addRoute(r.method, r.path, r.handler);
|
|
102
57
|
});
|
|
103
58
|
}
|
|
104
|
-
return
|
|
59
|
+
return this;
|
|
105
60
|
}
|
|
106
61
|
use(arg1, ...handlers) {
|
|
107
62
|
if (typeof arg1 === 'string') {
|
|
@@ -125,22 +80,30 @@ class Hono extends defineDynamicClass() {
|
|
|
125
80
|
}
|
|
126
81
|
addRoute(method, path, handler) {
|
|
127
82
|
method = method.toUpperCase();
|
|
128
|
-
if (
|
|
129
|
-
path = (0, url_1.mergePath)(
|
|
83
|
+
if (this._tempPath) {
|
|
84
|
+
path = (0, url_1.mergePath)(this._tempPath, path);
|
|
130
85
|
}
|
|
131
|
-
|
|
86
|
+
this._router.add(method, path, handler);
|
|
87
|
+
const r = { path: path, method: method, handler: handler };
|
|
88
|
+
this.routes.push(r);
|
|
132
89
|
}
|
|
133
90
|
async matchRoute(method, path) {
|
|
134
|
-
return
|
|
91
|
+
return this._router.match(method, path);
|
|
135
92
|
}
|
|
136
93
|
async dispatch(request, event, env) {
|
|
137
94
|
const path = (0, url_1.getPathFromURL)(request.url, { strict: this.strict });
|
|
138
95
|
const method = request.method;
|
|
139
96
|
const result = await this.matchRoute(method, path);
|
|
140
|
-
request.param = (key) => {
|
|
141
|
-
if (result)
|
|
142
|
-
|
|
143
|
-
|
|
97
|
+
request.param = ((key) => {
|
|
98
|
+
if (result) {
|
|
99
|
+
if (key) {
|
|
100
|
+
return result.params[key];
|
|
101
|
+
}
|
|
102
|
+
else {
|
|
103
|
+
return result.params;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
});
|
|
144
107
|
const handlers = result ? result.handlers : [this.notFoundHandler];
|
|
145
108
|
const c = new context_1.Context(request, { env: env, event: event, res: undefined });
|
|
146
109
|
c.notFound = () => this.notFoundHandler(c);
|
|
@@ -175,4 +138,3 @@ class Hono extends defineDynamicClass() {
|
|
|
175
138
|
}
|
|
176
139
|
}
|
|
177
140
|
exports.Hono = Hono;
|
|
178
|
-
_Hono_router = new WeakMap(), _Hono_tempPath = new WeakMap();
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Context = exports.
|
|
3
|
+
exports.Context = exports.Hono = void 0;
|
|
4
4
|
var hono_1 = require("./hono");
|
|
5
5
|
Object.defineProperty(exports, "Hono", { enumerable: true, get: function () { return hono_1.Hono; } });
|
|
6
|
-
Object.defineProperty(exports, "Route", { enumerable: true, get: function () { return hono_1.Route; } });
|
|
7
6
|
var context_1 = require("./context");
|
|
8
7
|
Object.defineProperty(exports, "Context", { enumerable: true, get: function () { return context_1.Context; } });
|
|
@@ -2,10 +2,13 @@ import type { Context } from '../../context';
|
|
|
2
2
|
import type { Next } from '../../hono';
|
|
3
3
|
declare global {
|
|
4
4
|
interface Request {
|
|
5
|
-
cookie:
|
|
5
|
+
cookie: {
|
|
6
|
+
(name: string): string;
|
|
7
|
+
(): Record<string, string>;
|
|
8
|
+
};
|
|
6
9
|
}
|
|
7
10
|
}
|
|
8
|
-
declare module '
|
|
11
|
+
declare module '../../context' {
|
|
9
12
|
interface Context {
|
|
10
13
|
cookie: (name: string, value: string, options?: CookieOptions) => void;
|
|
11
14
|
}
|
|
@@ -3,12 +3,17 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.cookie = void 0;
|
|
4
4
|
const cookie = () => {
|
|
5
5
|
return async (c, next) => {
|
|
6
|
-
c.req.cookie = (name) => {
|
|
6
|
+
c.req.cookie = ((name) => {
|
|
7
7
|
const cookie = c.req.headers.get('Cookie');
|
|
8
8
|
const obj = parse(cookie);
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
if (name) {
|
|
10
|
+
const value = obj[name];
|
|
11
|
+
return value;
|
|
12
|
+
}
|
|
13
|
+
else {
|
|
14
|
+
return obj;
|
|
15
|
+
}
|
|
16
|
+
});
|
|
12
17
|
c.cookie = (name, value, opt) => {
|
|
13
18
|
const cookie = serialize(name, value, opt);
|
|
14
19
|
c.header('Set-Cookie', cookie);
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
5
|
exports.errorMessages = exports.getGraphQLParams = exports.graphqlServer = void 0;
|
|
6
6
|
const graphql_1 = require("graphql");
|
|
7
|
-
const parse_body_1 = require("
|
|
7
|
+
const parse_body_1 = require("./parse-body");
|
|
8
8
|
const graphqlServer = (options) => {
|
|
9
9
|
var _a, _b;
|
|
10
10
|
const schema = options.schema;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export { RegExpRouter } from '
|
|
1
|
+
export { RegExpRouter } from './router';
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.RegExpRouter = void 0;
|
|
4
|
-
var router_1 = require("
|
|
4
|
+
var router_1 = require("./router");
|
|
5
5
|
Object.defineProperty(exports, "RegExpRouter", { enumerable: true, get: function () { return router_1.RegExpRouter; } });
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.RegExpRouter = void 0;
|
|
4
4
|
const router_1 = require("../../router");
|
|
5
|
-
const trie_1 = require("
|
|
5
|
+
const trie_1 = require("./trie");
|
|
6
6
|
const emptyParam = {};
|
|
7
7
|
const nullMatcher = [/^$/, []];
|
|
8
8
|
function initHint(path) {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type { ParamMap, Context } from '
|
|
2
|
-
import { Node } from '
|
|
3
|
-
export type { ParamMap } from '
|
|
1
|
+
import type { ParamMap, Context } from './node';
|
|
2
|
+
import { Node } from './node';
|
|
3
|
+
export type { ParamMap } from './node';
|
|
4
4
|
export declare type ReplacementMap = number[];
|
|
5
5
|
interface InitOptions {
|
|
6
6
|
reverse: boolean;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Trie = void 0;
|
|
4
|
-
const node_1 = require("
|
|
4
|
+
const node_1 = require("./node");
|
|
5
5
|
class Trie {
|
|
6
6
|
constructor({ reverse } = { reverse: false }) {
|
|
7
7
|
this.context = { varIndex: 0 };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export { TrieRouter } from '
|
|
1
|
+
export { TrieRouter } from './router';
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.TrieRouter = void 0;
|
|
4
|
-
var router_1 = require("
|
|
4
|
+
var router_1 = require("./router");
|
|
5
5
|
Object.defineProperty(exports, "TrieRouter", { enumerable: true, get: function () { return router_1.TrieRouter; } });
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.TrieRouter = void 0;
|
|
4
4
|
const router_1 = require("../../router");
|
|
5
|
-
const node_1 = require("
|
|
5
|
+
const node_1 = require("./node");
|
|
6
6
|
class TrieRouter extends router_1.Router {
|
|
7
7
|
constructor() {
|
|
8
8
|
super();
|
package/dist/utils/buffer.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.bufferToString = exports.timingSafeEqual = exports.equal = void 0;
|
|
4
|
-
const crypto_1 = require("
|
|
4
|
+
const crypto_1 = require("./crypto");
|
|
5
5
|
const equal = (a, b) => {
|
|
6
6
|
if (a === b) {
|
|
7
7
|
return true;
|
package/dist/utils/url.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hono",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.1",
|
|
4
4
|
"description": "Ultrafast web framework for Cloudflare Workers.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -11,8 +11,8 @@
|
|
|
11
11
|
"test": "jest",
|
|
12
12
|
"lint": "eslint --ext js,ts src .eslintrc.js",
|
|
13
13
|
"lint:fix": "eslint --ext js,ts src .eslintrc.js --fix",
|
|
14
|
-
"build": "rimraf dist && tsc --project tsconfig.build.json
|
|
15
|
-
"watch": "tsc
|
|
14
|
+
"build": "rimraf dist && tsc --project tsconfig.build.json",
|
|
15
|
+
"watch": "tsc --project tsconfig.build.json -w",
|
|
16
16
|
"prepublishOnly": "yarn build"
|
|
17
17
|
},
|
|
18
18
|
"exports": {
|
|
@@ -131,7 +131,6 @@
|
|
|
131
131
|
"prettier-plugin-md-nocjsp": "^1.2.0",
|
|
132
132
|
"rimraf": "^3.0.2",
|
|
133
133
|
"ts-jest": "^27.1.4",
|
|
134
|
-
"tsc-alias": "^1.6.7",
|
|
135
134
|
"typescript": "^4.6.3"
|
|
136
135
|
},
|
|
137
136
|
"engines": {
|