hono 2.3.1 → 2.4.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/dist/cjs/compose.js +82 -66
- package/dist/cjs/context.js +159 -144
- package/dist/cjs/hono.js +173 -150
- package/dist/cjs/index.js +31 -11
- package/dist/cjs/middleware/basic-auth/index.js +70 -39
- package/dist/cjs/middleware/bearer-auth/index.js +69 -51
- package/dist/cjs/middleware/cache/index.js +49 -28
- package/dist/cjs/middleware/compress/index.js +39 -16
- package/dist/cjs/middleware/cors/index.js +89 -71
- package/dist/cjs/middleware/etag/index.js +46 -24
- package/dist/cjs/middleware/html/index.js +50 -30
- package/dist/cjs/middleware/jsx/index.js +178 -166
- package/dist/cjs/middleware/jsx/jsx-dev-runtime.js +30 -7
- package/dist/cjs/middleware/jsx/jsx-runtime.js +30 -6
- package/dist/cjs/middleware/jwt/index.js +74 -54
- package/dist/cjs/middleware/logger/index.js +59 -38
- package/dist/cjs/middleware/powered-by/index.js +30 -7
- package/dist/cjs/middleware/pretty-json/index.js +31 -8
- package/dist/cjs/middleware/serve-static/bun.js +56 -38
- package/dist/cjs/middleware/serve-static/index.js +27 -4
- package/dist/cjs/middleware/serve-static/module.js +42 -0
- package/dist/cjs/middleware/serve-static/serve-static.js +58 -39
- package/dist/cjs/middleware/validator/index.js +27 -4
- package/dist/cjs/middleware/validator/middleware.js +125 -91
- package/dist/cjs/package.json +3 -0
- package/dist/cjs/request.js +129 -116
- package/dist/cjs/router/reg-exp-router/index.js +27 -4
- package/dist/cjs/router/reg-exp-router/node.js +108 -97
- package/dist/cjs/router/reg-exp-router/router.js +158 -141
- package/dist/cjs/router/reg-exp-router/trie.js +55 -39
- package/dist/cjs/router/smart-router/index.js +27 -4
- package/dist/cjs/router/smart-router/router.js +70 -49
- package/dist/cjs/router/static-router/index.js +27 -4
- package/dist/cjs/router/static-router/router.js +78 -64
- package/dist/cjs/router/trie-router/index.js +27 -4
- package/dist/cjs/router/trie-router/node.js +167 -159
- package/dist/cjs/router/trie-router/router.js +43 -20
- package/dist/cjs/router.js +35 -6
- package/dist/cjs/types.js +16 -0
- package/dist/cjs/utils/body.js +36 -15
- package/dist/cjs/utils/buffer.js +56 -31
- package/dist/cjs/utils/cloudflare.js +53 -34
- package/dist/cjs/utils/cookie.js +59 -35
- package/dist/cjs/utils/crypto.js +69 -43
- package/dist/cjs/utils/encode.js +92 -65
- package/dist/cjs/utils/filepath.js +39 -22
- package/dist/cjs/utils/html.js +54 -33
- package/dist/cjs/utils/http-status.js +68 -45
- package/dist/cjs/utils/json.js +91 -76
- package/dist/cjs/utils/jwt/index.js +32 -25
- package/dist/cjs/utils/jwt/jwt.js +124 -93
- package/dist/cjs/utils/jwt/types.js +65 -38
- package/dist/cjs/utils/mime.js +110 -87
- package/dist/cjs/utils/object.js +53 -35
- package/dist/cjs/utils/types.js +16 -0
- package/dist/cjs/utils/url.js +89 -78
- package/dist/cjs/validator/rule.js +80 -0
- package/dist/cjs/validator/sanitizer.js +30 -0
- package/dist/cjs/validator/schema.js +16 -0
- package/dist/cjs/validator/validator.js +439 -0
- package/dist/compose.d.ts +4 -3
- package/dist/compose.js +61 -64
- package/dist/context.d.ts +10 -9
- package/dist/context.js +139 -143
- package/dist/hono.d.ts +27 -42
- package/dist/hono.js +153 -149
- package/dist/index.d.ts +4 -1
- package/dist/index.js +9 -9
- package/dist/middleware/basic-auth/index.d.ts +1 -1
- package/dist/middleware/basic-auth/index.js +52 -40
- package/dist/middleware/bearer-auth/index.d.ts +1 -1
- package/dist/middleware/bearer-auth/index.js +48 -49
- package/dist/middleware/cache/index.d.ts +1 -1
- package/dist/middleware/cache/index.js +28 -26
- package/dist/middleware/compress/index.d.ts +1 -1
- package/dist/middleware/compress/index.js +18 -14
- package/dist/middleware/cors/index.d.ts +1 -1
- package/dist/middleware/cors/index.js +68 -69
- package/dist/middleware/etag/index.d.ts +1 -1
- package/dist/middleware/etag/index.js +25 -22
- package/dist/middleware/html/index.js +29 -28
- package/dist/middleware/jsx/index.js +164 -172
- package/dist/middleware/jsx/jsx-dev-runtime.js +9 -5
- package/dist/middleware/jsx/jsx-runtime.js +7 -2
- package/dist/middleware/jwt/index.d.ts +1 -1
- package/dist/middleware/jwt/index.js +53 -52
- package/dist/middleware/logger/index.d.ts +1 -1
- package/dist/middleware/logger/index.js +35 -39
- package/dist/middleware/powered-by/index.d.ts +1 -1
- package/dist/middleware/powered-by/index.js +9 -5
- package/dist/middleware/pretty-json/index.d.ts +1 -1
- package/dist/middleware/pretty-json/index.js +10 -6
- package/dist/middleware/serve-static/bun.d.ts +1 -1
- package/dist/middleware/serve-static/bun.js +35 -36
- package/dist/middleware/serve-static/index.js +5 -1
- package/dist/middleware/serve-static/module.d.ts +3 -0
- package/dist/middleware/serve-static/module.js +13 -0
- package/dist/middleware/serve-static/serve-static.d.ts +2 -3
- package/dist/middleware/serve-static/serve-static.js +36 -36
- package/dist/middleware/validator/index.js +5 -2
- package/dist/middleware/validator/middleware.d.ts +9 -15
- package/dist/middleware/validator/middleware.js +104 -89
- package/dist/request.d.ts +2 -2
- package/dist/request.js +108 -114
- package/dist/router/reg-exp-router/index.js +5 -1
- package/dist/router/reg-exp-router/node.js +87 -96
- package/dist/router/reg-exp-router/router.js +140 -142
- package/dist/router/reg-exp-router/trie.js +35 -38
- package/dist/router/smart-router/index.js +5 -1
- package/dist/router/smart-router/router.js +50 -48
- package/dist/router/static-router/index.js +5 -1
- package/dist/router/static-router/router.js +58 -63
- package/dist/router/trie-router/index.js +5 -1
- package/dist/router/trie-router/node.js +147 -158
- package/dist/router/trie-router/router.js +23 -19
- package/dist/router.js +12 -5
- package/dist/types.d.ts +22 -0
- package/dist/types.js +0 -0
- package/dist/utils/body.js +15 -13
- package/dist/utils/buffer.js +35 -29
- package/dist/utils/cloudflare.js +32 -32
- package/dist/utils/cookie.js +38 -33
- package/dist/utils/crypto.js +48 -41
- package/dist/utils/encode.js +70 -62
- package/dist/utils/filepath.js +18 -20
- package/dist/utils/html.js +34 -32
- package/dist/utils/http-status.js +48 -44
- package/dist/utils/json.js +71 -75
- package/dist/utils/jwt/index.js +5 -1
- package/dist/utils/jwt/jwt.js +95 -90
- package/dist/utils/jwt/types.js +47 -41
- package/dist/utils/mime.js +90 -86
- package/dist/utils/object.js +31 -32
- package/dist/utils/types.d.ts +3 -0
- package/dist/utils/types.js +0 -0
- package/dist/utils/url.js +69 -77
- package/dist/{middleware/validator → validator}/rule.d.ts +0 -0
- package/dist/validator/rule.js +57 -0
- package/dist/{middleware/validator → validator}/sanitizer.d.ts +0 -0
- package/dist/validator/sanitizer.js +7 -0
- package/dist/validator/schema.d.ts +7 -0
- package/dist/validator/schema.js +0 -0
- package/dist/{middleware/validator → validator}/validator.d.ts +10 -7
- package/dist/validator/validator.js +406 -0
- package/package.json +14 -8
- package/dist/cjs/middleware/validator/rule.js +0 -66
- package/dist/cjs/middleware/validator/sanitizer.js +0 -6
- package/dist/cjs/middleware/validator/validator.js +0 -417
- package/dist/middleware/serve-static/module.d.mts +0 -3
- package/dist/middleware/serve-static/module.mjs +0 -13
- package/dist/middleware/validator/rule.js +0 -63
- package/dist/middleware/validator/sanitizer.js +0 -3
- package/dist/middleware/validator/validator.js +0 -403
package/dist/compose.js
CHANGED
|
@@ -1,66 +1,63 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
else {
|
|
24
|
-
try {
|
|
25
|
-
res = handler(context, () => {
|
|
26
|
-
const dispatchRes = dispatch(i + 1);
|
|
27
|
-
return dispatchRes instanceof Promise ? dispatchRes : Promise.resolve(dispatchRes);
|
|
28
|
-
});
|
|
29
|
-
}
|
|
30
|
-
catch (err) {
|
|
31
|
-
if (err instanceof Error && context instanceof HonoContext && onError) {
|
|
32
|
-
context.error = err;
|
|
33
|
-
res = onError(err, context);
|
|
34
|
-
isError = true;
|
|
35
|
-
}
|
|
36
|
-
else {
|
|
37
|
-
throw err;
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
if (!(res instanceof Promise)) {
|
|
42
|
-
if (res && (context.finalized === false || isError)) {
|
|
43
|
-
context.res = res;
|
|
44
|
-
}
|
|
45
|
-
return context;
|
|
46
|
-
}
|
|
47
|
-
else {
|
|
48
|
-
return res
|
|
49
|
-
.then((res) => {
|
|
50
|
-
if (res && context.finalized === false) {
|
|
51
|
-
context.res = res;
|
|
52
|
-
}
|
|
53
|
-
return context;
|
|
54
|
-
})
|
|
55
|
-
.catch((err) => {
|
|
56
|
-
if (err instanceof Error && context instanceof HonoContext && onError) {
|
|
57
|
-
context.error = err;
|
|
58
|
-
context.res = onError(err, context);
|
|
59
|
-
return context;
|
|
60
|
-
}
|
|
61
|
-
throw err;
|
|
62
|
-
});
|
|
63
|
-
}
|
|
1
|
+
// src/compose.ts
|
|
2
|
+
import { HonoContext } from "./context.js";
|
|
3
|
+
var compose = (middleware, onNotFound, onError) => {
|
|
4
|
+
const middlewareLength = middleware.length;
|
|
5
|
+
return (context, next) => {
|
|
6
|
+
let index = -1;
|
|
7
|
+
return dispatch(0);
|
|
8
|
+
function dispatch(i) {
|
|
9
|
+
if (i <= index) {
|
|
10
|
+
throw new Error("next() called multiple times");
|
|
11
|
+
}
|
|
12
|
+
let handler = middleware[i];
|
|
13
|
+
index = i;
|
|
14
|
+
if (i === middlewareLength && next)
|
|
15
|
+
handler = next;
|
|
16
|
+
let res;
|
|
17
|
+
let isError = false;
|
|
18
|
+
if (!handler) {
|
|
19
|
+
if (context instanceof HonoContext && context.finalized === false && onNotFound) {
|
|
20
|
+
res = onNotFound(context);
|
|
64
21
|
}
|
|
65
|
-
|
|
22
|
+
} else {
|
|
23
|
+
try {
|
|
24
|
+
res = handler(context, () => {
|
|
25
|
+
const dispatchRes = dispatch(i + 1);
|
|
26
|
+
return dispatchRes instanceof Promise ? dispatchRes : Promise.resolve(dispatchRes);
|
|
27
|
+
});
|
|
28
|
+
} catch (err) {
|
|
29
|
+
if (err instanceof Error && context instanceof HonoContext && onError) {
|
|
30
|
+
context.error = err;
|
|
31
|
+
res = onError(err, context);
|
|
32
|
+
isError = true;
|
|
33
|
+
} else {
|
|
34
|
+
throw err;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
if (!(res instanceof Promise)) {
|
|
39
|
+
if (res && (context.finalized === false || isError)) {
|
|
40
|
+
context.res = res;
|
|
41
|
+
}
|
|
42
|
+
return context;
|
|
43
|
+
} else {
|
|
44
|
+
return res.then((res2) => {
|
|
45
|
+
if (res2 && context.finalized === false) {
|
|
46
|
+
context.res = res2;
|
|
47
|
+
}
|
|
48
|
+
return context;
|
|
49
|
+
}).catch((err) => {
|
|
50
|
+
if (err instanceof Error && context instanceof HonoContext && onError) {
|
|
51
|
+
context.error = err;
|
|
52
|
+
context.res = onError(err, context);
|
|
53
|
+
return context;
|
|
54
|
+
}
|
|
55
|
+
throw err;
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
};
|
|
61
|
+
export {
|
|
62
|
+
compose
|
|
66
63
|
};
|
package/dist/context.d.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
/// <reference types="@cloudflare/workers-types" />
|
|
2
|
-
import type { Environment, NotFoundHandler, ContextVariableMap
|
|
2
|
+
import type { Environment, NotFoundHandler, ContextVariableMap } from './types';
|
|
3
3
|
import type { CookieOptions } from './utils/cookie';
|
|
4
4
|
import type { StatusCode } from './utils/http-status';
|
|
5
|
+
import type { Schema, SchemaToProp } from './validator/schema';
|
|
5
6
|
declare type Headers = Record<string, string | string[]>;
|
|
6
7
|
export declare type Data = string | ArrayBuffer | ReadableStream;
|
|
7
|
-
export interface Context<
|
|
8
|
-
req: Request<
|
|
8
|
+
export interface Context<P extends string = string, E extends Partial<Environment> = Environment, S extends Partial<Schema> | unknown = Schema> {
|
|
9
|
+
req: Request<P, S extends Schema ? SchemaToProp<S> : any>;
|
|
9
10
|
env: E['Bindings'];
|
|
10
11
|
event: FetchEvent;
|
|
11
12
|
executionCtx: ExecutionContext;
|
|
@@ -20,7 +21,7 @@ export interface Context<RequestParamKeyType extends string = string, E extends
|
|
|
20
21
|
set: {
|
|
21
22
|
<Key extends keyof ContextVariableMap>(key: Key, value: ContextVariableMap[Key]): void;
|
|
22
23
|
<Key extends keyof E['Variables']>(key: Key, value: E['Variables'][Key]): void;
|
|
23
|
-
(key: string, value:
|
|
24
|
+
(key: string, value: unknown): void;
|
|
24
25
|
};
|
|
25
26
|
get: {
|
|
26
27
|
<Key extends keyof ContextVariableMap>(key: Key): ContextVariableMap[Key];
|
|
@@ -37,8 +38,8 @@ export interface Context<RequestParamKeyType extends string = string, E extends
|
|
|
37
38
|
cookie: (name: string, value: string, options?: CookieOptions) => void;
|
|
38
39
|
notFound: () => Response | Promise<Response>;
|
|
39
40
|
}
|
|
40
|
-
export declare class HonoContext<
|
|
41
|
-
req: Request<
|
|
41
|
+
export declare class HonoContext<P extends string = string, E extends Partial<Environment> = Environment, S extends Partial<Schema> = Schema> implements Context<P, E, S> {
|
|
42
|
+
req: Request<P, S extends Schema ? SchemaToProp<S> : any>;
|
|
42
43
|
env: E['Bindings'];
|
|
43
44
|
finalized: boolean;
|
|
44
45
|
error: Error | undefined;
|
|
@@ -50,7 +51,7 @@ export declare class HonoContext<RequestParamKeyType extends string = string, E
|
|
|
50
51
|
private _headers;
|
|
51
52
|
private _res;
|
|
52
53
|
private notFoundHandler;
|
|
53
|
-
constructor(req: Request<
|
|
54
|
+
constructor(req: Request<P>, env?: E['Bindings'], executionCtx?: FetchEvent | ExecutionContext | undefined, notFoundHandler?: NotFoundHandler<P, E, S>);
|
|
54
55
|
get event(): FetchEvent;
|
|
55
56
|
get executionCtx(): ExecutionContext;
|
|
56
57
|
get res(): Response;
|
|
@@ -61,10 +62,10 @@ export declare class HonoContext<RequestParamKeyType extends string = string, E
|
|
|
61
62
|
status(status: StatusCode): void;
|
|
62
63
|
set<Key extends keyof ContextVariableMap>(key: Key, value: ContextVariableMap[Key]): void;
|
|
63
64
|
set<Key extends keyof E['Variables']>(key: Key, value: E['Variables'][Key]): void;
|
|
64
|
-
set(key: string, value:
|
|
65
|
+
set(key: string, value: unknown): void;
|
|
65
66
|
get<Key extends keyof ContextVariableMap>(key: Key): ContextVariableMap[Key];
|
|
66
67
|
get<Key extends keyof E['Variables']>(key: Key): E['Variables'][Key];
|
|
67
|
-
get<T
|
|
68
|
+
get<T>(key: string): T;
|
|
68
69
|
pretty(prettyJSON: boolean, space?: number): void;
|
|
69
70
|
newResponse(data: Data | null, status: StatusCode, headers?: Headers): Response;
|
|
70
71
|
private _finalizeHeaders;
|
package/dist/context.js
CHANGED
|
@@ -1,144 +1,140 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
finalizedHeaders.push([key, value]);
|
|
98
|
-
}
|
|
99
|
-
else {
|
|
100
|
-
for (const v of value) {
|
|
101
|
-
finalizedHeaders.push([key, v]);
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
delete headersKv[key];
|
|
1
|
+
// src/context.ts
|
|
2
|
+
import { serialize } from "./utils/cookie.js";
|
|
3
|
+
var HonoContext = class {
|
|
4
|
+
constructor(req, env = {}, executionCtx = void 0, notFoundHandler = () => new Response()) {
|
|
5
|
+
this.error = void 0;
|
|
6
|
+
this._status = 200;
|
|
7
|
+
this._pretty = false;
|
|
8
|
+
this._prettySpace = 2;
|
|
9
|
+
this._executionCtx = executionCtx;
|
|
10
|
+
this.req = req;
|
|
11
|
+
this.env = env || {};
|
|
12
|
+
this.notFoundHandler = notFoundHandler;
|
|
13
|
+
this.finalized = false;
|
|
14
|
+
}
|
|
15
|
+
get event() {
|
|
16
|
+
if (this._executionCtx instanceof FetchEvent) {
|
|
17
|
+
return this._executionCtx;
|
|
18
|
+
} else {
|
|
19
|
+
throw Error("This context has no FetchEvent");
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
get executionCtx() {
|
|
23
|
+
if (this._executionCtx) {
|
|
24
|
+
return this._executionCtx;
|
|
25
|
+
} else {
|
|
26
|
+
throw Error("This context has no ExecutionContext");
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
get res() {
|
|
30
|
+
return this._res || (this._res = new Response("404 Not Found", { status: 404 }));
|
|
31
|
+
}
|
|
32
|
+
set res(_res) {
|
|
33
|
+
this._res = _res;
|
|
34
|
+
this.finalized = true;
|
|
35
|
+
}
|
|
36
|
+
header(name, value, options) {
|
|
37
|
+
this._headers || (this._headers = {});
|
|
38
|
+
const key = name.toLowerCase();
|
|
39
|
+
let shouldAppend = false;
|
|
40
|
+
if (options && options.append) {
|
|
41
|
+
const vAlreadySet = this._headers[key];
|
|
42
|
+
if (vAlreadySet && vAlreadySet.length) {
|
|
43
|
+
shouldAppend = true;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
if (shouldAppend) {
|
|
47
|
+
this._headers[key].push(value);
|
|
48
|
+
} else {
|
|
49
|
+
this._headers[key] = [value];
|
|
50
|
+
}
|
|
51
|
+
if (this.finalized) {
|
|
52
|
+
if (shouldAppend) {
|
|
53
|
+
this.res.headers.append(name, value);
|
|
54
|
+
} else {
|
|
55
|
+
this.res.headers.set(name, value);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
status(status) {
|
|
60
|
+
this._status = status;
|
|
61
|
+
}
|
|
62
|
+
set(key, value) {
|
|
63
|
+
this._map || (this._map = {});
|
|
64
|
+
this._map[key] = value;
|
|
65
|
+
}
|
|
66
|
+
get(key) {
|
|
67
|
+
if (!this._map) {
|
|
68
|
+
return void 0;
|
|
69
|
+
}
|
|
70
|
+
return this._map[key];
|
|
71
|
+
}
|
|
72
|
+
pretty(prettyJSON, space = 2) {
|
|
73
|
+
this._pretty = prettyJSON;
|
|
74
|
+
this._prettySpace = space;
|
|
75
|
+
}
|
|
76
|
+
newResponse(data, status, headers = {}) {
|
|
77
|
+
return new Response(data, {
|
|
78
|
+
status: status || this._status || 200,
|
|
79
|
+
headers: this._finalizeHeaders(headers)
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
_finalizeHeaders(incomingHeaders) {
|
|
83
|
+
const finalizedHeaders = [];
|
|
84
|
+
const headersKv = this._headers || {};
|
|
85
|
+
if (this._res) {
|
|
86
|
+
this._res.headers.forEach((v, k) => {
|
|
87
|
+
headersKv[k] = [v];
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
for (const key of Object.keys(incomingHeaders)) {
|
|
91
|
+
const value = incomingHeaders[key];
|
|
92
|
+
if (typeof value === "string") {
|
|
93
|
+
finalizedHeaders.push([key, value]);
|
|
94
|
+
} else {
|
|
95
|
+
for (const v of value) {
|
|
96
|
+
finalizedHeaders.push([key, v]);
|
|
105
97
|
}
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
}
|
|
98
|
+
}
|
|
99
|
+
delete headersKv[key];
|
|
100
|
+
}
|
|
101
|
+
for (const key of Object.keys(headersKv)) {
|
|
102
|
+
for (const value of headersKv[key]) {
|
|
103
|
+
const kv = [key, value];
|
|
104
|
+
finalizedHeaders.push(kv);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return finalizedHeaders;
|
|
108
|
+
}
|
|
109
|
+
body(data, status = this._status, headers = {}) {
|
|
110
|
+
return this.newResponse(data, status, headers);
|
|
111
|
+
}
|
|
112
|
+
text(text, status = this._status, headers = {}) {
|
|
113
|
+
headers["content-type"] = "text/plain; charset=UTF-8";
|
|
114
|
+
return this.body(text, status, headers);
|
|
115
|
+
}
|
|
116
|
+
json(object, status = this._status, headers = {}) {
|
|
117
|
+
const body = this._pretty ? JSON.stringify(object, null, this._prettySpace) : JSON.stringify(object);
|
|
118
|
+
headers["content-type"] = "application/json; charset=UTF-8";
|
|
119
|
+
return this.body(body, status, headers);
|
|
120
|
+
}
|
|
121
|
+
html(html, status = this._status, headers = {}) {
|
|
122
|
+
headers["content-type"] = "text/html; charset=UTF-8";
|
|
123
|
+
return this.body(html, status, headers);
|
|
124
|
+
}
|
|
125
|
+
redirect(location, status = 302) {
|
|
126
|
+
return this.newResponse(null, status, {
|
|
127
|
+
Location: location
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
cookie(name, value, opt) {
|
|
131
|
+
const cookie = serialize(name, value, opt);
|
|
132
|
+
this.header("set-cookie", cookie, { append: true });
|
|
133
|
+
}
|
|
134
|
+
notFound() {
|
|
135
|
+
return this.notFoundHandler(this);
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
export {
|
|
139
|
+
HonoContext
|
|
140
|
+
};
|
package/dist/hono.d.ts
CHANGED
|
@@ -1,58 +1,43 @@
|
|
|
1
1
|
/// <reference types="@cloudflare/workers-types" />
|
|
2
|
-
import type { Context } from './context';
|
|
3
2
|
import type { Router } from './router';
|
|
4
|
-
|
|
3
|
+
import type { Handler, Environment, ParamKeys, ErrorHandler, NotFoundHandler } from './types';
|
|
4
|
+
import type { Schema } from './validator/schema';
|
|
5
|
+
interface HandlerInterface<P extends string, E extends Partial<Environment>, S extends Partial<Schema>, U = Hono<E, P, S>> {
|
|
6
|
+
<Path extends string, Data extends Schema>(...handlers: Handler<ParamKeys<Path> extends never ? string : ParamKeys<Path>, E, Data>[]): U;
|
|
7
|
+
(...handlers: Handler<string, E, S>[]): U;
|
|
8
|
+
<Path extends string, Data extends Partial<Schema> = Schema>(path: Path, ...handlers: Handler<ParamKeys<Path> extends never ? string : ParamKeys<Path>, E, Data>[]): U;
|
|
9
|
+
<Path extends string, Data extends Schema>(path: Path, ...handlers: Handler<string, E, Data>[]): U;
|
|
10
|
+
(path: string, ...handlers: Handler<string, E, S>[]): U;
|
|
5
11
|
}
|
|
6
|
-
|
|
7
|
-
export declare type Variables = Record<string, any>;
|
|
8
|
-
export declare type Environment = {
|
|
9
|
-
Bindings: Bindings;
|
|
10
|
-
Variables: Variables;
|
|
11
|
-
};
|
|
12
|
-
export declare type ValidatedData = Record<string, any>;
|
|
13
|
-
export declare type Handler<RequestParamKeyType extends string = string, E extends Partial<Environment> = Environment, D extends ValidatedData = ValidatedData> = (c: Context<RequestParamKeyType, E, D>, next: Next) => Response | Promise<Response> | Promise<void> | Promise<Response | undefined | void>;
|
|
14
|
-
export declare type MiddlewareHandler = <E extends Partial<Environment> = Environment>(c: Context<string, E>, next: Next) => Promise<void> | Promise<Response | undefined>;
|
|
15
|
-
export declare type NotFoundHandler<E extends Partial<Environment> = Environment> = (c: Context<string, E>) => Response | Promise<Response>;
|
|
16
|
-
export declare type ErrorHandler<E extends Partial<Environment> = Environment> = (err: Error, c: Context<string, E>) => Response;
|
|
17
|
-
export declare type Next = () => Promise<void>;
|
|
18
|
-
declare type ParamKeyName<NameWithPattern> = NameWithPattern extends `${infer Name}{${infer _Pattern}` ? Name : NameWithPattern;
|
|
19
|
-
declare type ParamKey<Component> = Component extends `:${infer NameWithPattern}` ? ParamKeyName<NameWithPattern> : never;
|
|
20
|
-
declare type ParamKeys<Path> = Path extends `${infer Component}/${infer Rest}` ? ParamKey<Component> | ParamKeys<Rest> : ParamKey<Path>;
|
|
21
|
-
interface HandlerInterface<T extends string, E extends Partial<Environment>, U = Hono<E, T>> {
|
|
22
|
-
<Path extends string, Data extends ValidatedData>(...handlers: Handler<ParamKeys<Path> extends never ? string : ParamKeys<Path>, E, Data>[]): U;
|
|
23
|
-
(...handlers: Handler<string, E>[]): U;
|
|
24
|
-
<Path extends string, Data extends ValidatedData>(path: Path, ...handlers: Handler<ParamKeys<Path> extends never ? string : ParamKeys<Path>, E, Data>[]): U;
|
|
25
|
-
(path: string, ...handlers: Handler<string, E>[]): U;
|
|
26
|
-
}
|
|
27
|
-
interface Route<E extends Partial<Environment> = Environment, D extends ValidatedData = ValidatedData> {
|
|
12
|
+
interface Route<P extends string = string, E extends Partial<Environment> = Environment, S extends Partial<Schema> = Schema> {
|
|
28
13
|
path: string;
|
|
29
14
|
method: string;
|
|
30
|
-
handler: Handler<
|
|
15
|
+
handler: Handler<P, E, S>;
|
|
31
16
|
}
|
|
32
|
-
declare const Hono_base: new <E_1 extends Partial<Environment> = Environment,
|
|
33
|
-
all: HandlerInterface<
|
|
34
|
-
get: HandlerInterface<
|
|
35
|
-
post: HandlerInterface<
|
|
36
|
-
put: HandlerInterface<
|
|
37
|
-
delete: HandlerInterface<
|
|
38
|
-
head: HandlerInterface<
|
|
39
|
-
options: HandlerInterface<
|
|
40
|
-
patch: HandlerInterface<
|
|
17
|
+
declare const Hono_base: new <E_1 extends Partial<Environment> = Environment, P_1 extends string = string, S_1 extends Partial<Schema> = Schema, U = Hono<E_1, P_1, S_1>>() => {
|
|
18
|
+
all: HandlerInterface<P_1, E_1, S_1, U>;
|
|
19
|
+
get: HandlerInterface<P_1, E_1, S_1, U>;
|
|
20
|
+
post: HandlerInterface<P_1, E_1, S_1, U>;
|
|
21
|
+
put: HandlerInterface<P_1, E_1, S_1, U>;
|
|
22
|
+
delete: HandlerInterface<P_1, E_1, S_1, U>;
|
|
23
|
+
head: HandlerInterface<P_1, E_1, S_1, U>;
|
|
24
|
+
options: HandlerInterface<P_1, E_1, S_1, U>;
|
|
25
|
+
patch: HandlerInterface<P_1, E_1, S_1, U>;
|
|
41
26
|
};
|
|
42
|
-
export declare class Hono<E extends Partial<Environment> = Environment, P extends string = '/',
|
|
43
|
-
readonly router: Router<Handler<
|
|
27
|
+
export declare class Hono<E extends Partial<Environment> = Environment, P extends string = '/', S extends Partial<Schema> = Schema> extends Hono_base<E, P, S, Hono<E, P, S>> {
|
|
28
|
+
readonly router: Router<Handler<P, E, S>>;
|
|
44
29
|
readonly strict: boolean;
|
|
45
30
|
private _tempPath;
|
|
46
31
|
private path;
|
|
47
|
-
routes: Route<E,
|
|
32
|
+
routes: Route<P, E, S>[];
|
|
48
33
|
constructor(init?: Partial<Pick<Hono, 'router' | 'strict'>>);
|
|
49
34
|
private notFoundHandler;
|
|
50
35
|
private errorHandler;
|
|
51
|
-
route(path: string, app?: Hono<
|
|
52
|
-
use<Path extends string = string, Data extends
|
|
53
|
-
use<Path extends string = string, Data extends
|
|
54
|
-
onError(handler: ErrorHandler<E>):
|
|
55
|
-
notFound(handler: NotFoundHandler<E>):
|
|
36
|
+
route(path: string, app?: Hono<E, P, S>): this;
|
|
37
|
+
use<Path extends string = string, Data extends Partial<Schema> = Schema>(...middleware: Handler<Path, E, Data>[]): Hono<E, Path, S>;
|
|
38
|
+
use<Path extends string = string, Data extends Partial<Schema> = Schema>(arg1: string, ...middleware: Handler<Path, E, Data>[]): Hono<E, Path, S>;
|
|
39
|
+
onError(handler: ErrorHandler<P, E, S>): this;
|
|
40
|
+
notFound(handler: NotFoundHandler<P, E, S>): this;
|
|
56
41
|
private addRoute;
|
|
57
42
|
private matchRoute;
|
|
58
43
|
private handleError;
|