|
1 | 1 | import { Hono } from "hono/tiny"
|
2 | 2 | import {
|
3 |
| - createErrorResponse, |
4 |
| - PluginErrorType, |
5 |
| - PluginRequestPayload |
| 3 | + createErrorResponse, |
| 4 | + PluginErrorType, |
| 5 | + PluginRequestPayload |
6 | 6 | } from "@lobehub/chat-plugin-sdk"
|
7 | 7 |
|
8 | 8 | export const apiGateway = new Hono<{ Bindings: Bindings }>()
|
9 | 9 | // @ts-expect-error No overload matches this call
|
10 |
| - .post("/", async (c) => { |
11 |
| - const data = await c.req.json() as PluginRequestPayload |
12 |
| - const u = new URL(c.req.url) |
13 |
| - const apiUrl = data.manifest?.api.find((a) => a.name === data.apiName)?.url |
14 |
| - if (!apiUrl) { |
15 |
| - return createErrorResponse(PluginErrorType.PluginApiNotFound, { |
16 |
| - message: `API not found for ${data.apiName}`, |
17 |
| - }) |
18 |
| - } |
19 |
| - const au = new URL(apiUrl) |
20 |
| - if (au.host !== u.host) { |
21 |
| - return createErrorResponse(PluginErrorType.Forbidden, { |
22 |
| - message: `API URL ${apiUrl} is not allowed for security reasons`, |
23 |
| - }) |
24 |
| - } |
25 |
| - const headers: Record<string, string> = {} |
26 |
| - c.req.raw.headers.forEach((v, k) => { |
27 |
| - if (k.toLowerCase().startsWith("x-lobe")) { |
28 |
| - headers[k] = v |
29 |
| - } |
30 |
| - }) |
31 |
| - return await c.env.MYSELF.fetch(apiUrl, { |
32 |
| - method: "POST", |
33 |
| - headers: { |
34 |
| - "Content-Type": "application/json", |
35 |
| - ...headers, |
36 |
| - }, |
37 |
| - body: data.arguments, |
38 |
| - }) |
39 |
| - }) |
| 10 | + .post("/", async (c) => { |
| 11 | + const data = await c.req.json() as PluginRequestPayload |
| 12 | + const u = new URL(c.req.url) |
| 13 | + const apiUrl = data.manifest?.api.find((a) => a.name === data.apiName)?.url |
| 14 | + if (!apiUrl) { |
| 15 | + return createErrorResponse(PluginErrorType.PluginApiNotFound, { |
| 16 | + message: `API not found for ${data.apiName}`, |
| 17 | + }) |
| 18 | + } |
| 19 | + const au = new URL(apiUrl) |
| 20 | + if (au.host !== u.host) { |
| 21 | + return createErrorResponse(PluginErrorType.Forbidden, { |
| 22 | + message: `API URL ${apiUrl} is not allowed for security reasons`, |
| 23 | + }) |
| 24 | + } |
| 25 | + const headers: Record<string, string> = {} |
| 26 | + c.req.raw.headers.forEach((v, k) => { |
| 27 | + if (k.toLowerCase().startsWith("x-lobe")) { |
| 28 | + headers[k] = v |
| 29 | + } |
| 30 | + }) |
| 31 | + return await c.env.MYSELF.fetch(apiUrl, { |
| 32 | + method: "POST", |
| 33 | + headers: { |
| 34 | + "Content-Type": "application/json", |
| 35 | + ...headers, |
| 36 | + }, |
| 37 | + body: data.arguments, |
| 38 | + }) |
| 39 | + }) |
40 | 40 |
|
0 commit comments