@translated/lara 1.4.0 → 1.5.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 CHANGED
@@ -1,3 +1,7 @@
1
1
  # Lara Node.js Library
2
2
 
3
- This library is a Node.js library for the Lara platform. It provides a simple way to interact with the Lara API.
3
+ This SDK empowers you to build your own branded translation AI leveraging our translation fine-tuned language model.
4
+
5
+ All major translation features are accessible, making it easy to integrate and customize for your needs.
6
+
7
+ Lara’s SDK full documentation is available at https://developers.laratranslate.com/
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.default = instance;
3
4
  const browser_crypto_1 = require("./browser-crypto");
4
5
  const node_crypto_1 = require("./node-crypto");
5
6
  let _instance = undefined;
@@ -12,4 +13,3 @@ function instance() {
12
13
  }
13
14
  return _instance;
14
15
  }
15
- exports.default = instance;
@@ -15,13 +15,23 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
15
15
  }) : function(o, v) {
16
16
  o["default"] = v;
17
17
  });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
25
35
  Object.defineProperty(exports, "__esModule", { value: true });
26
36
  exports.NodeCrypto = void 0;
27
37
  const crypto = __importStar(require("crypto"));
package/lib/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  export { version } from "./sdk-version";
2
2
  export { Credentials } from "./credentials";
3
3
  export { LaraError, TimeoutError, LaraApiError } from "./errors";
4
- export { Translator, TranslatorOptions, Memories, MemoryImportCallback, TranslateOptions } from "./translator/translator";
5
- export { Memory, MemoryImport, TextBlock, TextResult } from "./translator/models";
4
+ export { Translator, TranslatorOptions, Memories, MemoryImportCallback, TranslateOptions, Documents, DocumentTranslateOptions } from "./translator/translator";
5
+ export { Memory, MemoryImport, TextBlock, TextResult, Document, DocumentUploadOptions, DocumentDownloadOptions } from "./translator/models";
6
6
  export { MultiPartFile } from "./net/client";
package/lib/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Memories = exports.Translator = exports.LaraApiError = exports.TimeoutError = exports.LaraError = exports.Credentials = exports.version = void 0;
3
+ exports.Documents = exports.Memories = exports.Translator = exports.LaraApiError = exports.TimeoutError = exports.LaraError = exports.Credentials = exports.version = void 0;
4
4
  var sdk_version_1 = require("./sdk-version");
5
5
  Object.defineProperty(exports, "version", { enumerable: true, get: function () { return sdk_version_1.version; } });
6
6
  var credentials_1 = require("./credentials");
