@lingo.dev/_sdk 0.16.0 → 0.16.2

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
@@ -35,10 +35,10 @@ function trackEvent(apiKey, apiUrl, event, properties) {
35
35
  );
36
36
  }
37
37
  async function resolveIdentityAndCapture(apiKey, apiUrl, event, properties) {
38
- const identityInfo = await getDistinctId(apiKey, apiUrl);
38
+ const { identity, email } = await getDistinctId(apiKey, apiUrl);
39
39
  if (process.env.DEBUG === "true") {
40
40
  console.log(
41
- `[Tracking] Event: ${event}, ID: ${identityInfo.distinct_id}, Source: ${identityInfo.distinct_id_source}`
41
+ `[Tracking] Event: ${event}, ID: ${identity.distinct_id}, Source: ${identity.distinct_id_source}`
42
42
  );
43
43
  }
44
44
  const { PostHog } = await Promise.resolve().then(() => _interopRequireWildcard(require("posthog-node")));
@@ -47,17 +47,27 @@ async function resolveIdentityAndCapture(apiKey, apiUrl, event, properties) {
47
47
  flushAt: 1,
48
48
  flushInterval: 0
49
49
  });
50
- await posthog.capture({
51
- distinctId: identityInfo.distinct_id,
52
- event,
53
- properties: {
54
- ...properties,
55
- tracking_version: TRACKING_VERSION,
56
- sdk_package: SDK_PACKAGE,
57
- distinct_id_source: identityInfo.distinct_id_source
50
+ try {
51
+ await posthog.capture({
52
+ distinctId: identity.distinct_id,
53
+ event,
54
+ properties: {
55
+ ...properties,
56
+ $set: { ..._optionalChain([properties, 'optionalAccess', _ => _.$set]) || {}, ...email ? { email } : {} },
57
+ tracking_version: TRACKING_VERSION,
58
+ sdk_package: SDK_PACKAGE,
59
+ distinct_id_source: identity.distinct_id_source
60
+ }
61
+ });
62
+ if (email) {
63
+ await posthog.alias({
64
+ distinctId: identity.distinct_id,
65
+ alias: email
66
+ });
58
67
  }
59
- });
60
- await posthog.shutdown();
68
+ } finally {
69
+ await posthog.shutdown();
70
+ }
61
71
  }
62
72
  async function getDistinctId(apiKey, apiUrl) {
63
73
  const cached = identityCache.get(apiKey);
@@ -72,24 +82,27 @@ async function getDistinctId(apiKey, apiUrl) {
72
82
  });
