@translated/lara 1.3.3 → 1.4.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/lib/index.d.ts CHANGED
@@ -3,3 +3,4 @@ export { Credentials } from "./credentials";
3
3
  export { LaraError, TimeoutError, LaraApiError } from "./errors";
4
4
  export { Translator, TranslatorOptions, Memories, MemoryImportCallback, TranslateOptions } from "./translator/translator";
5
5
  export { Memory, MemoryImport, TextBlock, TextResult } from "./translator/models";
6
+ export { MultiPartFile } from "./net/client";
@@ -1,7 +1,8 @@
1
- import { BaseURL, ClientResponse, LaraClient } from "./client";
1
+ import { BaseURL, ClientResponse, LaraClient, MultiPartFile } from "./client";
2
2
  /** @internal */
3
3
  export declare class BrowserLaraClient extends LaraClient {
4
4
  private readonly baseUrl;
5
5
  constructor(baseUrl: BaseURL, accessKeyId: string, accessKeySecret: string);
6
6
  protected send(path: string, headers: Record<string, string>, body?: Record<string, any>): Promise<ClientResponse>;
7
+ protected wrapMultiPartFile(file: MultiPartFile): File;
7
8
  }
@@ -41,5 +41,10 @@ class BrowserLaraClient extends client_1.LaraClient {
41
41
  });
42
42
  return { statusCode: response.status, body: await response.json() };
43
43
  }
44
+ wrapMultiPartFile(file) {
45
+ if (file instanceof File)
46
+ return file;
47
+ throw new TypeError(`Invalid file input in the browser. Expected an instance of File but received ${typeof file}.`);
48
+ }
44
49
  }
45
50
  exports.BrowserLaraClient = BrowserLaraClient;
@@ -1,3 +1,5 @@
1
+ /// <reference types="node" />
2
+ import { Readable } from "node:stream";
1
3
  type HttpMethod = "GET" | "POST" | "PUT" | "DELETE";
2
4
  /** @internal */
3
5
  export type BaseURL = {
@@ -10,6 +12,9 @@ export type ClientResponse = {
10
12
  statusCode: number;
11
13
  body: any;
12
14
  };
15
+ export type BrowserMultiPartFile = File;
16
+ export type NodeMultiPartFile = Readable | string;
17
+ export type MultiPartFile = BrowserMultiPartFile | NodeMultiPartFile;
13
18
  /** @internal */
14
19
  export declare abstract class LaraClient {
15
20
  private readonly crypto;
@@ -20,10 +25,11 @@ export declare abstract class LaraClient {
20
25
  setExtraHeader(name: string, value: string): void;
21
26
  get<T>(path: string, params?: Record<string, any>): Promise<T>;
22
27
  delete<T>(path: string, params?: Record<string, any>): Promise<T>;
23
- post<T>(path: string, body?: Record<string, any>, files?: Record<string, string>): Promise<T>;
24
- put<T>(path: string, body?: Record<string, any>, files?: Record<string, string>): Promise<T>;
25
- protected request<T>(method: HttpMethod, path: string, body?: Record<string, any>, files?: Record<string, any>): Promise<T>;
28
+ post<T>(path: string, body?: Record<string, any>, files?: Record<string, MultiPartFile>): Promise<T>;
29
+ put<T>(path: string, body?: Record<string, any>, files?: Record<string, MultiPartFile>): Promise<T>;
30
+ protected request<T>(method: HttpMethod, path: string, body?: Record<string, any>, files?: Record<string, MultiPartFile>): Promise<T>;
26
31
  private sign;
27
32
  protected abstract send(path: string, headers: Record<string, string>, body?: Record<string, any>): Promise<ClientResponse>;
33
+ protected abstract wrapMultiPartFile(file: MultiPartFile): any;
28
34
  }
29
35
  export {};
package/lib/net/client.js CHANGED
@@ -73,6 +73,9 @@ class LaraClient {
73
73
  }
74
74
  let requestBody = undefined;
75
75
  if (files) {
76
+ // validate files
77
+ for (const [key, file] of Object.entries(files))
78
+ files[key] = this.wrapMultiPartFile(file);
76
79
  headers["Content-Type"] = "multipart/form-data";
77
80
  requestBody = Object.assign({}, files, body);
78
81
  }
@@ -1,8 +1,11 @@
1
- import { BaseURL, ClientResponse, LaraClient } from "./client";
1
+ /// <reference types="node" />
2
+ import { BaseURL, ClientResponse, LaraClient, MultiPartFile } from "./client";
3
+ import { Readable } from "stream";
2
4
  /** @internal */
3
5
  export declare class NodeLaraClient extends LaraClient {
4
6
  private readonly baseUrl;
5
7
  private readonly agent;
6
8
  constructor(baseUrl: BaseURL, accessKeyId: string, accessKeySecret: string);
7
9
  protected send(path: string, headers: Record<string, string>, body?: Record<string, any>): Promise<ClientResponse>;
10
+ protected wrapMultiPartFile(file: MultiPartFile): Readable;
8
11
  }
@@ -8,6 +8,8 @@ const http_1 = __importDefault(require("http"));
8
8
  const https_1 = __importDefault(require("https"));
9
9
  const form_data_1 = __importDefault(require("form-data"));
10
10
  const client_1 = require("./client");
11
+ const stream_1 = require("stream");
12
+ const fs_1 = __importDefault(require("fs"));
11
13
  /** @internal */
12
14
  class NodeLaraClient extends client_1.LaraClient {
13
15
  constructor(baseUrl, accessKeyId, accessKeySecret) {
@@ -70,9 +72,19 @@ class NodeLaraClient extends client_1.LaraClient {
70
72
  }
71
73
  else if (requestBody) {
72
74
  req.write(requestBody);
75
+ req.end();
76
+ }
77
+ else {
78
+ req.end();
73
79
  }
74
- req.end();
75
80
  });
76
81
  }
82
+ wrapMultiPartFile(file) {
83
+ if (typeof file === 'string')
84
+ file = fs_1.default.createReadStream(file);
85
+ if (file instanceof stream_1.Readable)
86
+ return file;
87
+ throw new TypeError(`Invalid file input in Node.js. Expected a Readable stream or a valid file path, but received ${typeof file}.`);
88
+ }
77
89
  }
