@lingo.dev/_sdk 0.16.0 → 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 CHANGED
@@ -112,7 +112,7 @@ 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
118
  get headers() {
@@ -121,6 +121,34 @@ var LingoDotDevEngine = (_class = class {
121
121
  "X-API-Key": this.config.apiKey
122
122
  };
123
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
+ }
124
152
  /**
125
153
  * Create a new LingoDotDevEngine instance
126
154
  * @param config - Configuration options for the Engine
@@ -142,7 +170,6 @@ var LingoDotDevEngine = (_class = class {
142
170
  const finalParams = localizationParamsSchema.parse(params);
143
171
  const chunkedPayload = this.extractPayloadChunks(finalPayload);
144
172
  const processedPayloadChunks = [];
145
- const workflowId = _cuid2.createId.call(void 0, );
146
173
  for (let i = 0; i < chunkedPayload.length; i++) {
147
174
  const chunk = chunkedPayload[i];
148
175
  const percentageCompleted = Math.round(
@@ -152,7 +179,6 @@ var LingoDotDevEngine = (_class = class {
152
179
  finalParams.sourceLocale,
153
180
  finalParams.targetLocale,
154
181
  { data: chunk, reference: params.reference, hints: params.hints },
155
- workflowId,
156
182
  params.fast || false,
157
183
  params.filePath,
158
184
  params.triggerType,
@@ -170,14 +196,13 @@ var LingoDotDevEngine = (_class = class {
170
196
  * @param sourceLocale - Source locale
171
197
  * @param targetLocale - Target locale
172
198
  * @param payload - Payload containing the chunk to be localized
173
- * @param workflowId - Workflow ID for tracking
174
199
  * @param fast - Whether to use fast mode
175
200
  * @param filePath - Optional file path for metadata
176
201
  * @param triggerType - Optional trigger type
177
202
  * @param signal - Optional AbortSignal to cancel the operation
178
203
  * @returns Localized chunk
179
204
  */
180
- async localizeChunk(sourceLocale, targetLocale, payload, workflowId, fast, filePath, triggerType, signal) {
205
+ async localizeChunk(sourceLocale, targetLocale, payload, fast, filePath, triggerType, signal) {
181
206
  const url = `${this.config.apiUrl}/process/localize`;
182
207
  const body = {
183
208
  params: { fast },
@@ -197,19 +222,7 @@ var LingoDotDevEngine = (_class = class {
197
222
  body: JSON.stringify(body, null, 2),
198
223
  signal
199
224
  });
200
- if (!res.ok) {
201
- if (res.status >= 500 && res.status < 600) {
202
- const errorText = await res.text();
203
- throw new Error(
204
- `Server error (${res.status}): ${res.statusText}. ${errorText}. This may be due to temporary service issues.`
205
- );
206
- } else if (res.status === 400) {
207
- throw new Error(`Invalid request: ${res.statusText}`);
208
- } else {
209
- const errorText = await res.text();
210
- throw new Error(errorText);
211
- }
212
- }
225
+ await _LingoDotDevEngine.throwOnHttpError(res);
213
226
  const jsonResponse = await res.json();
214
227
  if (!jsonResponse.data && jsonResponse.error) {
215
228
  throw new Error(jsonResponse.error);
@@ -549,7 +562,7 @@ var LingoDotDevEngine = (_class = class {
549
562
  break;
550
563
  }
551
564
  const siblings = Array.from(parent.childNodes).filter(
552
- (n) => n.nodeType === 1 || n.nodeType === 3 && _optionalChain([n, 'access', _2 => _2.textContent, 'optionalAccess', _3 => _3.trim, 'call', _4 => _4()])
565
+ (n) => n.nodeType === 1 || n.nodeType === 3 && _optionalChain([n, 'access', _3 => _3.textContent, 'optionalAccess', _4 => _4.trim, 'call', _5 => _5()])
553
566
  );
554
567
  const index = siblings.indexOf(current);
555
568
  if (index !== -1) {
@@ -569,7 +582,7 @@ var LingoDotDevEngine = (_class = class {
569
582
  parent = parent.parentElement;
570
583
  }
571
584
  if (node.nodeType === 3) {
572
- const text = _optionalChain([node, 'access', _5 => _5.textContent, 'optionalAccess', _6 => _6.trim, 'call', _7 => _7()]) || "";
585
+ const text = _optionalChain([node, 'access', _6 => _6.textContent, 'optionalAccess', _7 => _7.trim, 'call', _8 => _8()]) || "";
573
586
  if (text) {
574
587
  extractedContent[getPath(node)] = text;
575
588
  }
@@ -584,15 +597,15 @@ var LingoDotDevEngine = (_class = class {
584
597
  }
585
598
  });
586
599
  Array.from(element.childNodes).filter(
587
- (n) => n.nodeType === 1 || n.nodeType === 3 && _optionalChain([n, 'access', _8 => _8.textContent, 'optionalAccess', _9 => _9.trim, 'call', _10 => _10()])
600
+ (n) => n.nodeType === 1 || n.nodeType === 3 && _optionalChain([n, 'access', _9 => _9.textContent, 'optionalAccess', _10 => _10.trim, 'call', _11 => _11()])
588
601
  ).forEach(processNode);
589
602
  }
590
603
  };
591
604
  Array.from(document.head.childNodes).filter(
592
- (n) => n.nodeType === 1 || n.nodeType === 3 && _optionalChain([n, 'access', _11 => _11.textContent, 'optionalAccess', _12 => _12.trim, 'call', _13 => _13()])
605
+ (n) => n.nodeType === 1 || n.nodeType === 3 && _optionalChain([n, 'access', _12 => _12.textContent, 'optionalAccess', _13 => _13.trim, 'call', _14 => _14()])
593
606
  ).forEach(processNode);
594
607
  Array.from(document.body.childNodes).filter(
595
- (n) => n.nodeType === 1 || n.nodeType === 3 && _optionalChain([n, 'access', _14 => _14.textContent, 'optionalAccess', _15 => _15.trim, 'call', _16 => _16()])
608
+ (n) => n.nodeType === 1 || n.nodeType === 3 && _optionalChain([n, 'access', _15 => _15.textContent, 'optionalAccess', _16 => _16.trim, 'call', _17 => _17()])
596
609
  ).forEach(processNode);
597
610
  const localizedContent = await this._localizeRaw(
598
611
  extractedContent,
@@ -608,10 +621,10 @@ var LingoDotDevEngine = (_class = class {
608
621
  let current = parent;
609
622
  for (const index of indices) {
610
623
  const siblings = Array.from(parent.childNodes).filter(
611
- (n) => n.nodeType === 1 || n.nodeType === 3 && _optionalChain([n, 'access', _17 => _17.textContent, 'optionalAccess', _18 => _18.trim, 'call', _19 => _19()])
624
+ (n) => n.nodeType === 1 || n.nodeType === 3 && _optionalChain([n, 'access', _18 => _18.textContent, 'optionalAccess', _19 => _19.trim, 'call', _20 => _20()])
612
625
  );
613
626
  current = siblings[parseInt(index)] || null;
614
- if (_optionalChain([current, 'optionalAccess', _20 => _20.nodeType]) === 1) {
627
+ if (_optionalChain([current, 'optionalAccess', _21 => _21.nodeType]) === 1) {
615
628
  parent = current;
616
629
  }
617
630
  }
@@ -666,14 +679,10 @@ var LingoDotDevEngine = (_class = class {
666
679
  body: JSON.stringify({ text }),
667
680
  signal
668
681
  });
669
- if (!response.ok) {
670
- if (response.status >= 500 && response.status < 600) {
671
- throw new Error(
672
- `Server error (${response.status}): ${response.statusText}. This may be due to temporary service issues.`
673
- );
674
- }
675
- throw new Error(`Error recognizing locale: ${response.statusText}`);
676
- }
682
+ await _LingoDotDevEngine.throwOnHttpError(
683
+ response,
684
+ "Error recognizing locale"
685
+ );
677
686
  const jsonResponse = await response.json();
678
687
  trackEvent(
679
688
  this.config.apiKey,
@@ -697,34 +706,28 @@ var LingoDotDevEngine = (_class = class {
697
706
  }
698
707
  async whoami(signal) {
699
708
  const url = `${this.config.apiUrl}/users/me`;
700
- try {
701
- const res = await fetch(url, {
702
- method: "GET",
703
- headers: this.headers,
704
- signal
705
- });
706
- if (res.ok) {
707
- const payload = await res.json();
708
- if (!_optionalChain([payload, 'optionalAccess', _21 => _21.email])) {
709
- return null;
710
- }
711
- return {
712
- email: payload.email,
713
- id: payload.id
714
- };
715
- }
716
- if (res.status >= 500 && res.status < 600) {
717
- throw new Error(
718
- `Server error (${res.status}): ${res.statusText}. This may be due to temporary service issues.`
719
- );
720
- }
721
- return null;
722
- } catch (error) {
723
- if (error instanceof Error && error.message.includes("Server error")) {
724
- throw error;
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;
725
718
  }
726
- return null;
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
+ );
727
729
  }
730
+ return null;
728
731
  }
729
732
  }, _class);
730
733
  var ReplexicaEngine = (_class2 = class _ReplexicaEngine extends LingoDotDevEngine {
package/build/index.d.cts CHANGED
@@ -30,6 +30,8 @@ declare class LingoDotDevEngine {
30
30
  protected config: Z.infer<typeof engineParamsSchema>;
31
31
  private readonly sessionId;
32
32
  private get headers();
33
+ private static extractErrorMessage;
34
+ private static throwOnHttpError;
33
35
  /**
34
36
  * Create a new LingoDotDevEngine instance
35
37
  * @param config - Configuration options for the Engine
@@ -50,7 +52,6 @@ declare class LingoDotDevEngine {
50
52
  * @param sourceLocale - Source locale
51
53
  * @param targetLocale - Target locale
52
54
  * @param payload - Payload containing the chunk to be localized
53
- * @param workflowId - Workflow ID for tracking
54
55
  * @param fast - Whether to use fast mode
55
56
  * @param filePath - Optional file path for metadata
56
57
  * @param triggerType - Optional trigger type
package/build/index.d.ts CHANGED
@@ -30,6 +30,8 @@ declare class LingoDotDevEngine {
30
30
  protected config: Z.infer<typeof engineParamsSchema>;
31
31
  private readonly sessionId;
32
32
  private get headers();
33
+ private static extractErrorMessage;
34
+ private static throwOnHttpError;
33
35
  /**
34
36
  * Create a new LingoDotDevEngine instance
35
37
  * @param config - Configuration options for the Engine
@@ -50,7 +52,6 @@ declare class LingoDotDevEngine {
50
52
  * @param sourceLocale - Source locale
51
53
  * @param targetLocale - Target locale
52
54
  * @param payload - Payload containing the chunk to be localized
53
- * @param workflowId - Workflow ID for tracking
54
55
  * @param fast - Whether to use fast mode
55
56
  * @param filePath - Optional file path for metadata
56
57
  * @param triggerType - Optional trigger type
package/build/index.mjs CHANGED
@@ -112,7 +112,7 @@ 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
118
  get headers() {
@@ -121,6 +121,34 @@ var LingoDotDevEngine = class {
121
121
  "X-API-Key": this.config.apiKey
122
122
  };
123
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
+ }
124
152
  /**
125
153
  * Create a new LingoDotDevEngine instance
126
154
  * @param config - Configuration options for the Engine
@@ -142,7 +170,6 @@ var LingoDotDevEngine = class {
142
170
  const finalParams = localizationParamsSchema.parse(params);
143
171
  const chunkedPayload = this.extractPayloadChunks(finalPayload);
144
172
  const processedPayloadChunks = [];
145
- const workflowId = createId();
146
173
  for (let i = 0; i < chunkedPayload.length; i++) {
147
174
  const chunk = chunkedPayload[i];
148
175
  const percentageCompleted = Math.round(
@@ -152,7 +179,6 @@ var LingoDotDevEngine = class {
152
179
  finalParams.sourceLocale,
153
180
  finalParams.targetLocale,
154
181
  { data: chunk, reference: params.reference, hints: params.hints },
155
- workflowId,
156
182
  params.fast || false,
157
183
  params.filePath,
158
184
  params.triggerType,
@@ -170,14 +196,13 @@ var LingoDotDevEngine = class {
170
196
  * @param sourceLocale - Source locale
171
197
  * @param targetLocale - Target locale
172
198
  * @param payload - Payload containing the chunk to be localized
173
- * @param workflowId - Workflow ID for tracking
174
199
  * @param fast - Whether to use fast mode
175
200
  * @param filePath - Optional file path for metadata
176
201
  * @param triggerType - Optional trigger type
177
202
  * @param signal - Optional AbortSignal to cancel the operation
178
203
  * @returns Localized chunk
179
204
  */
180
- async localizeChunk(sourceLocale, targetLocale, payload, workflowId, fast, filePath, triggerType, signal) {
205
+ async localizeChunk(sourceLocale, targetLocale, payload, fast, filePath, triggerType, signal) {
181
206
  const url = `${this.config.apiUrl}/process/localize`;
182
207
  const body = {
183
208
  params: { fast },
@@ -197,19 +222,7 @@ var LingoDotDevEngine = class {
197
222
  body: JSON.stringify(body, null, 2),
198
223
  signal
199
224
  });
200
- if (!res.ok) {
201
- if (res.status >= 500 && res.status < 600) {
202
- const errorText = await res.text();
203
- throw new Error(
204
- `Server error (${res.status}): ${res.statusText}. ${errorText}. This may be due to temporary service issues.`
205
- );
206
- } else if (res.status === 400) {
207
- throw new Error(`Invalid request: ${res.statusText}`);
208
- } else {
209
- const errorText = await res.text();
210
- throw new Error(errorText);
211
- }
212
- }
225
+ await _LingoDotDevEngine.throwOnHttpError(res);
213
226
  const jsonResponse = await res.json();
214
227
  if (!jsonResponse.data && jsonResponse.error) {
215
228
  throw new Error(jsonResponse.error);
@@ -666,14 +679,10 @@ var LingoDotDevEngine = class {
666
679
  body: JSON.stringify({ text }),
667
680
  signal
668
681
  });
669
- if (!response.ok) {
670
- if (response.status >= 500 && response.status < 600) {
671
- throw new Error(
672
- `Server error (${response.status}): ${response.statusText}. This may be due to temporary service issues.`
673
- );
674
- }
675
- throw new Error(`Error recognizing locale: ${response.statusText}`);
676
- }
682
+ await _LingoDotDevEngine.throwOnHttpError(
683
+ response,
684
+ "Error recognizing locale"
685
+ );
677
686
  const jsonResponse = await response.json();
678
687
  trackEvent(
679
688
  this.config.apiKey,
@@ -697,34 +706,28 @@ var LingoDotDevEngine = class {
697
706
  }
698
707
  async whoami(signal) {
699
708
  const url = `${this.config.apiUrl}/users/me`;
700
- try {
701
- const res = await fetch(url, {
702
- method: "GET",
703
- headers: this.headers,
704
- signal
705
- });
706
- if (res.ok) {
707
- const payload = await res.json();
708
- if (!payload?.email) {
709
- return null;
710
- }
711
- return {
712
- email: payload.email,
713
- id: payload.id
714
- };
715
- }
716
- if (res.status >= 500 && res.status < 600) {
717
- throw new Error(
718
- `Server error (${res.status}): ${res.statusText}. This may be due to temporary service issues.`
719
- );
720
- }
721
- return null;
722
- } catch (error) {
723
- if (error instanceof Error && error.message.includes("Server error")) {
724
- throw error;
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;
725
718
  }
726
- return null;
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
+ );
727
729
  }
730
+ return null;
728
731
  }
729
732
  };
730
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.16.0",
3
+ "version": "0.16.1",
4
4
  "description": "Lingo.dev JS SDK",
5
5
  "private": false,
6
6
  "repository": {