73
83
  if (res.ok) {
74
84
  const payload = await res.json();
75
- if (_optionalChain([payload, 'optionalAccess', _ => _.email])) {
76
- const identity2 = {
77
- distinct_id: payload.email,
78
- distinct_id_source: "email"
85
+ if (_optionalChain([payload, 'optionalAccess', _2 => _2.id])) {
86
+ const result = {
87
+ identity: {
88
+ distinct_id: payload.id,
89
+ distinct_id_source: "database_id"
90
+ },
91
+ email: payload.email || void 0
79
92
  };
80
- identityCache.set(apiKey, identity2);
81
- return identity2;
93
+ identityCache.set(apiKey, result);
94
+ return result;
82
95
  }
83
96
  }
84
97
  } catch (e) {
85
98
  }
86
99
  const hash = _crypto.createHash.call(void 0, "sha256").update(apiKey).digest("hex").slice(0, 16);
87
- const identity = {
88
- distinct_id: `apikey-${hash}`,
89
- distinct_id_source: "api_key_hash"
100
+ return {
101
+ identity: {
102
+ distinct_id: `apikey-${hash}`,
103
+ distinct_id_source: "api_key_hash"
104
+ }
90
105
  };
91
- identityCache.set(apiKey, identity);
92
- return identity;
93
106
  }
94
107
 
95
108
  // src/index.ts
@@ -112,7 +125,7 @@ var localizationParamsSchema = _zod2.default.object({
112
125
  filePath: _zod2.default.string().optional(),
113
126
  triggerType: _zod2.default.enum(["cli", "ci"]).optional()
114
127
  });
115
- var LingoDotDevEngine = (_class = class {
128
+ var LingoDotDevEngine = (_class = class _LingoDotDevEngine {
116
129
 
117
130
  __init() {this.sessionId = _cuid2.createId.call(void 0, )}
118
131
  get headers() {
@@ -121,6 +134,34 @@ var LingoDotDevEngine = (_class = class {
121
134
  "X-API-Key": this.config.apiKey
122
135
  };
123
136
  }
137
+ static async extractErrorMessage(res) {
138
+ try {
139
+ const text = await res.text();
140
+ const parsed = JSON.parse(text);
141
+ if (parsed && typeof parsed.message === "string") {
142
+ return parsed.message;
143
+ }
144
+ if (_optionalChain([parsed, 'optionalAccess', _3 => _3._tag]) === "NotFoundError") {
145
+ return `${parsed.entityType} not found: ${parsed.id}`;
146
+ }
147
+ return text;
148
+ } catch (e2) {
149
+ return `Unexpected error (${res.status})`;
150
+ }
151
+ }
152
+ static async throwOnHttpError(res, context) {
153
+ if (res.ok) return;
154
+ const msg = await _LingoDotDevEngine.extractErrorMessage(res);
155
+ if (res.status >= 500 && res.status < 600) {
156
+ throw new Error(
157
+ `Server error (${res.status}): ${msg}. This may be due to temporary service issues.`
158
+ );
159
+ }
160
+ if (res.status === 400) {
161
+ throw new Error(`Invalid request: ${msg}`);
162
+ }
163
+ throw new Error(context ? `${context}: ${msg}` : msg);
164
+ }
124
165
  /**
125
166
  * Create a new LingoDotDevEngine instance
126
167
  * @param config - Configuration options for the Engine
@@ -142,7 +183,6 @@ var LingoDotDevEngine = (_class = class {
142
183
  const finalParams = localizationParamsSchema.parse(params);
143
184
  const chunkedPayload = this.extractPayloadChunks(finalPayload);
144
185
  const processedPayloadChunks = [];
145
- const workflowId = _cuid2.createId.call(void 0, );
146
186
  for (let i = 0; i < chunkedPayload.length; i++) {
147
187
  const chunk = chunkedPayload[i];
148
188
  const percentageCompleted = Math.round(
@@ -152,7 +192,6 @@ var LingoDotDevEngine = (_class = class {
152
192
  finalParams.sourceLocale,
153
193
  finalParams.targetLocale,
154
194
  { data: chunk, reference: params.reference, hints: params.hints },
155
- workflowId,
156
195
  params.fast || false,
157
196
  params.filePath,
158
197
  params.triggerType,
@@ -170,14 +209,13 @@ var LingoDotDevEngine = (_class = class {
170
209
  * @param sourceLocale - Source locale
171
210
  * @param targetLocale - Target locale
172
211
  * @param payload - Payload containing the chunk to be localized
173
- * @param workflowId - Workflow ID for tracking
174
212
  * @param fast - Whether to use fast mode
175
213
  * @param filePath - Optional file path for metadata
176
214
  * @param triggerType - Optional trigger type
177
215
  * @param signal - Optional AbortSignal to cancel the operation
178
216
  * @returns Localized chunk
179
217
  */
180
- async localizeChunk(sourceLocale, targetLocale, payload, workflowId, fast, filePath, triggerType, signal) {
218
+ async localizeChunk(sourceLocale, targetLocale, payload, fast, filePath, triggerType, signal) {
181
219
  const url = `${this.config.apiUrl}/process/localize`;
182
220
  const body = {
183
221
  params: { fast },
@@ -197,19 +235,7 @@ var LingoDotDevEngine = (_class = class {
197
235
  body: JSON.stringify(body, null, 2),
198
236
  signal
199
237
  });
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
- }
238
+ await _LingoDotDevEngine.throwOnHttpError(res);
213
239
  const jsonResponse = await res.json();
214
240
  if (!jsonResponse.data && jsonResponse.error) {
215
241
  throw new Error(jsonResponse.error);
@@ -549,7 +575,7 @@ var LingoDotDevEngine = (_class = class {
549
575
  break;
550
576
  }
551
577
  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()])
578
+ (n) => n.nodeType === 1 || n.nodeType === 3 && _optionalChain([n, 'access', _4 => _4.textContent, 'optionalAccess', _5 => _5.trim, 'call', _6 => _6()])
553
579
  );
554
580
  const index = siblings.indexOf(current);
555
581
  if (index !== -1) {
@@ -569,7 +595,7 @@ var LingoDotDevEngine = (_class = class {
569
595
  parent = parent.parentElement;
570
596
  }
571
597
  if (node.nodeType === 3) {
572
- const text = _optionalChain([node, 'access', _5 => _5.textContent, 'optionalAccess', _6 => _6.trim, 'call', _7 => _7()]) || "";
598
+ const text = _optionalChain([node, 'access', _7 => _7.textContent, 'optionalAccess', _8 => _8.trim, 'call', _9 => _9()]) || "";
573
599
  if (text) {
574
600
  extractedContent[getPath(node)] = text;
575
601
  }
@@ -584,15 +610,15 @@ var LingoDotDevEngine = (_class = class {
584
610
  }
585
611
  });
586
612
  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()])
613
+ (n) => n.nodeType === 1 || n.nodeType === 3 && _optionalChain([n, 'access', _10 => _10.textContent, 'optionalAccess', _11 => _11.trim, 'call', _12 => _12()])
588
614
  ).forEach(processNode);
589
615
  }
590
616
  };
591
617
  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()])
618
+ (n) => n.nodeType === 1 || n.nodeType === 3 && _optionalChain([n, 'access', _13 => _13.textContent, 'optionalAccess', _14 => _14.trim, 'call', _15 => _15()])
593
619
  ).forEach(processNode);
594
620
  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()])
