From df8c96569ae4c911953e6344e9ba1b5c396894f6 Mon Sep 17 00:00:00 2001 From: Gabe Date: Sun, 5 Oct 2025 17:47:29 +0800 Subject: [PATCH] fix: rename data to body --- src/apis/baidu.js | 4 +-- src/apis/deepl.js | 4 +-- src/apis/trans.js | 80 +++++++++++++++++++++++----------------------- src/config/api.js | 4 +-- src/config/i18n.js | 12 +++---- 5 files changed, 52 insertions(+), 52 deletions(-) diff --git a/src/apis/baidu.js b/src/apis/baidu.js index d1d735e..5505b19 100644 --- a/src/apis/baidu.js +++ b/src/apis/baidu.js @@ -1,7 +1,7 @@ import { DEFAULT_USER_AGENT } from "../config"; export const genBaidu = ({ texts, from, to }) => { - const data = { + const body = { from, to, query: texts.join(" "), @@ -15,5 +15,5 @@ export const genBaidu = ({ texts, from, to }) => { "User-Agent": DEFAULT_USER_AGENT, }; - return { url, data, headers }; + return { url, body, headers }; }; diff --git a/src/apis/deepl.js b/src/apis/deepl.js index 837355d..f54a931 100644 --- a/src/apis/deepl.js +++ b/src/apis/deepl.js @@ -9,7 +9,7 @@ export const genDeeplFree = ({ texts, from, to }) => { const url = "https://www2.deepl.com/jsonrpc"; - const data = { + const body = { jsonrpc: "2.0", method: "LMT_handle_texts", params: { @@ -46,5 +46,5 @@ export const genDeeplFree = ({ texts, from, to }) => { "x-app-version": "2.9.1", }; - return { url, data, headers }; + return { url, body, headers }; }; diff --git a/src/apis/trans.js b/src/apis/trans.js index c2b4189..33fa614 100644 --- a/src/apis/trans.js +++ b/src/apis/trans.js @@ -133,13 +133,13 @@ const genGoogle = ({ texts, from, to, url, key }) => { }; const genGoogle2 = ({ texts, from, to, url, key }) => { - const data = [[texts, from, to], "wt_lib"]; + const body = [[texts, from, to], "wt_lib"]; const headers = { "Content-Type": "application/json+protobuf", "X-Goog-API-Key": key, }; - return { url, data, headers }; + return { url, body, headers }; }; const genMicrosoft = ({ texts, from, to, token }) => { @@ -153,13 +153,13 @@ const genMicrosoft = ({ texts, from, to, token }) => { "Content-type": "application/json", Authorization: `Bearer ${token}`, }; - const data = texts.map((text) => ({ Text: text })); + const body = texts.map((text) => ({ Text: text })); - return { url, data, headers }; + return { url, body, headers }; }; const genDeepl = ({ texts, from, to, url, key }) => { - const data = { + const body = { text: texts, target_lang: to, source_lang: from, @@ -170,11 +170,11 @@ const genDeepl = ({ texts, from, to, url, key }) => { Authorization: `DeepL-Auth-Key ${key}`, }; - return { url, data, headers }; + return { url, body, headers }; }; const genDeeplX = ({ texts, from, to, url, key }) => { - const data = { + const body = { text: texts.join(" "), target_lang: to, source_lang: from, @@ -187,11 +187,11 @@ const genDeeplX = ({ texts, from, to, url, key }) => { headers.Authorization = `Bearer ${key}`; } - return { url, data, headers }; + return { url, body, headers }; }; const genNiuTrans = ({ texts, from, to, url, key, dictNo, memoryNo }) => { - const data = { + const body = { from, to, apikey: key, @@ -204,11 +204,11 @@ const genNiuTrans = ({ texts, from, to, url, key, dictNo, memoryNo }) => { "Content-type": "application/json", }; - return { url, data, headers }; + return { url, body, headers }; }; const genTencent = ({ texts, from, to }) => { - const data = { + const body = { header: { fn: "auto_translation", client_key: @@ -232,11 +232,11 @@ const genTencent = ({ texts, from, to }) => { referer: "https://transmart.qq.com/zh-CN/index", }; - return { url, data, headers }; + return { url, body, headers }; }; const genVolcengine = ({ texts, from, to }) => { - const data = { + const body = { source_language: from, target_language: to, text: texts.join(" "), @@ -247,7 +247,7 @@ const genVolcengine = ({ texts, from, to }) => { "Content-type": "application/json", }; - return { url, data, headers }; + return { url, body, headers }; }; const genOpenAI = ({ @@ -264,7 +264,7 @@ const genOpenAI = ({ role: "user", content: userPrompt, }; - const data = { + const body = { model, messages: [ { @@ -284,7 +284,7 @@ const genOpenAI = ({ // "api-key": key, // Azure OpenAI }; - return { url, data, headers, userMsg }; + return { url, body, headers, userMsg }; }; const genGemini = ({ @@ -302,7 +302,7 @@ const genGemini = ({ .replaceAll(INPUT_PLACE_KEY, key); const userMsg = { role: "user", parts: [{ text: userPrompt }] }; - const data = { + const body = { system_instruction: { parts: { text: systemPrompt, @@ -341,7 +341,7 @@ const genGemini = ({ "Content-type": "application/json", }; - return { url, data, headers, userMsg }; + return { url, body, headers, userMsg }; }; const genGemini2 = ({ @@ -358,7 +358,7 @@ const genGemini2 = ({ role: "user", content: userPrompt, }; - const data = { + const body = { model, messages: [ { @@ -377,7 +377,7 @@ const genGemini2 = ({ Authorization: `Bearer ${key}`, }; - return { url, data, headers, userMsg }; + return { url, body, headers, userMsg }; }; const genClaude = ({ @@ -394,7 +394,7 @@ const genClaude = ({ role: "user", content: userPrompt, }; - const data = { + const body = { model, system: systemPrompt, messages: [...hisMsgs, userMsg], @@ -409,7 +409,7 @@ const genClaude = ({ "x-api-key": key, }; - return { url, data, headers, userMsg }; + return { url, body, headers, userMsg }; }; const genOpenRouter = ({ @@ -426,7 +426,7 @@ const genOpenRouter = ({ role: "user", content: userPrompt, }; - const data = { + const body = { model, messages: [ { @@ -445,7 +445,7 @@ const genOpenRouter = ({ Authorization: `Bearer ${key}`, }; - return { url, data, headers, userMsg }; + return { url, body, headers, userMsg }; }; const genOllama = ({ @@ -463,7 +463,7 @@ const genOllama = ({ role: "user", content: userPrompt, }; - const data = { + const body = { model, messages: [ { @@ -486,11 +486,11 @@ const genOllama = ({ headers.Authorization = `Bearer ${key}`; } - return { url, data, headers, userMsg }; + return { url, body, headers, userMsg }; }; const genCloudflareAI = ({ texts, from, to, url, key }) => { - const data = { + const body = { text: texts.join(" "), source_lang: from, target_lang: to, @@ -501,17 +501,17 @@ const genCloudflareAI = ({ texts, from, to, url, key }) => { Authorization: `Bearer ${key}`, }; - return { url, data, headers }; + return { url, body, headers }; }; const genCustom = ({ texts, from, to, url, key }) => { - const data = { texts, from, to }; + const body = { texts, from, to }; const headers = { "Content-type": "application/json", Authorization: `Bearer ${key}`, }; - return { url, data, headers }; + return { url, body, headers }; }; const genReqFuncs = { @@ -537,7 +537,7 @@ const genReqFuncs = { const genInit = ({ url = "", - data = null, + body = null, headers = {}, userMsg = null, method = "POST", @@ -550,18 +550,18 @@ const genInit = ({ method, headers, }; - if (method !== "GET" && method !== "HEAD" && data) { - let body = JSON.stringify(data); - const id = data?.params?.id; + if (method !== "GET" && method !== "HEAD" && body) { + let payload = JSON.stringify(body); + const id = body?.params?.id; if (id) { - body = body.replace( + payload = payload.replace( 'method":"', (id + 3) % 13 === 0 || (id + 5) % 29 === 0 ? 'method" : "' : 'method": "' ); } - Object.assign(init, { body }); + Object.assign(init, { body: payload }); } return [url, init, userMsg]; @@ -610,7 +610,7 @@ export const genTransReq = async ({ reqHook, resHook, ...args }) => { const { url = "", - data = null, + body = null, headers = {}, userMsg = null, method = "POST", @@ -621,7 +621,7 @@ export const genTransReq = async ({ reqHook, resHook, ...args }) => { Object.assign(headers, parseJsonObj(customHeader)); } if (customBody?.trim()) { - Object.assign(data, parseJsonObj(customBody)); + Object.assign(body, parseJsonObj(customBody)); } // 执行 request hook @@ -630,7 +630,7 @@ export const genTransReq = async ({ reqHook, resHook, ...args }) => { interpreter.run(`exports.reqHook = ${reqHook}`); const hookResult = await interpreter.exports.reqHook(args, { url, - data, + body, headers, userMsg, method, @@ -643,7 +643,7 @@ export const genTransReq = async ({ reqHook, resHook, ...args }) => { } } - return genInit({ url, data, headers, userMsg, method }); + return genInit({ url, body, headers, userMsg, method }); }; /** diff --git a/src/config/api.js b/src/config/api.js index 71d3c9e..992fc7b 100644 --- a/src/config/api.js +++ b/src/config/api.js @@ -354,9 +354,9 @@ Output: {"translations":[{"id":1,"text":"一个React组件","sourceLangua Fail-safe: On any error, return {"translations":[]}.`; -const defaultRequestHook = `async (args, { url, data, headers, userMsg, method } = {}) => { +const defaultRequestHook = `async (args, { url, body, headers, userMsg, method } = {}) => { console.log("request hook args:", args); - // return { url, data, headers, userMsg, method }; + // return { url, body, headers, userMsg, method }; }`; const defaultResponseHook = `async ({ res, ...args }) => { diff --git a/src/config/i18n.js b/src/config/i18n.js index ad001f2..083c71d 100644 --- a/src/config/i18n.js +++ b/src/config/i18n.js @@ -137,23 +137,23 @@ ${customApiLangs} `; const requestHookHelperZH = `1、第一个参数包含如下字段:'texts', 'from', 'to', 'url', 'key', 'model', 'systemPrompt', ... -2、返回值必须是包含以下字段的对象: 'url', 'data', 'headers', 'userMsg', 'method' +2、返回值必须是包含以下字段的对象: 'url', 'body', 'headers', 'userMsg', 'method' 3、如返回空值,则hook函数不会产生任何效果。 // 示例 -async (args, { url, data, headers, userMsg, method } = {}) => { +async (args, { url, body, headers, userMsg, method } = {}) => { console.log("request hook args:", args); - return { url, data, headers, userMsg, method }; + return { url, body, headers, userMsg, method }; }`; const requestHookHelperEN = `1. The first parameter contains the following fields: 'texts', 'from', 'to', 'url', 'key', 'model', 'systemPrompt', ... -2. The return value must be an object containing the following fields: 'url', 'data', 'headers', 'userMsg', 'method' +2. The return value must be an object containing the following fields: 'url', 'body', 'headers', 'userMsg', 'method' 3. If a null value is returned, the hook function will have no effect. // Example -async (args, { url, data, headers, userMsg, method } = {}) => { +async (args, { url, body, headers, userMsg, method } = {}) => { console.log("request hook args:", args); - return { url, data, headers, userMsg, method }; + return { url, body, headers, userMsg, method }; }`; const responsetHookHelperZH = `1、第一个参数包含如下字段:'res', ...