@tolinku/react-native-sdk 0.5.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
@@ -4,7 +4,7 @@ import { useState, useRef, useMemo, useEffect, useCallback } from 'react';
4
4
  import { jsx, Fragment, jsxs } from 'react/jsx-runtime';
5
5
 
6
6
  // src/types.ts
7
- var SDK_VERSION = "0.5.0";
7
+ var SDK_VERSION = "0.6.0";
8
8
 
9
9
  // src/debug.ts
10
10
  var _debugEnabled = false;
@@ -68,6 +68,25 @@ var HttpClient = class {
68
68
  headers: this.publicHeaders()
69
69
  });
70
70
  }
71
+ /**
72
+ * POST to a host other than the configured one, without the API key.
73
+ *
74
+ * A few public endpoints work out which Appspace they belong to from the
75
+ * hostname the request arrived on rather than from a key or an id, so a
76
+ * question about a link on a customer's own domain has to be asked on that
77
+ * domain. Everything else about the request is unchanged: same timeout, same
78
+ * retries, same headers.
79
+ */
80
+ async postPublicToOrigin(origin, path, body) {
81
+ return this.executeWithRetry(origin.replace(/\/+$/, "") + path, {
82
+ method: "POST",
83
+ headers: {
84
+ ...this.publicHeaders(),
85
+ "Content-Type": "application/json"
86
+ },
87
+ body: body ? JSON.stringify(body) : void 0
88
+ });
89
+ }
71
90
  /** POST without API key auth (for public endpoints like deferred claim) */
72
91
  async postPublic(path, body) {
73
92
  return this.executeWithRetry(this.baseUrl + path, {
@@ -843,6 +862,51 @@ function override(value) {
843
862
  }
844
863
  return void 0;
845
864
  }
865
+
866
+ // src/links.ts
867
+ function isRoutablePath(path) {
868
+ return typeof path === "string" && path.startsWith("/") && !path.startsWith("//");
869
+ }
870
+ var Links = class {
871
+ constructor(client) {
872
+ this.client = client;
873
+ }
874
+ /**
875
+ * What this link means, or null if it means nothing here.
876
+ *
877
+ * `url` is a whole link as the app received it. The question goes to the
878
+ * link's own host, because that is how the platform knows which Appspace is
879
+ * being asked about, which also means a link on a domain that is not yours
880
+ * simply answers nothing.
881
+ *
882
+ * Never throws. A link that cannot be resolved, for a bad network or any
883
+ * other reason, is one the app should fall back to its own handling for, and
884
+ * an exception in the middle of a cold start is no way to say so.
885
+ */
886
+ async resolve(url) {
887
+ if (!url || typeof url !== "string") return null;
888
+ let parsed;
889
+ try {
890
+ parsed = new URL(url.trim());
891
+ } catch {
892
+ debugWarn(`Links.resolve: not a URL: ${url}`);
893
+ return null;
894
+ }
895
+ if (parsed.protocol !== "https:" && parsed.protocol !== "http:") return null;
896
+ try {
897
+ const result = await this.client.postPublicToOrigin(
898
+ parsed.origin,
899
+ "/v1/api/path",
900
+ { path: parsed.pathname }
901
+ );
902
+ if (!result || !result.route || !isRoutablePath(result.deep_link_path)) return null;
903
+ return result;
904
+ } catch (err) {
905
+ debugWarn(`Links.resolve failed: ${err.message}`);
906
+ return null;
907
+ }
908
+ }
909
+ };
846
910
  var BASE_KEY = "tolinku_message_dismissed";
847
911
  var IMPRESSIONS_BASE_KEY = "tolinku_message_impressions";
848
912
  var LAST_SHOWN_BASE_KEY = "tolinku_message_last_shown";
@@ -966,6 +1030,7 @@ var _Tolinku = class _Tolinku {
966
1030
  _Tolinku.ecommerceInstance = new Ecommerce(_Tolinku.client, () => _Tolinku._userId);
967
1031
  _Tolinku.referralsInstance = new Referrals(_Tolinku.client);
968
1032
  _Tolinku.deferredInstance = new Deferred(_Tolinku.client);
1033
+ _Tolinku.linksInstance = new Links(_Tolinku.client);
969
1034
  _Tolinku._initialized = true;
970
1035
  debugLog(`Tolinku SDK v${SDK_VERSION} initialized (baseUrl=${baseUrl})`);
971
1036
  }
@@ -1071,6 +1136,18 @@ var _Tolinku = class _Tolinku {
1071
1136
  }
1072
1137
  return _Tolinku.deferredInstance;
1073
1138
  }
1139
+ /**
1140
+ * Turning a link the app was handed into the route and token it means.
1141
+ *
1142
+ * Needed for short links, which arrive as an opaque code that nothing on the
1143
+ * device can interpret. See {@link Links.resolve}.
1144
+ */
1145
+ static get links() {
1146
+ if (!_Tolinku.linksInstance) {
1147
+ throw new Error("Tolinku: SDK not initialized. Call Tolinku.init() first.");
1148
+ }
1149
+ return _Tolinku.linksInstance;
1150
+ }
1074
1151
  /**
1075
1152
  * Shut down the SDK and release resources.
1076
1153
  * Flushes remaining analytics events, cancels timers, removes listeners,
@@ -1094,6 +1171,7 @@ var _Tolinku = class _Tolinku {
1094
1171
  _Tolinku.ecommerceInstance = null;
1095
1172
  _Tolinku.referralsInstance = null;
1096
1173
  _Tolinku.deferredInstance = null;
1174
+ _Tolinku.linksInstance = null;
1097
1175
  _Tolinku._initialized = false;
1098
1176
  _Tolinku._userId = null;
1099
1177
  setDebugEnabled(false);
@@ -1105,6 +1183,7 @@ _Tolinku.analyticsInstance = null;
1105
1183
  _Tolinku.ecommerceInstance = null;
1106
1184
  _Tolinku.referralsInstance = null;
1107
1185
  _Tolinku.deferredInstance = null;
1186
+ _Tolinku.linksInstance = null;
1108
1187
  _Tolinku._initialized = false;
1109
1188
  _Tolinku._userId = null;
1110
1189
  var Tolinku = _Tolinku;