-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.js
28 lines (27 loc) · 906 Bytes
/
main.js
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
async function translate(text, from, to, options) {
const { config, utils } = options;
const { tauriFetch: fetch } = utils;
let { requestPath: url } = config;
let plain_text = text.replaceAll("/", "@@");
let encode_text = encodeURIComponent(plain_text);
if (url === undefined || url.length === 0) {
url = "lingva.pot-app.com"
}
if (!url.startsWith("http")) {
url = `https://${url}`;
}
const res = await fetch(`${url}/api/v1/${from}/${to}/${encode_text}`, {
method: 'GET',
});
if (res.ok) {
let result = res.data;
const { translation } = result;
if (translation) {
return translation.replaceAll("@@", "/");;
} else {
throw JSON.stringify(result.trim());
}
} else {
throw `Http Request Error\nHttp Status: ${res.status}\n${JSON.stringify(res.data)}`;
}
}