hono 3.10.0 → 3.10.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.
@@ -26,7 +26,7 @@ const compose = (middleware, onError, onNotFound) => {
26
26
  return (context, next) => {
27
27
  let index = -1;
28
28
  return dispatch(0);
29
- function dispatch(i) {
29
+ async function dispatch(i) {
30
30
  if (i <= index) {
31
31
  throw new Error("next() called multiple times");
32
32
  }
@@ -44,50 +44,30 @@ const compose = (middleware, onError, onNotFound) => {
44
44
  }
45
45
  if (!handler) {
46
46
  if (context instanceof import_context.Context && context.finalized === false && onNotFound) {
47
- res = onNotFound(context);
47
+ res = await onNotFound(context);
48
48
  }
49
49
  } else {
50
50
  try {
51
- res = handler(context, () => {
52
- const dispatchRes = dispatch(i + 1);
53
- return dispatchRes instanceof Promise ? dispatchRes : Promise.resolve(dispatchRes);
51
+ res = await handler(context, () => {
52
+ return dispatch(i + 1);
54
53
  });
55
54
  } catch (err) {
56
55
  if (err instanceof Error && context instanceof import_context.Context && onError) {
57
56
  context.error = err;
58
- res = onError(err, context);
57
+ res = await onError(err, context);
59
58
  isError = true;
60
59
  } else {
61
60
  throw err;
62
61
  }
63
62
  }
64
63
  }
65
- if (!(res instanceof Promise)) {
66
- if (res !== void 0 && "response" in res) {
67
- res = res["response"];
68
- }
69
- if (res && (context.finalized === false || isError)) {
70
- context.res = res;
71
- }
72
- return context;
73
- } else {
74
- return res.then((res2) => {
75
- if (res2 !== void 0 && "response" in res2) {
76
- res2 = res2["response"];
77
- }
78
- if (res2 && context.finalized === false) {
79
- context.res = res2;
80
- }
81
- return context;
82
- }).catch(async (err) => {
83
- if (err instanceof Error && context instanceof import_context.Context && onError) {
84
- context.error = err;
85
- context.res = await onError(err, context);
86
- return context;
87
- }
88
- throw err;
89
- });
64
+ if (res !== void 0 && "response" in res) {
65
+ res = res["response"];
66
+ }
67
+ if (res && (context.finalized === false || isError)) {
68
+ context.res = res;
90
69
  }
70
+ return context;
91
71
  }
92
72
  };
93
73
  };
@@ -76,6 +76,7 @@ class Context {
76
76
  }
77
77
  };
78
78
  this.status = (status) => {
79
+ this._init = false;
79
80
  this._status = status;
80
81
  };
81
82
  this.set = (key, value) => {
@@ -212,7 +212,7 @@ class Hono extends defineDynamicClass() {
212
212
  }
213
213
  const path = this.getPath(request, { env });
214
214
  const [handlers, paramStash] = this.matchRoute(method, path);
215
- const c = new import_context.Context(new import_request.HonoRequest(request, path, paramStash || []), {
215
+ const c = new import_context.Context(new import_request.HonoRequest(request, path, paramStash), {
216
216
  env,
217
217
  executionCtx,
218
218
  notFoundHandler: this.notFoundHandler
@@ -255,8 +255,7 @@ class Hono extends defineDynamicClass() {
255
255
  const composed = (0, import_compose.compose)(handlers, this.errorHandler, this.notFoundHandler);
256
256
  return (async () => {
257
257
  try {
258
- const tmp = composed(c);
259
- const context = tmp.constructor.name === "Promise" ? await tmp : tmp;
258
+ const context = await composed(c);
260
259
  if (!context.finalized) {
261
260
  throw new Error(
262
261
  "Context is not finalized. You may forget returning Response object or `await next()`"
@@ -25,7 +25,7 @@ var import_body = require("./utils/body");
25
25
  var import_cookie = require("./utils/cookie");
26
26
  var import_url = require("./utils/url");
27
27
  class HonoRequest {
28
- constructor(request, path = "/", paramStash = []) {
28
+ constructor(request, path = "/", paramStash = void 0) {
29
29
  this._p = {};
30
30
  this.bodyCache = {};
31
31
  this.cachedBody = (key) => {
@@ -49,24 +49,21 @@ class HonoRequest {
49
49
  this._p = params;
50
50
  }
51
51
  param(key) {
52
- if (this._s) {
53
- if (key) {
54
- const param = this._s[this._p[key]] ?? this._p[key];
55
- return param ? /\%/.test(param) ? (0, import_url.decodeURIComponent_)(param) : param : void 0;
56
- } else {
57
- const decoded = {};
58
- const keys = Object.keys(this._p);
59
- for (let i = 0, len = keys.length; i < len; i++) {
60
- const key2 = keys[i];
61
- const value = this._s[this._p[key2]] ?? this._p[key2];
62
- if (value && typeof value === "string") {
63
- decoded[key2] = /\%/.test(value) ? (0, import_url.decodeURIComponent_)(value) : value;
64
- }
52
+ if (key) {
53
+ const param = this._s ? this._s[this._p[key]] : this._p[key];
54
+ return param ? /\%/.test(param) ? (0, import_url.decodeURIComponent_)(param) : param : void 0;
55
+ } else {
56
+ const decoded = {};
57
+ const keys = Object.keys(this._p);
58
+ for (let i = 0, len = keys.length; i < len; i++) {
59
+ const key2 = keys[i];
60
+ const value = this._s ? this._s[this._p[key2]] : this._p[key2];
61
+ if (value && typeof value === "string") {
62
+ decoded[key2] = /\%/.test(value) ? (0, import_url.decodeURIComponent_)(value) : value;
65
63
  }
66
- return decoded;
67
64
  }
65
+ return decoded;
68
66
  }
69
- return null;
70
67
  }
71
68
  query(key) {
72
69
  return (0, import_url.getQueryParam)(this.url, key);
package/dist/compose.js CHANGED
@@ -4,7 +4,7 @@ var compose = (middleware, onError, onNotFound) => {
4
4
  return (context, next) => {
5
5
  let index = -1;
6
6
  return dispatch(0);
7
- function dispatch(i) {
7
+ async function dispatch(i) {
8
8
  if (i <= index) {
9
9
  throw new Error("next() called multiple times");
10
10
  }
@@ -22,50 +22,30 @@ var compose = (middleware, onError, onNotFound) => {
22
22
  }
23
23
  if (!handler) {
24
24
  if (context instanceof Context && context.finalized === false && onNotFound) {
25
- res = onNotFound(context);
25
+ res = await onNotFound(context);
26
26
  }
27
27
  } else {
28
28
  try {
29
- res = handler(context, () => {
30
- const dispatchRes = dispatch(i + 1);
31
- return dispatchRes instanceof Promise ? dispatchRes : Promise.resolve(dispatchRes);
29
+ res = await handler(context, () => {
30
+ return dispatch(i + 1);
32
31
  });
33
32
  } catch (err) {
34
33
  if (err instanceof Error && context instanceof Context && onError) {
35
34
  context.error = err;
36
- res = onError(err, context);
35
+ res = await onError(err, context);
37
36
  isError = true;
38
37
  } else {
39
38
  throw err;
40
39
  }
41
40
  }
42
41
  }
43
- if (!(res instanceof Promise)) {
44
- if (res !== void 0 && "response" in res) {
45
- res = res["response"];
46
- }
47
- if (res && (context.finalized === false || isError)) {
48
- context.res = res;
49
- }
50
- return context;
51
- } else {
52
- return res.then((res2) => {
53
- if (res2 !== void 0 && "response" in res2) {
54
- res2 = res2["response"];
55
- }
56
- if (res2 && context.finalized === false) {
57
- context.res = res2;
58
- }
59
- return context;
60
- }).catch(async (err) => {
61
- if (err instanceof Error && context instanceof Context && onError) {
62
- context.error = err;
63
- context.res = await onError(err, context);
64
- return context;
65
- }
66
- throw err;
67
- });
42
+ if (res !== void 0 && "response" in res) {
43
+ res = res["response"];
44
+ }
45
+ if (res && (context.finalized === false || isError)) {
46
+ context.res = res;
68
47
  }
48
+ return context;
69
49
  }
70
50
  };
71
51
  };
package/dist/context.js CHANGED
@@ -54,6 +54,7 @@ var Context = class {
54
54
  }
55
55
  };
56
56
  this.status = (status) => {
57
+ this._init = false;
57
58
  this._status = status;
58
59
  };
59
60
  this.set = (key, value) => {
package/dist/hono-base.js CHANGED
@@ -190,7 +190,7 @@ var Hono = class extends defineDynamicClass() {
190
190
  }
191
191
  const path = this.getPath(request, { env });
192
192
  const [handlers, paramStash] = this.matchRoute(method, path);
193
- const c = new Context(new HonoRequest(request, path, paramStash || []), {
193
+ const c = new Context(new HonoRequest(request, path, paramStash), {
194
194
  env,
195
195
  executionCtx,
196
196
  notFoundHandler: this.notFoundHandler
@@ -233,8 +233,7 @@ var Hono = class extends defineDynamicClass() {
233
233
  const composed = compose(handlers, this.errorHandler, this.notFoundHandler);
234
234
  return (async () => {
235
235
  try {
236
- const tmp = composed(c);
237
- const context = tmp.constructor.name === "Promise" ? await tmp : tmp;
236
+ const context = await composed(c);
238
237
  if (!context.finalized) {
239
238
  throw new Error(
240
239
  "Context is not finalized. You may forget returning Response object or `await next()`"
package/dist/request.js CHANGED
@@ -3,7 +3,7 @@ import { parseBody } from "./utils/body.js";
3
3
  import { parse } from "./utils/cookie.js";
4
4
  import { getQueryParam, getQueryParams, decodeURIComponent_ } from "./utils/url.js";
5
5
  var HonoRequest = class {
6
- constructor(request, path = "/", paramStash = []) {
6
+ constructor(request, path = "/", paramStash = void 0) {
7
7
  this._p = {};
8
8
  this.bodyCache = {};
9
9
  this.cachedBody = (key) => {
@@ -27,24 +27,21 @@ var HonoRequest = class {
27
27
  this._p = params;
28
28
  }
29
29
  param(key) {
30
- if (this._s) {
31
- if (key) {
32
- const param = this._s[this._p[key]] ?? this._p[key];
33
- return param ? /\%/.test(param) ? decodeURIComponent_(param) : param : void 0;
34
- } else {
35
- const decoded = {};
36
- const keys = Object.keys(this._p);
37
- for (let i = 0, len = keys.length; i < len; i++) {
38
- const key2 = keys[i];
39
- const value = this._s[this._p[key2]] ?? this._p[key2];
40
- if (value && typeof value === "string") {
41
- decoded[key2] = /\%/.test(value) ? decodeURIComponent_(value) : value;
42
- }
30
+ if (key) {
31
+ const param = this._s ? this._s[this._p[key]] : this._p[key];
32
+ return param ? /\%/.test(param) ? decodeURIComponent_(param) : param : void 0;
33
+ } else {
34
+ const decoded = {};
35
+ const keys = Object.keys(this._p);
36
+ for (let i = 0, len = keys.length; i < len; i++) {
37
+ const key2 = keys[i];
38
+ const value = this._s ? this._s[this._p[key2]] : this._p[key2];
39
+ if (value && typeof value === "string") {
40
+ decoded[key2] = /\%/.test(value) ? decodeURIComponent_(value) : value;
43
41
  }
44
- return decoded;
45
42
  }
43
+ return decoded;
46
44
  }
47
- return null;
48
45
  }
49
46
  query(key) {
50
47
  return getQueryParam(this.url, key);
@@ -32,7 +32,7 @@ export interface LambdaFunctionUrlEvent {
32
32
  isBase64Encoded: boolean;
33
33
  requestContext: LambdaFunctionUrlRequestContext;
34
34
  }
35
- interface APIGatewayProxyResult {
35
+ export interface APIGatewayProxyResult {
36
36
  statusCode: number;
37
37
  body: string;
38
38
  headers: Record<string, string>;
@@ -49,4 +49,3 @@ export declare const streamHandle: <E extends Env = Env, S extends Schema = {},
49
49
  export declare const handle: <E extends Env = Env, S extends Schema = {}, BasePath extends string = "/">(app: Hono<E, S, BasePath>) => (event: APIGatewayProxyEvent | APIGatewayProxyEventV2 | LambdaFunctionUrlEvent, lambdaContext?: LambdaContext) => Promise<APIGatewayProxyResult>;
50
50
  export declare const isContentTypeBinary: (contentType: string) => boolean;
51
51
  export declare const isContentEncodingBinary: (contentEncoding: string | null) => boolean;
52
- export {};
@@ -1,3 +1,4 @@
1
1
  export { handle, streamHandle } from './handler';
2
+ export type { APIGatewayProxyResult } from './handler';
2
3
  export type { ApiGatewayRequestContext, LambdaFunctionUrlRequestContext } from './custom-context';
3
4
  export type { LambdaContext } from './types';
@@ -4,5 +4,5 @@ interface ComposeContext {
4
4
  finalized: boolean;
5
5
  res: unknown;
6
6
  }
7
- export declare const compose: <C extends ComposeContext, E extends Env = Env>(middleware: [Function, ParamIndexMap | Params][], onError?: ErrorHandler<E> | undefined, onNotFound?: NotFoundHandler<E> | undefined) => (context: C, next?: Function) => C | Promise<C>;
7
+ export declare const compose: <C extends ComposeContext, E extends Env = Env>(middleware: [Function, ParamIndexMap | Params][], onError?: ErrorHandler<E> | undefined, onNotFound?: NotFoundHandler<E> | undefined) => (context: C, next?: Function) => Promise<C>;
8
8
  export {};
@@ -20,7 +20,7 @@ export declare class HonoRequest<P extends string = '/', I extends Input['out']
20
20
  private _p;
21
21
  path: string;
22
22
  bodyCache: BodyCache;
23
- constructor(request: Request, path?: string, paramStash?: ParamStash);
23
+ constructor(request: Request, path?: string, paramStash?: ParamStash | undefined);
24
24
  setParams(params: ParamIndexMap | Params): void;
25
25
  param<P2 extends string = P>(key: RemoveQuestion<ParamKeys<P2>>): UndefinedIfHavingQuestion<ParamKeys<P2>>;
26
26
  param<P2 extends string = P>(): UnionToIntersection<ParamKeyToRecord<ParamKeys<P2>>>;
@@ -25,7 +25,7 @@ export interface HandlerInterface<E extends Env = Env, M extends string = string
25
25
  <P extends string = ExtractKey<S> extends never ? BasePath : ExtractKey<S>, I extends Input = {}, R extends HandlerResponse<any> = any, E2 extends Env = E>(handler: H<E2, P, I, R>): Hono<E & E2, S & ToSchema<M, P, I['in'], MergeTypedResponseData<R>>, BasePath>;
26
26
  <P extends string = ExtractKey<S> extends never ? BasePath : ExtractKey<S>, I extends Input = {}, R extends HandlerResponse<any> = any, E2 extends Env = E, E3 extends Env = IntersectNonAnyTypes<[E, E2]>>(...handlers: [H<E2, P, I, R>, H<E3, P, I, R>]): Hono<E & E2 & E3, S & ToSchema<M, P, I['in'], MergeTypedResponseData<R>>, BasePath>;
27
27
  <P extends string = ExtractKey<S> extends never ? BasePath : ExtractKey<S>, R extends HandlerResponse<any> = any, I extends Input = {}, I2 extends Input = I, I3 extends Input = I & I2, E2 extends Env = E, E3 extends Env = E, E4 extends Env = IntersectNonAnyTypes<[E, E2, E3]>>(...handlers: [H<E2, P, I, R>, H<E3, P, I2, R>, H<E4, P, I3, R>]): Hono<E & E2 & E3 & E4, S & ToSchema<M, P, I3['in'], MergeTypedResponseData<R>>, BasePath>;
28
- <P extends string = ExtractKey<S> extends never ? BasePath : ExtractKey<S>, R extends HandlerResponse<any> = any, I extends Input = {}, I2 extends Input = I, I3 extends Input = I & I2, I4 extends Input = I & I2 & I3, E2 extends Env = E, E3 extends Env = E, E4 extends Env = E, E5 extends Env = IntersectNonAnyTypes<[E, E2, E3, E4]>>(...handlers: [H<E2, P, I, R>, H<E3, P, I2, R>, H<E4, P, I3, R>, H<E5, P, I3, R>]): Hono<E & E2 & E3 & E4 & E5, S & ToSchema<M, P, I4['in'], MergeTypedResponseData<R>>, BasePath>;
28
+ <P extends string = ExtractKey<S> extends never ? BasePath : ExtractKey<S>, R extends HandlerResponse<any> = any, I extends Input = {}, I2 extends Input = I, I3 extends Input = I & I2, I4 extends Input = I & I2 & I3, E2 extends Env = E, E3 extends Env = E, E4 extends Env = E, E5 extends Env = IntersectNonAnyTypes<[E, E2, E3, E4]>>(...handlers: [H<E2, P, I, R>, H<E3, P, I2, R>, H<E4, P, I3, R>, H<E5, P, I4, R>]): Hono<E & E2 & E3 & E4 & E5, S & ToSchema<M, P, I4['in'], MergeTypedResponseData<R>>, BasePath>;
29
29
  <P extends string = ExtractKey<S> extends never ? BasePath : ExtractKey<S>, R extends HandlerResponse<any> = any, I extends Input = {}, I2 extends Input = I, I3 extends Input = I & I2, I4 extends Input = I2 & I3, I5 extends Input = I & I2 & I3 & I4, E2 extends Env = E, E3 extends Env = E, E4 extends Env = E, E5 extends Env = E, E6 extends Env = IntersectNonAnyTypes<[E, E2, E3, E4, E5]>>(...handlers: [
30
30
  H<E2, P, I, R>,
31
31
  H<E3, P, I2, R>,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hono",
3
- "version": "3.10.0",
3
+ "version": "3.10.2",
4
4
  "description": "Ultrafast web framework for the Edges",
5
5
  "main": "dist/cjs/index.js",
6
6
  "type": "module",