@@ -12,3 +12,4 @@ Object.defineProperty(exports, "LaraApiError", { enumerable: true, get: function
12
12
  var translator_1 = require("./translator/translator");
13
13
  Object.defineProperty(exports, "Translator", { enumerable: true, get: function () { return translator_1.Translator; } });
14
14
  Object.defineProperty(exports, "Memories", { enumerable: true, get: function () { return translator_1.Memories; } });
15
+ Object.defineProperty(exports, "Documents", { enumerable: true, get: function () { return translator_1.Documents; } });
@@ -1,4 +1,3 @@
1
- /// <reference types="node" />
2
1
  import { Readable } from "node:stream";
3
2
  type HttpMethod = "GET" | "POST" | "PUT" | "DELETE";
4
3
  /** @internal */
package/lib/net/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.LaraClient = void 0;
4
+ exports.default = create;
4
5
  const browser_client_1 = require("./browser-client");
5
6
  const node_client_1 = require("./node-client");
6
7
  var client_1 = require("./client");
@@ -20,4 +21,3 @@ function create(accessKeyId, accessKeySecret, baseUrl) {
20
21
  else
21
22
  return new node_client_1.NodeLaraClient(parsedURL, accessKeyId, accessKeySecret);
22
23
  }
23
- exports.default = create;
@@ -1,4 +1,3 @@
1
- /// <reference types="node" />
2
1
  import { BaseURL, ClientResponse, LaraClient, MultiPartFile } from "./client";
3
2
  import { Readable } from "stream";
4
3
  /** @internal */
@@ -0,0 +1,10 @@
1
+ import { S3Client } from "./client";
2
+ import { S3UploadFields } from "../../translator/translator";
3
+ import { MultiPartFile } from "../client";
4
+ /** @internal */
5
+ export declare class BrowserS3Client extends S3Client {
6
+ constructor();
7
+ _upload(url: string, fields: S3UploadFields, file: File): Promise<void>;
8
+ download(url: string): Promise<Blob>;
9
+ wrapMultiPartFile(file: MultiPartFile): File;
10
+ }
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BrowserS3Client = void 0;
4
+ const client_1 = require("./client");
5
+ /** @internal */
6
+ class BrowserS3Client extends client_1.S3Client {
7
+ constructor() {
8
+ super();
9
+ }
10
+ async _upload(url, fields, file) {
11
+ const formdata = new FormData();
12
+ for (const [key, value] of Object.entries(fields)) {
13
+ formdata.append(key, value);
14
+ }
15
+ formdata.append("file", file);
16
+ await fetch(url, {
17
+ method: 'POST',
18
+ body: formdata,
19
+ });
20
+ }
21
+ async download(url) {
22
+ const response = await fetch(url);
23
+ return response.blob();
24
+ }
25
+ wrapMultiPartFile(file) {
26
+ if (file instanceof File)
27
+ return file;
28
+ throw new TypeError(`Invalid file input in the browser. Expected an instance of File but received ${typeof file}.`);
29
+ }
30
+ }
31
+ exports.BrowserS3Client = BrowserS3Client;
@@ -0,0 +1,9 @@
1
+ import { S3UploadFields } from "../../translator/translator";
2
+ import { MultiPartFile } from "../client";
3
+ /** @internal */
4
+ export declare abstract class S3Client {
5
+ upload(url: string, fields: S3UploadFields, file: MultiPartFile): Promise<void>;
6
+ protected abstract _upload(url: string, fields: S3UploadFields, file: any): Promise<void>;
7
+ protected abstract download(url: string): Promise<Blob | Buffer>;
8
+ protected abstract wrapMultiPartFile(file: MultiPartFile): any;
9
+ }
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.S3Client = void 0;
4
+ /** @internal */
5
+ class S3Client {
6
+ async upload(url, fields, file) {
7
+ return this._upload(url, fields, this.wrapMultiPartFile(file));
8
+ }
9
+ }
10
+ exports.S3Client = S3Client;
@@ -0,0 +1,3 @@
1
+ import { BrowserS3Client } from "./browser-client";
2
+ import { NodeS3Client } from "./node-client";
3
+ export default function create(): BrowserS3Client | NodeS3Client;
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.default = create;
4
+ const browser_client_1 = require("./browser-client");
5
+ const node_client_1 = require("./node-client");
6
+ function create() {
7
+ if (typeof window !== "undefined") {
8
+ return new browser_client_1.BrowserS3Client();
9
+ }
10
+ else {
11
+ return new node_client_1.NodeS3Client();
12
+ }
13
+ }
@@ -0,0 +1,10 @@
1
+ import { S3Client } from "./client";
2
+ import { Readable } from "stream";
3
+ import { S3UploadFields } from "../../translator/translator";
4
+ import { MultiPartFile } from "../client";
5
+ /** @internal */
6
+ export declare class NodeS3Client extends S3Client {
7
+ protected _upload(url: string, fields: S3UploadFields, file: Readable): Promise<void>;
8
+ download(url: string): Promise<Buffer>;
9
+ wrapMultiPartFile(file: MultiPartFile): Readable;
10
+ }
@@ -0,0 +1,47 @@
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.NodeS3Client = void 0;
7
+ const client_1 = require("./client");
8
+ const stream_1 = require("stream");
9
+ const form_data_1 = __importDefault(require("form-data"));
10
+ const https_1 = __importDefault(require("https"));
11
+ const fs_1 = __importDefault(require("fs"));
12
+ /** @internal */
13
+ class NodeS3Client extends client_1.S3Client {
14
+ async _upload(url, fields, file) {
15
+ const formData = new form_data_1.default();
16
+ for (const [key, value] of Object.entries(fields)) {
17
+ formData.append(key, value);
18
+ }
19
+ formData.append("file", file);
20
+ return new Promise((resolve, reject) => {
21
+ formData.submit(url, (err) => {
22
+ if (err)
23
+ return reject(err);
24
+ resolve();
25
+ });
26
+ });
27
+ }
28
+ async download(url) {
29
+ return new Promise((resolve, reject) => {
30
+ const req = https_1.default.request(url, { method: 'GET' }, (res) => {
31
+ const chunks = [];
32
+ res.on('data', (chunk) => chunks.push(Buffer.from(chunk)));
33
+ res.on('end', () => resolve(Buffer.concat(chunks)));
34
+ });
35
+ req.on('error', reject);
36
+ req.end();
37
+ });
38
+ }
39
+ wrapMultiPartFile(file) {
40
+ if (typeof file === 'string')
41
+ file = fs_1.default.createReadStream(file);
42
+ if (file instanceof stream_1.Readable)
43
+ return file;
44
+ throw new TypeError(`Invalid file input in Node.js. Expected a Readable stream or a valid file path, but received ${typeof file}.`);
45
+ }
46
+ }
47
+ exports.NodeS3Client = NodeS3Client;
@@ -1 +1 @@
1
- export declare const version = "1.4.0";
1
+ export declare const version = "1.5.0";
@@ -1,4 +1,4 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.version = void 0;
4
- exports.version = "1.4.0";
4
+ exports.version = "1.5.0";
@@ -17,6 +17,36 @@ export interface MemoryImport {
17
17
  readonly size: number;
18
18
  readonly progress: number;
19
19
  }
20
+ export declare enum DocumentStatus {
21
+ INITIALIZED = "initialized",// just been created
22
+ ANALYZING = "analyzing",// being analyzed for language detection and chars count
23
+ PAUSED = "paused",// paused after analysis, needs user confirm
24
+ READY = "ready",// ready to be translated
25
+ TRANSLATING = "translating",
26
+ TRANSLATED = "translated",
27
+ ERROR = "error"
28
+ }
29
+ export type DocumentUploadOptions = {
30
+ adaptTo?: string[];
31
+ };
32
+ export type DocumentDownloadOptions = {
33
+ outputFormat?: string;
34
+ };
35
+ export interface DocumentOptions extends DocumentUploadOptions {
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
+ }
20
50
  export interface TextBlock {
21
51
  readonly text: string;
22
52
  readonly translatable?: boolean;
@@ -1,2 +1,13 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DocumentStatus = void 0;
4
+ var DocumentStatus;
5
+ (function (DocumentStatus) {
6
+ DocumentStatus["INITIALIZED"] = "initialized";
7
+ DocumentStatus["ANALYZING"] = "analyzing";
8
+ DocumentStatus["PAUSED"] = "paused";
9
+ DocumentStatus["READY"] = "ready";
10
+ DocumentStatus["TRANSLATING"] = "translating";
11
+ DocumentStatus["TRANSLATED"] = "translated";
12
+ DocumentStatus["ERROR"] = "error";
13
+ })(DocumentStatus || (exports.DocumentStatus = DocumentStatus = {}));
@@ -1,6 +1,6 @@
1
1
  import { Credentials } from "../credentials";
2
2
  import { LaraClient } from "../net";
3
- import { Memory, MemoryImport, TextBlock, TextResult } from "./models";
3
+ import { Document, DocumentDownloadOptions, DocumentUploadOptions, Memory, MemoryImport, TextBlock, TextResult } from "./models";
4
4
  import { MultiPartFile } from "../net/client";
5
5
  export type TranslatorOptions = {
6
6
  serverUrl?: string;
@@ -33,9 +33,25 @@ export type TranslateOptions = {
33
33
  useCache?: boolean | "overwrite";
34
34
  cacheTTLSeconds?: number;
35
35
  };
36
+ export type DocumentTranslateOptions = DocumentUploadOptions & DocumentDownloadOptions;
37
+ export type S3UploadFields = {
38
+ acl: string;
39
+ bucket: string;
40
+ key: string;
41
+ };
42
+ export declare class Documents {
43
+ private readonly client;
44
+ private readonly s3Client;
45
+ constructor(client: LaraClient);
46
+ upload(file: MultiPartFile, filename: string, source: string | null, target: string, options?: DocumentUploadOptions): Promise<Document>;
47
+ status(id: string): Promise<Document>;
48
+ download(id: string, options?: DocumentDownloadOptions): Promise<Blob | Buffer<ArrayBufferLike>>;
49
+ translate(file: MultiPartFile, filename: string, source: string | null, target: string, options?: DocumentTranslateOptions): Promise<Blob | Buffer<ArrayBufferLike>>;
50
+ }
36
51
  export declare class Translator {
37
52
  protected readonly client: LaraClient;
38
53
  readonly memories: Memories;
54
+ readonly documents: Documents;
39
55
  constructor(credentials: Credentials, options?: TranslatorOptions);
40
56
  getLanguages(): Promise<string[]>;
41
57
  translate<T extends string | string[] | TextBlock[]>(text: T, source: string | null, target: string, options?: TranslateOptions): Promise<TextResult<T>>;
@@ -3,8 +3,10 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.Translator = exports.Memories = void 0;
6
+ exports.Translator = exports.Documents = exports.Memories = void 0;
7
7
  const net_1 = __importDefault(require("../net"));
8
+ const s3_1 = __importDefault(require("../net/s3"));
9
+ const models_1 = require("./models");
8
10
  const errors_1 = require("../errors");
9
11
  class Memories {
10
12
  constructor(client) {
@@ -102,10 +104,55 @@ class Memories {
102
104
  }
103
105
  }
104
106
  exports.Memories = Memories;
107
+ class Documents {
108
+ constructor(client) {
109
+ this.client = client;
110
+ this.s3Client = (0, s3_1.default)();
111
+ }
112
+ async upload(file, filename, source, target, options) {
113
+ const { url, fields } = await this.client.get(`/documents/upload-url`, { filename });
114
+ await this.s3Client.upload(url, fields, file);
115
+ return this.client.post('/documents', {
116
+ source,
117
+ target,
118
+ s3key: fields.key,
119
+ adapt_to: options === null || options === void 0 ? void 0 : options.adaptTo,
120
+ });
121
+ }
122
+ async status(id) {
123
+ return await this.client.get(`/documents/${id}`);
124
+ }
125
+ async download(id, options) {
126
+ const { url } = await this.client.get(`/documents/${id}/download-url`, {
127
+ output_format: options === null || options === void 0 ? void 0 : options.outputFormat,
128
+ });
129
+ return await this.s3Client.download(url);
130
+ }
131
+ async translate(file, filename, source, target, options) {
132
+ const uploadOptions = (options === null || options === void 0 ? void 0 : options.adaptTo) ? { adaptTo: options.adaptTo } : undefined;
133
+ const { id } = await this.upload(file, filename, source, target, uploadOptions);
134
+ const downloadOptions = (options === null || options === void 0 ? void 0 : options.outputFormat) ? { outputFormat: options.outputFormat } : undefined;
135
+ const pollingInterval = 2000;
136
+ const maxWaitTime = 1000 * 60 * 15; // 15 minutes
137
+ const start = Date.now();
138
+ while (Date.now() - start < maxWaitTime) {
139
+ await new Promise(resolve => setTimeout(resolve, pollingInterval));
140
+ const { status, errorReason } = await this.status(id);
141
+ if (status === models_1.DocumentStatus.TRANSLATED)
142
+ return await this.download(id, downloadOptions);
143
+ if (status === models_1.DocumentStatus.ERROR) {
144
+ throw new errors_1.LaraApiError(500, "DocumentError", errorReason);
145
+ }
146
+ }
147
+ throw new errors_1.TimeoutError();
148
+ }
149
+ }
150
+ exports.Documents = Documents;
105
151
  class Translator {
106
152
  constructor(credentials, options) {
107
153
  this.client = (0, net_1.default)(credentials.accessKeyId, credentials.accessKeySecret, options === null || options === void 0 ? void 0 : options.serverUrl);
108
154
  this.memories = new Memories(this.client);
155
+ this.documents = new Documents(this.client);
109
156
  }
110
157
  async getLanguages() {
111
158
  return await this.client.get("/languages");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@translated/lara",
3
- "version": "1.4.0",
3
+ "version": "1.5.0",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "engines": {
@@ -22,23 +22,23 @@
22
22
  "api"
23
23
  ],
24
24
  "author": {
25
- "name": "Davide Caroselli",
26
- "email": "davide.caroselli@translated.com"
25
+ "name": "Translated",
26
+ "email": "support@laratranslate.com"
27
27
  },
28
28
  "license": "MIT",
29
- "homepage": "https://lara.translated.com",
29
+ "homepage": "https://laratranslate.com",
30
30
  "repository": {
31
31
  "type": "git",
32
32
  "url": "git+ssh://git@github.com/translated/lara-node.git"
33
33
  },
34
34
  "description": "Official Lara SDK for JavaScript and Node.js",
35
35
  "dependencies": {
36
- "form-data": "^4.0.1"
36
+ "form-data": "^4.0.2"
37
37
  },
38
38
  "devDependencies": {
39
- "@types/node": "^20.14.2",
40
- "typescript": "^5.4.5",
41
- "webpack": "^5.97.1",
39
+ "@types/node": "^22.15.18",
40
+ "typescript": "^5.8.3",
41
+ "webpack": "^5.99.8",
42
42
  "webpack-cli": "^5.1.4"
43
43
  }
44
44
  }