translate is working

This commit is contained in:
Andy L WSL 2026-02-28 10:22:16 +00:00
parent 974be37b3b
commit f37e215c22
7 changed files with 61 additions and 39 deletions

View File

@ -4,7 +4,7 @@
* @license Apache-2.0
*/
import OperationConfig from "./config/OperationConfig.json" assert {type: "json"};
import OperationConfig from "./config/OperationConfig.json" with {type: "json"};
import OperationError from "./errors/OperationError.mjs";
import Operation from "./Operation.mjs";
import DishError from "./errors/DishError.mjs";

View File

@ -1,4 +1,4 @@
import OperationConfig from "../config/OperationConfig.json" assert {type: "json"};
import OperationConfig from "../config/OperationConfig.json" with {type: "json"};
import Utils, { isWorkerEnvironment } from "../Utils.mjs";
import Recipe from "../Recipe.mjs";
import Dish from "../Dish.mjs";

View File

@ -59,7 +59,7 @@ class GoogleTranslate extends Operation {
* @param {Object[]} args
* @returns {string}
*/
run(input, args) {
async run(input, args) {
const [sourceLanguage, targetLanguage, authType, authString] = args;
if (input.length === 0) return "";
@ -90,29 +90,31 @@ class GoogleTranslate extends Operation {
cache: "no-cache",
};
return fetch(url, config)
.then(r => {
if (!r.ok) {
return r.json().then(err => {
let msg = err?.error?.message || r.statusText;
throw new OperationError(`Google Translation API Error (${r.status}): ${msg}`);
}).catch(() => {
throw new OperationError(`Google Translation API Error: ${r.status} ${r.statusText}`);
});
try {
const response = await fetch(url, config);
let responseData;
try {
responseData = await response.json();
} catch (err) {
throw new OperationError("Error: Failed to parse response from Google Translation API.");
}
return r.json();
})
.then(data => {
if (data && data.data && data.data.translations && data.data.translations.length > 0) {
return data.data.translations[0].translatedText;
if (!response.ok) {
const msg = responseData?.error?.message || response.statusText;
throw new OperationError(`Google Translation API Error (${response.status}): ${msg}`);
}
if (responseData && responseData.data && responseData.data.translations && responseData.data.translations.length > 0) {
return responseData.data.translations[0].translatedText;
}
throw new OperationError("Error: Unexpected response format from Google Translation API.");
})
.catch(e => {
if (e instanceof OperationError) throw e;
throw new OperationError(e.toString() +
} catch (e) {
if (e.name === "OperationError") throw e;
throw new OperationError(e.message || e.toString() +
"\n\nThis error could be caused by a network issue or invalid authentication.");
});
}
}
}

View File

@ -10,7 +10,7 @@
import NodeDish from "./NodeDish.mjs";
import NodeRecipe from "./NodeRecipe.mjs";
import OperationConfig from "../core/config/OperationConfig.json" assert {type: "json"};
import OperationConfig from "../core/config/OperationConfig.json" with {type: "json"};
import { sanitise, removeSubheadingsFromArray, sentenceToCamelCase } from "./apiUtils.mjs";
import ExcludedOperationError from "../core/errors/ExcludedOperationError.mjs";

View File

@ -1,5 +1,5 @@
import sm from "sitemap";
import OperationConfig from "../../core/config/OperationConfig.json" assert { type: "json" };
import OperationConfig from "../../core/config/OperationConfig.json" with {type: "json"};
/**
* Generates an XML sitemap for all CyberChef operations and a number of recipes.

View File

@ -1,6 +1,6 @@
import TestRegister from "../../lib/TestRegister.mjs";
import Categories from "../../../src/core/config/Categories.json" assert {type: "json"};
import OperationConfig from "../../../src/core/config/OperationConfig.json" assert {type: "json"};
import Categories from "../../../src/core/config/Categories.json" with {type: "json"};
import OperationConfig from "../../../src/core/config/OperationConfig.json" with {type: "json"};
import it from "../assertionHandler.mjs";
import assert from "assert";

View File

@ -0,0 +1,20 @@
import TestRegister from "../../lib/TestRegister.mjs";
TestRegister.addTests([
{
name: "Google Translate: Missing Auth String",
input: "Hello world",
expectedError: "Error: Please provide a valid GCP Auth String (API Key or OAuth Token).",
recipeConfig: [
{
"op": "Google Translate",
"args": [
"en",
"es",
"API Key",
""
]
}
]
}
]);