generaltranslation 2.0.43 → 2.0.45

Sign up to get free protection for your applications and to get access to all the features.
package/README.md CHANGED
@@ -186,7 +186,7 @@ console.log(result.translation); // { "type": "div", "props": { "children": "Hol
186
186
 
187
187
  - A promise that resolves to an object containing the translated content and optional error information.
188
188
 
189
- #### `bundleRequests(requests: any[]): Promise<Array<any | null>>`
189
+ #### `bundleTranslation(requests: any[]): Promise<Array<any | null>>`
190
190
 
191
191
  Bundles multiple requests and sends them to the server.
192
192
 
@@ -206,7 +206,7 @@ const requests = [
206
206
  }
207
207
 
208
208
  ];
209
- const results = await gt.bundleRequests(requests);
209
+ const results = await gt.bundleTranslation(requests);
210
210
  console.log(results); // ['Hola', 'Hallo']
211
211
  ```
212
212
 
package/dist/index.d.ts CHANGED
@@ -1,4 +1,6 @@
1
1
  import { LanguageObject } from './codes/codes';
2
+ import { Request } from './translation/_bundleTranslation';
3
+ import { Update } from './translation/_updateRemoteDictionary';
2
4
  /**
3
5
  * Type representing the constructor parameters for the GT class.
4
6
  */
@@ -73,11 +75,17 @@ declare class GT {
73
75
  error?: Error | unknown;
74
76
  }>;
75
77
  /**
76
- * Bundles multiple requests and sends them to the server.
78
+ * Bundles multiple translation requests and sends them to the server.
77
79
  * @param requests - Array of requests to be processed and sent.
78
80
  * @returns A promise that resolves to an array of processed results.
79
81
  */
80
- bundleRequests(requests: any[]): Promise<Array<any | null>>;
82
+ bundleTranslation(requests: Request[]): Promise<Array<any | null>>;
83
+ /**
84
+ *Pushes updates to a remotely cached translation dictionary.
85
+ * @param updates - Array of updates with optional targetLanguage.
86
+ * @returns A promise that resolves to a boolean indicating success or failure.
87
+ */
88
+ updateRemoteDictionary(updates: Update[]): Promise<boolean>;
81
89
  }
82
90
  export default GT;
83
91
  /**
package/dist/index.js CHANGED
@@ -22,10 +22,11 @@ exports.isSameLanguage = isSameLanguage;
22
22
  // ----- IMPORTS ----- //
23
23
  const codes_1 = require("./codes/codes");
24
24
  const getLanguageDirection_1 = __importDefault(require("./codes/getLanguageDirection"));
25
- const _bundleRequests_1 = __importDefault(require("./translation/_bundleRequests"));
25
+ const _bundleTranslation_1 = __importDefault(require("./translation/_bundleTranslation"));
26
26
  const _intl_1 = __importDefault(require("./translation/_intl"));
27
27
  const _translate_1 = __importDefault(require("./translation/_translate"));
28
28
  const _translateReactChildren_1 = __importDefault(require("./translation/_translateReactChildren"));
29
+ const _updateRemoteDictionary_1 = __importDefault(require("./translation/_updateRemoteDictionary"));
29
30
  // ----- CORE CLASS ----- //
30
31
  const getDefaultFromEnv = (VARIABLE) => {
31
32
  if (typeof process !== 'undefined' && process.env) {
@@ -93,13 +94,23 @@ class GT {
93
94
  });
94
95
  }
95
96
  /**
96
- * Bundles multiple requests and sends them to the server.
97
+ * Bundles multiple translation requests and sends them to the server.
97
98
  * @param requests - Array of requests to be processed and sent.
98
99
  * @returns A promise that resolves to an array of processed results.
99
100
  */
100
- bundleRequests(requests) {
101
+ bundleTranslation(requests) {
101
102
  return __awaiter(this, void 0, void 0, function* () {
102
- return (0, _bundleRequests_1.default)(this, requests);
103
+ return (0, _bundleTranslation_1.default)(this, requests);
104
+ });
105
+ }
106
+ /**
107
+ *Pushes updates to a remotely cached translation dictionary.
108
+ * @param updates - Array of updates with optional targetLanguage.
109
+ * @returns A promise that resolves to a boolean indicating success or failure.
110
+ */
111
+ updateRemoteDictionary(updates) {
112
+ return __awaiter(this, void 0, void 0, function* () {
113
+ return (0, _updateRemoteDictionary_1.default)(this, updates);
103
114
  });
104
115
  }
105
116
  }
@@ -0,0 +1,22 @@
1
+ export type Request = {
2
+ type: 'translate';
3
+ data: {
4
+ content: string;
5
+ targetLanguage: string;
6
+ metadata: Record<string, any>;
7
+ };
8
+ } | {
9
+ type: 'intl';
10
+ data: {
11
+ content: string;
12
+ targetLanguage: string;
13
+ metadata: Record<string, any>;
14
+ };
15
+ } | {
16
+ type: 'intl';
17
+ data: {
18
+ children: object | string;
19
+ targetLanguage: string;
20
+ metadata: Record<string, any>;
21
+ };
22
+ };
@@ -9,7 +9,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  });
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.default = _bundleRequests;
12
+ exports.default = _bundleTranslation;
13
13
  /**
14
14
  * Bundles multiple requests and sends them to the server.
15
15
  * @param {{ baseURL: string, apiKey: string }} gt - Contains the baseURL and apiKey for the server request.
@@ -18,12 +18,13 @@ exports.default = _bundleRequests;
18
18
  * @returns {Promise<Array<any | null>>} A promise that resolves to an array of processed results.
19
19
  * @internal
20
20
  */
21
- function _bundleRequests(gt_1, requests_1) {
22
- return __awaiter(this, arguments, void 0, function* (gt, requests, options = {}) {
21
+ function _bundleTranslation(gt, requests) {
22
+ return __awaiter(this, void 0, void 0, function* () {
23
+ var _a, _b, _c;
23
24
  const controller = new AbortController();
24
25
  const signal = controller.signal;
25
- if (options.timeout) {
26
- setTimeout(() => controller.abort(), options.timeout);
26
+ if ((_c = (_b = (_a = requests[0]) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.metadata) === null || _c === void 0 ? void 0 : _c.timeout) {
27
+ setTimeout(() => controller.abort(), requests[0].data.metadata.timeout);
27
28
  }
28
29
  try {
29
30
  const response = yield fetch(`${gt.baseURL}/bundle`, {
@@ -0,0 +1,15 @@
1
+ export type Update = {
2
+ type: 'react';
3
+ data: {
4
+ children: object | string;
5
+ targetLanguage?: string;
6
+ metadata: Record<string, any>;
7
+ };
8
+ } | {
9
+ type: 'intl';
10
+ data: {
11
+ content: string;
12
+ targetLanguage?: string;
13
+ metadata: Record<string, any>;
14
+ };
15
+ };
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.default = _updateRemoteDictionary;
13
+ /**
14
+ * Pushes updates to a remotely cached translation dictionary.
15
+ * @param {{ baseURL: string, apiKey: string }} gt - Contains the baseURL and apiKey for the server request.
16
+ * @param {Update[]} updates - Array of requests to be processed and sent.
17
+ * @returns {Promise<boolean>} A promise that resolves to an array of processed results.
18
+ * @internal
19
+ */
20
+ function _updateRemoteDictionary(gt, updates) {
21
+ return __awaiter(this, void 0, void 0, function* () {
22
+ try {
23
+ const response = yield fetch(`${gt.baseURL}/update`, {
24
+ method: 'POST',
25
+ headers: {
26
+ 'Content-Type': 'application/json',
27
+ 'gtx-api-key': gt.apiKey,
28
+ },
29
+ body: JSON.stringify(updates)
30
+ });
31
+ if (!response.ok) {
32
+ throw new Error(`${response.status}: ${yield response.text()}`);
33
+ }
34
+ const result = yield response.json();
35
+ return result.success;
36
+ }
37
+ catch (error) {
38
+ if (error instanceof Error && error.name === 'AbortError') {
39
+ console.error('Request timed out');
40
+ return false;
41
+ }
42
+ console.error(error);
43
+ return false;
44
+ }
45
+ });
46
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "generaltranslation",
3
- "version": "2.0.43",
3
+ "version": "2.0.45",
4
4
  "description": "A language toolkit for AI developers",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -1 +0,0 @@
1
- export {};