hono 1.1.1 → 1.2.2

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 (91) hide show
  1. package/README.md +126 -117
  2. package/dist/compose.d.ts +2 -2
  3. package/dist/compose.js +20 -8
  4. package/dist/compose.test.d.ts +1 -0
  5. package/dist/compose.test.js +511 -0
  6. package/dist/context.d.ts +8 -1
  7. package/dist/context.js +27 -37
  8. package/dist/context.test.d.ts +1 -0
  9. package/dist/context.test.js +127 -0
  10. package/dist/hono.d.ts +44 -21
  11. package/dist/hono.js +74 -43
  12. package/dist/hono.test.d.ts +1 -0
  13. package/dist/hono.test.js +592 -0
  14. package/dist/index.d.ts +2 -2
  15. package/dist/index.js +2 -1
  16. package/dist/middleware/basic-auth/index.js +2 -2
  17. package/dist/middleware/basic-auth/index.test.d.ts +1 -0
  18. package/dist/middleware/basic-auth/index.test.js +119 -0
  19. package/dist/middleware/body-parse/index.test.d.ts +1 -0
  20. package/dist/middleware/body-parse/index.test.js +59 -0
  21. package/dist/middleware/cookie/index.d.ts +1 -1
  22. package/dist/middleware/cookie/index.test.d.ts +1 -0
  23. package/dist/middleware/cookie/index.test.js +54 -0
  24. package/dist/middleware/cors/index.test.d.ts +1 -0
  25. package/dist/middleware/cors/index.test.js +59 -0
  26. package/dist/middleware/etag/index.test.d.ts +1 -0
  27. package/dist/middleware/etag/index.test.js +45 -0
  28. package/dist/middleware/graphql-server/index.js +1 -1
  29. package/dist/middleware/graphql-server/index.test.d.ts +1 -0
  30. package/dist/middleware/graphql-server/index.test.js +480 -0
  31. package/dist/middleware/graphql-server/parse-body.test.d.ts +1 -0
  32. package/dist/middleware/graphql-server/parse-body.test.js +57 -0
  33. package/dist/middleware/jwt/index.js +9 -11
  34. package/dist/middleware/jwt/index.test.d.ts +1 -0
  35. package/dist/middleware/jwt/index.test.js +51 -0
  36. package/dist/middleware/logger/index.test.d.ts +1 -0
  37. package/dist/middleware/logger/index.test.js +49 -0
  38. package/dist/middleware/mustache/index.test.d.ts +1 -0
  39. package/dist/middleware/mustache/index.test.js +49 -0
  40. package/dist/middleware/powered-by/index.test.d.ts +1 -0
  41. package/dist/middleware/powered-by/index.test.js +15 -0
  42. package/dist/middleware/pretty-json/index.test.d.ts +1 -0
  43. package/dist/middleware/pretty-json/index.test.js +28 -0
  44. package/dist/middleware/serve-static/index.test.d.ts +1 -0
  45. package/dist/middleware/serve-static/index.test.js +58 -0
  46. package/dist/router/reg-exp-router/index.d.ts +1 -1
  47. package/dist/router/reg-exp-router/index.js +1 -1
  48. package/dist/router/reg-exp-router/node.d.ts +3 -0
  49. package/dist/router/reg-exp-router/node.js +13 -7
  50. package/dist/router/reg-exp-router/router.d.ts +21 -2
  51. package/dist/router/reg-exp-router/router.js +301 -81
  52. package/dist/router/reg-exp-router/router.test.d.ts +1 -0
  53. package/dist/router/reg-exp-router/router.test.js +212 -0
  54. package/dist/router/reg-exp-router/trie.d.ts +7 -3
  55. package/dist/router/reg-exp-router/trie.js +3 -3
  56. package/dist/router/trie-router/index.d.ts +1 -1
  57. package/dist/router/trie-router/index.js +1 -1
  58. package/dist/router/trie-router/node.d.ts +4 -3
  59. package/dist/router/trie-router/node.js +123 -55
  60. package/dist/router/trie-router/node.test.d.ts +1 -0
  61. package/dist/router/trie-router/node.test.js +351 -0
  62. package/dist/router/trie-router/router.d.ts +2 -2
  63. package/dist/router/trie-router/router.js +1 -1
  64. package/dist/router/trie-router/router.test.d.ts +1 -0
  65. package/dist/router/trie-router/router.test.js +98 -0
  66. package/dist/router.d.ts +4 -3
  67. package/dist/router.js +5 -4
  68. package/dist/utils/body.js +2 -2
  69. package/dist/utils/body.test.d.ts +1 -0
  70. package/dist/utils/body.test.js +45 -0
  71. package/dist/utils/buffer.js +1 -1
  72. package/dist/utils/buffer.test.d.ts +1 -0
  73. package/dist/utils/buffer.test.js +36 -0
  74. package/dist/utils/cloudflare.test.d.ts +1 -0
  75. package/dist/utils/cloudflare.test.js +42 -0
  76. package/dist/utils/crypto.test.d.ts +1 -0
  77. package/dist/utils/crypto.test.js +15 -0
  78. package/dist/utils/encode.test.d.ts +1 -0
  79. package/dist/utils/encode.test.js +54 -0
  80. package/dist/utils/http-status.test.d.ts +1 -0
  81. package/dist/utils/http-status.test.js +8 -0
  82. package/dist/utils/jwt/jwt.test.d.ts +1 -0
  83. package/dist/utils/jwt/jwt.test.js +171 -0
  84. package/dist/utils/jwt/types.test.d.ts +1 -0
  85. package/dist/utils/jwt/types.test.js +12 -0
  86. package/dist/utils/mime.test.d.ts +1 -0
  87. package/dist/utils/mime.test.js +13 -0
  88. package/dist/utils/url.js +4 -4
  89. package/dist/utils/url.test.d.ts +1 -0
  90. package/dist/utils/url.test.js +87 -0
  91. package/package.json +3 -4
