-
Notifications
You must be signed in to change notification settings - Fork 284
Expand file tree
/
Copy pathpluginMethodManager.js
More file actions
43 lines (38 loc) · 1.51 KB
/
pluginMethodManager.js
File metadata and controls
43 lines (38 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/**
* 调用本地 Go /proxy 接口
* @param {Object} options
* @param {string} options.method - HTTP 方法 GET/POST/PUT/DELETE
* @param {string} options.url - 目标 URL
* @param {Object} [options.headers] - 请求头
* @param {Object} [options.body] - 请求 JSON body
* @param {number} [options.timeout] - 毫秒级超时
* @param {string} [options.goHost] - Go 服务 host 默认 http://127.0.0.1:57571
* @returns {Promise<Object>} { status, headers, body }
*/
export async function reqProxy({
method = "GET",
url,
headers = {},
body,
timeout,
goHost = "http://127.0.0.1:57571",
}) {
if (!url) throw new Error("url is required");
const proxyUrl = new URL("/proxy", goHost);
const controller = new AbortController();
if (timeout) {
setTimeout(() => controller.abort(), timeout);
}
const resp = await fetch(proxyUrl.toString(), {
method: "POST", // 统一用 POST 调用 Go /proxy
headers: {"Content-Type": "application/json"},
body: JSON.stringify({method, url, headers, body, timeout}),
signal: controller.signal,
});
if (!resp.ok) {
const text = await resp.text();
throw new Error(`Go proxy request failed: ${resp.status} ${text}`);
}
const data = await resp.json();
return data;
}