Skip to content

Commit 8a60ba6

Browse files
authored
chore: update hono to v4 (#7855)
1 parent 1ef329f commit 8a60ba6

File tree

15 files changed

+64
-23
lines changed

15 files changed

+64
-23
lines changed

packages/cli/plugin-bff/src/runtime/hono/adapter.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ export class HonoAdapter {
6464
if (result instanceof Response) {
6565
return result;
6666
}
67+
} else {
68+
logger.error(err);
6769
}
6870
} catch (configError) {
6971
logger.error(`Error in serverConfig.onError handler: ${configError}`);
@@ -104,11 +106,7 @@ export class HonoAdapter {
104106
before,
105107
handler: async (c: Context, next: Next) => {
106108
if (this.apiServer) {
107-
const response = await this.apiServer.fetch(
108-
c.req as unknown as Request,
109-
c.env,
110-
);
111-
109+
const response = await this.apiServer.fetch(c.req.raw, c.env);
112110
if (response.status !== 404) {
113111
return new Response(response.body, response);
114112
}

packages/cli/plugin-bff/src/utils/createHonoRoutes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ const handleResponseMeta = (c: Context, handler: Handler) => {
4444
case ResponseMetaType.Redirect:
4545
return c.redirect(meta.value as string);
4646
case ResponseMetaType.StatusCode:
47-
c.status(meta.value as number);
47+
c.status(meta.value as any);
4848
break;
4949
default:
5050
break;

packages/document/main-doc/docs/en/guides/advanced-features/bff/extend-server.mdx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@ Developers can use middleware by configuring middlewares in `server/modern.serve
1212
import {
1313
type MiddlewareHandler,
1414
defineServerConfig,
15+
getCookie
1516
} from '@modern-js/server-runtime';
1617

1718
const requireAuthForApi: MiddlewareHandler = async (c, next) => {
1819
if (c.req.path.startsWith('/api') && c.req.path !== '/api/login') {
19-
const sid = c.req.cookie('sid');
20+
const sid = getCookie(c, 'sid');
2021
if (!sid) {
2122
return c.json({ code: -1, message: 'need login' }, 400);
2223
}

packages/document/main-doc/docs/en/guides/advanced-features/web-server.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ type MiddlewareContext = {
406406
Differences between Middleware `Context` and Hono `Context`:
407407
| UnstableMiddleware | Hono | Description |
408408
| :----------------------- | :---------------------------- | :--------------------------------------------------------------------------- |
409-
| `c.request.cookie` | `c.req.cookie()` | Refer to [Hono Cookie Helper](https://hono.dev/docs/helpers/cookie) documentation |
409+
| `c.request.cookie` | `getCookie()` | Refer to [Hono Cookie Helper](https://hono.dev/docs/helpers/cookie) documentation |
410410
| `c.request.pathname` | `c.req.path` | Refer to [HonoRequest path](https://hono.dev/docs/api/request#path) documentation |
411411
| `c.request.url` | - | Hono `c.req.url` provides the full request URL, calculate manually from URL |
412412
| `c.request.host` | `c.req.header('Host')` | Obtain host through header |

packages/document/main-doc/docs/zh/guides/advanced-features/bff/extend-server.mdx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@ Modern.js 支持用户通过 [Middleware](/guides/advanced-features/web-server.h
1414
import {
1515
type MiddlewareHandler,
1616
defineServerConfig,
17+
getCookie
1718
} from '@modern-js/server-runtime';
1819

1920
const requireAuthForApi: MiddlewareHandler = async (c, next) => {
2021
if (c.req.path.startsWith('/api') && c.req.path !== '/api/login') {
21-
const sid = c.req.cookie('sid');
22+
const sid = getCookie(c, 'sid');
2223
if (!sid) {
2324
return c.json({ code: -1, message: 'need login' }, 400);
2425
}

packages/document/main-doc/docs/zh/guides/advanced-features/web-server.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ Middleware `Context` 和 Hono `Context` 的具体差异:
400400

401401
| UnstableMiddleware | Hono | 说明 |
402402
| :------------------- | :--------------------- | :----------------------------------------------------------------------- |
403-
| `c.request.cookie` | `c.req.cookie()` | 参考 [Hono Cookie Helper](https://hono.dev/docs/helpers/cookie) 文档 |
403+
| `c.request.cookie` | `getCookie()` | 参考 [Hono Cookie Helper](https://hono.dev/docs/helpers/cookie) 文档 |
404404
| `c.request.pathname` | `c.req.path` | 参考 [HonoRequest path](https://hono.dev/docs/api/request#path) 文档 |
405405
| `c.request.url` | - | Hono `c.req.url` 为完整请求路径,自行通过 url 计算 |
406406
| `c.request.host` | `c.req.header('Host')` | 通过 header 获取 host |

packages/runtime/plugin-i18n/src/server/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const getLanguageFromPath = (
1313
urlPath: string,
1414
languages: string[],
1515
): string | null => {
16-
const url = new URL(req.url, `http://${req.headers.host}`);
16+
const url = new URL(req.url, `http://${req.header().host}`);
1717
const pathname = url.pathname;
1818

1919
// Remove urlPath prefix to get remaining path

packages/server/core/package.json

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@
3232
"jsnext:source": "./src/adapters/node/index.ts",
3333
"import": "./dist/esm-node/adapters/node/index.js",
3434
"default": "./dist/cjs/adapters/node/index.js"
35+
},
36+
"./hono": {
37+
"types": "./dist/types/hono.d.ts",
38+
"jsnext:source": "./src/hono.ts",
39+
"import": "./dist/esm-node/hono.js",
40+
"default": "./dist/cjs/hono.js"
3541
}
3642
},
3743
"typesVersions": {
@@ -41,6 +47,9 @@
4147
],
4248
"node": [
4349
"./dist/types/adapters/node/index.d.ts"
50+
],
51+
"hono": [
52+
"./dist/types/hono.d.ts"
4453
]
4554
}
4655
},
@@ -64,7 +73,7 @@
6473
"@web-std/stream": "^1.0.3",
6574
"cloneable-readable": "^3.0.0",
6675
"flatted": "^3.3.3",
67-
"hono": "^3.12.2",
76+
"hono": "^4.10.4",
6877
"ts-deepmerge": "7.0.3"
6978
},
7079
"devDependencies": {
@@ -95,6 +104,11 @@
95104
"types": "./dist/types/adapters/node/index.d.ts",
96105
"import": "./dist/esm-node/adapters/node/index.js",
97106
"default": "./dist/cjs/adapters/node/index.js"
107+
},
108+
"./hono": {
109+
"types": "./dist/types/hono.d.ts",
110+
"import": "./dist/esm-node/hono.js",
111+
"default": "./dist/cjs/hono.js"
98112
}
99113
}
100114
}

