hono 1.5.1 → 1.5.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.
package/dist/context.d.ts CHANGED
@@ -1,11 +1,12 @@
1
1
  /// <reference types="@cloudflare/workers-types" />
2
2
  import type { NotFoundHandler } from './hono';
3
+ import type { HonoRequest } from './request';
3
4
  import type { StatusCode } from './utils/http-status';
4
5
  declare type Headers = Record<string, string>;
5
6
  export declare type Data = string | ArrayBuffer | ReadableStream;
6
7
  export declare type Env = Record<string, any>;
7
8
  export declare class Context<RequestParamKeyType extends string = string, E = Env> {
8
- req: Request<RequestParamKeyType>;
9
+ req: HonoRequest<RequestParamKeyType>;
9
10
  env: E;
10
11
  event: FetchEvent | undefined;
11
12
  executionCtx: ExecutionContext | undefined;
@@ -18,7 +19,7 @@ export declare class Context<RequestParamKeyType extends string = string, E = En
18
19
  private _res;
19
20
  private notFoundHandler;
20
21
  render: (content: string, params?: object, options?: object) => Response | Promise<Response>;
21
- constructor(req: Request<RequestParamKeyType>, env?: E | undefined, eventOrExecutionCtx?: FetchEvent | ExecutionContext | undefined, notFoundHandler?: NotFoundHandler);
22
+ constructor(req: HonoRequest | Request, env?: E | undefined, eventOrExecutionCtx?: FetchEvent | ExecutionContext | undefined, notFoundHandler?: NotFoundHandler);
22
23
  get res(): Response;
23
24
  set res(_res: Response);
24
25
  header(name: string, value: string): void;
package/dist/context.js CHANGED
@@ -1,13 +1,19 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Context = void 0;
4
+ const request_1 = require("./request");
4
5
  const url_1 = require("./utils/url");
5
6
  class Context {
6
7
  constructor(req, env = undefined, eventOrExecutionCtx = undefined, notFoundHandler = () => new Response()) {
7
8
  this._status = 200;
8
9
  this._pretty = false;
9
10
  this._prettySpace = 2;
10
- this.req = req;
11
+ if (req instanceof Request) {
12
+ this.req = (0, request_1.extendHonoRequest)(req);
13
+ }
14
+ else {
15
+ this.req = req;
16
+ }
11
17
  if (env) {
12
18
  this.env = env;
13
19
  }
package/dist/hono.js CHANGED
@@ -29,7 +29,6 @@ class Hono extends defineDynamicClass() {
29
29
  const message = 'Internal Server Error';
30
30
  return c.text(message, 500);
31
31
  };
32
- (0, request_1.extendRequestPrototype)(); // FIXME: should be executed at a better timing
33
32
  const allMethods = [...methods, router_1.METHOD_NAME_ALL_LOWERCASE];
34
33
  allMethods.map((method) => {
35
34
  this[method] = (args1, ...args) => {
@@ -92,6 +91,7 @@ class Hono extends defineDynamicClass() {
92
91
  return this.router.match(method, path);
93
92
  }
94
93
  async dispatch(request, executionCtx, env) {
94
+ request = (0, request_1.extendHonoRequest)(request);
95
95
  const path = (0, url_1.getPathFromURL)(request.url, this.strict);
96
96
  const method = request.method;
97
97
  const result = this.matchRoute(method, path);
@@ -1,7 +1,7 @@
1
1
  import type { Context } from '../../context';
2
2
  import type { Next } from '../../hono';
3
- declare global {
4
- interface Request {
3
+ declare module '../../request' {
4
+ interface HonoRequest {
5
5
  parsedBody: any;
6
6
  }
7
7
  }
@@ -1,7 +1,7 @@
1
1
  import type { Context } from '../../context';
2
2
  import type { Next } from '../../hono';
3
- declare global {
4
- interface Request {
3
+ declare module '../../request' {
4
+ interface HonoRequest {
5
5
  cookie: {
6
6
  (name: string): string;
7
7
  (): Record<string, string>;
@@ -2,7 +2,8 @@
2
2
  import type { Env } from '../../context';
3
3
  import type { Handler } from '../../hono';
4
4
  export declare type ServeStaticOptions = {
5
- root: string;
5
+ root?: string;
6
+ path?: string;
6
7
  manifest?: object | string;
7
8
  namespace?: KVNamespace;
8
9
  };
@@ -13,7 +13,7 @@ const serveStatic = (options = { root: '' }) => {
13
13
  }
14
14
  const url = new URL(c.req.url);
15
15
  const path = (0, cloudflare_1.getKVFilePath)({
16
- filename: url.pathname,
16
+ filename: options.path ?? url.pathname,
17
17
  root: options.root,
18
18
  defaultDocument: DEFAULT_DOCUMENT,
19
19
  });
package/dist/request.d.ts CHANGED
@@ -1,22 +1,20 @@
1
- declare global {
2
- interface Request<ParamKeyType extends string = string> {
3
- param: {
4
- (key: ParamKeyType): string;
5
- (): Record<ParamKeyType, string>;
6
- };
7
- paramData?: Record<ParamKeyType, string>;
8
- query: {
9
- (key: string): string;
10
- (): Record<string, string>;
11
- };
12
- queries: {
13
- (key: string): string[];
14
- (): Record<string, string[]>;
15
- };
16
- header: {
17
- (name: string): string;
18
- (): Record<string, string>;
19
- };
20
- }
1
+ export declare class HonoRequest<ParamKeyType extends string = string> extends Request {
2
+ param: {
3
+ (key: ParamKeyType): string;
4
+ (): Record<ParamKeyType, string>;
5
+ };
6
+ paramData?: Record<ParamKeyType, string>;
7
+ query: {
8
+ (key: string): string;
9
+ (): Record<string, string>;
10
+ };
11
+ queries: {
12
+ (key: string): string[];
13
+ (): Record<string, string[]>;
14
+ };
15
+ header: {
16
+ (name: string): string;
17
+ (): Record<string, string>;
18
+ };
21
19
  }
22
- export declare function extendRequestPrototype(): void;
20
+ export declare function extendHonoRequest(request: HonoRequest): HonoRequest;
package/dist/request.js CHANGED
@@ -1,12 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.extendRequestPrototype = void 0;
4
- function extendRequestPrototype() {
5
- if (!!Request.prototype.param) {
6
- // already extended
7
- return;
8
- }
9
- Request.prototype.param = function (key) {
3
+ exports.extendHonoRequest = exports.HonoRequest = void 0;
4
+ class HonoRequest extends Request {
5
+ }
6
+ exports.HonoRequest = HonoRequest;
7
+ function extendHonoRequest(request) {
8
+ request.param = function (key) {
10
9
  if (this.paramData) {
11
10
  if (key) {
12
11
  return this.paramData[key];
@@ -17,7 +16,7 @@ function extendRequestPrototype() {
17
16
  }
18
17
  return null;
19
18
  };
20
- Request.prototype.header = function (name) {
19
+ request.header = function (name) {
21
20
  if (name) {
22
21
  return this.headers.get(name);
23
22
  }
@@ -29,7 +28,7 @@ function extendRequestPrototype() {
29
28
  return result;
30
29
  }
31
30
  };
32
- Request.prototype.query = function (key) {
31
+ request.query = function (key) {
33
32
  const url = new URL(this.url);
34
33
  if (key) {
35
34
  return url.searchParams.get(key);
@@ -42,7 +41,7 @@ function extendRequestPrototype() {
42
41
  return result;
43
42
  }
44
43
  };
45
- Request.prototype.queries = function (key) {
44
+ request.queries = function (key) {
46
45
  const url = new URL(this.url);
47
46
  if (key) {
48
47
  return url.searchParams.getAll(key);
@@ -55,5 +54,6 @@ function extendRequestPrototype() {
55
54
  return result;
56
55
  }
57
56
  };
57
+ return request;
58
58
  }
59
- exports.extendRequestPrototype = extendRequestPrototype;
59
+ exports.extendHonoRequest = extendHonoRequest;
@@ -50,7 +50,7 @@ const getKVFilePath = (options) => {
50
50
  filename = filename.concat('/' + defaultDocument);
51
51
  }
52
52
  // /foo.html => foo.html
53
- filename = filename.replace(/^\//, '');
53
+ filename = filename.replace(/^\.?\//, '');
54
54
  // assets/ => assets
55
55
  root = root.replace(/\/$/, '');
56
56
  // ./assets/foo.html => assets/foo.html
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hono",
3
- "version": "1.5.1",
3
+ "version": "1.5.2",
4
4
  "description": "Ultrafast web framework for Cloudflare Workers.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -145,7 +145,7 @@
145
145
  "form-data": "^4.0.0",
146
146
  "graphql": "^16.4.0",
147
147
  "jest": "27.5.1",
148
- "jest-environment-miniflare": "^2.5.0",
148
+ "jest-environment-miniflare": "^2.5.1",
149
149
  "mustache": "^4.2.0",
150
150
  "prettier": "^2.6.2",
151
151
  "prettier-plugin-md-nocjsp": "^1.2.0",