hono 0.3.7 → 0.3.8
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 +4 -25
- package/dist/context.d.ts +3 -3
- package/dist/context.js +1 -1
- package/dist/hono.d.ts +23 -11
- package/dist/hono.js +1 -18
- package/package.json +3 -2
- package/dist/middleware.d.ts +0 -2
- package/dist/middleware.js +0 -6
package/README.md
CHANGED
|
@@ -13,7 +13,7 @@ app.fire()
|
|
|
13
13
|
|
|
14
14
|
## Features
|
|
15
15
|
|
|
16
|
-
- **Ultra fast** - the router is implemented with Trie-Tree structure.
|
|
16
|
+
- **Ultra fast** - the router is implemented with Trie-Tree structure. Not use loops.
|
|
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.
|
|
@@ -174,7 +174,7 @@ app.use('*', async (c, next) => {
|
|
|
174
174
|
// Add a custom header
|
|
175
175
|
app.use('/message/*', async (c, next) => {
|
|
176
176
|
await next()
|
|
177
|
-
await c.
|
|
177
|
+
await c.header('x-message', 'This is middleware!')
|
|
178
178
|
})
|
|
179
179
|
|
|
180
180
|
app.get('/message/hello', (c) => c.text('Hello Middleware!'))
|
|
@@ -193,30 +193,9 @@ app.use('*', async (c, next) => {
|
|
|
193
193
|
})
|
|
194
194
|
```
|
|
195
195
|
|
|
196
|
-
### Complex Pattern
|
|
197
|
-
|
|
198
|
-
You can also do this:
|
|
199
|
-
|
|
200
|
-
```js
|
|
201
|
-
// Output response time
|
|
202
|
-
app.use('*', async (c, next) => {
|
|
203
|
-
await next()
|
|
204
|
-
const responseTime = await c.res.headers.get('X-Response-Time')
|
|
205
|
-
console.log(`X-Response-Time: ${responseTime}`)
|
|
206
|
-
})
|
|
207
|
-
|
|
208
|
-
// Add X-Response-Time header
|
|
209
|
-
app.use('*', async (c, next) => {
|
|
210
|
-
const start = Date.now()
|
|
211
|
-
await next()
|
|
212
|
-
const ms = Date.now() - start
|
|
213
|
-
await c.res.headers.append('X-Response-Time', `${ms}ms`)
|
|
214
|
-
})
|
|
215
|
-
```
|
|
216
|
-
|
|
217
196
|
## Context
|
|
218
197
|
|
|
219
|
-
To handle Request and Reponse
|
|
198
|
+
To handle Request and Reponse, you can use Context object:
|
|
220
199
|
|
|
221
200
|
### c.req
|
|
222
201
|
|
|
@@ -430,7 +409,7 @@ Run the development server locally. Then, access like `http://127.0.0.1:8787/` i
|
|
|
430
409
|
wrangler dev
|
|
431
410
|
```
|
|
432
411
|
|
|
433
|
-
### Publish
|
|
412
|
+
### 7. Publish
|
|
434
413
|
|
|
435
414
|
Deploy to Cloudflare. That's all!
|
|
436
415
|
|
package/dist/context.d.ts
CHANGED
|
@@ -5,8 +5,8 @@ declare type Headers = {
|
|
|
5
5
|
declare type Data = string | ArrayBuffer | ReadableStream;
|
|
6
6
|
export interface Env {
|
|
7
7
|
}
|
|
8
|
-
export declare class Context {
|
|
9
|
-
req: Request
|
|
8
|
+
export declare class Context<RequestParamKeyType = string> {
|
|
9
|
+
req: Request<RequestParamKeyType>;
|
|
10
10
|
res: Response;
|
|
11
11
|
env: Env;
|
|
12
12
|
event: FetchEvent;
|
|
@@ -14,7 +14,7 @@ export declare class Context {
|
|
|
14
14
|
private _status;
|
|
15
15
|
private _statusText;
|
|
16
16
|
render: (template: string, params?: object, options?: object) => Promise<Response>;
|
|
17
|
-
constructor(req: Request
|
|
17
|
+
constructor(req: Request<RequestParamKeyType>, opts?: {
|
|
18
18
|
res: Response;
|
|
19
19
|
env: Env;
|
|
20
20
|
event: FetchEvent;
|
package/dist/context.js
CHANGED
package/dist/hono.d.ts
CHANGED
|
@@ -4,15 +4,18 @@ import { Node } from './node';
|
|
|
4
4
|
import { Context } from './context';
|
|
5
5
|
import type { Env } from './context';
|
|
6
6
|
declare global {
|
|
7
|
-
interface Request {
|
|
8
|
-
param: (key:
|
|
7
|
+
interface Request<ParamKeyType = string> {
|
|
8
|
+
param: (key: ParamKeyType) => string;
|
|
9
9
|
query: (key: string) => string;
|
|
10
10
|
header: (name: string) => string;
|
|
11
11
|
parsedBody: any;
|
|
12
12
|
}
|
|
13
13
|
}
|
|
14
|
-
export declare type Handler = (c: Context
|
|
14
|
+
export declare type Handler<RequestParamKeyType = string> = (c: Context<RequestParamKeyType>, next?: Function) => Response | Promise<Response>;
|
|
15
15
|
export declare type MiddlewareHandler = (c: Context, next: Function) => Promise<void>;
|
|
16
|
+
declare type ParamKeyName<NameWithPattern> = NameWithPattern extends `${infer Name}{${infer _Pattern}` ? Name : NameWithPattern;
|
|
17
|
+
declare type ParamKey<Component> = Component extends `:${infer NameWithPattern}` ? ParamKeyName<NameWithPattern> : never;
|
|
18
|
+
declare type ParamKeys<Path> = Path extends `${infer Component}/${infer Rest}` ? ParamKey<Component> | ParamKeys<Rest> : ParamKey<Path>;
|
|
16
19
|
export declare class Router<T> {
|
|
17
20
|
node: Node<T>;
|
|
18
21
|
constructor();
|
|
@@ -24,14 +27,22 @@ export declare class Hono {
|
|
|
24
27
|
middlewareRouters: Router<MiddlewareHandler>[];
|
|
25
28
|
tempPath: string;
|
|
26
29
|
constructor();
|
|
27
|
-
get(arg:
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
30
|
+
get<Path extends string>(arg: Path, ...args: Handler<ParamKeys<Path>>[]): Hono;
|
|
31
|
+
get(arg: Handler<never>, ...args: Handler<never>[]): Hono;
|
|
32
|
+
post<Path extends string>(arg: Path, ...args: Handler<ParamKeys<Path>>[]): Hono;
|
|
33
|
+
post(arg: Handler, ...args: Handler[]): Hono;
|
|
34
|
+
put<Path extends string>(arg: Path, ...args: Handler<ParamKeys<Path>>[]): Hono;
|
|
35
|
+
put(arg: Handler, ...args: Handler[]): Hono;
|
|
36
|
+
head<Path extends string>(arg: Path, ...args: Handler<ParamKeys<Path>>[]): Hono;
|
|
37
|
+
head(arg: Handler, ...args: Handler[]): Hono;
|
|
38
|
+
delete<Path extends string>(arg: Path, ...args: Handler<ParamKeys<Path>>[]): Hono;
|
|
39
|
+
delete(arg: Handler, ...args: Handler[]): Hono;
|
|
40
|
+
options<Path extends string>(arg: Path, ...args: Handler<ParamKeys<Path>>[]): Hono;
|
|
41
|
+
options(arg: Handler, ...args: Handler[]): Hono;
|
|
42
|
+
patch<Path extends string>(arg: Path, ...args: Handler<ParamKeys<Path>>[]): Hono;
|
|
43
|
+
patch(arg: Handler, ...args: Handler[]): Hono;
|
|
44
|
+
all<Path extends string>(arg: Path, ...args: Handler<ParamKeys<Path>>[]): Hono;
|
|
45
|
+
all(arg: Handler<never>, ...args: Handler<never>[]): Hono;
|
|
35
46
|
route(path: string): Hono;
|
|
36
47
|
use(path: string, middleware: MiddlewareHandler): void;
|
|
37
48
|
addRoute(method: string, arg: string | Handler, ...args: Handler[]): Hono;
|
|
@@ -43,3 +54,4 @@ export declare class Hono {
|
|
|
43
54
|
onError(err: Error): Response;
|
|
44
55
|
notFound(): Response;
|
|
45
56
|
}
|
|
57
|
+
export {};
|
package/dist/hono.js
CHANGED
|
@@ -24,7 +24,6 @@ class Hono {
|
|
|
24
24
|
this.middlewareRouters = [];
|
|
25
25
|
this.tempPath = '/';
|
|
26
26
|
}
|
|
27
|
-
/* HTTP METHODS */
|
|
28
27
|
get(arg, ...args) {
|
|
29
28
|
return this.addRoute('get', arg, ...args);
|
|
30
29
|
}
|
|
@@ -46,22 +45,6 @@ class Hono {
|
|
|
46
45
|
patch(arg, ...args) {
|
|
47
46
|
return this.addRoute('patch', arg, ...args);
|
|
48
47
|
}
|
|
49
|
-
/*
|
|
50
|
-
We may implement these HTTP methods:
|
|
51
|
-
trace
|
|
52
|
-
copy
|
|
53
|
-
lock
|
|
54
|
-
purge
|
|
55
|
-
unlock
|
|
56
|
-
report
|
|
57
|
-
checkout
|
|
58
|
-
merge
|
|
59
|
-
notify
|
|
60
|
-
subscribe
|
|
61
|
-
unsubscribe
|
|
62
|
-
search
|
|
63
|
-
connect
|
|
64
|
-
*/
|
|
65
48
|
all(arg, ...args) {
|
|
66
49
|
return this.addRoute('all', arg, ...args);
|
|
67
50
|
}
|
|
@@ -158,7 +141,7 @@ class Hono {
|
|
|
158
141
|
}
|
|
159
142
|
notFound() {
|
|
160
143
|
const message = 'Not Found';
|
|
161
|
-
return new Response(
|
|
144
|
+
return new Response(message, {
|
|
162
145
|
status: 404,
|
|
163
146
|
headers: {
|
|
164
147
|
'Content-Length': message.length.toString(),
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hono",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.8",
|
|
4
4
|
"description": "[炎] Ultrafast web framework for Cloudflare Workers.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -17,7 +17,8 @@
|
|
|
17
17
|
"./mustache": "./dist/middleware/mustache/mustache.js",
|
|
18
18
|
"./powered-by": "./dist/middleware/powered-by/powered-by.js",
|
|
19
19
|
"./serve-static": "./dist/middleware/serve-static/serve-static.js",
|
|
20
|
-
"./utils/buffer": "./dist/utils/buffer.js"
|
|
20
|
+
"./utils/buffer": "./dist/utils/buffer.js",
|
|
21
|
+
"./package.json": "./package.json"
|
|
21
22
|
},
|
|
22
23
|
"typesVersions": {
|
|
23
24
|
"*": {
|
package/dist/middleware.d.ts
DELETED