diff --git a/.changeset/flat-years-thank.md b/.changeset/flat-years-thank.md new file mode 100644 index 000000000000..b1391656172a --- /dev/null +++ b/.changeset/flat-years-thank.md @@ -0,0 +1,7 @@ +--- +'@modern-js/create-request': patch +'@modern-js/plugin-bff': patch +--- + +feat: hono bff supports return custom res +feat: hono bff 支持返回自定义响应 diff --git a/packages/cli/plugin-bff/src/utils/createHonoRoutes.ts b/packages/cli/plugin-bff/src/utils/createHonoRoutes.ts index cdfc4440c331..70f2369e5b96 100644 --- a/packages/cli/plugin-bff/src/utils/createHonoRoutes.ts +++ b/packages/cli/plugin-bff/src/utils/createHonoRoutes.ts @@ -67,6 +67,9 @@ export const createHonoHandler = (handler: Handler) => { if (c.finalized) return; const result = await handler(input); + if (result instanceof Response) { + return result; + } return result && typeof result === 'object' ? c.json(result) : c.body(result); @@ -95,6 +98,9 @@ export const createHonoHandler = (handler: Handler) => { } if (typeof body !== 'undefined') { + if (body instanceof Response) { + return body; + } return c.json(body); } } catch { diff --git a/packages/server/create-request/src/handleRes.ts b/packages/server/create-request/src/handleRes.ts index c3286d64614a..ebba7ab45de2 100644 --- a/packages/server/create-request/src/handleRes.ts +++ b/packages/server/create-request/src/handleRes.ts @@ -36,6 +36,10 @@ const handleRes = async (res: Response | NodeResponse) => { return res.arrayBuffer(); } + if (contentType?.includes('image/png')) { + return res; + } + return res.text(); }; diff --git a/tests/integration/bff-hono/api/index.ts b/tests/integration/bff-hono/api/index.ts index 1e52c3109412..aa2052b52241 100644 --- a/tests/integration/bff-hono/api/index.ts +++ b/tests/integration/bff-hono/api/index.ts @@ -11,7 +11,6 @@ import { useHonoContext, } from '@modern-js/plugin-bff/hono'; import { z } from 'zod'; - export default async () => { return { message: 'Hello Modern.js', @@ -93,3 +92,23 @@ export const getHello = Api( }; }, ); + +export const getImage = Api(Get('/hello/image'), async () => { + const validBase64 = + 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=='; + const base64Data = validBase64.split(',')[1]; + const binaryString = atob(base64Data); + const uint8Array = new Uint8Array(binaryString.length); + + for (let i = 0; i < binaryString.length; i++) { + uint8Array[i] = binaryString.charCodeAt(i); + } + + return new Response(uint8Array.buffer, { + status: 200, + headers: { + 'Content-Type': 'image/png', + 'Cache-Control': 'no-store', + }, + }); +}); diff --git a/tests/integration/bff-hono/src/routes/base/page.tsx b/tests/integration/bff-hono/src/routes/base/page.tsx index 5ffb95079c1e..2ce8c15e016b 100644 --- a/tests/integration/bff-hono/src/routes/base/page.tsx +++ b/tests/integration/bff-hono/src/routes/base/page.tsx @@ -1,24 +1,21 @@ -import hello, { post, postHello, getHello } from '@api/index'; -import { configure } from '@modern-js/runtime/bff'; +import hello, { post, postHello, getHello, getImage } from '@api/index'; import { useEffect, useState } from 'react'; -configure({ - interceptor(request) { - return async (url, params) => { - const res = await request(url, params); - return res.json(); - }; - }, -}); - const Page = () => { const [message, setMessage] = useState('bff-hono'); const [username, setUsername] = useState('username'); + const [imageUrl, setImageUrl] = useState(''); + + const fetchImage = async () => { + const response = await getImage(); + const blob = await (response as any).blob(); + const url = URL.createObjectURL(blob); + setImageUrl(url); + }; useEffect(() => { const fetchData = async () => { const res = await hello(); - // 加一个延时,帮助集测取第一次的 message 值 await new Promise(resolve => setTimeout(resolve, 50)); setMessage(res.message); }; @@ -44,6 +41,7 @@ const Page = () => { user: 'modern@email.com', }, }); + fetchImage(); }, []); useEffect(() => { @@ -65,6 +63,14 @@ const Page = () => {