Files
kiss-translator/src/apis/index.js

193 lines
4.1 KiB
JavaScript
Raw Normal View History

2023-07-20 13:45:41 +08:00
import queryString from "query-string";
2023-08-05 18:15:01 +08:00
import { fetchPolyfill } from "../libs/fetch";
2023-07-20 13:45:41 +08:00
import {
OPT_TRANS_GOOGLE,
OPT_TRANS_MICROSOFT,
2023-09-02 16:57:09 +08:00
OPT_TRANS_DEEPL,
2023-10-20 17:44:48 +08:00
OPT_TRANS_DEEPLFREE,
2023-10-17 11:33:26 +08:00
OPT_TRANS_DEEPLX,
2023-10-20 17:44:48 +08:00
OPT_TRANS_BAIDU,
OPT_TRANS_TENCENT,
2023-07-20 13:45:41 +08:00
OPT_TRANS_OPENAI,
2023-10-26 11:13:50 +08:00
OPT_TRANS_CLOUDFLAREAI,
2023-09-06 00:25:46 +08:00
OPT_TRANS_CUSTOMIZE,
2023-10-20 17:44:48 +08:00
URL_CACHE_TRAN,
2023-08-20 23:30:08 +08:00
KV_SALT_SYNC,
2023-10-10 18:03:05 +08:00
URL_BAIDU_LANGDETECT,
OPT_LANGS_BAIDU,
URL_TENCENT_TRANSMART,
OPT_LANGS_TENCENT,
OPT_LANGS_SPECIAL,
2023-07-20 13:45:41 +08:00
} from "../config";
2023-08-20 23:30:08 +08:00
import { sha256 } from "../libs/utils";
2023-07-20 13:45:41 +08:00
2023-07-31 15:08:51 +08:00
/**
* 同步数据
* @param {*} url
* @param {*} key
* @param {*} data
* @returns
*/
2023-09-20 17:47:23 +08:00
export const apiSyncData = async (url, key, data) =>
2023-08-21 23:46:42 +08:00
fetchPolyfill(url, {
headers: {
"Content-type": "application/json",
Authorization: `Bearer ${await sha256(key, KV_SALT_SYNC)}`,
2023-07-31 15:08:51 +08:00
},
2023-08-21 23:46:42 +08:00
method: "POST",
body: JSON.stringify(data),
});
2023-07-31 15:08:51 +08:00
2023-08-30 18:05:37 +08:00
/**
2023-09-08 21:41:32 +08:00
* 下载数据
2023-08-30 18:05:37 +08:00
* @param {*} url
* @returns
*/
2023-09-20 17:47:23 +08:00
export const apiFetch = (url) => fetchPolyfill(url);
2023-08-30 18:05:37 +08:00
2023-07-20 13:45:41 +08:00
/**
* 百度语言识别
2023-07-31 03:10:09 +08:00
* @param {*} text
* @returns
2023-07-20 13:45:41 +08:00
*/
export const apiBaiduLangdetect = async (text) => {
const res = await fetchPolyfill(URL_BAIDU_LANGDETECT, {
headers: {
"Content-type": "application/json",
2023-07-20 13:45:41 +08:00
},
method: "POST",
body: JSON.stringify({
query: text,
}),
useCache: true,
2023-09-06 00:25:46 +08:00
});
2023-09-06 14:57:02 +08:00
if (res.error === 0) {
return OPT_LANGS_BAIDU.get(res.lan) ?? res.lan;
}
return "";
2023-09-06 00:25:46 +08:00
};
/**
* 腾讯语言识别
2023-09-06 00:25:46 +08:00
* @param {*} text
* @returns
*/
export const apiTencentLangdetect = async (text) => {
const body = JSON.stringify({
header: {
fn: "text_analysis",
2023-09-06 00:25:46 +08:00
},
text,
});
2023-07-20 13:45:41 +08:00
const res = await fetchPolyfill(URL_TENCENT_TRANSMART, {
2023-10-10 18:03:05 +08:00
headers: {
"Content-type": "application/json",
},
method: "POST",
body,
2023-10-10 18:03:05 +08:00
useCache: true,
});
return OPT_LANGS_TENCENT.get(res.language) ?? res.language;
2023-10-10 18:03:05 +08:00
};
2023-07-20 13:45:41 +08:00
/**
* 统一翻译接口
* @param {*} param0
* @returns
*/
2023-10-20 17:44:48 +08:00
export const apiTranslate = async ({
2023-08-30 18:05:37 +08:00
translator,
2023-09-06 14:57:02 +08:00
text,
2023-08-30 18:05:37 +08:00
fromLang,
toLang,
2023-10-20 17:44:48 +08:00
apiSetting = {},
useCache = true,
usePool = true,
2023-08-30 18:05:37 +08:00
}) => {
2023-10-20 17:44:48 +08:00
let trText = "";
let isSame = false;
2023-10-13 10:48:01 +08:00
const from =
OPT_LANGS_SPECIAL[translator].get(fromLang) ??
OPT_LANGS_SPECIAL[translator].get("auto");
const to = OPT_LANGS_SPECIAL[translator].get(toLang);
2023-10-24 17:58:37 +08:00
if (!text || !to) {
console.log(`[trans] target lang: ${toLang} not support`);
return [trText, isSame];
}
const cacheOpts = {
2023-10-20 17:44:48 +08:00
translator,
text,
fromLang,
toLang,
};
2023-10-13 10:48:01 +08:00
const transOpts = {
translator,
text,
from,
to,
};
2023-10-20 17:44:48 +08:00
const res = await fetchPolyfill(
`${URL_CACHE_TRAN}?${queryString.stringify(cacheOpts)}`,
2023-10-20 17:44:48 +08:00
{
useCache,
usePool,
transOpts,
apiSetting,
}
);
2023-07-20 13:45:41 +08:00
2023-09-06 14:57:02 +08:00
switch (translator) {
case OPT_TRANS_GOOGLE:
trText = res.sentences.map((item) => item.trans).join(" ");
isSame = to === res.src;
2023-10-20 17:44:48 +08:00
break;
2023-09-06 14:57:02 +08:00
case OPT_TRANS_MICROSOFT:
trText = res[0].translations.map((item) => item.text).join(" ");
isSame = text === trText;
2023-10-20 17:44:48 +08:00
break;
2023-09-06 14:57:02 +08:00
case OPT_TRANS_DEEPL:
trText = res.translations.map((item) => item.text).join(" ");
isSame = to === res.translations[0].detected_source_language;
2023-10-20 17:44:48 +08:00
break;
case OPT_TRANS_DEEPLFREE:
trText = res.result?.texts.map((item) => item.text).join(" ");
isSame = to === res.result?.lang;
2023-10-20 17:44:48 +08:00
break;
2023-10-17 11:33:26 +08:00
case OPT_TRANS_DEEPLX:
trText = res.data;
isSame = to === res.source_lang;
2023-10-20 17:44:48 +08:00
break;
case OPT_TRANS_BAIDU:
trText = res.trans_result?.data.map((item) => item.dst).join(" ");
isSame = res.trans_result?.to === res.trans_result?.from;
2023-10-20 17:44:48 +08:00
break;
case OPT_TRANS_TENCENT:
trText = res.auto_translation;
isSame = text === trText;
break;
2023-09-06 14:57:02 +08:00
case OPT_TRANS_OPENAI:
trText = res?.choices?.[0].message.content;
isSame = text === trText;
2023-10-20 17:44:48 +08:00
break;
2023-10-26 11:13:50 +08:00
case OPT_TRANS_CLOUDFLAREAI:
trText = res?.result?.translated_text;
isSame = text === trText;
break;
2023-09-06 14:57:02 +08:00
case OPT_TRANS_CUSTOMIZE:
trText = res.text;
isSame = to === res.from;
2023-10-20 17:44:48 +08:00
break;
2023-09-06 14:57:02 +08:00
default:
2023-07-20 13:45:41 +08:00
}
2023-10-20 17:44:48 +08:00
return [trText, isSame, res];
2023-07-20 13:45:41 +08:00
};