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