@@ -0,0 +1,127 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const context_1 = require("./context");
4
+ describe('Context', () => {
5
+ const req = new Request('http://localhost/');
6
+ let c;
7
+ beforeEach(() => {
8
+ c = new context_1.Context(req);
9
+ });
10
+ it('c.text()', async () => {
11
+ const res = c.text('text in c', 201, { 'X-Custom': 'Message' });
12
+ expect(res.status).toBe(201);
13
+ expect(res.headers.get('Content-Type')).toBe('text/plain; charset=UTF-8');
14
+ expect(await res.text()).toBe('text in c');
15
+ expect(res.headers.get('X-Custom')).toBe('Message');
16
+ });
17
+ it('c.json()', async () => {
18
+ const res = c.json({ message: 'Hello' }, 201, { 'X-Custom': 'Message' });
19
+ expect(res.status).toBe(201);
20
+ expect(res.headers.get('Content-Type')).toMatch('application/json; charset=UTF-8');
21
+ const text = await res.text();
22
+ expect(text).toBe('{"message":"Hello"}');
23
+ expect(res.headers.get('X-Custom')).toBe('Message');
24
+ });
25
+ it('c.json() with c.pretty(true)', async () => {
26
+ c.pretty(true);
27
+ const res = c.json({ message: 'Hello' });
28
+ const text = await res.text();
29
+ expect(text).toBe(`{
30
+ "message": "Hello"
31
+ }`);
32
+ });
33
+ it('c.json() with c.pretty(true, 4)', async () => {
34
+ c.pretty(true, 4);
35
+ const res = c.json({ message: 'Hello' });
36
+ const text = await res.text();
37
+ expect(text).toBe(`{
38
+ "message": "Hello"
39
+ }`);
40
+ });
41
+ it('c.html()', async () => {
42
+ const res = c.html('<h1>Hello! Hono!</h1>', 201, { 'X-Custom': 'Message' });
43
+ expect(res.status).toBe(201);
44
+ expect(res.headers.get('Content-Type')).toMatch('text/html');
45
+ expect(await res.text()).toBe('<h1>Hello! Hono!</h1>');
46
+ expect(res.headers.get('X-Custom')).toBe('Message');
47
+ });
48
+ it('c.redirect()', async () => {
49
+ let res = c.redirect('/destination');
50
+ expect(res.status).toBe(302);
51
+ expect(res.headers.get('Location')).toMatch(/^https?:\/\/.+\/destination$/);
52
+ res = c.redirect('https://example.com/destination');
53
+ expect(res.status).toBe(302);
54
+ expect(res.headers.get('Location')).toBe('https://example.com/destination');
55
+ });
56
+ it('c.header()', async () => {
57
+ c.header('X-Foo', 'Bar');
58
+ const res = c.body('Hi');
59
+ const foo = res.headers.get('X-Foo');
60
+ expect(foo).toBe('Bar');
61
+ });
62
+ it('c.status() and c.statusText()', async () => {
63
+ c.status(201);
64
+ const res = c.body('Hi');
65
+ expect(res.status).toBe(201);
66
+ expect(res.statusText).toBe('Created');
67
+ });
68
+ it('Complex pattern', async () => {
69
+ c.status(404);
70
+ const res = c.json({ hono: 'great app' });
71
+ expect(res.status).toBe(404);
72
+ expect(res.statusText).toBe('Not Found');
73
+ expect(res.headers.get('Content-Type')).toMatch('application/json; charset=UTF-8');
74
+ const obj = await res.json();
75
+ expect(obj['hono']).toBe('great app');
76
+ });
77
+ it('Has headers, status, and statusText', async () => {
78
+ c.header('X-Custom1', 'Message1');
79
+ c.header('X-Custom2', 'Message2');
80
+ c.status(200);
81
+ const res = c.newResponse('this is body', {
82
+ status: 201,
83
+ headers: {
84
+ 'X-Custom3': 'Message3',
85
+ 'X-Custom2': 'Message2-Override',
86
+ },
87
+ });
88
+ expect(res.headers.get('X-Custom1')).toBe('Message1');
89
+ expect(res.headers.get('X-Custom2')).toBe('Message2-Override');
90
+ expect(res.headers.get('X-Custom3')).toBe('Message3');
91
+ expect(res.status).toBe(201);
92
+ expect(await res.text()).toBe('this is body');
93
+ // res is already set.
94
+ c.res = res;
95
+ c.header('X-Custom4', 'Message4');
96
+ c.status(202);
97
+ expect(c.res.headers.get('X-Custom4')).toBe('Message4');
98
+ expect(c.res.status).toBe(201);
99
+ expect(c.res.statusText).toBe('OK');
100
+ });
101
+ it('Should return 200 response', async () => {
102
+ const res = c.text('Text');
103
+ expect(res.status).toBe(200);
104
+ expect(res.statusText).toBe('OK');
105
+ });
106
+ it('Should be able read env', async () => {
107
+ const req = new Request('http://localhost/');
108
+ const key = 'a-secret-key';
109
+ const ctx = new context_1.Context(req, {
110
+ env: {
111
+ API_KEY: key,
112
+ },
113
+ res: null,
114
+ event: null,
115
+ });
116
+ expect(ctx.env.API_KEY).toBe(key);
117
+ });
118
+ it('set and set', async () => {
119
+ const ctx = new context_1.Context(req);
120
+ expect(ctx.get('k-foo')).toEqual(undefined);
121
+ ctx.set('k-foo', 'v-foo');
122
+ expect(ctx.get('k-foo')).toEqual('v-foo');
123
+ expect(ctx.get('k-bar')).toEqual(undefined);
124
+ ctx.set('k-bar', { k: 'v' });
125
+ expect(ctx.get('k-bar')).toEqual({ k: 'v' });
126
+ });
127
+ });
package/dist/hono.d.ts CHANGED
@@ -2,6 +2,7 @@
2
2
  import { Context } from './context';
