@lingo.dev/_sdk 0.17.2 → 0.17.3

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
@@ -29,20 +29,16 @@ function trackEvent(apiKey, apiUrl, event, properties) {
29
29
  if (process.env.DO_NOT_TRACK === "1") {
30
30
  return;
31
31
  }
32
- resolveIdentityAndCapture(apiKey, apiUrl, event, properties).catch(
33
- (error) => {
34
- if (process.env.DEBUG === "true") {
35
- console.error("[Tracking] Error:", error);
36
- }
32
+ resolveIdentityAndCapture(apiKey, apiUrl, event, properties).catch((error) => {
33
+ if (process.env.DEBUG === "true") {
34
+ console.error("[Tracking] Error:", error);
37
35
  }
38
- );
36
+ });
39
37
  }
40
38
  async function resolveIdentityAndCapture(apiKey, apiUrl, event, properties) {
41
- const { identity, email } = await getDistinctId(apiKey, apiUrl);
39
+ const { identity, email, organizationId } = await getDistinctId(apiKey, apiUrl);
42
40
  if (process.env.DEBUG === "true") {
43
- console.log(
44
- `[Tracking] Event: ${event}, ID: ${identity.distinct_id}, Source: ${identity.distinct_id_source}`
45
- );
41
+ console.log(`[Tracking] Event: ${event}, ID: ${identity.distinct_id}, Source: ${identity.distinct_id_source}`);
46
42
  }
47
43
  const { PostHog } = await Promise.resolve().then(() => _interopRequireWildcard(require("posthog-node")));
48
44
  const posthog = new PostHog(POSTHOG_API_KEY, {
@@ -54,6 +50,7 @@ async function resolveIdentityAndCapture(apiKey, apiUrl, event, properties) {
54
50
  await posthog.capture({
55
51
  distinctId: identity.distinct_id,
56
52
  event,
53
+ ...organizationId ? { groups: { organization: organizationId } } : {},
57
54
  properties: {
58
55
  ...properties,
59
56
  $set: { ..._optionalChain([properties, 'optionalAccess', _ => _.$set]) || {}, ...email ? { email } : {} },
@@ -62,12 +59,6 @@ async function resolveIdentityAndCapture(apiKey, apiUrl, event, properties) {
62
59
  distinct_id_source: identity.distinct_id_source
63
60
  }
64
61
  });
65
- if (email) {
66
- await posthog.alias({
67
- distinctId: identity.distinct_id,
68
- alias: email
69
- });
70
- }
71
62
  } finally {
72
63
  await posthog.shutdown();
73
64
  }
@@ -76,8 +67,8 @@ async function getDistinctId(apiKey, apiUrl) {
76
67
  const cached = identityCache.get(apiKey);
77
68
  if (cached) return cached;
78
69
  try {
79
- const res = await fetch(`${apiUrl}/users/me`, {
80
- method: "GET",
70
+ const res = await fetch(`${apiUrl}/whoami`, {
71
+ method: "POST",
81
72
  headers: {
82
73
  "X-API-Key": apiKey,
83
74
  "Content-Type": "application/json"
@@ -85,14 +76,27 @@ async function getDistinctId(apiKey, apiUrl) {
85
76
  });
86
77
  if (res.ok) {
87
78
  const payload = await res.json();
88
- if (_optionalChain([payload, 'optionalAccess', _2 => _2.id])) {
89
- const result = {
90
- identity: {
91
- distinct_id: payload.id,
92
- distinct_id_source: "database_id"
93
- },
94
- email: payload.email || void 0
79
+ const organizationId = typeof _optionalChain([payload, 'optionalAccess', _2 => _2.organizationId]) === "string" ? payload.organizationId : void 0;
80
+ let result;
81
+ if (typeof _optionalChain([payload, 'optionalAccess', _3 => _3.userId]) === "string") {
82
+ result = {
83
+ identity: { distinct_id: payload.userId, distinct_id_source: "database_id" },
84
+ email: payload.email || void 0,
85
+ organizationId
86
+ };
87
+ } else if (typeof _optionalChain([payload, 'optionalAccess', _4 => _4.keyId]) === "string") {
88
+ result = {
89
+ identity: { distinct_id: payload.keyId, distinct_id_source: "api_key_id" },
90
+ organizationId
91
+ };
92
+ } else if (_optionalChain([payload, 'optionalAccess', _5 => _5.id])) {
93
+ result = {
94
+ identity: { distinct_id: payload.id, distinct_id_source: "database_id" },
95
+ email: payload.email || void 0,
96
+ organizationId
95
97
  };
98
+ }
99
+ if (result) {
96
100
  identityCache.set(apiKey, result);
97
101
  return result;
98
102
  }
@@ -157,7 +161,7 @@ var LingoDotDevEngine = (_class = class _LingoDotDevEngine {
157
161
  if (parsed && typeof parsed.message === "string") {
158
162
  return parsed.message;
159
163
  }
160
- if (_optionalChain([parsed, 'optionalAccess', _3 => _3._tag]) === "NotFoundError") {
164
+ if (_optionalChain([parsed, 'optionalAccess', _6 => _6._tag]) === "NotFoundError") {
161
165
  return `${parsed.entityType} not found: ${parsed.id}`;
162
166
  }
163
167
  return text;
@@ -183,7 +187,7 @@ var LingoDotDevEngine = (_class = class _LingoDotDevEngine {
183
187
  */
184
188
  static sleep(ms, signal) {
185
189
  return new Promise((resolve, reject) => {
186
- if (_optionalChain([signal, 'optionalAccess', _4 => _4.aborted])) {
190
+ if (_optionalChain([signal, 'optionalAccess', _7 => _7.aborted])) {
187
191
  reject(new Error("Operation was aborted"));
188
192
  return;
189
193
  }
@@ -192,10 +196,10 @@ var LingoDotDevEngine = (_class = class _LingoDotDevEngine {
192
196
  reject(new Error("Operation was aborted"));
193
197
  };
194
198
  const timer = setTimeout(() => {
195
- _optionalChain([signal, 'optionalAccess', _5 => _5.removeEventListener, 'call', _6 => _6("abort", onAbort)]);
199
+ _optionalChain([signal, 'optionalAccess', _8 => _8.removeEventListener, 'call', _9 => _9("abort", onAbort)]);
196
200
  resolve();
197
201
  }, ms);
198
- _optionalChain([signal, 'optionalAccess', _7 => _7.addEventListener, 'call', _8 => _8("abort", onAbort, { once: true })]);
202
+ _optionalChain([signal, 'optionalAccess', _10 => _10.addEventListener, 'call', _11 => _11("abort", onAbort, { once: true })]);
199
203
  });
200
204
  }
201
205
  /**
@@ -221,20 +225,20 @@ var LingoDotDevEngine = (_class = class _LingoDotDevEngine {
221
225
  async fetchWithRetry(url, init, signal) {
222
226
  const { maxRetries } = this.config;
223
227
  for (let attempt = 0; attempt <= maxRetries; attempt++) {
224
- if (_optionalChain([signal, 'optionalAccess', _9 => _9.aborted])) {
228
+ if (_optionalChain([signal, 'optionalAccess', _12 => _12.aborted])) {
225
229
  throw new Error("Operation was aborted");
226
230
  }
227
231
  try {
228
232
  const res = await fetch(url, init);
229
233
  const isServerError = res.status >= 500 && res.status < 600;
230
234
  if (isServerError && attempt < maxRetries) {
231
- await _optionalChain([res, 'access', _10 => _10.body, 'optionalAccess', _11 => _11.cancel, 'call', _12 => _12()]);
235
+ await _optionalChain([res, 'access', _13 => _13.body, 'optionalAccess', _14 => _14.cancel, 'call', _15 => _15()]);
232
236
  await _LingoDotDevEngine.sleep(this.backoffDelay(attempt), signal);
233
237
  continue;
234
238
  }
235
239
  return res;
236
240
  } catch (error) {
237
- if (_optionalChain([signal, 'optionalAccess', _13 => _13.aborted])) {
241
+ if (_optionalChain([signal, 'optionalAccess', _16 => _16.aborted])) {
238
242
  throw error;
239
243
  }
240
244
  if (attempt < maxRetries) {
@@ -663,7 +667,7 @@ var LingoDotDevEngine = (_class = class _LingoDotDevEngine {
663
667
  break;
664
668
  }
665
669
  const siblings = Array.from(parent.childNodes).filter(
666
- (n) => n.nodeType === 1 || n.nodeType === 3 && _optionalChain([n, 'access', _14 => _14.textContent, 'optionalAccess', _15 => _15.trim, 'call', _16 => _16()])
670
+ (n) => n.nodeType === 1 || n.nodeType === 3 && _optionalChain([n, 'access', _17 => _17.textContent, 'optionalAccess', _18 => _18.trim, 'call', _19 => _19()])
667
671
  );
668
672
  const index = siblings.indexOf(current);
669
673
  if (index !== -1) {
@@ -683,7 +687,7 @@ var LingoDotDevEngine = (_class = class _LingoDotDevEngine {
683
687
  parent = parent.parentElement;
684
688
  }
685
689
  if (node.nodeType === 3) {
686
- const text = _optionalChain([node, 'access', _17 => _17.textContent, 'optionalAccess', _18 => _18.trim, 'call', _19 => _19()]) || "";
690
+ const text = _optionalChain([node, 'access', _20 => _20.textContent, 'optionalAccess', _21 => _21.trim, 'call', _22 => _22()]) || "";
687
691
  if (text) {
688
692
  extractedContent[getPath(node)] = text;
689
693
  }
@@ -698,15 +702,15 @@ var LingoDotDevEngine = (_class = class _LingoDotDevEngine {
698
702
  }
699
703
  });
700
704
  Array.from(element.childNodes).filter(
701
- (n) => n.nodeType === 1 || n.nodeType === 3 && _optionalChain([n, 'access', _20 => _20.textContent, 'optionalAccess', _21 => _21.trim, 'call', _22 => _22()])
705
+ (n) => n.nodeType === 1 || n.nodeType === 3 && _optionalChain([n, 'access', _23 => _23.textContent, 'optionalAccess', _24 => _24.trim, 'call', _25 => _25()])
702
706
  ).forEach(processNode);
703
707
  }
704
708
  };
705
709
  Array.from(document.head.childNodes).filter(
706
- (n) => n.nodeType === 1 || n.nodeType === 3 && _optionalChain([n, 'access', _23 => _23.textContent, 'optionalAccess', _24 => _24.trim, 'call', _25 => _25()])
710
+ (n) => n.nodeType === 1 || n.nodeType === 3 && _optionalChain([n, 'access', _26 => _26.textContent, 'optionalAccess', _27 => _27.trim, 'call', _28 => _28()])
707
711
  ).forEach(processNode);
708
712
  Array.from(document.body.childNodes).filter(
709
- (n) => n.nodeType === 1 || n.nodeType === 3 && _optionalChain([n, 'access', _26 => _26.textContent, 'optionalAccess', _27 => _27.trim, 'call', _28 => _28()])
713
+ (n) => n.nodeType === 1 || n.nodeType === 3 && _optionalChain([n, 'access', _29 => _29.textContent, 'optionalAccess', _30 => _30.trim, 'call', _31 => _31()])
710
714
  ).forEach(processNode);
711
715
  const localizedContent = await this._localizeRaw(
712
716
  extractedContent,
@@ -722,10 +726,10 @@ var LingoDotDevEngine = (_class = class _LingoDotDevEngine {
722
726
  let current = parent;
723
727
  for (const index of indices) {
724
728
  const siblings = Array.from(parent.childNodes).filter(
725
- (n) => n.nodeType === 1 || n.nodeType === 3 && _optionalChain([n, 'access', _29 => _29.textContent, 'optionalAccess', _30 => _30.trim, 'call', _31 => _31()])
729
+ (n) => n.nodeType === 1 || n.nodeType === 3 && _optionalChain([n, 'access', _32 => _32.textContent, 'optionalAccess', _33 => _33.trim, 'call', _34 => _34()])
726
730
  );
727
731
  current = siblings[parseInt(index)] || null;
728
- if (_optionalChain([current, 'optionalAccess', _32 => _32.nodeType]) === 1) {
732
+ if (_optionalChain([current, 'optionalAccess', _35 => _35.nodeType]) === 1) {
729
733
  parent = current;
730
734
  }
731
735
  }
@@ -835,7 +839,7 @@ var LingoDotDevEngine = (_class = class _LingoDotDevEngine {
835
839
  });
836
840
  if (res.ok) {
837
841
  const payload = await res.json();
838
- if (!_optionalChain([payload, 'optionalAccess', _33 => _33.email])) {
842
+ if (!_optionalChain([payload, 'optionalAccess', _36 => _36.email])) {
839
843
  return null;
840
844
  }
841
845
  return {
package/build/index.mjs CHANGED
@@ -29,20 +29,16 @@ function trackEvent(apiKey, apiUrl, event, properties) {
29
29
  if (process.env.DO_NOT_TRACK === "1") {
30
30
  return;
31
31
  }
32
- resolveIdentityAndCapture(apiKey, apiUrl, event, properties).catch(
33
- (error) => {
34
- if (process.env.DEBUG === "true") {
35
- console.error("[Tracking] Error:", error);
36
- }
32
+ resolveIdentityAndCapture(apiKey, apiUrl, event, properties).catch((error) => {
33
+ if (process.env.DEBUG === "true") {
34
+ console.error("[Tracking] Error:", error);
37
35
  }
38
- );
36
+ });
39
37
  }
40
38
  async function resolveIdentityAndCapture(apiKey, apiUrl, event, properties) {
41
- const { identity, email } = await getDistinctId(apiKey, apiUrl);
39
+ const { identity, email, organizationId } = await getDistinctId(apiKey, apiUrl);
42
40
  if (process.env.DEBUG === "true") {
43
- console.log(
44
- `[Tracking] Event: ${event}, ID: ${identity.distinct_id}, Source: ${identity.distinct_id_source}`
45
- );
41
+ console.log(`[Tracking] Event: ${event}, ID: ${identity.distinct_id}, Source: ${identity.distinct_id_source}`);
46
42
  }
47
43
  const { PostHog } = await import("posthog-node");
48
44
  const posthog = new PostHog(POSTHOG_API_KEY, {
@@ -54,6 +50,7 @@ async function resolveIdentityAndCapture(apiKey, apiUrl, event, properties) {
54
50
  await posthog.capture({
55
51
  distinctId: identity.distinct_id,
56
52
  event,
53
+ ...organizationId ? { groups: { organization: organizationId } } : {},
57
54
  properties: {
58
55
  ...properties,
59
56
  $set: { ...properties?.$set || {}, ...email ? { email } : {} },
@@ -62,12 +59,6 @@ async function resolveIdentityAndCapture(apiKey, apiUrl, event, properties) {
62
59
  distinct_id_source: identity.distinct_id_source
63
60
  }
64
61
  });
65
- if (email) {
66
- await posthog.alias({
67
- distinctId: identity.distinct_id,
68
- alias: email
69
- });
70
- }
71
62
  } finally {
72
63
  await posthog.shutdown();
73
64
  }
@@ -76,8 +67,8 @@ async function getDistinctId(apiKey, apiUrl) {
76
67
  const cached = identityCache.get(apiKey);
77
68
  if (cached) return cached;
78
69
  try {
79
- const res = await fetch(`${apiUrl}/users/me`, {
80
- method: "GET",
70
+ const res = await fetch(`${apiUrl}/whoami`, {
71
+ method: "POST",
81
72
  headers: {
82
73
  "X-API-Key": apiKey,
83
74
  "Content-Type": "application/json"
@@ -85,14 +76,27 @@ async function getDistinctId(apiKey, apiUrl) {
85
76
  });
86
77
  if (res.ok) {
87
78
  const payload = await res.json();
88
- if (payload?.id) {
89
- const result = {
90
- identity: {
91
- distinct_id: payload.id,
92
- distinct_id_source: "database_id"
93
- },
94
- email: payload.email || void 0
79
+ const organizationId = typeof payload?.organizationId === "string" ? payload.organizationId : void 0;
80
+ let result;
81
+ if (typeof payload?.userId === "string") {
82
+ result = {
83
+ identity: { distinct_id: payload.userId, distinct_id_source: "database_id" },
84
+ email: payload.email || void 0,
85
+ organizationId
86
+ };
87
+ } else if (typeof payload?.keyId === "string") {
88
+ result = {
89
+ identity: { distinct_id: payload.keyId, distinct_id_source: "api_key_id" },
90
+ organizationId
95
91
  };
92
+ } else if (payload?.id) {
93
+ result = {
94
+ identity: { distinct_id: payload.id, distinct_id_source: "database_id" },
95
+ email: payload.email || void 0,
96
+ organizationId
97
+ };
98
+ }
99
+ if (result) {
96
100
  identityCache.set(apiKey, result);
97
101
  return result;
98
102
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lingo.dev/_sdk",
3
- "version": "0.17.2",
3
+ "version": "0.17.3",
4
4
  "description": "Lingo.dev JS SDK",
5
5
  "private": false,
6
6
  "repository": {