@lokalise/tm-sdk 2.0.0 → 2.1.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
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
# Translation Memory client SDK
|
|
2
|
+
|
|
3
|
+
Translation Memory service provides a REST API to interact with it, but we recommend using this [@lokalise/tm-sdk](https://www.npmjs.com/package/@lokalise/tm-sdk) NPM package to integrate TM capabilities into your Node.js application.
|
|
4
|
+
|
|
5
|
+
## Usage
|
|
6
|
+
|
|
7
|
+
Use the following command to add the package to your project:
|
|
8
|
+
|
|
9
|
+
```
|
|
10
|
+
npm install @lokalise/tm-sdk
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
After that, you can start using the SDK like this:
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
import { TranslationMemoryClient } from '@lokalise/tm-sdk'
|
|
17
|
+
|
|
18
|
+
const client = new TranslationMemoryClient({
|
|
19
|
+
isEnabled: true,
|
|
20
|
+
baseUrl: 'https://<tm-service-server-url>',
|
|
21
|
+
jwtToken: '<JWT auth token provided by the TM service>',
|
|
22
|
+
// Undici retry config (see RetryConfig in https://github.com/kibertoad/undici-retry)
|
|
23
|
+
retryConfig: {},
|
|
24
|
+
// See types in https://github.com/lokalise/node-core/blob/main/src/errors/errorReporterTypes.ts
|
|
25
|
+
errorReporter: {
|
|
26
|
+
report: (errorReport: ErrorReport) => {},
|
|
27
|
+
},
|
|
28
|
+
})
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
To create new records, call `upsertRecords()` method:
|
|
32
|
+
|
|
33
|
+
```typescript
|
|
34
|
+
const records = [
|
|
35
|
+
{
|
|
36
|
+
ownerId: '018dac6e-bff7-689a-f0ba-e84f49c5e432',
|
|
37
|
+
sourceLocale: 'en',
|
|
38
|
+
targetLocale: 'de',
|
|
39
|
+
sourcePrevContent: 'Localization meets AI',
|
|
40
|
+
sourceText: 'Your one-stop solution for AI-powered translations.',
|
|
41
|
+
targetText: 'Ihre Lösung aus einer Hand für KI-gestützte Übersetzungen.',
|
|
42
|
+
sourceNextContent:
|
|
43
|
+
'Save money, speed up your market entry, and charm customers in every language.',
|
|
44
|
+
},
|
|
45
|
+
]
|
|
46
|
+
client.upsertRecords('my-tm-group', records)
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
To search for exact matching records, you can use `findMatches()` API.
|
|
50
|
+
|
|
51
|
+
```typescript
|
|
52
|
+
const searchRequest = {
|
|
53
|
+
sourceText: 'Your one-stop solution for AI-powered translations and automated localization.',
|
|
54
|
+
sourceLocale: 'en',
|
|
55
|
+
targetLocale: 'de',
|
|
56
|
+
prevContent: 'Localization meets AI',
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* [{
|
|
60
|
+
* sourceText: 'Your one-stop solution for AI-powered translations.'
|
|
61
|
+
* targetText: 'Ihre Lösung aus einer Hand für KI-gestützte Übersetzungen.'
|
|
62
|
+
* }]
|
|
63
|
+
*/
|
|
64
|
+
const matches = client.findMatches('my-tm-group', searchRequest)
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
You can also use `bulkFindMatches()` and `bulkFindMatchesIterative()` APIs to perform the same kind of lookup for multiple separate search requests.
|
|
@@ -94,8 +94,13 @@ class TranslationMemoryClient {
|
|
|
94
94
|
if (!this.isEnabled) {
|
|
95
95
|
return;
|
|
96
96
|
}
|
|
97
|
+
// Avoiding sending empty records
|
|
98
|
+
const recordsToInsert = records.filter((record) => record.sourceText.trim().length > 0 && record.targetText.trim().length > 0);
|
|
99
|
+
if (recordsToInsert.length === 0) {
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
97
102
|
try {
|
|
98
|
-
await (0, node_core_1.sendPut)(this.httpClient, `/v1/groups/${groupId}/records`,
|
|
103
|
+
await (0, node_core_1.sendPut)(this.httpClient, `/v1/groups/${groupId}/records`, recordsToInsert, {
|
|
99
104
|
headers: this.headers,
|
|
100
105
|
retryConfig: this.retryConfig,
|
|
101
106
|
requestLabel: '(tm-sdk) Create or update records',
|
|
@@ -106,8 +111,28 @@ class TranslationMemoryClient {
|
|
|
106
111
|
}
|
|
107
112
|
}
|
|
108
113
|
async internalBulkFindMatches(groupId, request) {
|
|
114
|
+
const result = [];
|
|
109
115
|
if (!this.isEnabled) {
|
|
110
|
-
return
|
|
116
|
+
return result;
|
|
117
|
+
}
|
|
118
|
+
if (request.options?.ignoreEmptyFilters) {
|
|
119
|
+
const { emptyFilters, nonEmptyFilters } = request.filters.reduce((acc, filter) => {
|
|
120
|
+
if (filter.sourceText.trim().length === 0) {
|
|
121
|
+
acc.emptyFilters.push(filter);
|
|
122
|
+
}
|
|
123
|
+
else {
|
|
124
|
+
acc.nonEmptyFilters.push(filter);
|
|
125
|
+
}
|
|
126
|
+
return acc;
|
|
127
|
+
}, {
|
|
128
|
+
emptyFilters: [],
|
|
129
|
+
nonEmptyFilters: [],
|
|
130
|
+
});
|
|
131
|
+
result.push(...emptyFilters.map((filter) => ({
|
|
132
|
+
filterId: filter.filterId,
|
|
133
|
+
matches: [{ sourceText: filter.sourceText, targetText: '' }],
|
|
134
|
+
})));
|
|
135
|
+
request.filters = nonEmptyFilters;
|
|
111
136
|
}
|
|
112
137
|
try {
|
|
113
138
|
const response = await (0, node_core_1.sendPost)(this.httpClient, `/v1/groups/${groupId}/actions/bulk-find-matches`, request, {
|
|
@@ -115,7 +140,8 @@ class TranslationMemoryClient {
|
|
|
115
140
|
retryConfig: this.retryConfig,
|
|
116
141
|
requestLabel: '(tm-sdk) Bulk find matches',
|
|
117
142
|
});
|
|
118
|
-
|
|
143
|
+
result.push(...response.result.body.data);
|
|
144
|
+
return result;
|
|
119
145
|
}
|
|
120
146
|
catch (e) {
|
|
121
147
|
throw this.handleError(e);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TranslationMemoryClient.js","sourceRoot":"","sources":["../../src/sdk/TranslationMemoryClient.ts"],"names":[],"mappings":";;;AAE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AAI,CAAC;YACH,MAAM,QAAQ,GAAG,MAAM,IAAA,oBAAQ,EAC7B,IAAI,CAAC,UAAU,EACf,cAAc,OAAO,4BAA4B,EACjD,OAAO,EACP;gBACE,OAAO,EAAE,IAAI,CAAC,OAAO;gBACrB,WAAW,EAAE,IAAI,CAAC,WAAW;gBAC7B,YAAY,EAAE,4BAA4B;aAC3C,CACF,CAAA;YACD,MAAM,CAAC,IAAI,CAAC,GAAG,QAAQ,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;YACzC,OAAO,MAAM,CAAA;QACf,CAAC;QAAC,OAAO,CAAC,EAAE,CAAC;YACX,MAAM,IAAI,CAAC,WAAW,CAAC,CAAC,CAAC,CAAA;QAC3B,CAAC;IACH,CAAC;IAEO,WAAW,CAAC,CAAU;QAC5B,IAAI,KAAwB,CAAA;QAC5B,IAAI,CAAC,YAAY,KAAK,EAAE,CAAC;YACvB,KAAK,GAAG,IAAA,iCAAqB,EAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,2DAA4B,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAA;QAC5E,CAAC;aAAM,CAAC;YACN,KAAK,GAAG,IAAI,yBAAa,CAAC;gBACxB,OAAO,EAAE,eAAe;gBACxB,SAAS,EAAE,eAAe;gBAC1B,OAAO,EAAE,EAAE,KAAK,EAAE,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE;aACtC,CAAC,CAAA;QACJ,CAAC;QAED,IAAI,CAAC,aAAa,EAAE,MAAM,CAAC,EAAE,KAAK,EAAE,CAAC,CAAA;QACrC,OAAO,KAAK,CAAA;IACd,CAAC;CACF;AA9MD,0DA8MC"}
|
|
@@ -7,11 +7,12 @@ export type FindMatchesFilter = {
|
|
|
7
7
|
};
|
|
8
8
|
export type FindMatchesOptions = {
|
|
9
9
|
maxMatches?: number;
|
|
10
|
+
ignoreEmptyFilters?: boolean;
|
|
10
11
|
};
|
|
11
12
|
export type BulkFindMatchesFilter = FindMatchesFilter & {
|
|
12
13
|
filterId?: string;
|
|
13
14
|
};
|
|
14
|
-
export type FindMatchesRequest = FindMatchesFilter & FindMatchesOptions
|
|
15
|
+
export type FindMatchesRequest = FindMatchesFilter & Omit<FindMatchesOptions, 'ignoreEmptyFilters'>;
|
|
15
16
|
export type BulkFindMatchesRequest = {
|
|
16
17
|
filters: BulkFindMatchesFilter[];
|
|
17
18
|
options?: FindMatchesOptions;
|
package/package.json
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lokalise/tm-sdk",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.1.0",
|
|
4
4
|
"author": {
|
|
5
5
|
"name": "Lokalise",
|
|
6
6
|
"url": "https://lokalise.com/"
|
|
7
7
|
},
|
|
8
|
+
"description": "REST API client for the Lokalise Translation Memory service",
|
|
8
9
|
"files": [
|
|
9
10
|
"dist/**"
|
|
10
11
|
],
|
|
@@ -16,6 +17,9 @@
|
|
|
16
17
|
"main": "dist/index.js",
|
|
17
18
|
"types": "dist/index.d.ts",
|
|
18
19
|
"type": "commonjs",
|
|
20
|
+
"publishConfig": {
|
|
21
|
+
"access": "public"
|
|
22
|
+
},
|
|
19
23
|
"scripts": {
|
|
20
24
|
"build": "shx rm -rf ./dist && tsc",
|
|
21
25
|
"build:publish": "shx rm -rf ./dist && tsc -p tsconfig.publish.json",
|
|
@@ -24,25 +28,24 @@
|
|
|
24
28
|
"test": "vitest",
|
|
25
29
|
"test:coverage": "npm test -- --coverage",
|
|
26
30
|
"test:ci": "npm run test:coverage",
|
|
27
|
-
"version": "
|
|
31
|
+
"package-version": "echo $npm_package_version",
|
|
28
32
|
"prepublishOnly": "npm run build:publish"
|
|
29
33
|
},
|
|
30
34
|
"dependencies": {
|
|
31
|
-
"@lokalise/api-common": "^
|
|
35
|
+
"@lokalise/api-common": "^3.0.1",
|
|
32
36
|
"@lokalise/id-utils": "^1.0.0",
|
|
33
|
-
"@lokalise/node-core": "^9.
|
|
34
|
-
"undici": "^6.
|
|
37
|
+
"@lokalise/node-core": "^9.10.1",
|
|
38
|
+
"undici": "^6.6.2",
|
|
35
39
|
"undici-retry": "^5.0.2"
|
|
36
40
|
},
|
|
37
41
|
"devDependencies": {
|
|
38
|
-
"@types/node": "20.11.
|
|
39
|
-
"@typescript-eslint/eslint-plugin": "^
|
|
40
|
-
"@typescript-eslint/parser": "^
|
|
41
|
-
"@vitest/coverage-v8": "^1.
|
|
42
|
-
"
|
|
43
|
-
"eslint": "^8.56.0",
|
|
42
|
+
"@types/node": "20.11.20",
|
|
43
|
+
"@typescript-eslint/eslint-plugin": "^7.0.2",
|
|
44
|
+
"@typescript-eslint/parser": "^7.0.2",
|
|
45
|
+
"@vitest/coverage-v8": "^1.3.1",
|
|
46
|
+
"eslint": "^8.57.0",
|
|
44
47
|
"eslint-plugin-import": "^2.29.1",
|
|
45
|
-
"eslint-plugin-vitest": "^0.3.
|
|
48
|
+
"eslint-plugin-vitest": "^0.3.22",
|
|
46
49
|
"mockttp": "^3.10.1",
|
|
47
50
|
"prettier": "^3.2.4",
|
|
48
51
|
"shx": "^0.3.4",
|