Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/flat-years-thank.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@modern-js/create-request': patch
'@modern-js/plugin-bff': patch
---

feat: hono bff supports return custom res
feat: hono bff 支持返回自定义响应
6 changes: 6 additions & 0 deletions packages/cli/plugin-bff/src/utils/createHonoRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -95,6 +98,9 @@ export const createHonoHandler = (handler: Handler) => {
}

if (typeof body !== 'undefined') {
if (body instanceof Response) {
return body;
}
return c.json(body);
}
} catch {
Expand Down
4 changes: 4 additions & 0 deletions packages/server/create-request/src/handleRes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ const handleRes = async (res: Response | NodeResponse) => {
return res.arrayBuffer();
}

if (contentType?.includes('image/png')) {
return res;
}

return res.text();
};

Expand Down
21 changes: 20 additions & 1 deletion tests/integration/bff-hono/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
useHonoContext,
} from '@modern-js/plugin-bff/hono';
import { z } from 'zod';

export default async () => {
return {
message: 'Hello Modern.js',
Expand Down Expand Up @@ -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',
},
});
});
30 changes: 18 additions & 12 deletions tests/integration/bff-hono/src/routes/base/page.tsx
Original file line number Diff line number Diff line change
@@ -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);
};
Expand All @@ -44,6 +41,7 @@ const Page = () => {
user: 'modern@email.com',
},
});
fetchImage();
}, []);

useEffect(() => {
Expand All @@ -65,6 +63,14 @@ const Page = () => {
<div>
<div className="hello">{message}</div>
<div className="username">{username}</div>
<img
src={imageUrl}
alt=""
className="captcha-img"
onError={e => {
console.error('error', e);
}}
/>
</div>
);
};
Expand Down
30 changes: 30 additions & 0 deletions tests/integration/bff-hono/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,21 @@ describe('bff hono in dev', () => {
expect(text).toBe('mock_image.png');
});

test('custom res', async () => {
await page.goto(`${host}:${port}/${BASE_PAGE}`);
await new Promise(resolve => setTimeout(resolve, 1000));

const imgElement = await page.$('.captcha-img');
expect(imgElement).not.toBeNull();

const isLoaded = await imgElement!.evaluate(
img =>
(img as HTMLImageElement).complete &&
(img as HTMLImageElement).naturalWidth > 0,
);
expect(isLoaded).toBe(true);
});

afterAll(async () => {
await killApp(app);
await page.close();
Expand Down Expand Up @@ -141,6 +156,21 @@ describe('bff hono in prod', () => {
expect(text).toBe('mock_image.png');
});

test('custom res', async () => {
await page.goto(`${host}:${port}/${BASE_PAGE}`);
await new Promise(resolve => setTimeout(resolve, 1000));

const imgElement = await page.$('.captcha-img');
expect(imgElement).not.toBeNull();

const isLoaded = await imgElement!.evaluate(
img =>
(img as HTMLImageElement).complete &&
(img as HTMLImageElement).naturalWidth > 0,
);
expect(isLoaded).toBe(true);
});

afterAll(async () => {
await killApp(app);
await page.close();
Expand Down