@tolinku/web-sdk 0.4.0 → 0.6.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/dist/index.mjs CHANGED
@@ -1,3 +1,6 @@
1
+ // src/types.ts
2
+ var SDK_VERSION = "0.6.0";
3
+
1
4
  // src/client.ts
2
5
  var MAX_RETRIES = 3;
3
6
  var BASE_DELAY_MS = 500;
@@ -63,6 +66,28 @@ var HttpClient = class {
63
66
  }
64
67
  return this.parseJson(res);
65
68
  }
69
+ /**
70
+ * POST to a host other than the configured one, without the API key.
71
+ *
72
+ * A few public endpoints work out which Appspace they belong to from the
73
+ * hostname the request arrives on rather than from a key or an id, so a
74
+ * question about a link on a customer's own domain has to be asked on that
75
+ * domain. No key goes with it: the origin comes from a URL the caller was
76
+ * handed, which is not necessarily ours.
77
+ */
78
+ async postPublicToOrigin(origin, path, body) {
79
+ const res = await this.fetchWithRetry(origin.replace(/\/+$/, "") + path, {
80
+ method: "POST",
81
+ headers: { "Content-Type": "application/json" },
82
+ body: body ? JSON.stringify(body) : void 0,
83
+ signal: this.signal
84
+ });
85
+ if (!res.ok) {
86
+ const data = await res.json().catch(() => ({ error: res.statusText }));
87
+ throw new TolinkuError(data.error || res.statusText, res.status, data.code);
88
+ }
89
+ return this.parseJson(res);
90
+ }
66
91
  /** GET without API key auth (for public endpoints like banner config) */