packages/server/core/src/adapters/node/hono.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ export const httpCallBack2HonoMid = (handler: Handler) => {
4242
}
4343

4444
if (isResFinalized(res)) {
45+
// Fix: ensure context.res is initialized before setting finalized
46+
// This prevents c.#res from being undefined in Hono
47+
const _ = context.res;
4548
context.finalized = true;
4649
} else {
4750
await next();
@@ -92,6 +95,9 @@ export const connectMockMid2HonoMid = (
9295
// The function lenth < 3 means the handler is not a function with next
9396
if (handler.length < 3) {
9497
res.once('finish', () => {
98+
// Fix: ensure context.res is initialized before setting finalized
99+
// This prevents c.#res from being undefined in Hono
100+
const _ = context.res;
95101
context.finalized = true;
96102
resolve();
97103
});

packages/server/core/src/hono.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// === Export Hono.js Core Types and APIs ===
2+
3+
// Core types from Hono
4+
export type {
5+
Context,
6+
Next,
7+
MiddlewareHandler,
8+
MiddlewareHandler as Middleware,
9+
HonoRequest,
10+
} from 'hono';
11+
12+
// Hono utilities
13+
export {
14+
setCookie,
15+
getCookie,
16+
deleteCookie,
17+
} from 'hono/cookie';

0 commit comments

Comments
 (0)