@translated/lara 1.6.6 → 2.0.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/lib/Documents.d.ts +12 -0
- package/lib/Documents.js +71 -0
- package/lib/Glossaries.d.ts +17 -0
- package/lib/Glossaries.js +68 -0
- package/lib/Memories.d.ts +18 -0
- package/lib/Memories.js +95 -0
- package/lib/Translator.d.ts +24 -0
- package/lib/Translator.js +152 -0
- package/lib/index.d.ts +2 -6
- package/lib/index.js +2 -17
- package/lib/models/Authentication.d.ts +12 -0
- package/lib/models/Authentication.js +1 -0
- package/lib/models/UploadableFile.d.ts +1 -0
- package/lib/models/UploadableFile.js +1 -0
- package/lib/models/documents.d.ts +51 -0
- package/lib/{translator/models.js → models/documents.js} +2 -6
- package/lib/models/glossaries.d.ts +19 -0
- package/lib/models/glossaries.js +1 -0
- package/lib/models/index.d.ts +6 -0
- package/lib/models/index.js +6 -0
- package/lib/models/memories.d.ts +19 -0
- package/lib/models/memories.js +1 -0
- package/lib/models/translator.d.ts +48 -0
- package/lib/models/translator.js +1 -0
- package/lib/sdk-version.js +1 -4
- package/lib/utils/errors.js +12 -0
- package/lib/utils/formdata/browser.d.ts +2 -0
- package/lib/utils/formdata/browser.js +15 -0
- package/lib/utils/formdata/index.d.ts +4 -0
- package/lib/utils/formdata/index.js +10 -0
- package/lib/utils/formdata/node.d.ts +5 -0
- package/lib/utils/formdata/node.js +16 -0
- package/lib/utils/sleep.d.ts +2 -0
- package/lib/utils/sleep.js +1 -0
- package/lib/utils/toCamelCase.d.ts +2 -0
- package/lib/utils/toCamelCase.js +16 -0
- package/lib/utils/toSnakeCase.js +1 -3
- package/package.json +23 -13
- package/lib/credentials.d.ts +0 -5
- package/lib/credentials.js +0 -10
- package/lib/crypto/browser-crypto.d.ts +0 -11
- package/lib/crypto/browser-crypto.js +0 -28
- package/lib/crypto/index.d.ts +0 -3
- package/lib/crypto/index.js +0 -15
- package/lib/crypto/node-crypto.d.ts +0 -6
- package/lib/crypto/node-crypto.js +0 -55
- package/lib/crypto/portable-crypto.d.ts +0 -5
- package/lib/crypto/portable-crypto.js +0 -2
- package/lib/errors.js +0 -18
- 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/index.js +0 -23
- package/lib/net/node-client.d.ts +0 -10
- package/lib/net/node-client.js +0 -103
- package/lib/net/s3/browser-client.d.ts +0 -9
- package/lib/net/s3/browser-client.js +0 -28
- package/lib/net/s3/client.d.ts +0 -9
- package/lib/net/s3/client.js +0 -10
- package/lib/net/s3/index.d.ts +0 -3
- package/lib/net/s3/index.js +0 -13
- package/lib/net/s3/node-client.d.ts +0 -10
- package/lib/net/s3/node-client.js +0 -47
- package/lib/translator/models.d.ts +0 -105
- package/lib/translator/translator.d.ts +0 -80
- package/lib/translator/translator.js +0 -265
- /package/lib/{errors.d.ts → utils/errors.d.ts} +0 -0
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
export interface TranslatorOptions {
|
|
2
|
+
serverUrl?: string;
|
|
3
|
+
extraHeaders?: Record<string, any>;
|
|
4
|
+
}
|
|
5
|
+
export interface TextBlock {
|
|
6
|
+
readonly text: string;
|
|
7
|
+
readonly translatable?: boolean;
|
|
8
|
+
}
|
|
9
|
+
export type TranslationStyle = 'faithful' | 'fluid' | 'creative';
|
|
10
|
+
export type TranslateOptions = {
|
|
11
|
+
sourceHint?: string;
|
|
12
|
+
adaptTo?: string[];
|
|
13
|
+
instructions?: string[];
|
|
14
|
+
glossaries?: string[];
|
|
15
|
+
contentType?: string;
|
|
16
|
+
multiline?: boolean;
|
|
17
|
+
timeoutInMillis?: number;
|
|
18
|
+
priority?: 'normal' | 'background';
|
|
19
|
+
useCache?: boolean | 'overwrite';
|
|
20
|
+
cacheTTLSeconds?: number;
|
|
21
|
+
noTrace?: boolean;
|
|
22
|
+
verbose?: boolean;
|
|
23
|
+
headers?: Record<string, string>;
|
|
24
|
+
style?: TranslationStyle;
|
|
25
|
+
};
|
|
26
|
+
export interface NGMemoryMatch {
|
|
27
|
+
memory: string;
|
|
28
|
+
tuid?: string;
|
|
29
|
+
language: [string, string];
|
|
30
|
+
sentence: string;
|
|
31
|
+
translation: string;
|
|
32
|
+
score: number;
|
|
33
|
+
}
|
|
34
|
+
export interface NGGlossaryMatch {
|
|
35
|
+
glossary: string;
|
|
36
|
+
language: [string, string];
|
|
37
|
+
term: string;
|
|
38
|
+
translation: string;
|
|
39
|
+
}
|
|
40
|
+
export interface TextResult<T extends string | string[] | TextBlock[]> {
|
|
41
|
+
readonly contentType: string;
|
|
42
|
+
readonly sourceLanguage: string;
|
|
43
|
+
readonly translation: T;
|
|
44
|
+
readonly adaptedTo?: string[];
|
|
45
|
+
readonly glossaries?: string[];
|
|
46
|
+
readonly adaptedToMatches?: NGMemoryMatch[] | NGMemoryMatch[][];
|
|
47
|
+
readonly glossariesMatches?: NGGlossaryMatch[] | NGGlossaryMatch[][];
|
|
48
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/lib/sdk-version.js
CHANGED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export class LaraError extends Error {
|
|
2
|
+
}
|
|
3
|
+
export class TimeoutError extends LaraError {
|
|
4
|
+
}
|
|
5
|
+
export class LaraApiError extends LaraError {
|
|
6
|
+
constructor(statusCode, type, message) {
|
|
7
|
+
super(message);
|
|
8
|
+
this.statusCode = statusCode;
|
|
9
|
+
this.type = type;
|
|
10
|
+
this.message = message;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export default (data) => {
|
|
2
|
+
const formData = new FormData();
|
|
3
|
+
for (const [key, value] of Object.entries(data)) {
|
|
4
|
+
if (!value)
|
|
5
|
+
continue;
|
|
6
|
+
if (Array.isArray(value)) {
|
|
7
|
+
for (const v of value)
|
|
8
|
+
formData.append(key, v);
|
|
9
|
+
}
|
|
10
|
+
else {
|
|
11
|
+
formData.append(key, value);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
return [formData, {}];
|
|
15
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import FormData from 'form-data';
|
|
2
|
+
export default (data) => {
|
|
3
|
+
const formData = new FormData();
|
|
4
|
+
for (const [key, value] of Object.entries(data)) {
|
|
5
|
+
if (!value)
|
|
6
|
+
continue;
|
|
7
|
+
if (Array.isArray(value)) {
|
|
8
|
+
for (const v of value)
|
|
9
|
+
formData.append(key, v);
|
|
10
|
+
}
|
|
11
|
+
else {
|
|
12
|
+
formData.append(key, value);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
return [formData, { ...formData.getHeaders() }];
|
|
16
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default (millis) => new Promise((resolve) => setTimeout(resolve, millis));
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
const toCamelCase = (content) => {
|
|
2
|
+
if (typeof content === 'string')
|
|
3
|
+
return content;
|
|
4
|
+
if (Array.isArray(content))
|
|
5
|
+
return content.map(toCamelCase);
|
|
6
|
+
if (typeof content === 'object' && content !== null) {
|
|
7
|
+
const result = {};
|
|
8
|
+
for (const [key, value] of Object.entries(content)) {
|
|
9
|
+
const camelKey = key.replace(/_([a-z])/g, (_, letter) => letter.toUpperCase());
|
|
10
|
+
result[camelKey] = toCamelCase(value);
|
|
11
|
+
}
|
|
12
|
+
return result;
|
|
13
|
+
}
|
|
14
|
+
return content;
|
|
15
|
+
};
|
|
16
|
+
export default toCamelCase;
|
package/lib/utils/toSnakeCase.js
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
1
|
const toSnakeCase = (content) => {
|
|
4
2
|
if (typeof content === 'string')
|
|
5
3
|
return content;
|
|
@@ -15,4 +13,4 @@ const toSnakeCase = (content) => {
|
|
|
15
13
|
}
|
|
16
14
|
return content;
|
|
17
15
|
};
|
|
18
|
-
|
|
16
|
+
export default toSnakeCase;
|
package/package.json
CHANGED
|
@@ -1,16 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@translated/lara",
|
|
3
|
-
"version": "
|
|
4
|
-
"main": "lib/index.js",
|
|
5
|
-
"types": "lib/index.d.ts",
|
|
3
|
+
"version": "2.0.0-beta.0",
|
|
4
|
+
"main": "./lib/index.js",
|
|
5
|
+
"types": "./lib/index.d.ts",
|
|
6
6
|
"engines": {
|
|
7
7
|
"node": ">=12.*"
|
|
8
8
|
},
|
|
9
9
|
"scripts": {
|
|
10
10
|
"build": "rm -rf lib/* lib-browser/* && node scripts/generate-sdk-version.js && tsc && webpack",
|
|
11
|
+
"_build": "rm -rf lib/* && tsc",
|
|
11
12
|
"version": "node scripts/generate-sdk-version.js && git add src/sdk-version.ts",
|
|
12
|
-
"
|
|
13
|
-
"
|
|
13
|
+
"lint:fix": "eslint --fix .",
|
|
14
|
+
"lint:check": "eslint ."
|
|
14
15
|
},
|
|
15
16
|
"files": [
|
|
16
17
|
"lib",
|
|
@@ -34,14 +35,23 @@
|
|
|
34
35
|
"url": "git+ssh://git@github.com/translated/lara-node.git"
|
|
35
36
|
},
|
|
36
37
|
"description": "Official Lara SDK for JavaScript and Node.js",
|
|
37
|
-
"dependencies": {
|
|
38
|
-
"form-data": "^4.0.2"
|
|
39
|
-
},
|
|
40
38
|
"devDependencies": {
|
|
41
|
-
"@
|
|
42
|
-
"@
|
|
43
|
-
"typescript": "^
|
|
44
|
-
"
|
|
45
|
-
"
|
|
39
|
+
"@types/node": "^24.9.1",
|
|
40
|
+
"@typescript-eslint/eslint-plugin": "^8.46.0",
|
|
41
|
+
"@typescript-eslint/parser": "^8.46.0",
|
|
42
|
+
"eslint": "^9.37.0",
|
|
43
|
+
"eslint-config-prettier": "^10.1.8",
|
|
44
|
+
"eslint-plugin-import": "^2.32.0",
|
|
45
|
+
"eslint-plugin-prettier": "^5.5.4",
|
|
46
|
+
"husky": "^9.1.7",
|
|
47
|
+
"prettier": "^3.6.2",
|
|
48
|
+
"tsup": "^8.5.0",
|
|
49
|
+
"typescript": "^5.9.3",
|
|
50
|
+
"webpack": "^5.102.1",
|
|
51
|
+
"webpack-cli": "^6.0.1"
|
|
52
|
+
},
|
|
53
|
+
"dependencies": {
|
|
54
|
+
"axios": "^1.12.2",
|
|
55
|
+
"form-data": "^4.0.4"
|
|
46
56
|
}
|
|
47
57
|
}
|
package/lib/credentials.d.ts
DELETED
package/lib/credentials.js
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Credentials = void 0;
|
|
4
|
-
class Credentials {
|
|
5
|
-
constructor(accessKeyId, accessKeySecret) {
|
|
6
|
-
this.accessKeyId = accessKeyId;
|
|
7
|
-
this.accessKeySecret = accessKeySecret;
|
|
8
|
-
}
|
|
9
|
-
}
|
|
10
|
-
exports.Credentials = Credentials;
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import type { PortableCrypto } from "./portable-crypto";
|
|
2
|
-
/** @internal */
|
|
3
|
-
export declare class BrowserCrypto implements PortableCrypto {
|
|
4
|
-
private readonly subtle;
|
|
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>;
|
|
10
|
-
hmac(key: string, data: string): Promise<string>;
|
|
11
|
-
}
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.BrowserCrypto = void 0;
|
|
4
|
-
/** @internal */
|
|
5
|
-
class BrowserCrypto {
|
|
6
|
-
constructor() {
|
|
7
|
-
this.subtle = window.crypto.subtle;
|
|
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) {
|
|
13
|
-
const encoder = new TextEncoder();
|
|
14
|
-
const buffer = (await this.subtle.digest("sha-256", encoder.encode(data))).slice(0, 16);
|
|
15
|
-
return [...new Uint8Array(buffer)].map((x) => x.toString(16).padStart(2, "0")).join("");
|
|
16
|
-
}
|
|
17
|
-
async hmac(key, data) {
|
|
18
|
-
const encoder = new TextEncoder();
|
|
19
|
-
encoder.encode(data);
|
|
20
|
-
const cKey = await this.subtle.importKey("raw", encoder.encode(key), {
|
|
21
|
-
name: "hmac",
|
|
22
|
-
hash: { name: "sha-256" }
|
|
23
|
-
}, false, ["sign"]);
|
|
24
|
-
const buffer = await this.subtle.sign("hmac", cKey, encoder.encode(data));
|
|
25
|
-
return btoa(String.fromCharCode(...new Uint8Array(buffer)));
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
exports.BrowserCrypto = BrowserCrypto;
|
package/lib/crypto/index.d.ts
DELETED
package/lib/crypto/index.js
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.default = instance;
|
|
4
|
-
const browser_crypto_1 = require("./browser-crypto");
|
|
5
|
-
const node_crypto_1 = require("./node-crypto");
|
|
6
|
-
let _instance;
|
|
7
|
-
function instance() {
|
|
8
|
-
if (_instance === undefined) {
|
|
9
|
-
if (typeof window !== "undefined")
|
|
10
|
-
_instance = new browser_crypto_1.BrowserCrypto();
|
|
11
|
-
else
|
|
12
|
-
_instance = new node_crypto_1.NodeCrypto();
|
|
13
|
-
}
|
|
14
|
-
return _instance;
|
|
15
|
-
}
|
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
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
|
-
})();
|
|
35
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
-
exports.NodeCrypto = void 0;
|
|
37
|
-
const crypto = __importStar(require("node:crypto"));
|
|
38
|
-
/** @internal */
|
|
39
|
-
class NodeCrypto {
|
|
40
|
-
digest(data) {
|
|
41
|
-
return new Promise((resolve) => {
|
|
42
|
-
const hash = crypto.createHash("md5");
|
|
43
|
-
hash.update(data);
|
|
44
|
-
resolve(hash.digest("hex"));
|
|
45
|
-
});
|
|
46
|
-
}
|
|
47
|
-
hmac(key, data) {
|
|
48
|
-
return new Promise((resolve) => {
|
|
49
|
-
const hmac = crypto.createHmac("sha256", key);
|
|
50
|
-
hmac.update(data);
|
|
51
|
-
resolve(hmac.digest("base64"));
|
|
52
|
-
});
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
exports.NodeCrypto = NodeCrypto;
|
package/lib/errors.js
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.LaraApiError = exports.TimeoutError = exports.LaraError = void 0;
|
|
4
|
-
class LaraError extends Error {
|
|
5
|
-
}
|
|
6
|
-
exports.LaraError = LaraError;
|
|
7
|
-
class TimeoutError extends LaraError {
|
|
8
|
-
}
|
|
9
|
-
exports.TimeoutError = TimeoutError;
|
|
10
|
-
class LaraApiError extends LaraError {
|
|
11
|
-
constructor(statusCode, type, message) {
|
|
12
|
-
super(message);
|
|
13
|
-
this.statusCode = statusCode;
|
|
14
|
-
this.type = type;
|
|
15
|
-
this.message = message;
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
exports.LaraApiError = LaraApiError;
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { type BaseURL, type ClientResponse, LaraClient, type MultiPartFile } from "./client";
|
|
2
|
-
/** @internal */
|
|
3
|
-
export declare class BrowserLaraClient extends LaraClient {
|
|
4
|
-
private readonly baseUrl;
|
|
5
|
-
constructor(baseUrl: BaseURL, accessKeyId: string, accessKeySecret: string);
|
|
6
|
-
protected send(path: string, headers: Record<string, string>, body?: Record<string, any>): Promise<ClientResponse>;
|
|
7
|
-
protected wrapMultiPartFile(file: MultiPartFile): File;
|
|
8
|
-
}
|
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.BrowserLaraClient = void 0;
|
|
4
|
-
const client_1 = require("./client");
|
|
5
|
-
function hasDefaultPort(port, secure) {
|
|
6
|
-
return (port === 80 && !secure) || (port === 443 && secure);
|
|
7
|
-
}
|
|
8
|
-
/** @internal */
|
|
9
|
-
class BrowserLaraClient extends client_1.LaraClient {
|
|
10
|
-
constructor(baseUrl, accessKeyId, accessKeySecret) {
|
|
11
|
-
super(accessKeyId, accessKeySecret);
|
|
12
|
-
let url = `${baseUrl.secure ? "https" : "http"}://${baseUrl.hostname}`;
|
|
13
|
-
if (!hasDefaultPort(baseUrl.port, baseUrl.secure))
|
|
14
|
-
url += `:${baseUrl.port}`;
|
|
15
|
-
this.baseUrl = url;
|
|
16
|
-
}
|
|
17
|
-
async send(path, headers, body) {
|
|
18
|
-
var _a;
|
|
19
|
-
let requestBody;
|
|
20
|
-
if (body) {
|
|
21
|
-
if (headers["Content-Type"] === "multipart/form-data") {
|
|
22
|
-
delete headers["Content-Type"]; // browser will set it automatically
|
|
23
|
-
const formBody = new FormData();
|
|
24
|
-
for (const [key, value] of Object.entries(body)) {
|
|
25
|
-
if (!value)
|
|
26
|
-
continue;
|
|
27
|
-
if (Array.isArray(value)) {
|
|
28
|
-
for (const v of value)
|
|
29
|
-
formBody.append(key, v);
|
|
30
|
-
}
|
|
31
|
-
else {
|
|
32
|
-
formBody.append(key, value);
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
requestBody = formBody;
|
|
36
|
-
}
|
|
37
|
-
else {
|
|
38
|
-
requestBody = JSON.stringify(body, undefined, 0);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
const response = await fetch(this.baseUrl + path, {
|
|
42
|
-
headers: headers,
|
|
43
|
-
method: "POST",
|
|
44
|
-
body: requestBody
|
|
45
|
-
});
|
|
46
|
-
if ((_a = response.headers.get("Content-Type")) === null || _a === void 0 ? void 0 : _a.includes("text/csv")) {
|
|
47
|
-
return {
|
|
48
|
-
statusCode: response.status,
|
|
49
|
-
body: {
|
|
50
|
-
content: await response.text()
|
|
51
|
-
}
|
|
52
|
-
};
|
|
53
|
-
}
|
|
54
|
-
return { statusCode: response.status, body: await response.json() };
|
|
55
|
-
}
|
|
56
|
-
wrapMultiPartFile(file) {
|
|
57
|
-
if (file instanceof File)
|
|
58
|
-
return file;
|
|
59
|
-
throw new TypeError(`Invalid file input in the browser. Expected an instance of File but received ${typeof file}.`);
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
exports.BrowserLaraClient = BrowserLaraClient;
|
package/lib/net/client.d.ts
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import type { Readable } from "node:stream";
|
|
2
|
-
type HttpMethod = "GET" | "POST" | "PUT" | "DELETE";
|
|
3
|
-
/** @internal */
|
|
4
|
-
export type BaseURL = {
|
|
5
|
-
secure: boolean;
|
|
6
|
-
hostname: string;
|
|
7
|
-
port: number;
|
|
8
|
-
};
|
|
9
|
-
/** @internal */
|
|
10
|
-
export type ClientResponse = {
|
|
11
|
-
statusCode: number;
|
|
12
|
-
body: any;
|
|
13
|
-
};
|
|
14
|
-
export type BrowserMultiPartFile = File;
|
|
15
|
-
export type NodeMultiPartFile = Readable | string;
|
|
16
|
-
export type MultiPartFile = BrowserMultiPartFile | NodeMultiPartFile;
|
|
17
|
-
/** @internal */
|
|
18
|
-
export declare abstract class LaraClient {
|
|
19
|
-
private readonly crypto;
|
|
20
|
-
private readonly accessKeyId;
|
|
21
|
-
private readonly accessKeySecret;
|
|
22
|
-
private readonly extraHeaders;
|
|
23
|
-
protected constructor(accessKeyId: string, accessKeySecret: string);
|
|
24
|
-
setExtraHeader(name: string, value: string): void;
|
|
25
|
-
get<T>(path: string, params?: Record<string, any>, headers?: Record<string, string>): Promise<T>;
|
|
26
|
-
delete<T>(path: string, params?: Record<string, any>, headers?: Record<string, string>): Promise<T>;
|
|
27
|
-
post<T>(path: string, body?: Record<string, any>, files?: Record<string, MultiPartFile>, headers?: Record<string, string>): Promise<T>;
|
|
28
|
-
put<T>(path: string, body?: Record<string, any>, files?: Record<string, MultiPartFile>, headers?: Record<string, string>): Promise<T>;
|
|
29
|
-
protected request<T>(method: HttpMethod, path: string, body?: Record<string, any>, files?: Record<string, MultiPartFile>, headers?: Record<string, string>): Promise<T>;
|
|
30
|
-
private sign;
|
|
31
|
-
protected abstract send(path: string, headers: Record<string, string>, body?: Record<string, any>): Promise<ClientResponse>;
|
|
32
|
-
protected abstract wrapMultiPartFile(file: MultiPartFile): any;
|
|
33
|
-
}
|
|
34
|
-
export {};
|
package/lib/net/client.js
DELETED
|
@@ -1,108 +0,0 @@
|
|
|
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.LaraClient = void 0;
|
|
7
|
-
const crypto_1 = __importDefault(require("../crypto"));
|
|
8
|
-
const errors_1 = require("../errors");
|
|
9
|
-
const sdk_version_1 = require("../sdk-version");
|
|
10
|
-
function parseContent(content) {
|
|
11
|
-
if (content === undefined || content === null)
|
|
12
|
-
return content;
|
|
13
|
-
if (Array.isArray(content))
|
|
14
|
-
return content.map(parseContent);
|
|
15
|
-
if (typeof content === "string") {
|
|
16
|
-
// Test if it's a date
|
|
17
|
-
if (content.match(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.[0-9]{3}Z$/))
|
|
18
|
-
return new Date(content);
|
|
19
|
-
else
|
|
20
|
-
return content;
|
|
21
|
-
}
|
|
22
|
-
if (typeof content === "object") {
|
|
23
|
-
const result = {};
|
|
24
|
-
for (const [key, value] of Object.entries(content)) {
|
|
25
|
-
const camelKey = key.replace(/_([a-z])/g, (_, letter) => letter.toUpperCase());
|
|
26
|
-
result[camelKey] = parseContent(value);
|
|
27
|
-
}
|
|
28
|
-
return result;
|
|
29
|
-
}
|
|
30
|
-
return content;
|
|
31
|
-
}
|
|
32
|
-
/** @internal */
|
|
33
|
-
class LaraClient {
|
|
34
|
-
constructor(accessKeyId, accessKeySecret) {
|
|
35
|
-
this.crypto = (0, crypto_1.default)();
|
|
36
|
-
this.extraHeaders = {};
|
|
37
|
-
this.accessKeyId = accessKeyId;
|
|
38
|
-
this.accessKeySecret = accessKeySecret;
|
|
39
|
-
}
|
|
40
|
-
setExtraHeader(name, value) {
|
|
41
|
-
this.extraHeaders[name] = value;
|
|
42
|
-
}
|
|
43
|
-
get(path, params, headers) {
|
|
44
|
-
return this.request("GET", path, params, undefined, headers);
|
|
45
|
-
}
|
|
46
|
-
delete(path, params, headers) {
|
|
47
|
-
return this.request("DELETE", path, params, undefined, headers);
|
|
48
|
-
}
|
|
49
|
-
post(path, body, files, headers) {
|
|
50
|
-
return this.request("POST", path, body, files, headers);
|
|
51
|
-
}
|
|
52
|
-
put(path, body, files, headers) {
|
|
53
|
-
return this.request("PUT", path, body, files, headers);
|
|
54
|
-
}
|
|
55
|
-
async request(method, path, body, files, headers) {
|
|
56
|
-
if (!path.startsWith("/"))
|
|
57
|
-
path = `/${path}`;
|
|
58
|
-
const _headers = {
|
|
59
|
-
"X-HTTP-Method-Override": method,
|
|
60
|
-
"X-Lara-Date": new Date().toUTCString(),
|
|
61
|
-
"X-Lara-SDK-Name": "lara-node",
|
|
62
|
-
"X-Lara-SDK-Version": sdk_version_1.version,
|
|
63
|
-
...this.extraHeaders,
|
|
64
|
-
...headers
|
|
65
|
-
};
|
|
66
|
-
if (body) {
|
|
67
|
-
body = Object.fromEntries(Object.entries(body).filter(([_, v]) => v !== undefined && v !== null));
|
|
68
|
-
if (Object.keys(body).length === 0)
|
|
69
|
-
body = undefined;
|
|
70
|
-
if (body) {
|
|
71
|
-
const jsonBody = JSON.stringify(body, undefined, 0);
|
|
72
|
-
_headers["Content-MD5"] = await this.crypto.digest(jsonBody);
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
let requestBody;
|
|
76
|
-
if (files) {
|
|
77
|
-
// validate files
|
|
78
|
-
for (const [key, file] of Object.entries(files))
|
|
79
|
-
files[key] = this.wrapMultiPartFile(file);
|
|
80
|
-
_headers["Content-Type"] = "multipart/form-data";
|
|
81
|
-
requestBody = Object.assign({}, files, body);
|
|
82
|
-
}
|
|
83
|
-
else {
|
|
84
|
-
_headers["Content-Type"] = "application/json";
|
|
85
|
-
if (body)
|
|
86
|
-
requestBody = body;
|
|
87
|
-
}
|
|
88
|
-
const signature = await this.sign(method, path, _headers);
|
|
89
|
-
_headers.Authorization = `Lara ${this.accessKeyId}:${signature}`;
|
|
90
|
-
const response = await this.send(path, _headers, requestBody);
|
|
91
|
-
if (200 <= response.statusCode && response.statusCode < 300) {
|
|
92
|
-
return parseContent(response.body.content);
|
|
93
|
-
}
|
|
94
|
-
else {
|
|
95
|
-
const error = response.body.error || {};
|
|
96
|
-
throw new errors_1.LaraApiError(response.statusCode, error.type || "UnknownError", error.message || "An unknown error occurred");
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
async sign(method, path, headers) {
|
|
100
|
-
const date = headers["X-Lara-Date"].trim();
|
|
101
|
-
const contentMD5 = (headers["Content-MD5"] || "").trim();
|
|
102
|
-
const contentType = (headers["Content-Type"] || "").trim();
|
|
103
|
-
const httpMethod = (headers["X-HTTP-Method-Override"] || method).trim().toUpperCase();
|
|
104
|
-
const challenge = `${httpMethod}\n${path}\n${contentMD5}\n${contentType}\n${date}`;
|
|
105
|
-
return await this.crypto.hmac(this.accessKeySecret, challenge);
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
exports.LaraClient = LaraClient;
|
package/lib/net/index.d.ts
DELETED
package/lib/net/index.js
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.LaraClient = void 0;
|
|
4
|
-
exports.default = create;
|
|
5
|
-
const browser_client_1 = require("./browser-client");
|
|
6
|
-
const node_client_1 = require("./node-client");
|
|
7
|
-
var client_1 = require("./client");
|
|
8
|
-
Object.defineProperty(exports, "LaraClient", { enumerable: true, get: function () { return client_1.LaraClient; } });
|
|
9
|
-
const DEFAULT_BASE_URL = "https://api.laratranslate.com";
|
|
10
|
-
function create(accessKeyId, accessKeySecret, baseUrl) {
|
|
11
|
-
const url = new URL(baseUrl || DEFAULT_BASE_URL);
|
|
12
|
-
if (url.protocol !== "https:" && url.protocol !== "http:")
|
|
13
|
-
throw new TypeError(`Invalid URL (protocol): ${url.protocol}`);
|
|
14
|
-
const parsedURL = {
|
|
15
|
-
secure: url.protocol === "https:",
|
|
16
|
-
hostname: url.hostname,
|
|
17
|
-
port: url.port ? parseInt(url.port, 10) : url.protocol === "https:" ? 443 : 80
|
|
18
|
-
};
|
|
19
|
-
if (typeof window !== "undefined")
|
|
20
|
-
return new browser_client_1.BrowserLaraClient(parsedURL, accessKeyId, accessKeySecret);
|
|
21
|
-
else
|
|
22
|
-
return new node_client_1.NodeLaraClient(parsedURL, accessKeyId, accessKeySecret);
|
|
23
|
-
}
|