generaltranslation 2.0.39 → 2.0.41

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.
package/dist/index.js CHANGED
@@ -129,7 +129,7 @@ exports.isValidLanguageCode = isValidLanguageCode;
129
129
  const standardizeLanguageCode = (code) => (0, codes_1._standardizeLanguageCode)(code);
130
130
  exports.standardizeLanguageCode = standardizeLanguageCode;
131
131
  function getLanguageObject(codes) {
132
- return Array.isArray(codes) ? (0, codes_1._getLanguageObject)(codes) : (0, codes_1._getLanguageObject)([codes]);
132
+ return Array.isArray(codes) ? (0, codes_1._getLanguageObject)(codes) : (0, codes_1._getLanguageObject)(codes);
133
133
  }
134
134
  function getLanguageCode(languages) {
135
135
  return (0, codes_1._getLanguageCode)(languages);
@@ -12,24 +12,29 @@ Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.default = _bundleRequests;
13
13
  /**
14
14
  * Bundles multiple requests and sends them to the server.
15
- * @param gt - Contains the baseURL and apiKey for the server request.
16
- * @param requests - Array of requests to be processed and sent.
17
- * @returns A promise that resolves to an array of processed results.
15
+ * @param {{ baseURL: string, apiKey: string }} gt - Contains the baseURL and apiKey for the server request.
16
+ * @param {Request[]} requests - Array of requests to be processed and sent.
17
+ * @param {{ timeout?: number }} options - Additional options for the request, including timeout.
18
+ * @returns {Promise<Array<any | null>>} A promise that resolves to an array of processed results.
18
19
  * @internal
19
- */
20
- function _bundleRequests(gt, requests) {
21
- return __awaiter(this, void 0, void 0, function* () {
20
+ */
21
+ function _bundleRequests(gt_1, requests_1) {
22
+ return __awaiter(this, arguments, void 0, function* (gt, requests, options = {}) {
23
+ const controller = new AbortController();
24
+ const signal = controller.signal;
25
+ if (options.timeout) {
26
+ setTimeout(() => controller.abort(), options.timeout);
27
+ }
22
28
  try {
23
- // Send the processed requests to the server as a bundled request.
24
29
  const response = yield fetch(`${gt.baseURL}/bundle`, {
25
30
  method: 'POST',
26
31
  headers: {
27
32
  'Content-Type': 'application/json',
28
33
  'gtx-api-key': gt.apiKey,
29
34
  },
30
- body: JSON.stringify(requests)
35
+ body: JSON.stringify(requests),
36
+ signal
31
37
  });
32
- // Check for response errors.
33
38
  if (!response.ok) {
34
39
  throw new Error(`${response.status}: ${yield response.text()}`);
35
40
  }
@@ -37,6 +42,10 @@ function _bundleRequests(gt, requests) {
37
42
  return resultArray;
38
43
  }
39
44
  catch (error) {
45
+ if (error instanceof Error && error.name === 'AbortError') {
46
+ console.error('Request timed out');
47
+ return Array.from(requests, () => ({ translation: null, error: 'Request timed out' }));
48
+ }
40
49
  console.error(error);
41
50
  return Array.from(requests, () => ({ translation: null, error: error }));
42
51
  }
@@ -11,17 +11,22 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.default = _translate;
13
13
  /**
14
- * Translates a single piece of content and caches for use in a public project.
15
- * @param {{ baseURL: string, apiKey: string }} gt - The translation service configuration.
16
- * @param {string} content - The content to translate.
17
- * @param {string} targetLanguage - The target language for the translation.
18
- * @param {string} projectID - The ID of the project
19
- * @param {{ dictionaryName?: string, notes?: string, [key: string]: any }} metadata - Additional metadata for the translation request.
20
- * @returns {Promise<{ translation: string, error?: Error | unknown }>} - The translated content with optional error information.
21
- * @internal
14
+ * Translates a single piece of content and caches for use in a public project.
15
+ * @param {{ baseURL: string, apiKey: string }} gt - The translation service configuration.
16
+ * @param {string} content - The content to translate.
17
+ * @param {string} targetLanguage - The target language for the translation.
18
+ * @param {string} projectID - The ID of the project
19
+ * @param {{ dictionaryName?: string, notes?: string, timeout?: number, [key: string]: any }} metadata - Additional metadata for the translation request.
20
+ * @returns {Promise<{ translation: string, error?: Error | unknown }>} - The translated content with optional error information.
21
+ * @internal
22
22
  */
23
23
  function _translate(gt, content, targetLanguage, projectID, metadata) {
24
24
  return __awaiter(this, void 0, void 0, function* () {
25
+ const controller = new AbortController();
26
+ const signal = controller.signal;
27
+ if (metadata.timeout) {
28
+ setTimeout(() => controller.abort(), metadata.timeout);
29
+ }
25
30
  try {
26
31
  const response = yield fetch(`${gt.baseURL}/translate`, {
27
32
  method: 'POST',
@@ -31,7 +36,8 @@ function _translate(gt, content, targetLanguage, projectID, metadata) {
31
36
  },
32
37
  body: JSON.stringify({
33
38
  content, targetLanguage, projectID, metadata
34
- })
39
+ }),
40
+ signal
35
41
  });
36
42
  if (!response.ok) {
37
43
  throw new Error(`${response.status}: ${yield response.text()}`);
@@ -40,6 +46,13 @@ function _translate(gt, content, targetLanguage, projectID, metadata) {
40
46
  return result;
41
47
  }
42
48
  catch (error) {
49
+ if (error instanceof Error && error.name === 'AbortError') {
50
+ console.error('Request timed out');
51
+ return {
52
+ translation: content,
53
+ error: 'Request timed out'
54
+ };
55
+ }
43
56
  console.error(error);
44
57
  return {
45
58
  translation: content,
@@ -15,12 +15,17 @@ exports.default = _translate;
15
15
  * @param {{ baseURL: string, apiKey: string }} gt - The translation service configuration.
16
16
  * @param {string} content - The content to translate.
17
17
  * @param {string} targetLanguage - The target language for the translation.
18
- * @param {{ notes?: string, [key: string]: any }} metadata - Additional metadata for the translation request.
18
+ * @param {{ notes?: string, timeout?: number, [key: string]: any }} metadata - Additional metadata for the translation request.
19
19
  * @returns {Promise<{ translation: string, error?: Error | unknown }>} - The translated content with optional error information.
20
20
  * @internal
21
21
  */
22
22
  function _translate(gt, content, targetLanguage, metadata) {
23
23
  return __awaiter(this, void 0, void 0, function* () {
24
+ const controller = new AbortController();
25
+ const signal = controller.signal;
26
+ if (metadata.timeout) {
27
+ setTimeout(() => controller.abort(), metadata.timeout);
28
+ }
24
29
  try {
25
30
  const response = yield fetch(`${gt.baseURL}/translate`, {
26
31
  method: 'POST',
@@ -30,7 +35,8 @@ function _translate(gt, content, targetLanguage, metadata) {
30
35
  },
31
36
  body: JSON.stringify({
32
37
  content, targetLanguage, metadata
33
- })
38
+ }),
39
+ signal
34
40
  });
35
41
  if (!response.ok) {
36
42
  throw new Error(`${response.status}: ${yield response.text()}`);
@@ -39,6 +45,13 @@ function _translate(gt, content, targetLanguage, metadata) {
39
45
  return result;
40
46
  }
41
47
  catch (error) {
48
+ if (error instanceof Error && error.name === 'AbortError') {
49
+ console.error('Request timed out');
50
+ return {
51
+ translation: content,
52
+ error: 'Request timed out'
53
+ };
54
+ }
42
55
  console.error(error);
43
56
  return {
44
57
  translation: content,
@@ -13,18 +13,23 @@ exports.default = _translateReactChildren;
13
13
  /**
14
14
  * Translates the given content into the target language using a specified API.
15
15
  *
16
- * @param {Object} gt - An object containing baseURL and apiKey for the API.
17
- * @param {JSON} content - The content to be translated. This can be of any type.
16
+ * @param {{ baseURL: string, apiKey: string }} gt - An object containing baseURL and apiKey for the API.
17
+ * @param {any} content - The content to be translated. This can be of any type.
18
18
  * @param {string} targetLanguage - The target language code (e.g., 'en', 'fr') for the translation.
19
- * @param {Object} metadata - Additional metadata to be sent with the translation request.
19
+ * @param {{ [key: string]: any }} metadata - Additional metadata to be sent with the translation request.
20
20
  *
21
- * @returns {Promise<any | null>} - A promise that resolves to the translated content as an object, or null if an error occurs.
21
+ * @returns {Promise<{ translation: any | null, error?: Error | unknown }>} - A promise that resolves to the translated content as an object, or null if an error occurs.
22
22
  *
23
23
  * @throws {Error} - Throws an error if the response from the API is not ok (status code not in the range 200-299).
24
24
  * @internal
25
25
  **/
26
26
  function _translateReactChildren(gt, content, targetLanguage, metadata) {
27
27
  return __awaiter(this, void 0, void 0, function* () {
28
+ const controller = new AbortController();
29
+ const signal = controller.signal;
30
+ if (metadata.timeout) {
31
+ setTimeout(() => controller.abort(), metadata.timeout);
32
+ }
28
33
  try {
29
34
  const response = yield fetch(`${gt.baseURL}/react`, {
30
35
  method: 'POST',
@@ -36,7 +41,8 @@ function _translateReactChildren(gt, content, targetLanguage, metadata) {
36
41
  content: content,
37
42
  targetLanguage: targetLanguage,
38
43
  metadata: metadata
39
- })
44
+ }),
45
+ signal
40
46
  });
41
47
  if (!response.ok) {
42
48
  throw new Error(`${response.status}: ${yield response.text()}`);
@@ -44,6 +50,13 @@ function _translateReactChildren(gt, content, targetLanguage, metadata) {
44
50
  return yield response.json();
45
51
  }
46
52
  catch (error) {
53
+ if (error instanceof Error && error.name === 'AbortError') {
54
+ console.error('Request timed out');
55
+ return {
56
+ translation: null,
57
+ error: 'Request timed out'
58
+ };
59
+ }
47
60
  console.error(error);
48
61
  return {
49
62
  translation: null,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "generaltranslation",
3
- "version": "2.0.39",
3
+ "version": "2.0.41",
4
4
  "description": "A language toolkit for AI developers",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",