@lingo.dev/_sdk 0.15.3 → 0.16.1
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/build/index.cjs +75 -90
- package/build/index.d.cts +3 -3
- package/build/index.d.ts +3 -3
- package/build/index.mjs +68 -83
- package/package.json +2 -2
package/build/index.cjs
CHANGED
|
@@ -63,10 +63,10 @@ async function getDistinctId(apiKey, apiUrl) {
|
|
|
63
63
|
const cached = identityCache.get(apiKey);
|
|
64
64
|
if (cached) return cached;
|
|
65
65
|
try {
|
|
66
|
-
const res = await fetch(`${apiUrl}/
|
|
67
|
-
method: "
|
|
66
|
+
const res = await fetch(`${apiUrl}/users/me`, {
|
|
67
|
+
method: "GET",
|
|
68
68
|
headers: {
|
|
69
|
-
|
|
69
|
+
"X-API-Key": apiKey,
|
|
70
70
|
"Content-Type": "application/json"
|
|
71
71
|
}
|
|
72
72
|
});
|
|
@@ -95,7 +95,7 @@ async function getDistinctId(apiKey, apiUrl) {
|
|
|
95
95
|
// src/index.ts
|
|
96
96
|
var engineParamsSchema = _zod2.default.object({
|
|
97
97
|
apiKey: _zod2.default.string(),
|
|
98
|
-
apiUrl: _zod2.default.string().url().default("https://
|
|
98
|
+
apiUrl: _zod2.default.string().url().default("https://api.lingo.dev"),
|
|
99
99
|
batchSize: _zod2.default.number().int().gt(0).lte(250).default(25),
|
|
100
100
|
idealBatchItemSize: _zod2.default.number().int().gt(0).lte(2500).default(250),
|
|
101
101
|
engineId: _zod2.default.string().optional()
|
|
@@ -112,31 +112,49 @@ var localizationParamsSchema = _zod2.default.object({
|
|
|
112
112
|
filePath: _zod2.default.string().optional(),
|
|
113
113
|
triggerType: _zod2.default.enum(["cli", "ci"]).optional()
|
|
114
114
|
});
|
|
115
|
-
var LingoDotDevEngine = (_class = class {
|
|
115
|
+
var LingoDotDevEngine = (_class = class _LingoDotDevEngine {
|
|
116
116
|
|
|
117
117
|
__init() {this.sessionId = _cuid2.createId.call(void 0, )}
|
|
118
|
-
get isVNext() {
|
|
119
|
-
return !!this.config.engineId;
|
|
120
|
-
}
|
|
121
118
|
get headers() {
|
|
122
|
-
return
|
|
119
|
+
return {
|
|
123
120
|
"Content-Type": "application/json; charset=utf-8",
|
|
124
121
|
"X-API-Key": this.config.apiKey
|
|
125
|
-
} : {
|
|
126
|
-
"Content-Type": "application/json; charset=utf-8",
|
|
127
|
-
Authorization: `Bearer ${this.config.apiKey}`
|
|
128
122
|
};
|
|
129
123
|
}
|
|
124
|
+
static async extractErrorMessage(res) {
|
|
125
|
+
try {
|
|
126
|
+
const text = await res.text();
|
|
127
|
+
const parsed = JSON.parse(text);
|
|
128
|
+
if (parsed && typeof parsed.message === "string") {
|
|
129
|
+
return parsed.message;
|
|
130
|
+
}
|
|
131
|
+
if (_optionalChain([parsed, 'optionalAccess', _2 => _2._tag]) === "NotFoundError") {
|
|
132
|
+
return `${parsed.entityType} not found: ${parsed.id}`;
|
|
133
|
+
}
|
|
134
|
+
return text;
|
|
135
|
+
} catch (e2) {
|
|
136
|
+
return `Unexpected error (${res.status})`;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
static async throwOnHttpError(res, context) {
|
|
140
|
+
if (res.ok) return;
|
|
141
|
+
const msg = await _LingoDotDevEngine.extractErrorMessage(res);
|
|
142
|
+
if (res.status >= 500 && res.status < 600) {
|
|
143
|
+
throw new Error(
|
|
144
|
+
`Server error (${res.status}): ${msg}. This may be due to temporary service issues.`
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
if (res.status === 400) {
|
|
148
|
+
throw new Error(`Invalid request: ${msg}`);
|
|
149
|
+
}
|
|
150
|
+
throw new Error(context ? `${context}: ${msg}` : msg);
|
|
151
|
+
}
|
|
130
152
|
/**
|
|
131
153
|
* Create a new LingoDotDevEngine instance
|
|
132
154
|
* @param config - Configuration options for the Engine
|
|
133
155
|
*/
|
|
134
156
|
constructor(config) {;_class.prototype.__init.call(this);
|
|
135
|
-
|
|
136
|
-
if (!config.apiUrl && parsed.engineId) {
|
|
137
|
-
parsed.apiUrl = "https://api.lingo.dev";
|
|
138
|
-
}
|
|
139
|
-
this.config = parsed;
|
|
157
|
+
this.config = engineParamsSchema.parse(config);
|
|
140
158
|
}
|
|
141
159
|
/**
|
|
142
160
|
* Localize content using the Lingo.dev API
|
|
@@ -152,7 +170,6 @@ var LingoDotDevEngine = (_class = class {
|
|
|
152
170
|
const finalParams = localizationParamsSchema.parse(params);
|
|
153
171
|
const chunkedPayload = this.extractPayloadChunks(finalPayload);
|
|
154
172
|
const processedPayloadChunks = [];
|
|
155
|
-
const workflowId = _cuid2.createId.call(void 0, );
|
|
156
173
|
for (let i = 0; i < chunkedPayload.length; i++) {
|
|
157
174
|
const chunk = chunkedPayload[i];
|
|
158
175
|
const percentageCompleted = Math.round(
|
|
@@ -162,7 +179,6 @@ var LingoDotDevEngine = (_class = class {
|
|
|
162
179
|
finalParams.sourceLocale,
|
|
163
180
|
finalParams.targetLocale,
|
|
164
181
|
{ data: chunk, reference: params.reference, hints: params.hints },
|
|
165
|
-
workflowId,
|
|
166
182
|
params.fast || false,
|
|
167
183
|
params.filePath,
|
|
168
184
|
params.triggerType,
|
|
@@ -180,16 +196,15 @@ var LingoDotDevEngine = (_class = class {
|
|
|
180
196
|
* @param sourceLocale - Source locale
|
|
181
197
|
* @param targetLocale - Target locale
|
|
182
198
|
* @param payload - Payload containing the chunk to be localized
|
|
183
|
-
* @param workflowId - Workflow ID for tracking
|
|
184
199
|
* @param fast - Whether to use fast mode
|
|
185
200
|
* @param filePath - Optional file path for metadata
|
|
186
|
-
* @param triggerType - Optional trigger type
|
|
201
|
+
* @param triggerType - Optional trigger type
|
|
187
202
|
* @param signal - Optional AbortSignal to cancel the operation
|
|
188
203
|
* @returns Localized chunk
|
|
189
204
|
*/
|
|
190
|
-
async localizeChunk(sourceLocale, targetLocale, payload,
|
|
191
|
-
const url =
|
|
192
|
-
const body =
|
|
205
|
+
async localizeChunk(sourceLocale, targetLocale, payload, fast, filePath, triggerType, signal) {
|
|
206
|
+
const url = `${this.config.apiUrl}/process/localize`;
|
|
207
|
+
const body = {
|
|
193
208
|
params: { fast },
|
|
194
209
|
sourceLocale,
|
|
195
210
|
targetLocale,
|
|
@@ -198,16 +213,8 @@ var LingoDotDevEngine = (_class = class {
|
|
|
198
213
|
hints: payload.hints,
|
|
199
214
|
sessionId: this.sessionId,
|
|
200
215
|
triggerType,
|
|
201
|
-
metadata: filePath ? { filePath } : void 0
|
|
202
|
-
|
|
203
|
-
params: { workflowId, fast },
|
|
204
|
-
locale: {
|
|
205
|
-
source: sourceLocale,
|
|
206
|
-
target: targetLocale
|
|
207
|
-
},
|
|
208
|
-
data: payload.data,
|
|
209
|
-
reference: payload.reference,
|
|
210
|
-
hints: payload.hints
|
|
216
|
+
metadata: filePath ? { filePath } : void 0,
|
|
217
|
+
...this.config.engineId && { engineId: this.config.engineId }
|
|
211
218
|
};
|
|
212
219
|
const res = await fetch(url, {
|
|
213
220
|
method: "POST",
|
|
@@ -215,19 +222,7 @@ var LingoDotDevEngine = (_class = class {
|
|
|
215
222
|
body: JSON.stringify(body, null, 2),
|
|
216
223
|
signal
|
|
217
224
|
});
|
|
218
|
-
|
|
219
|
-
if (res.status >= 500 && res.status < 600) {
|
|
220
|
-
const errorText = await res.text();
|
|
221
|
-
throw new Error(
|
|
222
|
-
`Server error (${res.status}): ${res.statusText}. ${errorText}. This may be due to temporary service issues.`
|
|
223
|
-
);
|
|
224
|
-
} else if (res.status === 400) {
|
|
225
|
-
throw new Error(`Invalid request: ${res.statusText}`);
|
|
226
|
-
} else {
|
|
227
|
-
const errorText = await res.text();
|
|
228
|
-
throw new Error(errorText);
|
|
229
|
-
}
|
|
230
|
-
}
|
|
225
|
+
await _LingoDotDevEngine.throwOnHttpError(res);
|
|
231
226
|
const jsonResponse = await res.json();
|
|
232
227
|
if (!jsonResponse.data && jsonResponse.error) {
|
|
233
228
|
throw new Error(jsonResponse.error);
|
|
@@ -567,7 +562,7 @@ var LingoDotDevEngine = (_class = class {
|
|
|
567
562
|
break;
|
|
568
563
|
}
|
|
569
564
|
const siblings = Array.from(parent.childNodes).filter(
|
|
570
|
-
(n) => n.nodeType === 1 || n.nodeType === 3 && _optionalChain([n, 'access',
|
|
565
|
+
(n) => n.nodeType === 1 || n.nodeType === 3 && _optionalChain([n, 'access', _3 => _3.textContent, 'optionalAccess', _4 => _4.trim, 'call', _5 => _5()])
|
|
571
566
|
);
|
|
572
567
|
const index = siblings.indexOf(current);
|
|
573
568
|
if (index !== -1) {
|
|
@@ -587,7 +582,7 @@ var LingoDotDevEngine = (_class = class {
|
|
|
587
582
|
parent = parent.parentElement;
|
|
588
583
|
}
|
|
589
584
|
if (node.nodeType === 3) {
|
|
590
|
-
const text = _optionalChain([node, 'access',
|
|
585
|
+
const text = _optionalChain([node, 'access', _6 => _6.textContent, 'optionalAccess', _7 => _7.trim, 'call', _8 => _8()]) || "";
|
|
591
586
|
if (text) {
|
|
592
587
|
extractedContent[getPath(node)] = text;
|
|
593
588
|
}
|
|
@@ -602,15 +597,15 @@ var LingoDotDevEngine = (_class = class {
|
|
|
602
597
|
}
|
|
603
598
|
});
|
|
604
599
|
Array.from(element.childNodes).filter(
|
|
605
|
-
(n) => n.nodeType === 1 || n.nodeType === 3 && _optionalChain([n, 'access',
|
|
600
|
+
(n) => n.nodeType === 1 || n.nodeType === 3 && _optionalChain([n, 'access', _9 => _9.textContent, 'optionalAccess', _10 => _10.trim, 'call', _11 => _11()])
|
|
606
601
|
).forEach(processNode);
|
|
607
602
|
}
|
|
608
603
|
};
|
|
609
604
|
Array.from(document.head.childNodes).filter(
|
|
610
|
-
(n) => n.nodeType === 1 || n.nodeType === 3 && _optionalChain([n, 'access',
|
|
605
|
+
(n) => n.nodeType === 1 || n.nodeType === 3 && _optionalChain([n, 'access', _12 => _12.textContent, 'optionalAccess', _13 => _13.trim, 'call', _14 => _14()])
|
|
611
606
|
).forEach(processNode);
|
|
612
607
|
Array.from(document.body.childNodes).filter(
|
|
613
|
-
(n) => n.nodeType === 1 || n.nodeType === 3 && _optionalChain([n, 'access',
|
|
608
|
+
(n) => n.nodeType === 1 || n.nodeType === 3 && _optionalChain([n, 'access', _15 => _15.textContent, 'optionalAccess', _16 => _16.trim, 'call', _17 => _17()])
|
|
614
609
|
).forEach(processNode);
|
|
615
610
|
const localizedContent = await this._localizeRaw(
|
|
616
611
|
extractedContent,
|
|
@@ -626,10 +621,10 @@ var LingoDotDevEngine = (_class = class {
|
|
|
626
621
|
let current = parent;
|
|
627
622
|
for (const index of indices) {
|
|
628
623
|
const siblings = Array.from(parent.childNodes).filter(
|
|
629
|
-
(n) => n.nodeType === 1 || n.nodeType === 3 && _optionalChain([n, 'access',
|
|
624
|
+
(n) => n.nodeType === 1 || n.nodeType === 3 && _optionalChain([n, 'access', _18 => _18.textContent, 'optionalAccess', _19 => _19.trim, 'call', _20 => _20()])
|
|
630
625
|
);
|
|
631
626
|
current = siblings[parseInt(index)] || null;
|
|
632
|
-
if (_optionalChain([current, 'optionalAccess',
|
|
627
|
+
if (_optionalChain([current, 'optionalAccess', _21 => _21.nodeType]) === 1) {
|
|
633
628
|
parent = current;
|
|
634
629
|
}
|
|
635
630
|
}
|
|
@@ -677,21 +672,17 @@ var LingoDotDevEngine = (_class = class {
|
|
|
677
672
|
trackProps
|
|
678
673
|
);
|
|
679
674
|
try {
|
|
680
|
-
const url =
|
|
675
|
+
const url = `${this.config.apiUrl}/process/recognize`;
|
|
681
676
|
const response = await fetch(url, {
|
|
682
677
|
method: "POST",
|
|
683
678
|
headers: this.headers,
|
|
684
679
|
body: JSON.stringify({ text }),
|
|
685
680
|
signal
|
|
686
681
|
});
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
);
|
|
692
|
-
}
|
|
693
|
-
throw new Error(`Error recognizing locale: ${response.statusText}`);
|
|
694
|
-
}
|
|
682
|
+
await _LingoDotDevEngine.throwOnHttpError(
|
|
683
|
+
response,
|
|
684
|
+
"Error recognizing locale"
|
|
685
|
+
);
|
|
695
686
|
const jsonResponse = await response.json();
|
|
696
687
|
trackEvent(
|
|
697
688
|
this.config.apiKey,
|
|
@@ -714,35 +705,29 @@ var LingoDotDevEngine = (_class = class {
|
|
|
714
705
|
}
|
|
715
706
|
}
|
|
716
707
|
async whoami(signal) {
|
|
717
|
-
const url =
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
return null;
|
|
728
|
-
}
|
|
729
|
-
return {
|
|
730
|
-
email: payload.email,
|
|
731
|
-
id: payload.id
|
|
732
|
-
};
|
|
733
|
-
}
|
|
734
|
-
if (res.status >= 500 && res.status < 600) {
|
|
735
|
-
throw new Error(
|
|
736
|
-
`Server error (${res.status}): ${res.statusText}. This may be due to temporary service issues.`
|
|
737
|
-
);
|
|
738
|
-
}
|
|
739
|
-
return null;
|
|
740
|
-
} catch (error) {
|
|
741
|
-
if (error instanceof Error && error.message.includes("Server error")) {
|
|
742
|
-
throw error;
|
|
708
|
+
const url = `${this.config.apiUrl}/users/me`;
|
|
709
|
+
const res = await fetch(url, {
|
|
710
|
+
method: "GET",
|
|
711
|
+
headers: this.headers,
|
|
712
|
+
signal
|
|
713
|
+
});
|
|
714
|
+
if (res.ok) {
|
|
715
|
+
const payload = await res.json();
|
|
716
|
+
if (!_optionalChain([payload, 'optionalAccess', _22 => _22.email])) {
|
|
717
|
+
return null;
|
|
743
718
|
}
|
|
744
|
-
return
|
|
719
|
+
return {
|
|
720
|
+
email: payload.email,
|
|
721
|
+
id: payload.id
|
|
722
|
+
};
|
|
723
|
+
}
|
|
724
|
+
if (res.status >= 500 && res.status < 600) {
|
|
725
|
+
const msg = await _LingoDotDevEngine.extractErrorMessage(res);
|
|
726
|
+
throw new Error(
|
|
727
|
+
`Server error (${res.status}): ${msg}. This may be due to temporary service issues.`
|
|
728
|
+
);
|
|
745
729
|
}
|
|
730
|
+
return null;
|
|
746
731
|
}
|
|
747
732
|
}, _class);
|
|
748
733
|
var ReplexicaEngine = (_class2 = class _ReplexicaEngine extends LingoDotDevEngine {
|
package/build/index.d.cts
CHANGED
|
@@ -29,8 +29,9 @@ declare const localizationParamsSchema: Z.ZodObject<{
|
|
|
29
29
|
declare class LingoDotDevEngine {
|
|
30
30
|
protected config: Z.infer<typeof engineParamsSchema>;
|
|
31
31
|
private readonly sessionId;
|
|
32
|
-
private get isVNext();
|
|
33
32
|
private get headers();
|
|
33
|
+
private static extractErrorMessage;
|
|
34
|
+
private static throwOnHttpError;
|
|
34
35
|
/**
|
|
35
36
|
* Create a new LingoDotDevEngine instance
|
|
36
37
|
* @param config - Configuration options for the Engine
|
|
@@ -51,10 +52,9 @@ declare class LingoDotDevEngine {
|
|
|
51
52
|
* @param sourceLocale - Source locale
|
|
52
53
|
* @param targetLocale - Target locale
|
|
53
54
|
* @param payload - Payload containing the chunk to be localized
|
|
54
|
-
* @param workflowId - Workflow ID for tracking
|
|
55
55
|
* @param fast - Whether to use fast mode
|
|
56
56
|
* @param filePath - Optional file path for metadata
|
|
57
|
-
* @param triggerType - Optional trigger type
|
|
57
|
+
* @param triggerType - Optional trigger type
|
|
58
58
|
* @param signal - Optional AbortSignal to cancel the operation
|
|
59
59
|
* @returns Localized chunk
|
|
60
60
|
*/
|
package/build/index.d.ts
CHANGED
|
@@ -29,8 +29,9 @@ declare const localizationParamsSchema: Z.ZodObject<{
|
|
|
29
29
|
declare class LingoDotDevEngine {
|
|
30
30
|
protected config: Z.infer<typeof engineParamsSchema>;
|
|
31
31
|
private readonly sessionId;
|
|
32
|
-
private get isVNext();
|
|
33
32
|
private get headers();
|
|
33
|
+
private static extractErrorMessage;
|
|
34
|
+
private static throwOnHttpError;
|
|
34
35
|
/**
|
|
35
36
|
* Create a new LingoDotDevEngine instance
|
|
36
37
|
* @param config - Configuration options for the Engine
|
|
@@ -51,10 +52,9 @@ declare class LingoDotDevEngine {
|
|
|
51
52
|
* @param sourceLocale - Source locale
|
|
52
53
|
* @param targetLocale - Target locale
|
|
53
54
|
* @param payload - Payload containing the chunk to be localized
|
|
54
|
-
* @param workflowId - Workflow ID for tracking
|
|
55
55
|
* @param fast - Whether to use fast mode
|
|
56
56
|
* @param filePath - Optional file path for metadata
|
|
57
|
-
* @param triggerType - Optional trigger type
|
|
57
|
+
* @param triggerType - Optional trigger type
|
|
58
58
|
* @param signal - Optional AbortSignal to cancel the operation
|
|
59
59
|
* @returns Localized chunk
|
|
60
60
|
*/
|
package/build/index.mjs
CHANGED
|
@@ -63,10 +63,10 @@ async function getDistinctId(apiKey, apiUrl) {
|
|
|
63
63
|
const cached = identityCache.get(apiKey);
|
|
64
64
|
if (cached) return cached;
|
|
65
65
|
try {
|
|
66
|
-
const res = await fetch(`${apiUrl}/
|
|
67
|
-
method: "
|
|
66
|
+
const res = await fetch(`${apiUrl}/users/me`, {
|
|
67
|
+
method: "GET",
|
|
68
68
|
headers: {
|
|
69
|
-
|
|
69
|
+
"X-API-Key": apiKey,
|
|
70
70
|
"Content-Type": "application/json"
|
|
71
71
|
}
|
|
72
72
|
});
|
|
@@ -95,7 +95,7 @@ async function getDistinctId(apiKey, apiUrl) {
|
|
|
95
95
|
// src/index.ts
|
|
96
96
|
var engineParamsSchema = Z.object({
|
|
97
97
|
apiKey: Z.string(),
|
|
98
|
-
apiUrl: Z.string().url().default("https://
|
|
98
|
+
apiUrl: Z.string().url().default("https://api.lingo.dev"),
|
|
99
99
|
batchSize: Z.number().int().gt(0).lte(250).default(25),
|
|
100
100
|
idealBatchItemSize: Z.number().int().gt(0).lte(2500).default(250),
|
|
101
101
|
engineId: Z.string().optional()
|
|
@@ -112,31 +112,49 @@ var localizationParamsSchema = Z.object({
|
|
|
112
112
|
filePath: Z.string().optional(),
|
|
113
113
|
triggerType: Z.enum(["cli", "ci"]).optional()
|
|
114
114
|
});
|
|
115
|
-
var LingoDotDevEngine = class {
|
|
115
|
+
var LingoDotDevEngine = class _LingoDotDevEngine {
|
|
116
116
|
config;
|
|
117
117
|
sessionId = createId();
|
|
118
|
-
get isVNext() {
|
|
119
|
-
return !!this.config.engineId;
|
|
120
|
-
}
|
|
121
118
|
get headers() {
|
|
122
|
-
return
|
|
119
|
+
return {
|
|
123
120
|
"Content-Type": "application/json; charset=utf-8",
|
|
124
121
|
"X-API-Key": this.config.apiKey
|
|
125
|
-
} : {
|
|
126
|
-
"Content-Type": "application/json; charset=utf-8",
|
|
127
|
-
Authorization: `Bearer ${this.config.apiKey}`
|
|
128
122
|
};
|
|
129
123
|
}
|
|
124
|
+
static async extractErrorMessage(res) {
|
|
125
|
+
try {
|
|
126
|
+
const text = await res.text();
|
|
127
|
+
const parsed = JSON.parse(text);
|
|
128
|
+
if (parsed && typeof parsed.message === "string") {
|
|
129
|
+
return parsed.message;
|
|
130
|
+
}
|
|
131
|
+
if (parsed?._tag === "NotFoundError") {
|
|
132
|
+
return `${parsed.entityType} not found: ${parsed.id}`;
|
|
133
|
+
}
|
|
134
|
+
return text;
|
|
135
|
+
} catch {
|
|
136
|
+
return `Unexpected error (${res.status})`;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
static async throwOnHttpError(res, context) {
|
|
140
|
+
if (res.ok) return;
|
|
141
|
+
const msg = await _LingoDotDevEngine.extractErrorMessage(res);
|
|
142
|
+
if (res.status >= 500 && res.status < 600) {
|
|
143
|
+
throw new Error(
|
|
144
|
+
`Server error (${res.status}): ${msg}. This may be due to temporary service issues.`
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
if (res.status === 400) {
|
|
148
|
+
throw new Error(`Invalid request: ${msg}`);
|
|
149
|
+
}
|
|
150
|
+
throw new Error(context ? `${context}: ${msg}` : msg);
|
|
151
|
+
}
|
|
130
152
|
/**
|
|
131
153
|
* Create a new LingoDotDevEngine instance
|
|
132
154
|
* @param config - Configuration options for the Engine
|
|
133
155
|
*/
|
|
134
156
|
constructor(config) {
|
|
135
|
-
|
|
136
|
-
if (!config.apiUrl && parsed.engineId) {
|
|
137
|
-
parsed.apiUrl = "https://api.lingo.dev";
|
|
138
|
-
}
|
|
139
|
-
this.config = parsed;
|
|
157
|
+
this.config = engineParamsSchema.parse(config);
|
|
140
158
|
}
|
|
141
159
|
/**
|
|
142
160
|
* Localize content using the Lingo.dev API
|
|
@@ -152,7 +170,6 @@ var LingoDotDevEngine = class {
|
|
|
152
170
|
const finalParams = localizationParamsSchema.parse(params);
|
|
153
171
|
const chunkedPayload = this.extractPayloadChunks(finalPayload);
|
|
154
172
|
const processedPayloadChunks = [];
|
|
155
|
-
const workflowId = createId();
|
|
156
173
|
for (let i = 0; i < chunkedPayload.length; i++) {
|
|
157
174
|
const chunk = chunkedPayload[i];
|
|
158
175
|
const percentageCompleted = Math.round(
|
|
@@ -162,7 +179,6 @@ var LingoDotDevEngine = class {
|
|
|
162
179
|
finalParams.sourceLocale,
|
|
163
180
|
finalParams.targetLocale,
|
|
164
181
|
{ data: chunk, reference: params.reference, hints: params.hints },
|
|
165
|
-
workflowId,
|
|
166
182
|
params.fast || false,
|
|
167
183
|
params.filePath,
|
|
168
184
|
params.triggerType,
|
|
@@ -180,16 +196,15 @@ var LingoDotDevEngine = class {
|
|
|
180
196
|
* @param sourceLocale - Source locale
|
|
181
197
|
* @param targetLocale - Target locale
|
|
182
198
|
* @param payload - Payload containing the chunk to be localized
|
|
183
|
-
* @param workflowId - Workflow ID for tracking
|
|
184
199
|
* @param fast - Whether to use fast mode
|
|
185
200
|
* @param filePath - Optional file path for metadata
|
|
186
|
-
* @param triggerType - Optional trigger type
|
|
201
|
+
* @param triggerType - Optional trigger type
|
|
187
202
|
* @param signal - Optional AbortSignal to cancel the operation
|
|
188
203
|
* @returns Localized chunk
|
|
189
204
|
*/
|
|
190
|
-
async localizeChunk(sourceLocale, targetLocale, payload,
|
|
191
|
-
const url =
|
|
192
|
-
const body =
|
|
205
|
+
async localizeChunk(sourceLocale, targetLocale, payload, fast, filePath, triggerType, signal) {
|
|
206
|
+
const url = `${this.config.apiUrl}/process/localize`;
|
|
207
|
+
const body = {
|
|
193
208
|
params: { fast },
|
|
194
209
|
sourceLocale,
|
|
195
210
|
targetLocale,
|
|
@@ -198,16 +213,8 @@ var LingoDotDevEngine = class {
|
|
|
198
213
|
hints: payload.hints,
|
|
199
214
|
sessionId: this.sessionId,
|
|
200
215
|
triggerType,
|
|
201
|
-
metadata: filePath ? { filePath } : void 0
|
|
202
|
-
|
|
203
|
-
params: { workflowId, fast },
|
|
204
|
-
locale: {
|
|
205
|
-
source: sourceLocale,
|
|
206
|
-
target: targetLocale
|
|
207
|
-
},
|
|
208
|
-
data: payload.data,
|
|
209
|
-
reference: payload.reference,
|
|
210
|
-
hints: payload.hints
|
|
216
|
+
metadata: filePath ? { filePath } : void 0,
|
|
217
|
+
...this.config.engineId && { engineId: this.config.engineId }
|
|
211
218
|
};
|
|
212
219
|
const res = await fetch(url, {
|
|
213
220
|
method: "POST",
|
|
@@ -215,19 +222,7 @@ var LingoDotDevEngine = class {
|
|
|
215
222
|
body: JSON.stringify(body, null, 2),
|
|
216
223
|
signal
|
|
217
224
|
});
|
|
218
|
-
|
|
219
|
-
if (res.status >= 500 && res.status < 600) {
|
|
220
|
-
const errorText = await res.text();
|
|
221
|
-
throw new Error(
|
|
222
|
-
`Server error (${res.status}): ${res.statusText}. ${errorText}. This may be due to temporary service issues.`
|
|
223
|
-
);
|
|
224
|
-
} else if (res.status === 400) {
|
|
225
|
-
throw new Error(`Invalid request: ${res.statusText}`);
|
|
226
|
-
} else {
|
|
227
|
-
const errorText = await res.text();
|
|
228
|
-
throw new Error(errorText);
|
|
229
|
-
}
|
|
230
|
-
}
|
|
225
|
+
await _LingoDotDevEngine.throwOnHttpError(res);
|
|
231
226
|
const jsonResponse = await res.json();
|
|
232
227
|
if (!jsonResponse.data && jsonResponse.error) {
|
|
233
228
|
throw new Error(jsonResponse.error);
|
|
@@ -677,21 +672,17 @@ var LingoDotDevEngine = class {
|
|
|
677
672
|
trackProps
|
|
678
673
|
);
|
|
679
674
|
try {
|
|
680
|
-
const url =
|
|
675
|
+
const url = `${this.config.apiUrl}/process/recognize`;
|
|
681
676
|
const response = await fetch(url, {
|
|
682
677
|
method: "POST",
|
|
683
678
|
headers: this.headers,
|
|
684
679
|
body: JSON.stringify({ text }),
|
|
685
680
|
signal
|
|
686
681
|
});
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
);
|
|
692
|
-
}
|
|
693
|
-
throw new Error(`Error recognizing locale: ${response.statusText}`);
|
|
694
|
-
}
|
|
682
|
+
await _LingoDotDevEngine.throwOnHttpError(
|
|
683
|
+
response,
|
|
684
|
+
"Error recognizing locale"
|
|
685
|
+
);
|
|
695
686
|
const jsonResponse = await response.json();
|
|
696
687
|
trackEvent(
|
|
697
688
|
this.config.apiKey,
|
|
@@ -714,35 +705,29 @@ var LingoDotDevEngine = class {
|
|
|
714
705
|
}
|
|
715
706
|
}
|
|
716
707
|
async whoami(signal) {
|
|
717
|
-
const url =
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
return null;
|
|
728
|
-
}
|
|
729
|
-
return {
|
|
730
|
-
email: payload.email,
|
|
731
|
-
id: payload.id
|
|
732
|
-
};
|
|
733
|
-
}
|
|
734
|
-
if (res.status >= 500 && res.status < 600) {
|
|
735
|
-
throw new Error(
|
|
736
|
-
`Server error (${res.status}): ${res.statusText}. This may be due to temporary service issues.`
|
|
737
|
-
);
|
|
738
|
-
}
|
|
739
|
-
return null;
|
|
740
|
-
} catch (error) {
|
|
741
|
-
if (error instanceof Error && error.message.includes("Server error")) {
|
|
742
|
-
throw error;
|
|
708
|
+
const url = `${this.config.apiUrl}/users/me`;
|
|
709
|
+
const res = await fetch(url, {
|
|
710
|
+
method: "GET",
|
|
711
|
+
headers: this.headers,
|
|
712
|
+
signal
|
|
713
|
+
});
|
|
714
|
+
if (res.ok) {
|
|
715
|
+
const payload = await res.json();
|
|
716
|
+
if (!payload?.email) {
|
|
717
|
+
return null;
|
|
743
718
|
}
|
|
744
|
-
return
|
|
719
|
+
return {
|
|
720
|
+
email: payload.email,
|
|
721
|
+
id: payload.id
|
|
722
|
+
};
|
|
723
|
+
}
|
|
724
|
+
if (res.status >= 500 && res.status < 600) {
|
|
725
|
+
const msg = await _LingoDotDevEngine.extractErrorMessage(res);
|
|
726
|
+
throw new Error(
|
|
727
|
+
`Server error (${res.status}): ${msg}. This may be due to temporary service issues.`
|
|
728
|
+
);
|
|
745
729
|
}
|
|
730
|
+
return null;
|
|
746
731
|
}
|
|
747
732
|
};
|
|
748
733
|
var ReplexicaEngine = class _ReplexicaEngine extends LingoDotDevEngine {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lingo.dev/_sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.16.1",
|
|
4
4
|
"description": "Lingo.dev JS SDK",
|
|
5
5
|
"private": false,
|
|
6
6
|
"repository": {
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
"jsdom": "25.0.1",
|
|
29
29
|
"posthog-node": "5.14.0",
|
|
30
30
|
"zod": "4.1.12",
|
|
31
|
-
"@lingo.dev/_spec": "0.
|
|
31
|
+
"@lingo.dev/_spec": "0.49.0"
|
|
32
32
|
},
|
|
33
33
|
"devDependencies": {
|
|
34
34
|
"@types/jsdom": "21.1.7",
|