67
92
  async getPublic(path, params) {
68
93
  let url = this._baseUrl + path;
@@ -168,6 +193,11 @@ var HttpClient = class {
168
193
  headers() {
169
194
  return {
170
195
  "X-API-Key": this.apiKey,
196
+ // Which SDK and version made the call. The other Tolinku SDKs say this in
197
+ // User-Agent, which a browser will not let a script set, so it goes in a
198
+ // header of our own. Without it a web integration is unidentifiable in the
199
+ // field and a version-specific bug cannot be traced to a version.
200
+ "X-Tolinku-SDK": `web/${SDK_VERSION}`,
171
201
  // Browsers default to "Accept: */*", which a server content-negotiating an
172
202
  // error page reads as "HTML is fine". Saying so explicitly keeps an error
173
203
  // parseable instead of arriving as a rendered page.
@@ -598,12 +628,12 @@ var Deferred = class {
598
628
  try {
599
629
  const link = await this.client.postPublic("/v1/api/deferred/claim-by-signals", {
600
630
  appspace_id: options.appspaceId,
601
- timezone: options.timezone || Intl.DateTimeFormat().resolvedOptions().timeZone,
602
- language: options.language || navigator.language,
603
- screen_width: options.screenWidth || window.screen.width,
604
- screen_height: options.screenHeight || window.screen.height,
631
+ timezone: override(options.timezone) || Intl.DateTimeFormat().resolvedOptions().timeZone,
632
+ language: override(options.language) || navigator.language,
633
+ screen_width: override(options.screenWidth) || window.screen.width,
634
+ screen_height: override(options.screenHeight) || window.screen.height,
605
635
  // Separates devices reporting identical logical dimensions.
606
- device_pixel_ratio: options.devicePixelRatio || window.devicePixelRatio || 1
636
+ device_pixel_ratio: override(options.devicePixelRatio) || window.devicePixelRatio || 1
607
637
  });
608
638
  return { link, settled: true };
609
639
  } catch (err) {
@@ -625,6 +655,71 @@ var Deferred = class {
625
655
  }
626
656
  }
627
657
  };
658
+ function override(value) {
659
+ if (typeof value === "string") {
660
+ const trimmed = value.trim();
661
+ return trimmed === "" ? void 0 : trimmed;
662
+ }
663
+ if (typeof value === "number") {
664
+ return Number.isFinite(value) && value > 0 ? value : void 0;
665
+ }
666
+ return void 0;
667
+ }
668
+
669
+ // src/links.ts
670
+ function isRoutablePath(path) {
671
+ return typeof path === "string" && path.startsWith("/") && !path.startsWith("//");
672
+ }
673
+ var Links = class {
674
+ constructor(client) {
675
+ this.client = client;
676
+ }
677
+ /**
678
+ * What this link means, or null if it means nothing here.
679
+ *
680
+ * The question goes to the link's own host, because that is how Tolinku knows
681
+ * which Appspace is being asked about, which also means a link on a domain
682
+ * that is not yours simply answers nothing.
683
+ *
684
+ * Never throws. A link that cannot be resolved, for a bad network or any
685
+ * other reason, is one to fall back to your own handling for.
686
+ */
687
+ async resolve(url) {
688
+ if (!url || typeof url !== "string") return null;
689
+ let parsed;
690
+ try {
691
+ parsed = new URL(url.trim());
692
+ } catch {
693
+ return null;
694
+ }
695
+ if (parsed.protocol !== "https:" && parsed.protocol !== "http:") return null;
696
+ try {
697
+ const result = await this.client.postPublicToOrigin(
698
+ parsed.origin,
699
+ "/v1/api/path",
700
+ { path: parsed.pathname }
701
+ );
702
+ if (!result || !result.route || !isRoutablePath(result.deep_link_path)) return null;
703
+ return result;
704
+ } catch {
705
+ return null;
706
+ }
707
+ }
708
+ };
709
+
710
+ // src/url-safety.ts
711
+ function isSafeUrl(url) {
712
+ if (typeof url !== "string") return false;
713
+ const trimmed = url.trim();
714
+ if (!trimmed) return false;
715
+ try {
716
+ const base = typeof window !== "undefined" ? window.location.href : void 0;
717
+ const parsed = new URL(trimmed, base);
718
+ return parsed.protocol === "http:" || parsed.protocol === "https:";
719
+ } catch {
720
+ return false;
721
+ }
722
+ }
628
723
 
629
724
  // src/storage.ts
630
725
  var BANNER_KEY = "tolinku_banner_dismissed";
@@ -743,14 +838,6 @@ function sanitizeClass(val) {
743
838
  if (!val || typeof val !== "string") return "";
744
839
  return val.replace(/[^a-zA-Z0-9_\-\s]/g, "").slice(0, 100).trim();
745
840
  }
746
- function isSafeUrl(url) {
747
- try {
748
- const parsed = new URL(url, window.location.href);
749
- return parsed.protocol === "http:" || parsed.protocol === "https:";
750
- } catch {
751
- return false;
752
- }
753
- }
754
841
  var Banners = class {
755
842
  constructor(client) {
756
843
  this.client = client;
@@ -1176,7 +1263,7 @@ var Messages = class {
1176
1263
  }
1177
1264
  case "Image": {
1178
1265
  const url = props.url || "";
1179
- if (!isSafeUrl2(url)) return null;
1266
+ if (!isSafeUrl(url)) return null;
1180
1267
  const el = document.createElement("img");
1181
1268
  el.src = url;
1182
1269
  el.alt = props.alt || "";
@@ -1207,7 +1294,7 @@ var Messages = class {
1207
1294
  if (options.onButtonPress) {
1208
1295
  options.onButtonPress(action, messageId);
1209
1296
  } else if (action) {
1210
- if (isSafeUrl2(action)) {
1297
+ if (isSafeUrl(action)) {
1211
1298
  window.location.href = action;
1212
1299
  }
1213
1300
  }
@@ -1252,16 +1339,8 @@ var Messages = class {
1252
1339
  }
1253
1340
  }
1254
1341
  };
1255
- function isSafeUrl2(url) {
1256
- try {
1257
- const parsed = new URL(url, window.location.href);
1258
- return parsed.protocol === "http:" || parsed.protocol === "https:";
1259
- } catch {
1260
- return false;
1261
- }
1262
- }
1263
1342
  function sanitizeCssUrl(url) {
1264
- if (!isSafeUrl2(url)) return null;
1343
+ if (!isSafeUrl(url)) return null;
1265
1344
  return url.replace(/\\/g, "\\\\").replace(/"/g, '\\"').replace(/\n/g, "").replace(/\r/g, "");
1266
1345
  }
1267
1346
  function sanitizeTextAlign(value) {
@@ -1310,6 +1389,7 @@ var Tolinku = class {
1310
1389
  this.ecommerce = new Ecommerce(this.client, () => this._userId);
1311
1390
  this.referrals = new Referrals(this.client);
1312
1391
  this.deferred = new Deferred(this.client);
1392
+ this.links = new Links(this.client);
1313
1393
  this.banners = new Banners(this.client);
1314
1394
  this.messages = new Messages(this.client);
1315
1395
  }