621
+ (n) => n.nodeType === 1 || n.nodeType === 3 && _optionalChain([n, 'access', _16 => _16.textContent, 'optionalAccess', _17 => _17.trim, 'call', _18 => _18()])
596
622
  ).forEach(processNode);
597
623
  const localizedContent = await this._localizeRaw(
598
624
  extractedContent,
@@ -608,10 +634,10 @@ var LingoDotDevEngine = (_class = class {
608
634
  let current = parent;
609
635
  for (const index of indices) {
610
636
  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()])
637
+ (n) => n.nodeType === 1 || n.nodeType === 3 && _optionalChain([n, 'access', _19 => _19.textContent, 'optionalAccess', _20 => _20.trim, 'call', _21 => _21()])
612
638
  );
613
639
  current = siblings[parseInt(index)] || null;
614
- if (_optionalChain([current, 'optionalAccess', _20 => _20.nodeType]) === 1) {
640
+ if (_optionalChain([current, 'optionalAccess', _22 => _22.nodeType]) === 1) {
615
641
  parent = current;
616
642
  }
617
643
  }
@@ -666,14 +692,10 @@ var LingoDotDevEngine = (_class = class {
666
692
  body: JSON.stringify({ text }),
667
693
  signal
668
694
  });
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
- }
695
+ await _LingoDotDevEngine.throwOnHttpError(
696
+ response,
697
+ "Error recognizing locale"
698
+ );
677
699
  const jsonResponse = await response.json();
678
700
  trackEvent(
679
701
  this.config.apiKey,
@@ -697,34 +719,28 @@ var LingoDotDevEngine = (_class = class {
697
719
  }
698
720
  async whoami(signal) {
699
721
  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;
722
+ const res = await fetch(url, {
723
+ method: "GET",
724
+ headers: this.headers,
725
+ signal
726
+ });
727
+ if (res.ok) {
728
+ const payload = await res.json();
729
+ if (!_optionalChain([payload, 'optionalAccess', _23 => _23.email])) {
730
+ return null;
725
731
  }
726
- return null;
732
+ return {
733
+ email: payload.email,
734
+ id: payload.id
735
+ };
736
+ }
737
+ if (res.status >= 500 && res.status < 600) {
738
+ const msg = await _LingoDotDevEngine.extractErrorMessage(res);
739
+ throw new Error(
740
+ `Server error (${res.status}): ${msg}. This may be due to temporary service issues.`
741
+ );
727
742
  }
743
+ return null;
728
744
  }
729
745
  }, _class);
730
746
  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
@@ -35,10 +35,10 @@ function trackEvent(apiKey, apiUrl, event, properties) {
35
35
  );
36
36
  }
