hono 4.9.7 → 4.9.9

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.
@@ -27,17 +27,17 @@ module.exports = __toCommonJS(route_exports);
27
27
  var import_constants = require("../../request/constants");
28
28
  var import_url = require("../../utils/url");
29
29
  const matchedRoutes = (c) => c.req[import_constants.GET_MATCH_RESULT][0].map(([[, route]]) => route);
30
- const routePath = (c) => matchedRoutes(c)[c.req.routeIndex].path;
31
- const baseRoutePath = (c) => matchedRoutes(c)[c.req.routeIndex].basePath;
30
+ const routePath = (c, index) => matchedRoutes(c).at(index ?? c.req.routeIndex)?.path ?? "";
31
+ const baseRoutePath = (c, index) => matchedRoutes(c).at(index ?? c.req.routeIndex)?.basePath ?? "";
32
32
  const basePathCacheMap = /* @__PURE__ */ new WeakMap();
33
- const basePath = (c) => {
34
- const routeIndex = c.req.routeIndex;
33
+ const basePath = (c, index) => {
34
+ index ??= c.req.routeIndex;
35
35
  const cache = basePathCacheMap.get(c) || [];
36
- if (typeof cache[routeIndex] === "string") {
37
- return cache[routeIndex];
36
+ if (typeof cache[index] === "string") {
37
+ return cache[index];
38
38
  }
39
39
  let result;
40
- const rp = baseRoutePath(c);
40
+ const rp = baseRoutePath(c, index);
41
41
  if (!/[:*]/.test(rp)) {
42
42
  result = rp;
43
43
  } else {
@@ -56,7 +56,7 @@ const basePath = (c) => {
56
56
  }
57
57
  result = reqPath.substring(0, basePathLength);
58
58
  }
59
- cache[routeIndex] = result;
59
+ cache[index] = result;
60
60
  basePathCacheMap.set(c, cache);
61
61
  return result;
62
62
  };
@@ -44,14 +44,14 @@ class HonoRequest {
44
44
  #getDecodedParam(key) {
45
45
  const paramKey = this.#matchResult[0][this.routeIndex][1][key];
46
46
  const param = this.#getParamValue(paramKey);
47
- return param ? /\%/.test(param) ? tryDecodeURIComponent(param) : param : void 0;
47
+ return param && /\%/.test(param) ? tryDecodeURIComponent(param) : param;
48
48
  }
49
49
  #getAllDecodedParams() {
50
50
  const decoded = {};
51
51
  const keys = Object.keys(this.#matchResult[0][this.routeIndex][1]);
52
52
  for (const key of keys) {
53
53
  const value = this.#getParamValue(this.#matchResult[0][this.routeIndex][1][key]);
54
- if (value && typeof value === "string") {
54
+ if (value !== void 0) {
55
55
  decoded[key] = /\%/.test(value) ? tryDecodeURIComponent(value) : value;
56
56
  }
57
57
  }
@@ -2,17 +2,17 @@
2
2
  import { GET_MATCH_RESULT } from "../../request/constants.js";
3
3
  import { getPattern, splitRoutingPath } from "../../utils/url.js";
4
4
  var matchedRoutes = (c) => c.req[GET_MATCH_RESULT][0].map(([[, route]]) => route);
5
- var routePath = (c) => matchedRoutes(c)[c.req.routeIndex].path;
6
- var baseRoutePath = (c) => matchedRoutes(c)[c.req.routeIndex].basePath;
5
+ var routePath = (c, index) => matchedRoutes(c).at(index ?? c.req.routeIndex)?.path ?? "";
6
+ var baseRoutePath = (c, index) => matchedRoutes(c).at(index ?? c.req.routeIndex)?.basePath ?? "";
7
7
  var basePathCacheMap = /* @__PURE__ */ new WeakMap();
8
- var basePath = (c) => {
9
- const routeIndex = c.req.routeIndex;
8
+ var basePath = (c, index) => {
9
+ index ??= c.req.routeIndex;
10
10
  const cache = basePathCacheMap.get(c) || [];
11
- if (typeof cache[routeIndex] === "string") {
12
- return cache[routeIndex];
11
+ if (typeof cache[index] === "string") {
12
+ return cache[index];
13
13
  }
14
14
  let result;
15
- const rp = baseRoutePath(c);
15
+ const rp = baseRoutePath(c, index);
16
16
  if (!/[:*]/.test(rp)) {
17
17
  result = rp;
18
18
  } else {
@@ -31,7 +31,7 @@ var basePath = (c) => {
31
31
  }
32
32
  result = reqPath.substring(0, basePathLength);
33
33
  }
34
- cache[routeIndex] = result;
34
+ cache[index] = result;
35
35
  basePathCacheMap.set(c, cache);
36
36
  return result;
37
37
  };
package/dist/request.js CHANGED
@@ -22,14 +22,14 @@ var HonoRequest = class {
22
22
  #getDecodedParam(key) {
23
23
  const paramKey = this.#matchResult[0][this.routeIndex][1][key];
24
24
  const param = this.#getParamValue(paramKey);
25
- return param ? /\%/.test(param) ? tryDecodeURIComponent(param) : param : void 0;
25
+ return param && /\%/.test(param) ? tryDecodeURIComponent(param) : param;
26
26
  }
27
27
  #getAllDecodedParams() {
28
28
  const decoded = {};
29
29
  const keys = Object.keys(this.#matchResult[0][this.routeIndex][1]);
30
30
  for (const key of keys) {
31
31
  const value = this.#getParamValue(this.#matchResult[0][this.routeIndex][1][key]);
32
- if (value && typeof value === "string") {
32
+ if (value !== void 0) {
33
33
  decoded[key] = /\%/.test(value) ? tryDecodeURIComponent(value) : value;
34
34
  }
35
35
  }
@@ -3,6 +3,7 @@
3
3
  * @module
4
4
  */
5
5
  import type { Hono } from '../../hono';
6
+ import type { Env, Schema } from '../../types';
6
7
  import type { FetchEvent } from './types';
7
8
  type Handler = (evt: FetchEvent) => void;
8
9
  export type HandleOptions = {
@@ -11,5 +12,5 @@ export type HandleOptions = {
11
12
  /**
12
13
  * Adapter for Service Worker
13
14
  */
14
- export declare const handle: (app: Hono, opts?: HandleOptions) => Handler;
15
+ export declare const handle: <E extends Env, S extends Schema, BasePath extends string>(app: Hono<E, S, BasePath>, opts?: HandleOptions) => Handler;
15
16
  export {};
@@ -3,6 +3,7 @@
3
3
  * @module
4
4
  */
5
5
  import type { Hono } from '../../hono';
6
+ import type { Env, Schema } from '../../types';
6
7
  import { handle } from './handler';
7
8
  import type { HandleOptions } from './handler';
8
9
  /**
@@ -23,5 +24,5 @@ import type { HandleOptions } from './handler';
23
24
  * fire(app)
24
25
  * ```
25
26
  */
26
- declare const fire: (app: Hono, options?: HandleOptions) => void;
27
+ declare const fire: <E extends Env, S extends Schema, BasePath extends string>(app: Hono<E, S, BasePath>, options?: HandleOptions) => void;
27
28
  export { handle, fire };
@@ -4,7 +4,7 @@ import type { Env, FetchEventLike, H, Input, NotFoundHandler, RouterRoute, Typed
4
4
  import type { ResponseHeader } from './utils/headers';
5
5
  import type { ContentfulStatusCode, RedirectStatusCode, StatusCode } from './utils/http-status';
6
6
  import type { BaseMime } from './utils/mime';
7
- import type { InvalidJSONValue, IsAny, JSONParsed, JSONValue, SimplifyDeepArray } from './utils/types';
7
+ import type { InvalidJSONValue, IsAny, JSONParsed, JSONValue } from './utils/types';
8
8
  type HeaderRecord = Record<"Content-Type", BaseMime> | Record<ResponseHeader, string | string[]> | Record<string, string | string[]>;
9
9
  /**
10
10
  * Data type can be a string, ArrayBuffer, Uint8Array (buffer), or ReadableStream.
@@ -128,16 +128,16 @@ interface TextRespond {
128
128
  * @returns {JSONRespondReturn<T, U>} - The response after rendering the JSON object, typed with the provided object and status code types.
129
129
  */
130
130
  interface JSONRespond {
131
- <T extends JSONValue | SimplifyDeepArray<unknown> | InvalidJSONValue, U extends ContentfulStatusCode = ContentfulStatusCode>(object: T, status?: U, headers?: HeaderRecord): JSONRespondReturn<T, U>;
132
- <T extends JSONValue | SimplifyDeepArray<unknown> | InvalidJSONValue, U extends ContentfulStatusCode = ContentfulStatusCode>(object: T, init?: ResponseOrInit<U>): JSONRespondReturn<T, U>;
131
+ <T extends JSONValue | {} | InvalidJSONValue, U extends ContentfulStatusCode = ContentfulStatusCode>(object: T, status?: U, headers?: HeaderRecord): JSONRespondReturn<T, U>;
132
+ <T extends JSONValue | {} | InvalidJSONValue, U extends ContentfulStatusCode = ContentfulStatusCode>(object: T, init?: ResponseOrInit<U>): JSONRespondReturn<T, U>;
133
133
  }
134
134
  /**
135
135
  * @template T - The type of the JSON value or simplified unknown type.
136
136
  * @template U - The type of the status code.
137
137
  *
138
- * @returns {Response & TypedResponse<SimplifyDeepArray<T> extends JSONValue ? (JSONValue extends SimplifyDeepArray<T> ? never : JSONParsed<T>) : never, U, 'json'>} - The response after rendering the JSON object, typed with the provided object and status code types.
138
+ * @returns {Response & TypedResponse<JSONParsed<T>, U, 'json'>} - The response after rendering the JSON object, typed with the provided object and status code types.
139
139
  */
140
- type JSONRespondReturn<T extends JSONValue | SimplifyDeepArray<unknown> | InvalidJSONValue, U extends ContentfulStatusCode> = Response & TypedResponse<SimplifyDeepArray<T> extends JSONValue ? JSONValue extends SimplifyDeepArray<T> ? never : JSONParsed<T> : never, U, "json">;
140
+ export type JSONRespondReturn<T extends JSONValue | {} | InvalidJSONValue, U extends ContentfulStatusCode> = Response & TypedResponse<JSONParsed<T>, U, "json">;
141
141
  /**
142
142
  * Interface representing a function that responds with HTML content.
143
143
  *
@@ -31,22 +31,30 @@ export declare const matchedRoutes: (c: Context) => RouterRoute[];
31
31
  * Get the route path registered within the handler
32
32
  *
33
33
  * @param {Context} c - The context object
34
+ * @param {number} index - The index of the root from which to retrieve the path, similar to Array.prototype.at(), where a negative number is the index counted from the end of the matching root. Defaults to the current root index.
34
35
  * @returns The route path registered within the handler
35
36
  *
36
37
  * @example
37
38
  * ```ts
38
39
  * import { routePath } from 'hono/route'
39
40
  *
41
+ * app.use('*', (c, next) => {
42
+ * console.log(routePath(c)) // '*'
43
+ * console.log(routePath(c, -1)) // '/posts/:id'
44
+ * return next()
45
+ * })
46
+ *
40
47
  * app.get('/posts/:id', (c) => {
41
48
  * return c.text(routePath(c)) // '/posts/:id'
42
49
  * })
43
50
  * ```
44
51
  */
45
- export declare const routePath: (c: Context) => string;
52
+ export declare const routePath: (c: Context, index?: number) => string;
46
53
  /**
47
54
  * Get the basePath of the as-is route specified by routing.
48
55
  *
49
56
  * @param {Context} c - The context object
57
+ * @param {number} index - The index of the root from which to retrieve the path, similar to Array.prototype.at(), where a negative number is the index counted from the end of the matching root. Defaults to the current root index.
50
58
  * @returns The basePath of the as-is route specified by routing.
51
59
  *
52
60
  * @example
@@ -63,5 +71,5 @@ export declare const routePath: (c: Context) => string;
63
71
  * app.route('/:sub', subApp)
64
72
  * ```
65
73
  */
66
- export declare const baseRoutePath: (c: Context) => string;
67
- export declare const basePath: (c: Context) => string;
74
+ export declare const baseRoutePath: (c: Context, index?: number) => string;
75
+ export declare const basePath: (c: Context, index?: number) => string;
@@ -23,15 +23,28 @@ type OmitSymbolKeys<T> = {
23
23
  [K in keyof T as K extends symbol ? never : K]: T[K];
24
24
  };
25
25
  export type JSONValue = JSONObject | JSONArray | JSONPrimitive;
26
- export type JSONParsed<T> = T extends {
26
+ /**
27
+ * Convert a type to a JSON-compatible type.
28
+ *
29
+ * Non-JSON values such as `Date` implement `.toJSON()`,
30
+ * so they can be transformed to a value assignable to `JSONObject`
31
+ *
32
+ * `JSON.stringify()` throws a `TypeError` when it encounters a `bigint` value,
33
+ * unless a custom `replacer` function or `.toJSON()` method is provided.
34
+ *
35
+ * This behaviour can be controlled by the `TError` generic type parameter,
36
+ * which defaults to `bigint | ReadonlyArray<bigint>`.
37
+ * You can set it to `never` to disable this check.
38
+ */
39
+ export type JSONParsed<T, TError = bigint | ReadonlyArray<bigint>> = T extends TError ? never : T extends {
27
40
  toJSON(): infer J;
28
41
  } ? (() => J) extends () => JSONPrimitive ? J : (() => J) extends () => {
29
42
  toJSON(): unknown;
30
- } ? {} : JSONParsed<J> : T extends JSONPrimitive ? T : T extends InvalidJSONValue ? never : T extends ReadonlyArray<unknown> ? {
31
- [K in keyof T]: JSONParsed<InvalidToNull<T[K]>>;
32
- } : T extends Set<unknown> | Map<unknown, unknown> ? {} : T extends object ? {
33
- [K in keyof OmitSymbolKeys<T> as IsInvalid<T[K]> extends true ? never : K]: boolean extends IsInvalid<T[K]> ? JSONParsed<T[K]> | undefined : JSONParsed<T[K]>;
34
- } : never;
43
+ } ? {} : JSONParsed<J, TError> : T extends JSONPrimitive ? T : T extends InvalidJSONValue ? never : T extends ReadonlyArray<unknown> ? {
44
+ [K in keyof T]: JSONParsed<InvalidToNull<T[K]>, TError>;
45
+ } : T extends Set<unknown> | Map<unknown, unknown> | Record<string, never> ? {} : T extends object ? T[keyof T] extends TError ? never : {
46
+ [K in keyof OmitSymbolKeys<T> as IsInvalid<T[K]> extends true ? never : K]: boolean extends IsInvalid<T[K]> ? JSONParsed<T[K], TError> | undefined : JSONParsed<T[K], TError>;
47
+ } : T extends unknown ? JSONValue : never;
35
48
  /**
36
49
  * Useful to flatten the type output to improve type hints shown in editors. And also to transform an interface into a type to aide with assignability.
37
50
  * @copyright from sindresorhus/type-fest
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hono",
3
- "version": "4.9.7",
3
+ "version": "4.9.9",
4
4
  "description": "Web framework built on Web Standards",
5
5
  "main": "dist/cjs/index.js",
6
6
  "type": "module",