@translated/lara 1.6.7 → 1.8.0-beta.0
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/README.md +26 -0
- package/lib/credentials.d.ts +36 -4
- package/lib/credentials.js +40 -5
- package/lib/crypto/browser-crypto.d.ts +1 -4
- package/lib/crypto/browser-crypto.js +2 -5
- package/lib/crypto/index.browser.d.ts +3 -0
- package/lib/crypto/index.browser.js +11 -0
- package/lib/crypto/index.js +1 -5
- package/lib/crypto/node-crypto.d.ts +1 -1
- package/lib/crypto/node-crypto.js +3 -3
- package/lib/crypto/portable-crypto.d.ts +1 -1
- package/lib/documents.d.ts +61 -0
- package/lib/documents.js +109 -0
- package/lib/errors.d.ts +2 -1
- package/lib/errors.js +2 -1
- package/lib/glossaries.d.ts +37 -0
- package/lib/glossaries.js +65 -0
- package/lib/index.d.ts +8 -5
- package/lib/index.js +13 -8
- package/lib/memories.d.ts +38 -0
- package/lib/memories.js +101 -0
- package/lib/net/lara/browser-client.d.ts +14 -0
- package/lib/net/lara/browser-client.js +189 -0
- package/lib/net/lara/client.d.ts +53 -0
- package/lib/net/lara/client.js +242 -0
- package/lib/net/lara/index.browser.d.ts +5 -0
- package/lib/net/{index.js → lara/index.browser.js} +7 -9
- package/lib/net/lara/index.d.ts +5 -0
- package/lib/net/lara/index.js +21 -0
- package/lib/net/lara/node-client.d.ts +16 -0
- package/lib/net/lara/node-client.js +321 -0
- package/lib/net/s3/browser-client.d.ts +4 -2
- package/lib/net/s3/browser-client.js +7 -0
- package/lib/net/s3/client.d.ts +5 -4
- package/lib/net/s3/client.js +2 -2
- package/lib/net/s3/index.browser.d.ts +2 -0
- package/lib/net/s3/index.browser.js +7 -0
- package/lib/net/s3/index.d.ts +1 -2
- package/lib/net/s3/index.js +1 -7
- package/lib/net/s3/laraStream.browser.d.ts +1 -0
- package/lib/net/s3/laraStream.browser.js +2 -0
- package/lib/net/s3/laraStream.d.ts +2 -0
- package/lib/net/s3/laraStream.js +2 -0
- package/lib/net/s3/node-client.d.ts +5 -3
- package/lib/net/s3/node-client.js +11 -2
- package/lib/translator.d.ts +69 -0
- package/lib/translator.js +108 -0
- package/lib/utils/defaultBaseUrl.d.ts +1 -0
- package/lib/utils/defaultBaseUrl.js +4 -0
- package/lib/utils/parse-content.d.ts +6 -0
- package/lib/utils/parse-content.js +30 -0
- package/lib/utils/sdk-version.d.ts +1 -0
- package/lib/{sdk-version.js → utils/sdk-version.js} +1 -1
- package/lib/utils/{toSnakeCase.js → to-snake-case.js} +3 -3
- package/lib_browser/lara.min.js +1 -0
- package/package.json +5 -2
- package/lib/net/browser-client.d.ts +0 -8
- package/lib/net/browser-client.js +0 -62
- package/lib/net/client.d.ts +0 -34
- package/lib/net/client.js +0 -108
- package/lib/net/index.d.ts +0 -3
- package/lib/net/node-client.d.ts +0 -10
- package/lib/net/node-client.js +0 -103
- package/lib/sdk-version.d.ts +0 -1
- package/lib/translator/models.d.ts +0 -105
- package/lib/translator/models.js +0 -14
- package/lib/translator/translator.d.ts +0 -80
- package/lib/translator/translator.js +0 -265
- /package/lib/utils/{toSnakeCase.d.ts → to-snake-case.d.ts} +0 -0
package/README.md
CHANGED
|
@@ -181,6 +181,32 @@ const options = {
|
|
|
181
181
|
const result = await lara.translate("Hello", "en-US", "fr-FR", options);
|
|
182
182
|
```
|
|
183
183
|
|
|
184
|
+
#### Language Detection
|
|
185
|
+
|
|
186
|
+
Use detect() to automatically identify the language of one or multiple texts.
|
|
187
|
+
|
|
188
|
+
```javascript
|
|
189
|
+
// Single string detection
|
|
190
|
+
const single = await lara.detect("Bonjour tout le monde");
|
|
191
|
+
console.log(single.language); // fr
|
|
192
|
+
|
|
193
|
+
// Multiple strings detection
|
|
194
|
+
const multiple = await lara.detect(["Hello world", "How are you?"]);
|
|
195
|
+
console.log(multiple.languages); // "en"
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
You can provide a hint (expected source language) and a passlist (restrict candidates) to improve accuracy.
|
|
199
|
+
|
|
200
|
+
```javascript
|
|
201
|
+
// Detection with hint and passlist
|
|
202
|
+
const detected = await lara.detect(
|
|
203
|
+
"Es un día soleado",
|
|
204
|
+
"es", // hint (optional)
|
|
205
|
+
["es", "pt", "fr"] // passlist (optional)
|
|
206
|
+
);
|
|
207
|
+
console.log(detected.language); // es
|
|
208
|
+
```
|
|
209
|
+
|
|
184
210
|
### 📖 Document Translation
|
|
185
211
|
#### Simple document translation
|
|
186
212
|
|
package/lib/credentials.d.ts
CHANGED
|
@@ -1,5 +1,37 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Represents an access key for authenticating with the Lara API.
|
|
3
|
+
*/
|
|
4
|
+
export declare class AccessKey {
|
|
5
|
+
/** The access key ID. */
|
|
6
|
+
readonly id: string;
|
|
7
|
+
/** The access key secret. */
|
|
8
|
+
readonly secret: string;
|
|
9
|
+
/**
|
|
10
|
+
* Creates a new AccessKey instance.
|
|
11
|
+
* @param id - The access key ID.
|
|
12
|
+
* @param secret - The access key secret.
|
|
13
|
+
*/
|
|
14
|
+
constructor(id: string, secret: string);
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* @deprecated Use AccessKey instead.
|
|
18
|
+
*/
|
|
19
|
+
export declare class Credentials extends AccessKey {
|
|
20
|
+
get accessKeyId(): string;
|
|
21
|
+
get accessKeySecret(): string;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Represents an authentication token for authenticating with the Lara API.
|
|
25
|
+
*/
|
|
26
|
+
export declare class AuthToken {
|
|
27
|
+
/** The authentication token. */
|
|
28
|
+
readonly token: string;
|
|
29
|
+
/** The refresh token used to obtain a new authentication token. */
|
|
30
|
+
readonly refreshToken: string;
|
|
31
|
+
/**
|
|
32
|
+
* Creates a new AuthToken instance.
|
|
33
|
+
* @param token - The authentication token.
|
|
34
|
+
* @param refreshToken - The refresh token.
|
|
35
|
+
*/
|
|
36
|
+
constructor(token: string, refreshToken: string);
|
|
5
37
|
}
|
package/lib/credentials.js
CHANGED
|
@@ -1,10 +1,45 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Credentials = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
3
|
+
exports.AuthToken = exports.Credentials = exports.AccessKey = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Represents an access key for authenticating with the Lara API.
|
|
6
|
+
*/
|
|
7
|
+
class AccessKey {
|
|
8
|
+
/**
|
|
9
|
+
* Creates a new AccessKey instance.
|
|
10
|
+
* @param id - The access key ID.
|
|
11
|
+
* @param secret - The access key secret.
|
|
12
|
+
*/
|
|
13
|
+
constructor(id, secret) {
|
|
14
|
+
this.id = id;
|
|
15
|
+
this.secret = secret;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
exports.AccessKey = AccessKey;
|
|
19
|
+
/**
|
|
20
|
+
* @deprecated Use AccessKey instead.
|
|
21
|
+
*/
|
|
22
|
+
class Credentials extends AccessKey {
|
|
23
|
+
get accessKeyId() {
|
|
24
|
+
return this.id;
|
|
25
|
+
}
|
|
26
|
+
get accessKeySecret() {
|
|
27
|
+
return this.secret;
|
|
8
28
|
}
|
|
9
29
|
}
|
|
10
30
|
exports.Credentials = Credentials;
|
|
31
|
+
/**
|
|
32
|
+
* Represents an authentication token for authenticating with the Lara API.
|
|
33
|
+
*/
|
|
34
|
+
class AuthToken {
|
|
35
|
+
/**
|
|
36
|
+
* Creates a new AuthToken instance.
|
|
37
|
+
* @param token - The authentication token.
|
|
38
|
+
* @param refreshToken - The refresh token.
|
|
39
|
+
*/
|
|
40
|
+
constructor(token, refreshToken) {
|
|
41
|
+
this.token = token;
|
|
42
|
+
this.refreshToken = refreshToken;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
exports.AuthToken = AuthToken;
|
|
@@ -3,9 +3,6 @@ import type { PortableCrypto } from "./portable-crypto";
|
|
|
3
3
|
export declare class BrowserCrypto implements PortableCrypto {
|
|
4
4
|
private readonly subtle;
|
|
5
5
|
constructor();
|
|
6
|
-
|
|
7
|
-
* MD5 in browser is not supported, so we use SHA-256 instead and return the first 16 bytes
|
|
8
|
-
*/
|
|
9
|
-
digest(data: string): Promise<string>;
|
|
6
|
+
digestBase64(data: string): Promise<string>;
|
|
10
7
|
hmac(key: string, data: string): Promise<string>;
|
|
11
8
|
}
|
|
@@ -6,13 +6,10 @@ class BrowserCrypto {
|
|
|
6
6
|
constructor() {
|
|
7
7
|
this.subtle = window.crypto.subtle;
|
|
8
8
|
}
|
|
9
|
-
|
|
10
|
-
* MD5 in browser is not supported, so we use SHA-256 instead and return the first 16 bytes
|
|
11
|
-
*/
|
|
12
|
-
async digest(data) {
|
|
9
|
+
async digestBase64(data) {
|
|
13
10
|
const encoder = new TextEncoder();
|
|
14
11
|
const buffer = (await this.subtle.digest("sha-256", encoder.encode(data))).slice(0, 16);
|
|
15
|
-
return
|
|
12
|
+
return btoa(String.fromCharCode(...new Uint8Array(buffer)));
|
|
16
13
|
}
|
|
17
14
|
async hmac(key, data) {
|
|
18
15
|
const encoder = new TextEncoder();
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.default = instance;
|
|
4
|
+
const browser_crypto_1 = require("./browser-crypto");
|
|
5
|
+
let _instance;
|
|
6
|
+
function instance() {
|
|
7
|
+
if (_instance === undefined) {
|
|
8
|
+
_instance = new browser_crypto_1.BrowserCrypto();
|
|
9
|
+
}
|
|
10
|
+
return _instance;
|
|
11
|
+
}
|
package/lib/crypto/index.js
CHANGED
|
@@ -1,15 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.default = instance;
|
|
4
|
-
const browser_crypto_1 = require("./browser-crypto");
|
|
5
4
|
const node_crypto_1 = require("./node-crypto");
|
|
6
5
|
let _instance;
|
|
7
6
|
function instance() {
|
|
8
7
|
if (_instance === undefined) {
|
|
9
|
-
|
|
10
|
-
_instance = new browser_crypto_1.BrowserCrypto();
|
|
11
|
-
else
|
|
12
|
-
_instance = new node_crypto_1.NodeCrypto();
|
|
8
|
+
_instance = new node_crypto_1.NodeCrypto();
|
|
13
9
|
}
|
|
14
10
|
return _instance;
|
|
15
11
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { PortableCrypto } from "./portable-crypto";
|
|
2
2
|
/** @internal */
|
|
3
3
|
export declare class NodeCrypto implements PortableCrypto {
|
|
4
|
-
|
|
4
|
+
digestBase64(data: string): Promise<string>;
|
|
5
5
|
hmac(key: string, data: string): Promise<string>;
|
|
6
6
|
}
|
|
@@ -37,11 +37,11 @@ exports.NodeCrypto = void 0;
|
|
|
37
37
|
const crypto = __importStar(require("node:crypto"));
|
|
38
38
|
/** @internal */
|
|
39
39
|
class NodeCrypto {
|
|
40
|
-
|
|
40
|
+
digestBase64(data) {
|
|
41
41
|
return new Promise((resolve) => {
|
|
42
42
|
const hash = crypto.createHash("md5");
|
|
43
|
-
hash.update(data);
|
|
44
|
-
resolve(hash.digest("
|
|
43
|
+
hash.update(data, "utf8");
|
|
44
|
+
resolve(hash.digest("base64"));
|
|
45
45
|
});
|
|
46
46
|
}
|
|
47
47
|
hmac(key, data) {
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import type { LaraClient } from "./net/lara";
|
|
2
|
+
import type { MultiPartFile } from "./net/lara/client";
|
|
3
|
+
import type { LaraStream } from "./net/s3/laraStream";
|
|
4
|
+
import type { TranslationStyle } from "./translator";
|
|
5
|
+
export type S3UploadFields = {
|
|
6
|
+
acl: string;
|
|
7
|
+
bucket: string;
|
|
8
|
+
key: string;
|
|
9
|
+
};
|
|
10
|
+
export declare enum DocumentStatus {
|
|
11
|
+
INITIALIZED = "initialized",// just been created
|
|
12
|
+
ANALYZING = "analyzing",// being analyzed for language detection and chars count
|
|
13
|
+
PAUSED = "paused",// paused after analysis, needs user confirm
|
|
14
|
+
READY = "ready",// ready to be translated
|
|
15
|
+
TRANSLATING = "translating",
|
|
16
|
+
TRANSLATED = "translated",
|
|
17
|
+
ERROR = "error"
|
|
18
|
+
}
|
|
19
|
+
export interface DocxExtractionParams {
|
|
20
|
+
extractComments?: boolean;
|
|
21
|
+
acceptRevisions?: boolean;
|
|
22
|
+
}
|
|
23
|
+
export type DocumentOptions = {
|
|
24
|
+
adaptTo?: string[];
|
|
25
|
+
glossaries?: string[];
|
|
26
|
+
noTrace?: boolean;
|
|
27
|
+
style?: TranslationStyle;
|
|
28
|
+
};
|
|
29
|
+
export type DocumentDownloadOptions = {
|
|
30
|
+
outputFormat?: string;
|
|
31
|
+
};
|
|
32
|
+
export type DocumentUploadOptions = DocumentOptions & {
|
|
33
|
+
password?: string;
|
|
34
|
+
extractionParams?: DocxExtractionParams;
|
|
35
|
+
contentLength?: number;
|
|
36
|
+
};
|
|
37
|
+
export interface Document {
|
|
38
|
+
readonly id: string;
|
|
39
|
+
readonly status: DocumentStatus;
|
|
40
|
+
readonly source?: string;
|
|
41
|
+
readonly target: string;
|
|
42
|
+
readonly filename: string;
|
|
43
|
+
readonly createdAt: Date;
|
|
44
|
+
readonly updatedAt: Date;
|
|
45
|
+
readonly options?: DocumentOptions;
|
|
46
|
+
readonly translatedChars?: number;
|
|
47
|
+
readonly totalChars?: number;
|
|
48
|
+
readonly errorReason?: string;
|
|
49
|
+
}
|
|
50
|
+
export type DocumentTranslateOptions = DocumentUploadOptions & DocumentDownloadOptions;
|
|
51
|
+
export declare class Documents {
|
|
52
|
+
private readonly client;
|
|
53
|
+
private readonly s3Client;
|
|
54
|
+
constructor(client: LaraClient);
|
|
55
|
+
upload(file: MultiPartFile, filename: string, source: string | null, target: string, options?: DocumentUploadOptions): Promise<Document>;
|
|
56
|
+
status(id: string): Promise<Document>;
|
|
57
|
+
download(id: string, options?: DocumentDownloadOptions): Promise<Blob | Buffer>;
|
|
58
|
+
downloadStream(id: string, options?: DocumentDownloadOptions): Promise<LaraStream>;
|
|
59
|
+
translate(file: MultiPartFile, filename: string, source: string | null, target: string, options?: DocumentTranslateOptions): Promise<Blob | Buffer>;
|
|
60
|
+
translateStream(file: MultiPartFile, filename: string, source: string | null, target: string, options?: DocumentTranslateOptions): Promise<LaraStream>;
|
|
61
|
+
}
|
package/lib/documents.js
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.Documents = exports.DocumentStatus = void 0;
|
|
7
|
+
const errors_1 = require("./errors");
|
|
8
|
+
const s3_1 = __importDefault(require("./net/s3"));
|
|
9
|
+
const to_snake_case_1 = __importDefault(require("./utils/to-snake-case"));
|
|
10
|
+
// biome-ignore format: keep comments aligned
|
|
11
|
+
var DocumentStatus;
|
|
12
|
+
(function (DocumentStatus) {
|
|
13
|
+
DocumentStatus["INITIALIZED"] = "initialized";
|
|
14
|
+
DocumentStatus["ANALYZING"] = "analyzing";
|
|
15
|
+
DocumentStatus["PAUSED"] = "paused";
|
|
16
|
+
DocumentStatus["READY"] = "ready";
|
|
17
|
+
DocumentStatus["TRANSLATING"] = "translating";
|
|
18
|
+
DocumentStatus["TRANSLATED"] = "translated";
|
|
19
|
+
DocumentStatus["ERROR"] = "error";
|
|
20
|
+
})(DocumentStatus || (exports.DocumentStatus = DocumentStatus = {}));
|
|
21
|
+
class Documents {
|
|
22
|
+
constructor(client) {
|
|
23
|
+
this.client = client;
|
|
24
|
+
this.s3Client = (0, s3_1.default)();
|
|
25
|
+
}
|
|
26
|
+
async upload(file, filename, source, target, options) {
|
|
27
|
+
const { url, fields } = await this.client.get(`/v2/documents/upload-url`, { filename });
|
|
28
|
+
await this.s3Client.upload(url, fields, file, options === null || options === void 0 ? void 0 : options.contentLength);
|
|
29
|
+
const headers = (options === null || options === void 0 ? void 0 : options.noTrace) ? { "X-No-Trace": "true" } : {};
|
|
30
|
+
return this.client.post("/v2/documents", {
|
|
31
|
+
source,
|
|
32
|
+
target,
|
|
33
|
+
s3key: fields.key,
|
|
34
|
+
adapt_to: options === null || options === void 0 ? void 0 : options.adaptTo,
|
|
35
|
+
glossaries: options === null || options === void 0 ? void 0 : options.glossaries,
|
|
36
|
+
style: options === null || options === void 0 ? void 0 : options.style,
|
|
37
|
+
password: options === null || options === void 0 ? void 0 : options.password,
|
|
38
|
+
extraction_params: (options === null || options === void 0 ? void 0 : options.extractionParams) ? (0, to_snake_case_1.default)(options.extractionParams) : undefined
|
|
39
|
+
}, undefined, headers);
|
|
40
|
+
}
|
|
41
|
+
async status(id) {
|
|
42
|
+
return await this.client.get(`/v2/documents/${id}`);
|
|
43
|
+
}
|
|
44
|
+
async download(id, options) {
|
|
45
|
+
const { url } = await this.client.get(`/v2/documents/${id}/download-url`, {
|
|
46
|
+
output_format: options === null || options === void 0 ? void 0 : options.outputFormat
|
|
47
|
+
});
|
|
48
|
+
return await this.s3Client.download(url);
|
|
49
|
+
}
|
|
50
|
+
async downloadStream(id, options) {
|
|
51
|
+
const { url } = await this.client.get(`/v2/documents/${id}/download-url`, {
|
|
52
|
+
output_format: options === null || options === void 0 ? void 0 : options.outputFormat
|
|
53
|
+
});
|
|
54
|
+
return (await this.s3Client.downloadStream(url));
|
|
55
|
+
}
|
|
56
|
+
async translate(file, filename, source, target, options) {
|
|
57
|
+
const uploadOptions = {
|
|
58
|
+
adaptTo: options === null || options === void 0 ? void 0 : options.adaptTo,
|
|
59
|
+
glossaries: options === null || options === void 0 ? void 0 : options.glossaries,
|
|
60
|
+
noTrace: options === null || options === void 0 ? void 0 : options.noTrace,
|
|
61
|
+
style: options === null || options === void 0 ? void 0 : options.style,
|
|
62
|
+
password: options === null || options === void 0 ? void 0 : options.password,
|
|
63
|
+
extractionParams: options === null || options === void 0 ? void 0 : options.extractionParams,
|
|
64
|
+
contentLength: options === null || options === void 0 ? void 0 : options.contentLength
|
|
65
|
+
};
|
|
66
|
+
const { id } = await this.upload(file, filename, source, target, uploadOptions);
|
|
67
|
+
const downloadOptions = (options === null || options === void 0 ? void 0 : options.outputFormat) ? { outputFormat: options.outputFormat } : undefined;
|
|
68
|
+
const pollingInterval = 2000;
|
|
69
|
+
const maxWaitTime = 1000 * 60 * 15; // 15 minutes
|
|
70
|
+
const start = Date.now();
|
|
71
|
+
while (Date.now() - start < maxWaitTime) {
|
|
72
|
+
await new Promise((resolve) => setTimeout(resolve, pollingInterval));
|
|
73
|
+
const { status, errorReason } = await this.status(id);
|
|
74
|
+
if (status === DocumentStatus.TRANSLATED)
|
|
75
|
+
return await this.download(id, downloadOptions);
|
|
76
|
+
if (status === DocumentStatus.ERROR) {
|
|
77
|
+
throw new errors_1.LaraApiError(500, "DocumentError", errorReason);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
throw new errors_1.TimeoutError();
|
|
81
|
+
}
|
|
82
|
+
async translateStream(file, filename, source, target, options) {
|
|
83
|
+
const uploadOptions = {
|
|
84
|
+
adaptTo: options === null || options === void 0 ? void 0 : options.adaptTo,
|
|
85
|
+
glossaries: options === null || options === void 0 ? void 0 : options.glossaries,
|
|
86
|
+
noTrace: options === null || options === void 0 ? void 0 : options.noTrace,
|
|
87
|
+
style: options === null || options === void 0 ? void 0 : options.style,
|
|
88
|
+
password: options === null || options === void 0 ? void 0 : options.password,
|
|
89
|
+
extractionParams: options === null || options === void 0 ? void 0 : options.extractionParams,
|
|
90
|
+
contentLength: options === null || options === void 0 ? void 0 : options.contentLength
|
|
91
|
+
};
|
|
92
|
+
const { id } = await this.upload(file, filename, source, target, uploadOptions);
|
|
93
|
+
const downloadOptions = (options === null || options === void 0 ? void 0 : options.outputFormat) ? { outputFormat: options.outputFormat } : undefined;
|
|
94
|
+
const pollingInterval = 2000;
|
|
95
|
+
const maxWaitTime = 1000 * 60 * 15; // 15 minutes
|
|
96
|
+
const start = Date.now();
|
|
97
|
+
while (Date.now() - start < maxWaitTime) {
|
|
98
|
+
await new Promise((resolve) => setTimeout(resolve, pollingInterval));
|
|
99
|
+
const { status, errorReason } = await this.status(id);
|
|
100
|
+
if (status === DocumentStatus.TRANSLATED)
|
|
101
|
+
return await this.downloadStream(id, downloadOptions);
|
|
102
|
+
if (status === DocumentStatus.ERROR) {
|
|
103
|
+
throw new errors_1.LaraApiError(500, "DocumentError", errorReason);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
throw new errors_1.TimeoutError();
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
exports.Documents = Documents;
|
package/lib/errors.d.ts
CHANGED
|
@@ -6,5 +6,6 @@ export declare class LaraApiError extends LaraError {
|
|
|
6
6
|
readonly statusCode: number;
|
|
7
7
|
readonly type: string;
|
|
8
8
|
readonly message: string;
|
|
9
|
-
|
|
9
|
+
readonly idTransaction?: string;
|
|
10
|
+
constructor(statusCode: number, type: string, message: string, idTransaction?: string);
|
|
10
11
|
}
|
package/lib/errors.js
CHANGED
|
@@ -8,11 +8,12 @@ class TimeoutError extends LaraError {
|
|
|
8
8
|
}
|
|
9
9
|
exports.TimeoutError = TimeoutError;
|
|
10
10
|
class LaraApiError extends LaraError {
|
|
11
|
-
constructor(statusCode, type, message) {
|
|
11
|
+
constructor(statusCode, type, message, idTransaction) {
|
|
12
12
|
super(message);
|
|
13
13
|
this.statusCode = statusCode;
|
|
14
14
|
this.type = type;
|
|
15
15
|
this.message = message;
|
|
16
|
+
this.idTransaction = idTransaction;
|
|
16
17
|
}
|
|
17
18
|
}
|
|
18
19
|
exports.LaraApiError = LaraApiError;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { LaraClient } from "./net/lara";
|
|
2
|
+
import type { MultiPartFile } from "./net/lara/client";
|
|
3
|
+
export interface Glossary {
|
|
4
|
+
readonly id: string;
|
|
5
|
+
readonly name: string;
|
|
6
|
+
readonly ownerId: string;
|
|
7
|
+
readonly createdAt: Date;
|
|
8
|
+
readonly updatedAt: Date;
|
|
9
|
+
}
|
|
10
|
+
export interface GlossaryImport {
|
|
11
|
+
readonly id: string;
|
|
12
|
+
readonly begin: number;
|
|
13
|
+
readonly end: number;
|
|
14
|
+
readonly channel: number;
|
|
15
|
+
readonly size: number;
|
|
16
|
+
readonly progress: number;
|
|
17
|
+
}
|
|
18
|
+
export interface GlossaryCounts {
|
|
19
|
+
unidirectional?: Record<string, number>;
|
|
20
|
+
multidirectional?: number;
|
|
21
|
+
}
|
|
22
|
+
export type GlossaryImportCallback = (glossaryImport: GlossaryImport) => void;
|
|
23
|
+
export declare class Glossaries {
|
|
24
|
+
private readonly client;
|
|
25
|
+
private readonly pollingInterval;
|
|
26
|
+
constructor(client: LaraClient);
|
|
27
|
+
list(): Promise<Glossary[]>;
|
|
28
|
+
create(name: string): Promise<Glossary>;
|
|
29
|
+
get(id: string): Promise<Glossary | null>;
|
|
30
|
+
delete(id: string): Promise<Glossary>;
|
|
31
|
+
update(id: string, name: string): Promise<Glossary>;
|
|
32
|
+
importCsv(id: string, csv: MultiPartFile, gzip?: boolean): Promise<GlossaryImport>;
|
|
33
|
+
getImportStatus(id: string): Promise<GlossaryImport>;
|
|
34
|
+
waitForImport(gImport: GlossaryImport, updateCallback?: GlossaryImportCallback, maxWaitTime?: number): Promise<GlossaryImport>;
|
|
35
|
+
counts(id: string): Promise<GlossaryCounts>;
|
|
36
|
+
export(id: string, contentType: "csv/table-uni", source?: string): Promise<string>;
|
|
37
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Glossaries = void 0;
|
|
4
|
+
const errors_1 = require("./errors");
|
|
5
|
+
class Glossaries {
|
|
6
|
+
constructor(client) {
|
|
7
|
+
this.client = client;
|
|
8
|
+
this.pollingInterval = 2000;
|
|
9
|
+
}
|
|
10
|
+
async list() {
|
|
11
|
+
return await this.client.get("/v2/glossaries");
|
|
12
|
+
}
|
|
13
|
+
async create(name) {
|
|
14
|
+
return await this.client.post("/v2/glossaries", { name });
|
|
15
|
+
}
|
|
16
|
+
async get(id) {
|
|
17
|
+
try {
|
|
18
|
+
return await this.client.get(`/v2/glossaries/${id}`);
|
|
19
|
+
}
|
|
20
|
+
catch (e) {
|
|
21
|
+
if (e instanceof errors_1.LaraApiError && e.statusCode === 404) {
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
throw e;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
async delete(id) {
|
|
28
|
+
return await this.client.delete(`/v2/glossaries/${id}`);
|
|
29
|
+
}
|
|
30
|
+
async update(id, name) {
|
|
31
|
+
return await this.client.put(`/v2/glossaries/${id}`, { name });
|
|
32
|
+
}
|
|
33
|
+
async importCsv(id, csv, gzip = false) {
|
|
34
|
+
return await this.client.post(`/v2/glossaries/${id}/import`, {
|
|
35
|
+
compression: gzip ? "gzip" : undefined
|
|
36
|
+
}, {
|
|
37
|
+
csv
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
async getImportStatus(id) {
|
|
41
|
+
return await this.client.get(`/v2/glossaries/imports/${id}`);
|
|
42
|
+
}
|
|
43
|
+
async waitForImport(gImport, updateCallback, maxWaitTime) {
|
|
44
|
+
const start = Date.now();
|
|
45
|
+
while (gImport.progress < 1.0) {
|
|
46
|
+
if (maxWaitTime && Date.now() - start > maxWaitTime)
|
|
47
|
+
throw new errors_1.TimeoutError();
|
|
48
|
+
await new Promise((resolve) => setTimeout(resolve, this.pollingInterval));
|
|
49
|
+
gImport = await this.getImportStatus(gImport.id);
|
|
50
|
+
if (updateCallback)
|
|
51
|
+
updateCallback(gImport);
|
|
52
|
+
}
|
|
53
|
+
return gImport;
|
|
54
|
+
}
|
|
55
|
+
async counts(id) {
|
|
56
|
+
return await this.client.get(`/v2/glossaries/${id}/counts`);
|
|
57
|
+
}
|
|
58
|
+
async export(id, contentType, source) {
|
|
59
|
+
return await this.client.get(`/v2/glossaries/${id}/export`, {
|
|
60
|
+
content_type: contentType,
|
|
61
|
+
source
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
exports.Glossaries = Glossaries;
|
package/lib/index.d.ts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
|
-
export { Credentials } from "./credentials";
|
|
1
|
+
export { AccessKey, AuthToken, Credentials } from "./credentials";
|
|
2
|
+
export { Document, DocumentDownloadOptions, DocumentStatus, Documents, DocumentTranslateOptions, DocumentUploadOptions } from "./documents";
|
|
2
3
|
export { LaraApiError, LaraError, TimeoutError } from "./errors";
|
|
3
|
-
export {
|
|
4
|
-
export {
|
|
5
|
-
export {
|
|
6
|
-
export {
|
|
4
|
+
export { Glossaries, GlossaryImport, GlossaryImportCallback } from "./glossaries";
|
|
5
|
+
export { Memories, Memory, MemoryImport, MemoryImportCallback } from "./memories";
|
|
6
|
+
export { MultiPartFile } from "./net/lara/client";
|
|
7
|
+
export type { LaraStream } from "./net/s3/laraStream";
|
|
8
|
+
export { DetectResult, NGGlossaryMatch, NGMemoryMatch, TextBlock, TextResult, TranslateOptions, Translator, TranslatorOptions } from "./translator";
|
|
9
|
+
export { version } from "./utils/sdk-version";
|
package/lib/index.js
CHANGED
|
@@ -1,17 +1,22 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Translator = exports.Memories = exports.
|
|
3
|
+
exports.version = exports.Translator = exports.Memories = exports.Glossaries = exports.TimeoutError = exports.LaraError = exports.LaraApiError = exports.Documents = exports.DocumentStatus = exports.Credentials = exports.AuthToken = exports.AccessKey = void 0;
|
|
4
4
|
var credentials_1 = require("./credentials");
|
|
5
|
+
Object.defineProperty(exports, "AccessKey", { enumerable: true, get: function () { return credentials_1.AccessKey; } });
|
|
6
|
+
Object.defineProperty(exports, "AuthToken", { enumerable: true, get: function () { return credentials_1.AuthToken; } });
|
|
5
7
|
Object.defineProperty(exports, "Credentials", { enumerable: true, get: function () { return credentials_1.Credentials; } });
|
|
8
|
+
var documents_1 = require("./documents");
|
|
9
|
+
Object.defineProperty(exports, "DocumentStatus", { enumerable: true, get: function () { return documents_1.DocumentStatus; } });
|
|
10
|
+
Object.defineProperty(exports, "Documents", { enumerable: true, get: function () { return documents_1.Documents; } });
|
|
6
11
|
var errors_1 = require("./errors");
|
|
7
12
|
Object.defineProperty(exports, "LaraApiError", { enumerable: true, get: function () { return errors_1.LaraApiError; } });
|
|
8
13
|
Object.defineProperty(exports, "LaraError", { enumerable: true, get: function () { return errors_1.LaraError; } });
|
|
9
14
|
Object.defineProperty(exports, "TimeoutError", { enumerable: true, get: function () { return errors_1.TimeoutError; } });
|
|
10
|
-
var
|
|
11
|
-
Object.defineProperty(exports, "
|
|
12
|
-
var
|
|
13
|
-
Object.defineProperty(exports, "
|
|
14
|
-
var translator_1 = require("./translator
|
|
15
|
-
Object.defineProperty(exports, "Documents", { enumerable: true, get: function () { return translator_1.Documents; } });
|
|
16
|
-
Object.defineProperty(exports, "Memories", { enumerable: true, get: function () { return translator_1.Memories; } });
|
|
15
|
+
var glossaries_1 = require("./glossaries");
|
|
16
|
+
Object.defineProperty(exports, "Glossaries", { enumerable: true, get: function () { return glossaries_1.Glossaries; } });
|
|
17
|
+
var memories_1 = require("./memories");
|
|
18
|
+
Object.defineProperty(exports, "Memories", { enumerable: true, get: function () { return memories_1.Memories; } });
|
|
19
|
+
var translator_1 = require("./translator");
|
|
17
20
|
Object.defineProperty(exports, "Translator", { enumerable: true, get: function () { return translator_1.Translator; } });
|
|
21
|
+
var sdk_version_1 = require("./utils/sdk-version");
|
|
22
|
+
Object.defineProperty(exports, "version", { enumerable: true, get: function () { return sdk_version_1.version; } });
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { LaraClient } from "./net/lara";
|
|
2
|
+
import type { MultiPartFile } from "./net/lara/client";
|
|
3
|
+
export interface Memory {
|
|
4
|
+
readonly id: string;
|
|
5
|
+
readonly createdAt: Date;
|
|
6
|
+
readonly updatedAt: Date;
|
|
7
|
+
readonly sharedAt: Date;
|
|
8
|
+
readonly name: string;
|
|
9
|
+
readonly externalId?: string;
|
|
10
|
+
readonly secret?: string;
|
|
11
|
+
readonly ownerId: string;
|
|
12
|
+
readonly collaboratorsCount: number;
|
|
13
|
+
}
|
|
14
|
+
export interface MemoryImport {
|
|
15
|
+
readonly id: string;
|
|
16
|
+
readonly begin: number;
|
|
17
|
+
readonly end: number;
|
|
18
|
+
readonly channel: number;
|
|
19
|
+
readonly size: number;
|
|
20
|
+
readonly progress: number;
|
|
21
|
+
}
|
|
22
|
+
export type MemoryImportCallback = (memoryImport: MemoryImport) => void;
|
|
23
|
+
export declare class Memories {
|
|
24
|
+
private readonly client;
|
|
25
|
+
private readonly pollingInterval;
|
|
26
|
+
constructor(client: LaraClient);
|
|
27
|
+
list(): Promise<Memory[]>;
|
|
28
|
+
create(name: string, externalId?: string): Promise<Memory>;
|
|
29
|
+
get(id: string): Promise<Memory | null>;
|
|
30
|
+
delete(id: string): Promise<Memory>;
|
|
31
|
+
update(id: string, name: string): Promise<Memory>;
|
|
32
|
+
connect<T extends string | string[]>(ids: T): Promise<T extends string ? Memory : Memory[]>;
|
|
33
|
+
importTmx(id: string, tmx: MultiPartFile, gzip?: boolean): Promise<MemoryImport>;
|
|
34
|
+
addTranslation(id: string | string[], source: string, target: string, sentence: string, translation: string, tuid?: string, sentenceBefore?: string, sentenceAfter?: string, headers?: Record<string, string>): Promise<MemoryImport>;
|
|
35
|
+
deleteTranslation(id: string | string[], source: string, target: string, sentence?: string, translation?: string, tuid?: string, sentenceBefore?: string, sentenceAfter?: string): Promise<MemoryImport>;
|
|
36
|
+
getImportStatus(id: string): Promise<MemoryImport>;
|
|
37
|
+
waitForImport(mImport: MemoryImport, updateCallback?: MemoryImportCallback, maxWaitTime?: number): Promise<MemoryImport>;
|
|
38
|
+
}
|