37
37
  async function resolveIdentityAndCapture(apiKey, apiUrl, event, properties) {
38
- const identityInfo = await getDistinctId(apiKey, apiUrl);
38
+ const { identity, email } = await getDistinctId(apiKey, apiUrl);
39
39
  if (process.env.DEBUG === "true") {
40
40
  console.log(
41
- `[Tracking] Event: ${event}, ID: ${identityInfo.distinct_id}, Source: ${identityInfo.distinct_id_source}`
41
+ `[Tracking] Event: ${event}, ID: ${identity.distinct_id}, Source: ${identity.distinct_id_source}`
42
42
  );
43
43
  }
44
44
  const { PostHog } = await import("posthog-node");
@@ -47,17 +47,27 @@ async function resolveIdentityAndCapture(apiKey, apiUrl, event, properties) {
47
47
  flushAt: 1,
48
48
  flushInterval: 0
49
49
  });
50
- await posthog.capture({
51
- distinctId: identityInfo.distinct_id,
52
- event,
53
- properties: {
54
- ...properties,
55
- tracking_version: TRACKING_VERSION,
56
- sdk_package: SDK_PACKAGE,
57
- distinct_id_source: identityInfo.distinct_id_source
50
+ try {
51
+ await posthog.capture({
52
+ distinctId: identity.distinct_id,
53
+ event,
54
+ properties: {
55
+ ...properties,
56
+ $set: { ...properties?.$set || {}, ...email ? { email } : {} },
57
+ tracking_version: TRACKING_VERSION,
58
+ sdk_package: SDK_PACKAGE,
59
+ distinct_id_source: identity.distinct_id_source
60
+ }
61
+ });
62
+ if (email) {
63
+ await posthog.alias({
64
+ distinctId: identity.distinct_id,
65
+ alias: email
66
+ });
58
67
  }
59
- });
60
- await posthog.shutdown();
68
+ } finally {
69
+ await posthog.shutdown();
70
+ }
61
71
  }
62
72
  async function getDistinctId(apiKey, apiUrl) {
63
73
  const cached = identityCache.get(apiKey);
@@ -72,24 +82,27 @@ async function getDistinctId(apiKey, apiUrl) {
72
82
  });
