32 lines
1.1 KiB
TypeScript
32 lines
1.1 KiB
TypeScript
/**
|
||
* Jev 代理路由:POST /api/jev
|
||
* 透传 systemone 请求到 https://api.typesafe.ai/v1/systemone,
|
||
* key 从服务端环境变量 TYPESAFE_API_KEY 读取(不下发到前端)。
|
||
* 上游状态码(401/422/429/529 等)与响应体原样透传回前端。
|
||
* key 永不写入日志或响应。
|
||
*/
|
||
import { Hono } from 'hono';
|
||
|
||
const UPSTREAM_URL = 'https://api.typesafe.ai/v1/systemone';
|
||
|
||
export const jevRoute = new Hono().post('/', async (c) => {
|
||
const apiKey = process.env.TYPESAFE_API_KEY;
|
||
if (!apiKey) {
|
||
return c.json({ error: 'server 未配置 TYPESAFE_API_KEY(见 server/README.md)' }, 503);
|
||
}
|
||
const body = await c.req.text(); // 请求体原样透传,不解析不修改
|
||
const upstream = await fetch(UPSTREAM_URL, {
|
||
method: 'POST',
|
||
headers: {
|
||
'Content-Type': 'application/json',
|
||
Authorization: `Bearer ${apiKey}`,
|
||
},
|
||
body,
|
||
});
|
||
const text = await upstream.text();
|
||
return new Response(text, {
|
||
status: upstream.status,
|
||
headers: { 'Content-Type': upstream.headers.get('content-type') ?? 'application/json' },
|
||
});
|
||
});
|