78
90
  exports.NodeLaraClient = NodeLaraClient;
@@ -1 +1 @@
1
- export declare const version = "1.3.3";
1
+ export declare const version = "1.4.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.3.3";
4
+ exports.version = "1.4.0";
@@ -1,6 +1,7 @@
1
1
  import { Credentials } from "../credentials";
2
2
  import { LaraClient } from "../net";
3
3
  import { Memory, MemoryImport, TextBlock, TextResult } from "./models";
4
+ import { MultiPartFile } from "../net/client";
4
5
  export type TranslatorOptions = {
5
6
  serverUrl?: string;
6
7
  };
@@ -15,7 +16,7 @@ export declare class Memories {
15
16
  delete(id: string): Promise<Memory>;
16
17
  update(id: string, name: string): Promise<Memory>;
17
18
  connect<T extends string | string[]>(ids: T): Promise<T extends string ? Memory : Memory[]>;
18
- importTmx(id: string, tmx: any, gzip?: boolean): Promise<MemoryImport>;
19
+ importTmx(id: string, tmx: MultiPartFile, gzip?: boolean): Promise<MemoryImport>;
19
20
  addTranslation(id: string | string[], source: string, target: string, sentence: string, translation: string, tuid?: string, sentenceBefore?: string, sentenceAfter?: string): Promise<MemoryImport>;
20
21
  deleteTranslation(id: string | string[], source: string, target: string, sentence: string, translation: string, tuid?: string, sentenceBefore?: string, sentenceAfter?: string): Promise<MemoryImport>;
21
22
  getImportStatus(id: string): Promise<MemoryImport>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@translated/lara",
3
- "version": "1.3.3",
3
+ "version": "1.4.0",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "engines": {
@@ -33,7 +33,7 @@
33
33
  },
34
34
  "description": "Official Lara SDK for JavaScript and Node.js",
35
35
  "dependencies": {
36
- "form-data": "^4.0.0"
36
+ "form-data": "^4.0.1"
37
37
  },
38
38
  "devDependencies": {
39
39
  "@types/node": "^20.14.2",