fix: rename data to body
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
import { DEFAULT_USER_AGENT } from "../config";
|
import { DEFAULT_USER_AGENT } from "../config";
|
||||||
|
|
||||||
export const genBaidu = ({ texts, from, to }) => {
|
export const genBaidu = ({ texts, from, to }) => {
|
||||||
const data = {
|
const body = {
|
||||||
from,
|
from,
|
||||||
to,
|
to,
|
||||||
query: texts.join(" "),
|
query: texts.join(" "),
|
||||||
@@ -15,5 +15,5 @@ export const genBaidu = ({ texts, from, to }) => {
|
|||||||
"User-Agent": DEFAULT_USER_AGENT,
|
"User-Agent": DEFAULT_USER_AGENT,
|
||||||
};
|
};
|
||||||
|
|
||||||
return { url, data, headers };
|
return { url, body, headers };
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ export const genDeeplFree = ({ texts, from, to }) => {
|
|||||||
|
|
||||||
const url = "https://www2.deepl.com/jsonrpc";
|
const url = "https://www2.deepl.com/jsonrpc";
|
||||||
|
|
||||||
const data = {
|
const body = {
|
||||||
jsonrpc: "2.0",
|
jsonrpc: "2.0",
|
||||||
method: "LMT_handle_texts",
|
method: "LMT_handle_texts",
|
||||||
params: {
|
params: {
|
||||||
@@ -46,5 +46,5 @@ export const genDeeplFree = ({ texts, from, to }) => {
|
|||||||
"x-app-version": "2.9.1",
|
"x-app-version": "2.9.1",
|
||||||
};
|
};
|
||||||
|
|
||||||
return { url, data, headers };
|
return { url, body, headers };
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -133,13 +133,13 @@ const genGoogle = ({ texts, from, to, url, key }) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const genGoogle2 = ({ 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 = {
|
const headers = {
|
||||||
"Content-Type": "application/json+protobuf",
|
"Content-Type": "application/json+protobuf",
|
||||||
"X-Goog-API-Key": key,
|
"X-Goog-API-Key": key,
|
||||||
};
|
};
|
||||||
|
|
||||||
return { url, data, headers };
|
return { url, body, headers };
|
||||||
};
|
};
|
||||||
|
|
||||||
const genMicrosoft = ({ texts, from, to, token }) => {
|
const genMicrosoft = ({ texts, from, to, token }) => {
|
||||||
@@ -153,13 +153,13 @@ const genMicrosoft = ({ texts, from, to, token }) => {
|
|||||||
"Content-type": "application/json",
|
"Content-type": "application/json",
|
||||||
Authorization: `Bearer ${token}`,
|
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 genDeepl = ({ texts, from, to, url, key }) => {
|
||||||
const data = {
|
const body = {
|
||||||
text: texts,
|
text: texts,
|
||||||
target_lang: to,
|
target_lang: to,
|
||||||
source_lang: from,
|
source_lang: from,
|
||||||
@@ -170,11 +170,11 @@ const genDeepl = ({ texts, from, to, url, key }) => {
|
|||||||
Authorization: `DeepL-Auth-Key ${key}`,
|
Authorization: `DeepL-Auth-Key ${key}`,
|
||||||
};
|
};
|
||||||
|
|
||||||
return { url, data, headers };
|
return { url, body, headers };
|
||||||
};
|
};
|
||||||
|
|
||||||
const genDeeplX = ({ texts, from, to, url, key }) => {
|
const genDeeplX = ({ texts, from, to, url, key }) => {
|
||||||
const data = {
|
const body = {
|
||||||
text: texts.join(" "),
|
text: texts.join(" "),
|
||||||
target_lang: to,
|
target_lang: to,
|
||||||
source_lang: from,
|
source_lang: from,
|
||||||
@@ -187,11 +187,11 @@ const genDeeplX = ({ texts, from, to, url, key }) => {
|
|||||||
headers.Authorization = `Bearer ${key}`;
|
headers.Authorization = `Bearer ${key}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
return { url, data, headers };
|
return { url, body, headers };
|
||||||
};
|
};
|
||||||
|
|
||||||
const genNiuTrans = ({ texts, from, to, url, key, dictNo, memoryNo }) => {
|
const genNiuTrans = ({ texts, from, to, url, key, dictNo, memoryNo }) => {
|
||||||
const data = {
|
const body = {
|
||||||
from,
|
from,
|
||||||
to,
|
to,
|
||||||
apikey: key,
|
apikey: key,
|
||||||
@@ -204,11 +204,11 @@ const genNiuTrans = ({ texts, from, to, url, key, dictNo, memoryNo }) => {
|
|||||||
"Content-type": "application/json",
|
"Content-type": "application/json",
|
||||||
};
|
};
|
||||||
|
|
||||||
return { url, data, headers };
|
return { url, body, headers };
|
||||||
};
|
};
|
||||||
|
|
||||||
const genTencent = ({ texts, from, to }) => {
|
const genTencent = ({ texts, from, to }) => {
|
||||||
const data = {
|
const body = {
|
||||||
header: {
|
header: {
|
||||||
fn: "auto_translation",
|
fn: "auto_translation",
|
||||||
client_key:
|
client_key:
|
||||||
@@ -232,11 +232,11 @@ const genTencent = ({ texts, from, to }) => {
|
|||||||
referer: "https://transmart.qq.com/zh-CN/index",
|
referer: "https://transmart.qq.com/zh-CN/index",
|
||||||
};
|
};
|
||||||
|
|
||||||
return { url, data, headers };
|
return { url, body, headers };
|
||||||
};
|
};
|
||||||
|
|
||||||
const genVolcengine = ({ texts, from, to }) => {
|
const genVolcengine = ({ texts, from, to }) => {
|
||||||
const data = {
|
const body = {
|
||||||
source_language: from,
|
source_language: from,
|
||||||
target_language: to,
|
target_language: to,
|
||||||
text: texts.join(" "),
|
text: texts.join(" "),
|
||||||
@@ -247,7 +247,7 @@ const genVolcengine = ({ texts, from, to }) => {
|
|||||||
"Content-type": "application/json",
|
"Content-type": "application/json",
|
||||||
};
|
};
|
||||||
|
|
||||||
return { url, data, headers };
|
return { url, body, headers };
|
||||||
};
|
};
|
||||||
|
|
||||||
const genOpenAI = ({
|
const genOpenAI = ({
|
||||||
@@ -264,7 +264,7 @@ const genOpenAI = ({
|
|||||||
role: "user",
|
role: "user",
|
||||||
content: userPrompt,
|
content: userPrompt,
|
||||||
};
|
};
|
||||||
const data = {
|
const body = {
|
||||||
model,
|
model,
|
||||||
messages: [
|
messages: [
|
||||||
{
|
{
|
||||||
@@ -284,7 +284,7 @@ const genOpenAI = ({
|
|||||||
// "api-key": key, // Azure OpenAI
|
// "api-key": key, // Azure OpenAI
|
||||||
};
|
};
|
||||||
|
|
||||||
return { url, data, headers, userMsg };
|
return { url, body, headers, userMsg };
|
||||||
};
|
};
|
||||||
|
|
||||||
const genGemini = ({
|
const genGemini = ({
|
||||||
@@ -302,7 +302,7 @@ const genGemini = ({
|
|||||||
.replaceAll(INPUT_PLACE_KEY, key);
|
.replaceAll(INPUT_PLACE_KEY, key);
|
||||||
|
|
||||||
const userMsg = { role: "user", parts: [{ text: userPrompt }] };
|
const userMsg = { role: "user", parts: [{ text: userPrompt }] };
|
||||||
const data = {
|
const body = {
|
||||||
system_instruction: {
|
system_instruction: {
|
||||||
parts: {
|
parts: {
|
||||||
text: systemPrompt,
|
text: systemPrompt,
|
||||||
@@ -341,7 +341,7 @@ const genGemini = ({
|
|||||||
"Content-type": "application/json",
|
"Content-type": "application/json",
|
||||||
};
|
};
|
||||||
|
|
||||||
return { url, data, headers, userMsg };
|
return { url, body, headers, userMsg };
|
||||||
};
|
};
|
||||||
|
|
||||||
const genGemini2 = ({
|
const genGemini2 = ({
|
||||||
@@ -358,7 +358,7 @@ const genGemini2 = ({
|
|||||||
role: "user",
|
role: "user",
|
||||||
content: userPrompt,
|
content: userPrompt,
|
||||||
};
|
};
|
||||||
const data = {
|
const body = {
|
||||||
model,
|
model,
|
||||||
messages: [
|
messages: [
|
||||||
{
|
{
|
||||||
@@ -377,7 +377,7 @@ const genGemini2 = ({
|
|||||||
Authorization: `Bearer ${key}`,
|
Authorization: `Bearer ${key}`,
|
||||||
};
|
};
|
||||||
|
|
||||||
return { url, data, headers, userMsg };
|
return { url, body, headers, userMsg };
|
||||||
};
|
};
|
||||||
|
|
||||||
const genClaude = ({
|
const genClaude = ({
|
||||||
@@ -394,7 +394,7 @@ const genClaude = ({
|
|||||||
role: "user",
|
role: "user",
|
||||||
content: userPrompt,
|
content: userPrompt,
|
||||||
};
|
};
|
||||||
const data = {
|
const body = {
|
||||||
model,
|
model,
|
||||||
system: systemPrompt,
|
system: systemPrompt,
|
||||||
messages: [...hisMsgs, userMsg],
|
messages: [...hisMsgs, userMsg],
|
||||||
@@ -409,7 +409,7 @@ const genClaude = ({
|
|||||||
"x-api-key": key,
|
"x-api-key": key,
|
||||||
};
|
};
|
||||||
|
|
||||||
return { url, data, headers, userMsg };
|
return { url, body, headers, userMsg };
|
||||||
};
|
};
|
||||||
|
|
||||||
const genOpenRouter = ({
|
const genOpenRouter = ({
|
||||||
@@ -426,7 +426,7 @@ const genOpenRouter = ({
|
|||||||
role: "user",
|
role: "user",
|
||||||
content: userPrompt,
|
content: userPrompt,
|
||||||
};
|
};
|
||||||
const data = {
|
const body = {
|
||||||
model,
|
model,
|
||||||
messages: [
|
messages: [
|
||||||
{
|
{
|
||||||
@@ -445,7 +445,7 @@ const genOpenRouter = ({
|
|||||||
Authorization: `Bearer ${key}`,
|
Authorization: `Bearer ${key}`,
|
||||||
};
|
};
|
||||||
|
|
||||||
return { url, data, headers, userMsg };
|
return { url, body, headers, userMsg };
|
||||||
};
|
};
|
||||||
|
|
||||||
const genOllama = ({
|
const genOllama = ({
|
||||||
@@ -463,7 +463,7 @@ const genOllama = ({
|
|||||||
role: "user",
|
role: "user",
|
||||||
content: userPrompt,
|
content: userPrompt,
|
||||||
};
|
};
|
||||||
const data = {
|
const body = {
|
||||||
model,
|
model,
|
||||||
messages: [
|
messages: [
|
||||||
{
|
{
|
||||||
@@ -486,11 +486,11 @@ const genOllama = ({
|
|||||||
headers.Authorization = `Bearer ${key}`;
|
headers.Authorization = `Bearer ${key}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
return { url, data, headers, userMsg };
|
return { url, body, headers, userMsg };
|
||||||
};
|
};
|
||||||
|
|
||||||
const genCloudflareAI = ({ texts, from, to, url, key }) => {
|
const genCloudflareAI = ({ texts, from, to, url, key }) => {
|
||||||
const data = {
|
const body = {
|
||||||
text: texts.join(" "),
|
text: texts.join(" "),
|
||||||
source_lang: from,
|
source_lang: from,
|
||||||
target_lang: to,
|
target_lang: to,
|
||||||
@@ -501,17 +501,17 @@ const genCloudflareAI = ({ texts, from, to, url, key }) => {
|
|||||||
Authorization: `Bearer ${key}`,
|
Authorization: `Bearer ${key}`,
|
||||||
};
|
};
|
||||||
|
|
||||||
return { url, data, headers };
|
return { url, body, headers };
|
||||||
};
|
};
|
||||||
|
|
||||||
const genCustom = ({ texts, from, to, url, key }) => {
|
const genCustom = ({ texts, from, to, url, key }) => {
|
||||||
const data = { texts, from, to };
|
const body = { texts, from, to };
|
||||||
const headers = {
|
const headers = {
|
||||||
"Content-type": "application/json",
|
"Content-type": "application/json",
|
||||||
Authorization: `Bearer ${key}`,
|
Authorization: `Bearer ${key}`,
|
||||||
};
|
};
|
||||||
|
|
||||||
return { url, data, headers };
|
return { url, body, headers };
|
||||||
};
|
};
|
||||||
|
|
||||||
const genReqFuncs = {
|
const genReqFuncs = {
|
||||||
@@ -537,7 +537,7 @@ const genReqFuncs = {
|
|||||||
|
|
||||||
const genInit = ({
|
const genInit = ({
|
||||||
url = "",
|
url = "",
|
||||||
data = null,
|
body = null,
|
||||||
headers = {},
|
headers = {},
|
||||||
userMsg = null,
|
userMsg = null,
|
||||||
method = "POST",
|
method = "POST",
|
||||||
@@ -550,18 +550,18 @@ const genInit = ({
|
|||||||
method,
|
method,
|
||||||
headers,
|
headers,
|
||||||
};
|
};
|
||||||
if (method !== "GET" && method !== "HEAD" && data) {
|
if (method !== "GET" && method !== "HEAD" && body) {
|
||||||
let body = JSON.stringify(data);
|
let payload = JSON.stringify(body);
|
||||||
const id = data?.params?.id;
|
const id = body?.params?.id;
|
||||||
if (id) {
|
if (id) {
|
||||||
body = body.replace(
|
payload = payload.replace(
|
||||||
'method":"',
|
'method":"',
|
||||||
(id + 3) % 13 === 0 || (id + 5) % 29 === 0
|
(id + 3) % 13 === 0 || (id + 5) % 29 === 0
|
||||||
? 'method" : "'
|
? 'method" : "'
|
||||||
: 'method": "'
|
: 'method": "'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
Object.assign(init, { body });
|
Object.assign(init, { body: payload });
|
||||||
}
|
}
|
||||||
|
|
||||||
return [url, init, userMsg];
|
return [url, init, userMsg];
|
||||||
@@ -610,7 +610,7 @@ export const genTransReq = async ({ reqHook, resHook, ...args }) => {
|
|||||||
|
|
||||||
const {
|
const {
|
||||||
url = "",
|
url = "",
|
||||||
data = null,
|
body = null,
|
||||||
headers = {},
|
headers = {},
|
||||||
userMsg = null,
|
userMsg = null,
|
||||||
method = "POST",
|
method = "POST",
|
||||||
@@ -621,7 +621,7 @@ export const genTransReq = async ({ reqHook, resHook, ...args }) => {
|
|||||||
Object.assign(headers, parseJsonObj(customHeader));
|
Object.assign(headers, parseJsonObj(customHeader));
|
||||||
}
|
}
|
||||||
if (customBody?.trim()) {
|
if (customBody?.trim()) {
|
||||||
Object.assign(data, parseJsonObj(customBody));
|
Object.assign(body, parseJsonObj(customBody));
|
||||||
}
|
}
|
||||||
|
|
||||||
// 执行 request hook
|
// 执行 request hook
|
||||||
@@ -630,7 +630,7 @@ export const genTransReq = async ({ reqHook, resHook, ...args }) => {
|
|||||||
interpreter.run(`exports.reqHook = ${reqHook}`);
|
interpreter.run(`exports.reqHook = ${reqHook}`);
|
||||||
const hookResult = await interpreter.exports.reqHook(args, {
|
const hookResult = await interpreter.exports.reqHook(args, {
|
||||||
url,
|
url,
|
||||||
data,
|
body,
|
||||||
headers,
|
headers,
|
||||||
userMsg,
|
userMsg,
|
||||||
method,
|
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 });
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -354,9 +354,9 @@ Output: {"translations":[{"id":1,"text":"一个<b>React</b>组件","sourceLangua
|
|||||||
|
|
||||||
Fail-safe: On any error, return {"translations":[]}.`;
|
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);
|
console.log("request hook args:", args);
|
||||||
// return { url, data, headers, userMsg, method };
|
// return { url, body, headers, userMsg, method };
|
||||||
}`;
|
}`;
|
||||||
|
|
||||||
const defaultResponseHook = `async ({ res, ...args }) => {
|
const defaultResponseHook = `async ({ res, ...args }) => {
|
||||||
|
|||||||
@@ -137,23 +137,23 @@ ${customApiLangs}
|
|||||||
`;
|
`;
|
||||||
|
|
||||||
const requestHookHelperZH = `1、第一个参数包含如下字段:'texts', 'from', 'to', 'url', 'key', 'model', 'systemPrompt', ...
|
const requestHookHelperZH = `1、第一个参数包含如下字段:'texts', 'from', 'to', 'url', 'key', 'model', 'systemPrompt', ...
|
||||||
2、返回值必须是包含以下字段的对象: 'url', 'data', 'headers', 'userMsg', 'method'
|
2、返回值必须是包含以下字段的对象: 'url', 'body', 'headers', 'userMsg', 'method'
|
||||||
3、如返回空值,则hook函数不会产生任何效果。
|
3、如返回空值,则hook函数不会产生任何效果。
|
||||||
|
|
||||||
// 示例
|
// 示例
|
||||||
async (args, { url, data, headers, userMsg, method } = {}) => {
|
async (args, { url, body, headers, userMsg, method } = {}) => {
|
||||||
console.log("request hook args:", args);
|
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', ...
|
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.
|
3. If a null value is returned, the hook function will have no effect.
|
||||||
|
|
||||||
// Example
|
// Example
|
||||||
async (args, { url, data, headers, userMsg, method } = {}) => {
|
async (args, { url, body, headers, userMsg, method } = {}) => {
|
||||||
console.log("request hook args:", args);
|
console.log("request hook args:", args);
|
||||||
return { url, data, headers, userMsg, method };
|
return { url, body, headers, userMsg, method };
|
||||||
}`;
|
}`;
|
||||||
|
|
||||||
const responsetHookHelperZH = `1、第一个参数包含如下字段:'res', ...
|
const responsetHookHelperZH = `1、第一个参数包含如下字段:'res', ...
|
||||||
|
|||||||
Reference in New Issue
Block a user