@yxw007/translate 0.0.14 → 0.0.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,16 @@
1
- // translate v0.0.14 Copyright (c) 2024 Potter<aa4790139@gmail.com> and contributors
1
+ // translate v0.0.15 Copyright (c) 2024 Potter<aa4790139@gmail.com> and contributors
2
2
  import { TranslateClient, TranslateTextCommand } from '@aws-sdk/client-translate';
3
3
  import deeplEngine from 'deepl-node';
4
4
 
5
+ class TranslationError extends Error {
6
+ region;
7
+ constructor(region, message) {
8
+ super(`${region}: ${message}`);
9
+ this.region = region;
10
+ Error.captureStackTrace(this, this.constructor);
11
+ }
12
+ }
13
+
5
14
  function google$1(options) {
6
15
  const base = "https://translate.googleapis.com/translate_a/single";
7
16
  return {
@@ -16,7 +25,7 @@ function google$1(options) {
16
25
  const res = await fetch(url);
17
26
  const body = await res.json();
18
27
  if (!body || body.length === 0) {
19
- throw new Error("Translate fail ! translate's result is null or empty");
28
+ throw new TranslationError(this.name, "Translate fail ! translate's result is null or empty");
20
29
  }
21
30
  const translations = [];
22
31
  for (let i = 0; body[0] && i < body[0].length; i++) {
@@ -36,10 +45,18 @@ function google$1(options) {
36
45
  */
37
46
  function azure$1(options) {
38
47
  const { key, region } = options;
48
+ const name = "azure";
49
+ const checkOptions = () => {
50
+ if (!key || !region) {
51
+ throw new TranslationError(name, `${name} key and region is required`);
52
+ }
53
+ };
54
+ checkOptions();
39
55
  const base = "https://api.cognitive.microsofttranslator.com/translate?api-version=3.0";
40
56
  return {
41
- name: "azure",
57
+ name,
42
58
  async translate(text, opts) {
59
+ checkOptions();
43
60
  const { from, to } = opts;
44
61
  const url = `${base}&to=${to}${from && from !== "auto" ? `&from=${from}` : ""}`;
45
62
  if (!Array.isArray(text)) {
@@ -56,11 +73,11 @@ function azure$1(options) {
56
73
  });
57
74
  const bodyRes = await res.json();
58
75
  if (bodyRes.error) {
59
- throw new Error(`Translate fail ! code: ${bodyRes.error.code}, message: ${bodyRes.error.message}`);
76
+ throw new TranslationError(this.name, `Translate fail ! code: ${bodyRes.error.code}, message: ${bodyRes.error.message}`);
60
77
  }
61
78
  const body = bodyRes;
62
79
  if (!body || body.length === 0) {
63
- throw new Error("Translate fail ! translate's result is null or empty");
80
+ throw new TranslationError(this.name, "Translate fail ! translate's result is null or empty");
64
81
  }
65
82
  const translations = [];
66
83
  for (const translation of body) {
@@ -76,9 +93,17 @@ function azure$1(options) {
76
93
 
77
94
  function amazon$1(options) {
78
95
  const { region, accessKeyId, secretAccessKey } = options;
96
+ const name = "amazon";
97
+ const checkOptions = () => {
98
+ if (!region || !accessKeyId || !secretAccessKey) {
99
+ throw new TranslationError(name, `${name} region, accessKeyId ,secretAccessKey is required`);
100
+ }
101
+ };
102
+ checkOptions();
79
103
  return {
80
- name: "amazon",
104
+ name,
81
105
  async translate(text, opts) {
106
+ checkOptions();
82
107
  const { from = "auto", to } = opts;
83
108
  const translateClient = new TranslateClient({ region: region, credentials: { accessKeyId, secretAccessKey } });
84
109
  if (!Array.isArray(text)) {
@@ -1192,9 +1217,17 @@ var md5 = /*@__PURE__*/getDefaultExportFromCjs(md5Exports);
1192
1217
  function baidu$1(options) {
1193
1218
  const { appId, secretKey } = options;
1194
1219
  const url = "https://fanyi-api.baidu.com/api/trans/vip/fieldtranslate";
1220
+ const name = "baidu";
1221
+ const checkOptions = () => {
1222
+ if (!appId || !secretKey) {
1223
+ throw new TranslationError(name, `${name} appId and secretKey is required`);
1224
+ }
1225
+ };
1226
+ checkOptions();
1195
1227
  return {
1196
- name: "baidu",
1228
+ name,
1197
1229
  async translate(text, opts) {
1230
+ checkOptions();
1198
1231
  const { to, from = "auto", domain = "it" } = opts;
1199
1232
  if (!Array.isArray(text)) {
1200
1233
  text = [text];
@@ -1219,7 +1252,7 @@ function baidu$1(options) {
1219
1252
  });
1220
1253
  const data = await res.json();
1221
1254
  if (!data || data.error_code || !data.trans_result || data.trans_result.length === 0) {
1222
- throw new Error("Failed to translate text");
1255
+ throw new TranslationError(this.name, `Translate fail ! error_code:${data.error_code}, error_msg: ${data.error_msg}`);
1223
1256
  }
1224
1257
  const translations = [];
1225
1258
  for (const translation of data.trans_result) {
@@ -1234,10 +1267,18 @@ function baidu$1(options) {
1234
1267
 
1235
1268
  function deepl$2(options) {
1236
1269
  const { key } = options;
1270
+ const name = "deepl";
1271
+ const checkOptions = () => {
1272
+ if (!key) {
1273
+ throw new TranslationError(name, `${name} key is required`);
1274
+ }
1275
+ };
1276
+ checkOptions();
1237
1277
  const translator = new deeplEngine.Translator(key);
1238
1278
  return {
1239
- name: "deepl",
1279
+ name,
1240
1280
  async translate(text, opts) {
1281
+ checkOptions();
1241
1282
  const { to, from = "auto" } = opts;
1242
1283
  if (!Array.isArray(text)) {
1243
1284
  text = [text];
@@ -1340,6 +1381,12 @@ function useLogger(name = "") {
1340
1381
  function getGapLine() {
1341
1382
  return "-".repeat(20);
1342
1383
  }
1384
+ function getErrorMessages(e, prefix = "Translate fail ! ") {
1385
+ if (e instanceof TypeError) {
1386
+ return prefix + (e.cause.message ?? e.message);
1387
+ }
1388
+ return prefix + e.message;
1389
+ }
1343
1390
 
1344
1391
  var azure = {
1345
1392
  Afrikaans: "af",
@@ -2145,6 +2192,8 @@ function getLanguage(engine) {
2145
2192
  };
2146
2193
  }
2147
2194
 
2195
+ const appName = "Translate";
2196
+
2148
2197
  const logger = useLogger();
2149
2198
  const cache = new Cache();
2150
2199
  class Translator {
@@ -2168,17 +2217,17 @@ class Translator {
2168
2217
  to = options.to = normalToLanguage(to, engine);
2169
2218
  //1. Check if engine exists
2170
2219
  if (!this.engines.has(engine)) {
2171
- throw new Error(`Engine ${engine} not found`);
2220
+ throw new TranslationError(appName, `Engine ${engine} not found`);
2172
2221
  }
2173
2222
  const engineInstance = this.engines.get(engine);
2174
2223
  if (!engineInstance) {
2175
- throw new Error(`Engine ${engine} not found`);
2224
+ throw new TranslationError(appName, `Engine ${engine} not found`);
2176
2225
  }
2177
2226
  if (!from) {
2178
- throw new Error(`Invalid origin language ${from}`);
2227
+ throw new TranslationError(appName, `Invalid origin language ${from}`);
2179
2228
  }
2180
2229
  if (!to) {
2181
- throw new Error(`Invalid target language ${to}`);
2230
+ throw new TranslationError(appName, `Invalid target language ${to}`);
2182
2231
  }
2183
2232
  const key = `${from}:${to}:${engine}:${text}`;
2184
2233
  //3. If the cache is matched, the cache is used directly
@@ -2188,12 +2237,17 @@ class Translator {
2188
2237
  return engineInstance
2189
2238
  .translate(text, options)
2190
2239
  .then((translated) => {
2191
- cache.set(key, translated, cache_time);
2240
+ cache.set(key, translated, cache_time ?? this.cache_time);
2192
2241
  return translated;
2193
2242
  })
2194
2243
  .catch((e) => {
2195
- logger.error(`Translate Failed: from=${from},to=${to},engine=${engine},translate text: \n${getGapLine()}\n${text}\n${getGapLine()}\n error: ${e.message}`);
2196
- throw e;
2244
+ logger.error(`${appName} Failed: from=${from},to=${to},engine=${engine},translate text: \n${getGapLine()}\n${text}\n${getGapLine()}\n error: ${getErrorMessages(e)}`);
2245
+ if (e instanceof TranslationError) {
2246
+ throw e;
2247
+ }
2248
+ else {
2249
+ throw new TranslationError(appName, getErrorMessages(e));
2250
+ }
2197
2251
  });
2198
2252
  }
2199
2253
  }
@@ -2206,5 +2260,5 @@ var index = {
2206
2260
  getLanguage,
2207
2261
  };
2208
2262
 
2209
- export { Cache, Translator, index as default, engines, getLanguage, translator };
2263
+ export { Cache, TranslationError, Translator, index as default, engines, getLanguage, translator };
2210
2264
  //# sourceMappingURL=index.js.map