3
3
  import type { Env } from './context';
4
4
  import type { Router } from './router';
5
+ import { METHOD_NAME_ALL_LOWERCASE } from './router';
5
6
  declare global {
6
7
  interface Request<ParamKeyType = string> {
7
8
  param: (key: ParamKeyType) => string;
@@ -9,31 +10,53 @@ declare global {
9
10
  header: (name: string) => string;
10
11
  }
11
12
  }
12
- export declare type Handler<RequestParamKeyType = string, E = Env> = (c: Context<RequestParamKeyType, E>) => Response | Promise<Response>;
13
- export declare type MiddlewareHandler<E = Env> = (c: Context<string, E>, next: Next) => Promise<void>;
14
- export declare type NotFoundHandler<E = Env> = (c: Context<string, E>) => Response | Promise<Response>;
13
+ export declare type Handler<RequestParamKeyType = string, E = Env> = (c: Context<RequestParamKeyType, E>, next: Next) => Response | Promise<Response> | void | Promise<void>;
14
+ export declare type NotFoundHandler<E = Env> = (c: Context<string, E>) => Response;
15
15
  export declare type ErrorHandler<E = Env> = (err: Error, c: Context<string, E>) => Response;
16
16
  export declare type Next = () => Promise<void>;
17
17
  declare type ParamKeyName<NameWithPattern> = NameWithPattern extends `${infer Name}{${infer _Pattern}` ? Name : NameWithPattern;
18
18
  declare type ParamKey<Component> = Component extends `:${infer NameWithPattern}` ? ParamKeyName<NameWithPattern> : never;
19
19
  declare type ParamKeys<Path> = Path extends `${infer Component}/${infer Rest}` ? ParamKey<Component> | ParamKeys<Rest> : ParamKey<Path>;
20
- interface HandlerInterface<T extends string, E = Env> {
21
- <Path extends string>(path: Path, handler: Handler<ParamKeys<Path>, E>): Hono<E, Path>;
22
- (path: string, handler: Handler<string, E>): Hono<E, T>;
23
- <Path extends T>(handler: Handler<ParamKeys<T>, E>): Hono<E, Path>;
24
- (handler: Handler<string, E>): Hono<E, T>;
20
+ interface HandlerInterface<T extends string, E = Env, U = Hono<E, T>> {
21
+ <Path extends string>(path: Path, ...handlers: Handler<ParamKeys<Path> extends never ? string : ParamKeys<Path>, E>[]): U;
22
+ (path: string, ...handlers: Handler<string, E>[]): U;
23
+ <Path extends string>(...handlers: Handler<ParamKeys<Path> extends never ? string : ParamKeys<Path>, E>[]): U;
24
+ (...handlers: Handler<string, E>[]): U;
25
25
  }
26
- declare const Hono_base: new <E_1 extends Env, T extends string>() => {
27
- delete: HandlerInterface<T, E_1>;
28
- get: HandlerInterface<T, E_1>;
29
- post: HandlerInterface<T, E_1>;
30
- put: HandlerInterface<T, E_1>;
31
- head: HandlerInterface<T, E_1>;
32
- options: HandlerInterface<T, E_1>;
33
- patch: HandlerInterface<T, E_1>;
34
- all: HandlerInterface<T, E_1>;
26
+ declare const methods: readonly ["get", "post", "put", "delete", "head", "options", "patch"];
27
+ declare type Methods = typeof methods[number] | typeof METHOD_NAME_ALL_LOWERCASE;
28
+ interface Routing<E extends Env> {
29
+ path: string;
30
+ method: Methods;
31
+ handler: Handler<string, E>;
32
+ }
33
+ declare const Route_base: new <E_1 extends Env, T extends string, U>() => {
34
+ delete: HandlerInterface<T, E_1, U>;
35
+ get: HandlerInterface<T, E_1, U>;
36
+ all: HandlerInterface<T, E_1, U>;
37
+ post: HandlerInterface<T, E_1, U>;
38
+ put: HandlerInterface<T, E_1, U>;
39
+ head: HandlerInterface<T, E_1, U>;
40
+ options: HandlerInterface<T, E_1, U>;
41
+ patch: HandlerInterface<T, E_1, U>;
42
+ };
43
+ export declare class Route<E = Env, P extends string = ''> extends Route_base<E, P, Route<E, P>> {
44
+ #private;
45
+ routes: Routing<E>[];
46
+ constructor();
47
+ private add;
48
+ }
49
+ declare const Hono_base: new <E_1 extends Env, T extends string, U>() => {
50
+ delete: HandlerInterface<T, E_1, U>;
51
+ get: HandlerInterface<T, E_1, U>;
52
+ all: HandlerInterface<T, E_1, U>;
53
+ post: HandlerInterface<T, E_1, U>;
54
+ put: HandlerInterface<T, E_1, U>;
55
+ head: HandlerInterface<T, E_1, U>;
56
+ options: HandlerInterface<T, E_1, U>;
57
+ patch: HandlerInterface<T, E_1, U>;
35
58
  };
36
- export declare class Hono<E = Env, P extends string = ''> extends Hono_base<E, P> {
59
+ export declare class Hono<E = Env, P extends string = ''> extends Hono_base<E, P, Hono<E, P>> {
37
60
  #private;
38
61
  readonly routerClass: {
39
62
  new (): Router<any>;
@@ -43,9 +66,9 @@ export declare class Hono<E = Env, P extends string = ''> extends Hono_base<E, P
43
66
  constructor(init?: Partial<Pick<Hono, 'routerClass' | 'strict'>>);
44
67
  private notFoundHandler;
45
68
  private errorHandler;
46
- route(path: string): Hono<E, P>;
47
- use(path: string, middleware: MiddlewareHandler<E>): Hono<E, P>;
48
- use(middleware: MiddlewareHandler<E>): Hono<E, P>;
69
+ route(path: string, route?: Route): Hono<E, P>;
70
+ use(path: string, ...middleware: Handler<string, E>[]): Hono<E, P>;
71
+ use(...middleware: Handler<string, E>[]): Hono<E, P>;
49
72
  onError(handler: ErrorHandler<E>): Hono<E, P>;
50
73
  notFound(handler: NotFoundHandler<E>): Hono<E, P>;
51
74
  private addRoute;
package/dist/hono.js CHANGED
@@ -10,26 +10,57 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
10
10
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
11
11
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
12
  };
13
- var _Hono_router, _Hono_middlewareRouters, _Hono_tempPath;
13
+ var _Route_path, _Hono_router, _Hono_tempPath;
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.Hono = void 0;
15
+ exports.Hono = exports.Route = void 0;
16
16
  const compose_1 = require("./compose");
17
17
  const context_1 = require("./context");
18
18
  const router_1 = require("./router");
19
+ const router_2 = require("./router");
19
20
  const trie_router_1 = require("./router/trie-router"); // Default Router
20
21
  const url_1 = require("./utils/url");
21
- const methods = ['get', 'post', 'put', 'delete', 'head', 'options', 'patch', 'all'];
22
+ const methods = ['get', 'post', 'put', 'delete', 'head', 'options', 'patch'];
22
23
  function defineDynamicClass() {
23
24
  return class {
24
25
  };
25
26
  }
27
+ class Route extends defineDynamicClass() {
28
+ constructor() {
29
+ super();
30
+ this.routes = [];
31
+ _Route_path.set(this, '');
32
+ const allMethods = [...methods, router_2.METHOD_NAME_ALL_LOWERCASE];
33
+ allMethods.map((method) => {
34
+ this[method] = (args1, ...args) => {
35
+ if (typeof args1 === 'string') {
36
+ __classPrivateFieldSet(this, _Route_path, args1, "f");
37
+ }
38
+ else {
39
+ this.add(method, __classPrivateFieldGet(this, _Route_path, "f"), args1);
40
+ }
41
+ args.map((handler) => {
42
+ if (typeof handler !== 'string') {
43
+ this.add(method, __classPrivateFieldGet(this, _Route_path, "f"), handler);
44
+ }
45
+ });
46
+ return this;
47
+ };
48
+ });
49
+ }
50
+ add(method, path, handler) {
51
+ const r = { path: path, method: method, handler: handler };
52
+ this.routes.push(r);
53
+ return this;
54
+ }
55
+ }
56
+ exports.Route = Route;
57
+ _Route_path = new WeakMap();
26
58
  class Hono extends defineDynamicClass() {
27
59
  constructor(init = {}) {
28
60
  super();
29
61
  this.routerClass = trie_router_1.TrieRouter;
30
62
  this.strict = true; // strict routing - default is true
31
63
  _Hono_router.set(this, void 0);
32
- _Hono_middlewareRouters.set(this, void 0);
33
64
  _Hono_tempPath.set(this, void 0);
34
65
  this.notFoundHandler = (c) => {
35
66
  const message = '404 Not Found';
@@ -40,41 +71,48 @@ class Hono extends defineDynamicClass() {
40
71
  const message = 'Internal Server Error';
41
72
  return c.text(message, 500);
42
73
  };
43
- methods.map((method) => {
44
- this[method] = (arg1, arg2) => {
45
- if (typeof arg1 === 'string') {
46
- this.path = arg1;
47
- return this.addRoute(method, this.path, arg2);
74
+ const allMethods = [...methods, router_2.METHOD_NAME_ALL_LOWERCASE];
75
+ allMethods.map((method) => {
76
+ this[method] = (args1, ...args) => {
77
+ if (typeof args1 === 'string') {
78
+ this.path = args1;
48
79
  }
49
- return this.addRoute(method, this.path, arg1);
80
+ else {
81
+ this.addRoute(method, this.path, args1);
82
+ }
83
+ args.map((handler) => {
84
+ if (typeof handler !== 'string') {
85
+ this.addRoute(method, this.path, handler);
86
+ }
87
+ });
88
+ return this;
50
89
  };
51
90
  });
52
91
  Object.assign(this, init);
53
92
  __classPrivateFieldSet(this, _Hono_router, new this.routerClass(), "f");
54
- __classPrivateFieldSet(this, _Hono_middlewareRouters, [], "f");
55
93
  __classPrivateFieldSet(this, _Hono_tempPath, null, "f");
56
94
  }
57
- route(path) {
95
+ route(path, route) {
58
96
  const newHono = new Hono();
59
97
  __classPrivateFieldSet(newHono, _Hono_tempPath, path, "f");
60
98
  __classPrivateFieldSet(newHono, _Hono_router, __classPrivateFieldGet(this, _Hono_router, "f"), "f");
99
+ if (route) {
100
+ route.routes.map((r) => {
101
+ newHono.addRoute(r.method, r.path, r.handler);
102
+ });
103
+ }
61
104
  return newHono;
62
105
  }
63
- use(arg1, arg2) {
64
- let handler;
106
+ use(arg1, ...handlers) {
65
107
  if (typeof arg1 === 'string') {
66
108
  this.path = arg1;
67
- handler = arg2;
68
109
  }
69
110
  else {
70
- handler = arg1;
71
- }
72
- if (handler.constructor.name !== 'AsyncFunction') {
73
- throw new TypeError('middleware must be a async function!');
111
+ handlers.unshift(arg1);
74
112
  }
75
- const router = new this.routerClass();
76
- router.add(router_1.METHOD_NAME_OF_ALL, this.path, handler);
77
- __classPrivateFieldGet(this, _Hono_middlewareRouters, "f").push(router);
113
+ handlers.map((handler) => {
114
+ this.addRoute(router_1.METHOD_NAME_ALL, this.path, handler);
115
+ });
78
116
  return this;
79
117
  }
80
118
  onError(handler) {
@@ -91,7 +129,6 @@ class Hono extends defineDynamicClass() {
91
129
  path = (0, url_1.mergePath)(__classPrivateFieldGet(this, _Hono_tempPath, "f"), path);
92
130
  }
93
131
  __classPrivateFieldGet(this, _Hono_router, "f").add(method, path, handler);
94
- return this;
95
132
  }
96
133
  async matchRoute(method, path) {
97
134
  return __classPrivateFieldGet(this, _Hono_router, "f").match(method, path);
@@ -100,31 +137,25 @@ class Hono extends defineDynamicClass() {
100
137
  const path = (0, url_1.getPathFromURL)(request.url, { strict: this.strict });
101
138
  const method = request.method;
102
139
  const result = await this.matchRoute(method, path);
103
- // Methods for Request object
104
140
  request.param = (key) => {
105
141
  if (result)
106
142
  return result.params[key];
107
143
  };
108
- const handler = result ? result.handler : this.notFoundHandler;
109
- const middleware = [];
110
- for (const mr of __classPrivateFieldGet(this, _Hono_middlewareRouters, "f")) {
111
- const mwResult = mr.match(router_1.METHOD_NAME_OF_ALL, path);
112
- if (mwResult)
113
- middleware.push(mwResult.handler);
144
+ const handlers = result ? result.handlers : [this.notFoundHandler];
145
+ const c = new context_1.Context(request, { env: env, event: event, res: undefined });
146
+ c.notFound = () => this.notFoundHandler(c);
147
+ const composed = (0, compose_1.compose)(handlers, this.errorHandler, this.notFoundHandler);
148
+ let context;
149
+ try {
150
+ context = await composed(c);
114
151
  }
115
- const wrappedHandler = async (context, next) => {
116
- const res = await handler(context);
117
- if (!(res instanceof Response)) {
118
- throw new TypeError('response must be a instance of Response');
152
+ catch (err) {
153
+ if (err instanceof Error) {
154
+ return this.errorHandler(err, c);
119
155
  }
120
- context.res = res;
121
- await next();
122
- };
123
- middleware.push(wrappedHandler);
124
- const composed = (0, compose_1.compose)(middleware, this.errorHandler);
125
- const c = new context_1.Context(request, { env: env, event: event, res: null });
126
- c.notFound = () => this.notFoundHandler(c);
127
- const context = await composed(c);
156
+ }
157
+ if (!context.res)
158
+ return context.notFound();
128
159
  return context.res;
129
160
  }
130
161
  async handleEvent(event) {
@@ -144,4 +175,4 @@ class Hono extends defineDynamicClass() {
144
175
  }
145
176
  }
146
177
  exports.Hono = Hono;
147
- _Hono_router = new WeakMap(), _Hono_middlewareRouters = new WeakMap(), _Hono_tempPath = new WeakMap();
178
+ _Hono_router = new WeakMap(), _Hono_tempPath = new WeakMap();
@@ -0,0 +1 @@
1
+ export {};