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
@@ -1,6 +1,10 @@
1
- import { jsx } from '.';
2
- export function jsxDEV(tag, props) {
3
- const children = props.children ?? [];
4
- delete props['children'];
5
- return jsx(tag, props, children);
1
+ // src/middleware/jsx/jsx-dev-runtime.ts
2
+ import { jsx } from "./index.js";
3
+ function jsxDEV(tag, props) {
4
+ const children = props.children ?? [];
5
+ delete props["children"];
6
+ return jsx(tag, props, children);
6
7
  }
8
+ export {
9
+ jsxDEV
10
+ };
@@ -1,2 +1,7 @@
1
- export { jsxDEV as jsx } from './jsx-dev-runtime';
2
- export { jsxDEV as jsxs } from './jsx-dev-runtime';
1
+ // src/middleware/jsx/jsx-runtime.ts
2
+ import { jsxDEV } from "./jsx-dev-runtime.js";
3
+ import { jsxDEV as jsxDEV2 } from "./jsx-dev-runtime.js";
4
+ export {
5
+ jsxDEV as jsx,
6
+ jsxDEV2 as jsxs
7
+ };
@@ -1,4 +1,4 @@
1
- import type { MiddlewareHandler } from '../../hono';
1
+ import type { MiddlewareHandler } from '../../types';
2
2
  export declare const jwt: (options: {
3
3
  secret: string;
4
4
  cookie?: string;
@@ -1,56 +1,57 @@
1
- import { Jwt } from '../../utils/jwt';
2
- export const jwt = (options) => {
3
- if (!options) {
4
- throw new Error('JWT auth middleware requires options for "secret');
1
+ // src/middleware/jwt/index.ts
2
+ import { Jwt } from "../../utils/jwt/index.js";
3
+ var jwt = (options) => {
4
+ if (!options) {
5
+ throw new Error('JWT auth middleware requires options for "secret');
6
+ }
7
+ if (!crypto.subtle || !crypto.subtle.importKey) {
8
+ throw new Error("`crypto.subtle.importKey` is undefined. JWT auth middleware requires it.");
9
+ }
10
+ return async (ctx, next) => {
11
+ const credentials = ctx.req.headers.get("Authorization");
12
+ let token;
13
+ if (credentials) {
14
+ const parts = credentials.split(/\s+/);
15
+ if (parts.length !== 2) {
16
+ return new Response("Unauthorized", {
17
+ status: 401,
18
+ headers: {
19
+ "WWW-Authenticate": `Bearer realm="${ctx.req.url}",error="invalid_request",error_description="invalid credentials structure"`
20
+ }
21
+ });
22
+ } else {
23
+ token = parts[1];
24
+ }
25
+ } else if (options.cookie) {
26
+ token = ctx.req.cookie(options.cookie);
5
27
  }
6
- if (!crypto.subtle || !crypto.subtle.importKey) {
7
- throw new Error('`crypto.subtle.importKey` is undefined. JWT auth middleware requires it.');
8
- }
9
- return async (ctx, next) => {
10
- const credentials = ctx.req.headers.get('Authorization');
11
- let token;
12
- if (credentials) {
13
- const parts = credentials.split(/\s+/);
14
- if (parts.length !== 2) {
15
- return new Response('Unauthorized', {
16
- status: 401,
17
- headers: {
18
- 'WWW-Authenticate': `Bearer realm="${ctx.req.url}",error="invalid_request",error_description="invalid credentials structure"`,
19
- },
20
- });
21
- }
22
- else {
23
- token = parts[1];
24
- }
25
- }
26
- else if (options.cookie) {
27
- token = ctx.req.cookie(options.cookie);
28
- }
29
- if (!token) {
30
- return new Response('Unauthorized', {
31
- status: 401,
32
- headers: {
33
- 'WWW-Authenticate': `Bearer realm="${ctx.req.url}",error="invalid_request",error_description="no authorization included in request"`,
34
- },
35
- });
36
- }
37
- let authorized = false;
38
- let msg = '';
39
- try {
40
- authorized = await Jwt.verify(token, options.secret, options.alg);
41
- }
42
- catch (e) {
43
- msg = `${e}`;
28
+ if (!token) {
29
+ return new Response("Unauthorized", {
30
+ status: 401,
31
+ headers: {
32
+ "WWW-Authenticate": `Bearer realm="${ctx.req.url}",error="invalid_request",error_description="no authorization included in request"`
44
33
  }
45
- if (!authorized) {
46
- return new Response('Unauthorized', {
47
- status: 401,
48
- statusText: msg,
49
- headers: {
50
- 'WWW-Authenticate': `Bearer realm="${ctx.req.url}",error="invalid_token",error_description="token verification failure"`,
51
- },
52
- });
34
+ });
35
+ }
36
+ let authorized = false;
37
+ let msg = "";
38
+ try {
39
+ authorized = await Jwt.verify(token, options.secret, options.alg);
40
+ } catch (e) {
41
+ msg = `${e}`;
42
+ }
43
+ if (!authorized) {
44
+ return new Response("Unauthorized", {
45
+ status: 401,
46
+ statusText: msg,
47
+ headers: {
48
+ "WWW-Authenticate": `Bearer realm="${ctx.req.url}",error="invalid_token",error_description="token verification failure"`
53
49
  }
54
- await next();
55
- };
50
+ });
51
+ }
52
+ await next();
53
+ };
54
+ };
55
+ export {
56
+ jwt
56
57
  };
@@ -1,4 +1,4 @@
1
- import type { MiddlewareHandler } from '../../hono';
1
+ import type { MiddlewareHandler } from '../../types';
2
2
  declare type PrintFunc = (str: string, ...rest: string[]) => void;
3
3
  export declare const logger: (fn?: PrintFunc) => MiddlewareHandler;
4
4
  export {};
@@ -1,45 +1,41 @@
1
- import { getPathFromURL } from '../../utils/url';
2
- var LogPrefix;
3
- (function (LogPrefix) {
4
- LogPrefix["Outgoing"] = "-->";
5
- LogPrefix["Incoming"] = "<--";
6
- LogPrefix["Error"] = "xxx";
7
- })(LogPrefix || (LogPrefix = {}));
8
- const humanize = (times) => {
9
- const [delimiter, separator] = [',', '.'];
10
- const orderTimes = times.map((v) => v.replace(/(\d)(?=(\d\d\d)+(?!\d))/g, '$1' + delimiter));
11
- return orderTimes.join(separator);
1
+ // src/middleware/logger/index.ts
2
+ import { getPathFromURL } from "../../utils/url.js";
3
+ var humanize = (times) => {
4
+ const [delimiter, separator] = [",", "."];
5
+ const orderTimes = times.map((v) => v.replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1" + delimiter));
6
+ return orderTimes.join(separator);
12
7
  };
13
- const time = (start) => {
14
- const delta = Date.now() - start;
15
- return humanize([delta < 1000 ? delta + 'ms' : Math.round(delta / 1000) + 's']);
8
+ var time = (start) => {
9
+ const delta = Date.now() - start;
10
+ return humanize([delta < 1e3 ? delta + "ms" : Math.round(delta / 1e3) + "s"]);
16
11
  };
17
- const colorStatus = (status) => {
18
- const out = {
19
- 7: `\x1b[35m${status}\x1b[0m`,
20
- 5: `\x1b[31m${status}\x1b[0m`,
21
- 4: `\x1b[33m${status}\x1b[0m`,
22
- 3: `\x1b[36m${status}\x1b[0m`,
23
- 2: `\x1b[32m${status}\x1b[0m`,
24
- 1: `\x1b[32m${status}\x1b[0m`,
25
- 0: `\x1b[33m${status}\x1b[0m`,
26
- };
27
- const calculateStatus = (status / 100) | 0;
28
- return out[calculateStatus];
12
+ var colorStatus = (status) => {
13
+ const out = {
14
+ 7: `\x1B[35m${status}\x1B[0m`,
15
+ 5: `\x1B[31m${status}\x1B[0m`,
16
+ 4: `\x1B[33m${status}\x1B[0m`,
17
+ 3: `\x1B[36m${status}\x1B[0m`,
18
+ 2: `\x1B[32m${status}\x1B[0m`,
19
+ 1: `\x1B[32m${status}\x1B[0m`,
20
+ 0: `\x1B[33m${status}\x1B[0m`
21
+ };
22
+ const calculateStatus = status / 100 | 0;
23
+ return out[calculateStatus];
29
24
  };
30
25
  function log(fn, prefix, method, path, status = 0, elapsed) {
31
- const out = prefix === LogPrefix.Incoming
32
- ? ` ${prefix} ${method} ${path}`
33
- : ` ${prefix} ${method} ${path} ${colorStatus(status)} ${elapsed}`;
34
- fn(out);
26
+ const out = prefix === "<--" /* Incoming */ ? ` ${prefix} ${method} ${path}` : ` ${prefix} ${method} ${path} ${colorStatus(status)} ${elapsed}`;
27
+ fn(out);
35
28
  }
36
- export const logger = (fn = console.log) => {
37
- return async (c, next) => {
38
- const { method } = c.req;
39
- const path = getPathFromURL(c.req.url);
40
- log(fn, LogPrefix.Incoming, method, path);
41
- const start = Date.now();
42
- await next();
43
- log(fn, LogPrefix.Outgoing, method, path, c.res.status, time(start));
44
- };
29
+ var logger = (fn = console.log) => {
30
+ return async (c, next) => {
31
+ const { method } = c.req;
32
+ const path = getPathFromURL(c.req.url);
33
+ log(fn, "<--" /* Incoming */, method, path);
34
+ const start = Date.now();
35
+ await next();
36
+ log(fn, "-->" /* Outgoing */, method, path, c.res.status, time(start));
37
+ };
38
+ };
39
+ export {
40
+ logger
45
41
  };
@@ -1,2 +1,2 @@
1
- import type { MiddlewareHandler } from '../../hono';
1
+ import type { MiddlewareHandler } from '../../types';
2
2
  export declare const poweredBy: () => MiddlewareHandler;
@@ -1,6 +1,10 @@
1
- export const poweredBy = () => {
2
- return async (c, next) => {
3
- await next();
4
- c.res.headers.append('X-Powered-By', 'Hono');
5
- };
1
+ // src/middleware/powered-by/index.ts
2
+ var poweredBy = () => {
3
+ return async (c, next) => {
4
+ await next();
5
+ c.res.headers.append("X-Powered-By", "Hono");
6
+ };
7
+ };
8
+ export {
9
+ poweredBy
6
10
  };
@@ -1,4 +1,4 @@
1
- import type { MiddlewareHandler } from '../../hono';
1
+ import type { MiddlewareHandler } from '../../types';
2
2
  declare type prettyOptions = {
3
3
  space: number;
4
4
  };
@@ -1,7 +1,11 @@
1
- export const prettyJSON = (options = { space: 2 }) => {
2
- return async (c, next) => {
3
- const pretty = c.req.query('pretty') || c.req.query('pretty') === '' ? true : false;
4
- c.pretty(pretty, options.space);
5
- await next();
6
- };
1
+ // src/middleware/pretty-json/index.ts
2
+ var prettyJSON = (options = { space: 2 }) => {
3
+ return async (c, next) => {
4
+ const pretty = c.req.query("pretty") || c.req.query("pretty") === "" ? true : false;
5
+ c.pretty(pretty, options.space);
6
+ await next();
7
+ };
8
+ };
9
+ export {
10
+ prettyJSON
7
11
  };
@@ -1,5 +1,5 @@
1
1
  import type { Context } from '../../context';
2
- import type { Next } from '../../hono';
2
+ import type { Next } from '../../types';
3
3
  export declare type ServeStaticOptions = {
4
4
  root?: string;
5
5
  path?: string;
@@ -1,38 +1,37 @@
1
- // @denoify-ignore
2
- /* eslint-disable @typescript-eslint/ban-ts-comment */
3
- import { existsSync } from 'fs';
4
- import { getFilePath } from '../../utils/filepath';
5
- import { getMimeType } from '../../utils/mime';
6
- // @ts-ignore
7
- const { file } = Bun;
8
- const DEFAULT_DOCUMENT = 'index.html';
9
- export const serveStatic = (options = { root: '' }) => {
10
- return async (c, next) => {
11
- // Do nothing if Response is already set
12
- if (c.finalized) {
13
- await next();
14
- return;
1
+ // src/middleware/serve-static/bun.ts
2
+ import { existsSync } from "fs";
3
+ import { getFilePath } from "../../utils/filepath.js";
4
+ import { getMimeType } from "../../utils/mime.js";
5
+ var { file } = Bun;
6
+ var DEFAULT_DOCUMENT = "index.html";
7
+ var serveStatic = (options = { root: "" }) => {
8
+ return async (c, next) => {
9
+ if (c.finalized) {
10
+ await next();
11
+ return;
12
+ }
13
+ const url = new URL(c.req.url);
14
+ let path = getFilePath({
15
+ filename: options.path ?? url.pathname,
16
+ root: options.root,
17
+ defaultDocument: DEFAULT_DOCUMENT
18
+ });
19
+ path = `./${path}`;
20
+ if (existsSync(path)) {
21
+ const content = file(path);
22
+ if (content) {
23
+ const mimeType = getMimeType(path);
24
+ if (mimeType) {
25
+ c.header("Content-Type", mimeType);
15
26
  }
16
- const url = new URL(c.req.url);
17
- let path = getFilePath({
18
- filename: options.path ?? url.pathname,
19
- root: options.root,
20
- defaultDocument: DEFAULT_DOCUMENT,
21
- });
22
- path = `./${path}`;
23
- if (existsSync(path)) {
24
- const content = file(path);
25
- if (content) {
26
- const mimeType = getMimeType(path);
27
- if (mimeType) {
28
- c.header('Content-Type', mimeType);
29
- }
30
- // Return Response object
31
- return c.body(content);
32
- }
33
- }
34
- console.warn(`Static file: ${path} is not found`);
35
- await next();
36
- return;
37
- };
27
+ return c.body(content);
28
+ }
29
+ }
30
+ console.warn(`Static file: ${path} is not found`);
31
+ await next();
32
+ return;
33
+ };
34
+ };
35
+ export {
36
+ serveStatic
38
37
  };
@@ -1 +1,5 @@
1
- export { serveStatic } from './serve-static';
1
+ // src/middleware/serve-static/index.ts
2
+ import { serveStatic } from "./serve-static.js";
3
+ export {
4
+ serveStatic
5
+ };
@@ -0,0 +1,3 @@
1
+ import type { ServeStaticOptions } from './serve-static';
2
+ declare const module: (options?: ServeStaticOptions) => import("../..").MiddlewareHandler<string, import("../../types").Environment, import("../../validator/schema").Schema>;
3
+ export { module as serveStatic };
@@ -0,0 +1,13 @@
1
+ // src/middleware/serve-static/module.ts
2
+ import manifest from "__STATIC_CONTENT_MANIFEST";
3
+ import { serveStatic } from "./serve-static.js";
4
+ var module = (options = { root: "" }) => {
5
+ return serveStatic({
6
+ root: options.root,
7
+ path: options.path,
8
+ manifest: options.manifest ? options.manifest : manifest
9
+ });
10
+ };
11
+ export {
12
+ module as serveStatic
13
+ };
@@ -1,10 +1,9 @@
1
1
  /// <reference types="@cloudflare/workers-types" />
2
- import type { Context } from '../../context';
3
- import type { Next } from '../../hono';
2
+ import type { MiddlewareHandler } from '../../types';
4
3
  export declare type ServeStaticOptions = {
5
4
  root?: string;
6
5
  path?: string;
7
6
  manifest?: object | string;
8
7
  namespace?: KVNamespace;
9
8
  };
10
- export declare const serveStatic: (options?: ServeStaticOptions) => (c: Context, next: Next) => Promise<Response | undefined>;
9
+ export declare const serveStatic: (options?: ServeStaticOptions) => MiddlewareHandler;
@@ -1,37 +1,37 @@
1
- import { getContentFromKVAsset } from '../../utils/cloudflare';
2
- import { getFilePath } from '../../utils/filepath';
3
- import { getMimeType } from '../../utils/mime';
4
- const DEFAULT_DOCUMENT = 'index.html';
5
- // This middleware is available only on Cloudflare Workers.
6
- export const serveStatic = (options = { root: '' }) => {
7
- return async (c, next) => {
8
- // Do nothing if Response is already set
9
- if (c.finalized) {
10
- await next();
11
- return;
12
- }
13
- const url = new URL(c.req.url);
14
- const path = getFilePath({
15
- filename: options.path ?? url.pathname,
16
- root: options.root,
17
- defaultDocument: DEFAULT_DOCUMENT,
18
- });
19
- const content = await getContentFromKVAsset(path, {
20
- manifest: options.manifest,
21
- namespace: options.namespace ? options.namespace : c.env ? c.env.__STATIC_CONTENT : undefined,
22
- });
23
- if (content) {
24
- const mimeType = getMimeType(path);
25
- if (mimeType) {
26
- c.header('Content-Type', mimeType);
27
- }
28
- // Return Response object
29
- return c.body(content);
30
- }
31
- else {
32
- console.warn(`Static file: ${path} is not found`);
33
- await next();
34
- }
35
- return;
36
- };
1
+ // src/middleware/serve-static/serve-static.ts
2
+ import { getContentFromKVAsset } from "../../utils/cloudflare.js";
3
+ import { getFilePath } from "../../utils/filepath.js";
4
+ import { getMimeType } from "../../utils/mime.js";
5
+ var DEFAULT_DOCUMENT = "index.html";
6
+ var serveStatic = (options = { root: "" }) => {
7
+ return async (c, next) => {
8
+ if (c.finalized) {
9
+ await next();
10
+ return;
11
+ }
12
+ const url = new URL(c.req.url);
13
+ const path = getFilePath({
14
+ filename: options.path ?? url.pathname,
15
+ root: options.root,
16
+ defaultDocument: DEFAULT_DOCUMENT
17
+ });
18
+ const content = await getContentFromKVAsset(path, {
19
+ manifest: options.manifest,
20
+ namespace: options.namespace ? options.namespace : c.env ? c.env.__STATIC_CONTENT : void 0
21
+ });
22
+ if (content) {
23
+ const mimeType = getMimeType(path);
24
+ if (mimeType) {
25
+ c.header("Content-Type", mimeType);
26
+ }
27
+ return c.body(content);
28
+ } else {
29
+ console.warn(`Static file: ${path} is not found`);
30
+ await next();
31
+ }
32
+ return;
33
+ };
34
+ };
35
+ export {
36
+ serveStatic
37
37
  };
@@ -1,2 +1,5 @@
1
- import { validatorMiddleware } from './middleware';
2
- export { validatorMiddleware as validator };
1
+ // src/middleware/validator/index.ts
2
+ import { validatorMiddleware } from "./middleware.js";
3
+ export {
4
+ validatorMiddleware as validator
5
+ };
@@ -1,22 +1,16 @@
1
1
  import type { Context } from '../../context';
2
- import type { Environment, Next, ValidatedData } from '../../hono';
3
- import { Validator, VObjectBase } from './validator';
4
- import type { VString, VNumber, VBoolean, VObject, VNumberArray, VStringArray, VBooleanArray, ValidateResult, VArray } from './validator';
5
- export declare type Schema = {
6
- [key: string]: VString | VNumber | VBoolean | VStringArray | VNumberArray | VBooleanArray | Schema | VObject<Schema> | VArray<Schema>;
7
- };
8
- declare type SchemaToProp<T> = {
9
- [K in keyof T]: T[K] extends VNumberArray ? number[] : T[K] extends VBooleanArray ? boolean[] : T[K] extends VStringArray ? string[] : T[K] extends VNumber ? number : T[K] extends VBoolean ? boolean : T[K] extends VString ? string : T[K] extends VObjectBase<Schema> ? T[K]['container'] extends VNumber ? number : T[K]['container'] extends VString ? string : T[K]['container'] extends VBoolean ? boolean : T[K] extends VArray<Schema> ? SchemaToProp<ReadonlyArray<T[K]['container']>> : T[K] extends VObject<Schema> ? SchemaToProp<T[K]['container']> : T[K] extends Schema ? SchemaToProp<T[K]> : never : SchemaToProp<T[K]>;
10
- };
2
+ import type { Environment, MiddlewareHandler } from '../../types';
3
+ import type { Schema } from '../../validator/schema';
4
+ import type { ValidateResult } from '../../validator/validator';
5
+ import { Validator } from '../../validator/validator';
11
6
  declare type ResultSet = {
12
7
  hasError: boolean;
13
8
  messages: string[];
14
9
  results: ValidateResult[];
15
10
  };
16
- declare type Done<Env extends Partial<Environment>> = (resultSet: ResultSet, context: Context<string, Env>) => Response | void;
17
- declare type ValidationFunction<T, Env extends Partial<Environment>> = (v: Validator, c: Context<string, Env>) => T;
18
- declare type MiddlewareHandler<Data extends ValidatedData = ValidatedData, Env extends Partial<Environment> = Environment> = (c: Context<string, Env, Data>, next: Next) => Promise<void> | Promise<Response | undefined>;
19
- export declare const validatorMiddleware: <T extends Schema, Env extends Partial<Environment>>(validationFunction: ValidationFunction<T, Env>, options?: {
20
- done?: Done<Env> | undefined;
21
- } | undefined) => MiddlewareHandler<SchemaToProp<T>, Env>;
11
+ declare type Done<P extends string, E extends Partial<Environment> = Environment> = (resultSet: ResultSet, c: Context<P, E>) => Response | void;
12
+ declare type ValidationFunction<P extends string, E extends Partial<Environment> = Environment, S extends Schema = Schema> = (v: Validator, c: Context<P, E>) => S;
13
+ export declare const validatorMiddleware: <P extends string, E extends Partial<Environment> = Environment, S extends Schema = Schema>(validationFunction: ValidationFunction<P, E, S>, options?: {
14
+ done?: Done<P, E> | undefined;
15
+ } | undefined) => MiddlewareHandler<string, E, S>;
22
16
  export {};