73
83
  if (res.ok) {
74
84
  const payload = await res.json();
75
- if (payload?.email) {
76
- const identity2 = {
77
- distinct_id: payload.email,
78
- distinct_id_source: "email"
85
+ if (payload?.id) {
86
+ const result = {
87
+ identity: {
88
+ distinct_id: payload.id,
89
+ distinct_id_source: "database_id"
90
+ },
91
+ email: payload.email || void 0
79
92
  };
80
- identityCache.set(apiKey, identity2);
81
- return identity2;
93
+ identityCache.set(apiKey, result);
94
+ return result;
82
95
  }
83
96
  }
84
97
  } catch {
85
98
  }
86
99
  const hash = createHash("sha256").update(apiKey).digest("hex").slice(0, 16);
87
- const identity = {
88
- distinct_id: `apikey-${hash}`,
89
- distinct_id_source: "api_key_hash"
100
+ return {
101
+ identity: {
102
+ distinct_id: `apikey-${hash}`,
103
+ distinct_id_source: "api_key_hash"
104
+ }
90
105
  };
91
- identityCache.set(apiKey, identity);
92
- return identity;
93
106
  }
94
107
 
95
108
  // src/index.ts
@@ -112,7 +125,7 @@ var localizationParamsSchema = Z.object({
112
125
  filePath: Z.string().optional(),
113
126
  triggerType: Z.enum(["cli", "ci"]).optional()
114
127
  });
115
- var LingoDotDevEngine = class {
128
+ var LingoDotDevEngine = class _LingoDotDevEngine {
116
129
  config;
117
130
  sessionId = createId();
118
131
  get headers() {
@@ -121,6 +134,34 @@ var LingoDotDevEngine = class {
121
134
  "X-API-Key": this.config.apiKey
122
135
  };
123
136
  }
137
+ static async extractErrorMessage(res) {
138
+ try {
139
+ const text = await res.text();
140
+ const parsed = JSON.parse(text);
141
+ if (parsed && typeof parsed.message === "string") {
142
+ return parsed.message;
143
+ }
144
+ if (parsed?._tag === "NotFoundError") {
145
+ return `${parsed.entityType} not found: ${parsed.id}`;
146
+ }
147
+ return text;
148
+ } catch {
149
+ return `Unexpected error (${res.status})`;
150
+ }
151
+ }
152
+ static async throwOnHttpError(res, context) {
153
+ if (res.ok) return;
154
+ const msg = await _LingoDotDevEngine.extractErrorMessage(res);
155
+ if (res.status >= 500 && res.status < 600) {
156
+ throw new Error(
157
+ `Server error (${res.status}): ${msg}. This may be due to temporary service issues.`
158
+ );
159
+ }
160
+ if (res.status === 400) {
161
+ throw new Error(`Invalid request: ${msg}`);
162
+ }
163
+ throw new Error(context ? `${context}: ${msg}` : msg);
164
+ }
124
165
  /**
125
166
  * Create a new LingoDotDevEngine instance
126
167
  * @param config - Configuration options for the Engine
@@ -142,7 +183,6 @@ var LingoDotDevEngine = class {
142
183
  const finalParams = localizationParamsSchema.parse(params);
143
184
  const chunkedPayload = this.extractPayloadChunks(finalPayload);
144
185
  const processedPayloadChunks = [];
145
- const workflowId = createId();
146
186
  for (let i = 0; i < chunkedPayload.length; i++) {
147
187
  const chunk = chunkedPayload[i];
148
188
  const percentageCompleted = Math.round(
@@ -152,7 +192,6 @@ var LingoDotDevEngine = class {
152
192
  finalParams.sourceLocale,
153
193
  finalParams.targetLocale,
154
194
  { data: chunk, reference: params.reference, hints: params.hints },
155
- workflowId,
156
195
  params.fast || false,
157
196
  params.filePath,
158
197
  params.triggerType,
@@ -170,14 +209,13 @@ var LingoDotDevEngine = class {
170
209
  * @param sourceLocale - Source locale
171
210
  * @param targetLocale - Target locale
172
211
  * @param payload - Payload containing the chunk to be localized
173
- * @param workflowId - Workflow ID for tracking
174
212
  * @param fast - Whether to use fast mode
175
213
  * @param filePath - Optional file path for metadata
176
214
  * @param triggerType - Optional trigger type
177
215
  * @param signal - Optional AbortSignal to cancel the operation
178
216
  * @returns Localized chunk
179
217
  */
180
- async localizeChunk(sourceLocale, targetLocale, payload, workflowId, fast, filePath, triggerType, signal) {
218
+ async localizeChunk(sourceLocale, targetLocale, payload, fast, filePath, triggerType, signal) {
181
219
  const url = `${this.config.apiUrl}/process/localize`;
182
220
  const body = {
183
221
  params: { fast },
@@ -197,19 +235,7 @@ var LingoDotDevEngine = class {
197
235
  body: JSON.stringify(body, null, 2),
198
236
  signal
199
237
  });
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
- }
238
+ await _LingoDotDevEngine.throwOnHttpError(res);
213
239
  const jsonResponse = await res.json();
214
240
  if (!jsonResponse.data && jsonResponse.error) {
215
241
  throw new Error(jsonResponse.error);
@@ -666,14 +692,10 @@ var LingoDotDevEngine = class {
666
692
  body: JSON.stringify({ text }),
667
693
  signal
668
694
  });
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
- }
695
+ await _LingoDotDevEngine.throwOnHttpError(
696
+ response,
697
+ "Error recognizing locale"
698
+ );
677
699
  const jsonResponse = await response.json();
678
700
  trackEvent(
679
701
  this.config.apiKey,
@@ -697,34 +719,28 @@ var LingoDotDevEngine = class {
697
719
  }
698
720
  async whoami(signal) {
699
721
  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;
722
+ const res = await fetch(url, {
723
+ method: "GET",
724
+ headers: this.headers,
725
+ signal
726
+ });
727
+ if (res.ok) {
728
+ const payload = await res.json();
729
+ if (!payload?.email) {
730
+ return null;
725
731
  }
726
- return null;
732
+ return {
733
+ email: payload.email,
734
+ id: payload.id
735
+ };
736
+ }
737
+ if (res.status >= 500 && res.status < 600) {
738
+ const msg = await _LingoDotDevEngine.extractErrorMessage(res);
739
+ throw new Error(
740
+ `Server error (${res.status}): ${msg}. This may be due to temporary service issues.`
741
+ );
727
742
  }
743
+ return null;
728
744
  }
729
745
  };
730
746
  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.2",
4
4
  "description": "Lingo.dev JS SDK",
5
5
  "private": false,
6
6
  "repository": {