@tolinku/react-native-sdk 0.8.1 → 0.8.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/dist/index.d.mts +46 -3
- package/dist/index.d.ts +46 -3
- package/dist/index.js +161 -28
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +161 -28
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -6,7 +6,7 @@ import { jsx, Fragment, jsxs } from 'react/jsx-runtime';
|
|
|
6
6
|
// src/Tolinku.ts
|
|
7
7
|
|
|
8
8
|
// src/types.ts
|
|
9
|
-
var SDK_VERSION = "0.8.
|
|
9
|
+
var SDK_VERSION = "0.8.2";
|
|
10
10
|
|
|
11
11
|
// src/debug.ts
|
|
12
12
|
var _debugEnabled = false;
|
|
@@ -92,6 +92,52 @@ var HttpClient = class {
|
|
|
92
92
|
body: body ? JSON.stringify(body) : void 0
|
|
93
93
|
});
|
|
94
94
|
}
|
|
95
|
+
/**
|
|
96
|
+
* The hostname of the configured API, lowercased and without a port, for
|
|
97
|
+
* telling whether some other URL points at the same host. Empty if the base
|
|
98
|
+
* URL cannot be read, which init's validation should already have ruled out.
|
|
99
|
+
*/
|
|
100
|
+
get apiHostname() {
|
|
101
|
+
const match = /^https?:\/\/(?:[^/?#@]*@)?([^/?#]+)/i.exec(this.baseUrl);
|
|
102
|
+
return match ? hostnameOf(match[1]) : "";
|
|
103
|
+
}
|
|
104
|
+
/** Whether there is a key to send, so callers can skip a call that would only be refused. */
|
|
105
|
+
get hasApiKey() {
|
|
106
|
+
return typeof this.apiKey === "string" && this.apiKey.length > 0;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* POST once, without the API key, to a host other than the configured one,
|
|
110
|
+
* and hand back whatever came back, whatever the status.
|
|
111
|
+
*
|
|
112
|
+
* For a question that must not be asked twice: no retries, and its own time
|
|
113
|
+
* limit rather than the client's. Throws only when there is no reply at all
|
|
114
|
+
* (a network failure, the time limit, or a destroyed client).
|
|
115
|
+
*/
|
|
116
|
+
async postPublicOnceToOrigin(origin, path, body, timeoutMs) {
|
|
117
|
+
return this.executeOnce(origin.replace(/\/+$/, "") + path, {
|
|
118
|
+
method: "POST",
|
|
119
|
+
headers: {
|
|
120
|
+
...this.publicHeaders(),
|
|
121
|
+
"Content-Type": "application/json"
|
|
122
|
+
},
|
|
123
|
+
body: JSON.stringify(body)
|
|
124
|
+
}, timeoutMs);
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* POST once, with the API key, to the configured API. Same terms as
|
|
128
|
+
* {@link postPublicOnceToOrigin}: one attempt, its own time limit, the reply
|
|
129
|
+
* handed back whatever its status.
|
|
130
|
+
*/
|
|
131
|
+
async postOnce(path, body, timeoutMs) {
|
|
132
|
+
return this.executeOnce(this.baseUrl + path, {
|
|
133
|
+
method: "POST",
|
|
134
|
+
headers: {
|
|
135
|
+
...this.authenticatedHeaders(),
|
|
136
|
+
"Content-Type": "application/json"
|
|
137
|
+
},
|
|
138
|
+
body: JSON.stringify(body)
|
|
139
|
+
}, timeoutMs);
|
|
140
|
+
}
|
|
95
141
|
/** POST without API key auth (for public endpoints like deferred claim) */
|
|
96
142
|
async postPublic(path, body) {
|
|
97
143
|
return this.executeWithRetry(this.baseUrl + path, {
|
|
@@ -127,6 +173,40 @@ var HttpClient = class {
|
|
|
127
173
|
"User-Agent": `TolinkuReactNativeSDK/${SDK_VERSION}`
|
|
128
174
|
};
|
|
129
175
|
}
|
|
176
|
+
/**
|
|
177
|
+
* One request, one attempt, aborted after `timeoutMs`. The body is read as
|
|
178
|
+
* JSON when it is JSON and left undefined when it is not, so the caller can
|
|
179
|
+
* tell a reply from our own server apart from somebody else's error page.
|
|
180
|
+
*/
|
|
181
|
+
async executeOnce(url, init, timeoutMs) {
|
|
182
|
+
if (this.destroyed) {
|
|
183
|
+
throw new TolinkuError("Tolinku: client has been destroyed. Call Tolinku.init() to reinitialize.", 0);
|
|
184
|
+
}
|
|
185
|
+
const controller = new AbortController();
|
|
186
|
+
this.pendingControllers.add(controller);
|
|
187
|
+
const timeoutId = setTimeout(() => controller.abort(), timeoutMs);
|
|
188
|
+
try {
|
|
189
|
+
const res = await fetch(url, { ...init, signal: controller.signal });
|
|
190
|
+
let body;
|
|
191
|
+
try {
|
|
192
|
+
body = JSON.parse(await res.text());
|
|
193
|
+
} catch {
|
|
194
|
+
body = void 0;
|
|
195
|
+
}
|
|
196
|
+
return { status: res.status, body };
|
|
197
|
+
} catch (err) {
|
|
198
|
+
if (this.destroyed) {
|
|
199
|
+
throw new TolinkuError("Tolinku: request aborted (client destroyed).", 0);
|
|
200
|
+
}
|
|
201
|
+
if (isAbortError(err)) {
|
|
202
|
+
throw new TolinkuError(`Tolinku: request timed out after ${timeoutMs}ms`, 0);
|
|
203
|
+
}
|
|
204
|
+
throw err;
|
|
205
|
+
} finally {
|
|
206
|
+
clearTimeout(timeoutId);
|
|
207
|
+
this.pendingControllers.delete(controller);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
130
210
|
/**
|
|
131
211
|
* Execute a fetch request with retry logic. Retries on:
|
|
132
212
|
* - Network errors (fetch throws)
|
|
@@ -210,6 +290,10 @@ var HttpClient = class {
|
|
|
210
290
|
throw lastError || new TolinkuError("Tolinku: request failed after retries", 0);
|
|
211
291
|
}
|
|
212
292
|
};
|
|
293
|
+
function hostnameOf(authority) {
|
|
294
|
+
const match = /^(\[[^\]]*\]|[^:]*)/.exec(authority);
|
|
295
|
+
return (match ? match[1] : authority).toLowerCase();
|
|
296
|
+
}
|
|
213
297
|
function sleep(ms) {
|
|
214
298
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
215
299
|
}
|
|
@@ -229,20 +313,6 @@ function urlScheme(url) {
|
|
|
229
313
|
const match = /^([a-zA-Z][a-zA-Z\d+\-._]*):/.exec(cleaned);
|
|
230
314
|
return match ? `${match[1].toLowerCase()}:` : null;
|
|
231
315
|
}
|
|
232
|
-
function parseHttpUrl(url) {
|
|
233
|
-
if (!url || typeof url !== "string") return null;
|
|
234
|
-
const cleaned = url.replace(/[\t\n\r]/g, "").trim();
|
|
235
|
-
const match = /^(https?):\/\/([^/?#]+)([^?#]*)/i.exec(cleaned);
|
|
236
|
-
if (!match) return null;
|
|
237
|
-
const [, scheme, authority, path] = match;
|
|
238
|
-
if (!authority) return null;
|
|
239
|
-
return {
|
|
240
|
-
origin: `${scheme.toLowerCase()}://${authority.toLowerCase()}`,
|
|
241
|
-
// A URL with no path means the root, which is what a server expects to be
|
|
242
|
-
// asked about.
|
|
243
|
-
pathname: path || "/"
|
|
244
|
-
};
|
|
245
|
-
}
|
|
246
316
|
function isSafeUrl(url) {
|
|
247
317
|
const scheme = urlScheme(url);
|
|
248
318
|
return scheme === "http:" || scheme === "https:";
|
|
@@ -912,6 +982,36 @@ function override(value) {
|
|
|
912
982
|
function isRoutablePath(path) {
|
|
913
983
|
return typeof path === "string" && path.startsWith("/") && !path.startsWith("//");
|
|
914
984
|
}
|
|
985
|
+
var RESOLVE_TIMEOUT_MS = 5e3;
|
|
986
|
+
function normaliseLink(url) {
|
|
987
|
+
if (!url || typeof url !== "string") return null;
|
|
988
|
+
const cleaned = url.replace(/[\t\n\r]/g, "").trim();
|
|
989
|
+
const match = /^https?:\/\/([^/?#]*)([^?#]*)(\?[^#]*)?/i.exec(cleaned);
|
|
990
|
+
if (!match) return null;
|
|
991
|
+
const [, rawAuthority, rawPath, rawQuery] = match;
|
|
992
|
+
const authority = rawAuthority.slice(rawAuthority.lastIndexOf("@") + 1);
|
|
993
|
+
const hostPort = /^(\[[^\]]*\]|[^:]*)(?::(\d*))?$/.exec(authority);
|
|
994
|
+
if (!hostPort || !hostPort[1]) return null;
|
|
995
|
+
const hostname = hostPort[1].toLowerCase();
|
|
996
|
+
const port = hostPort[2];
|
|
997
|
+
const keepPort = port && port !== "443" && port !== "80" ? `:${port}` : "";
|
|
998
|
+
const origin = `https://${hostname}${keepPort}`;
|
|
999
|
+
const path = rawPath || "/";
|
|
1000
|
+
const query = rawQuery && rawQuery.length > 1 ? rawQuery : "";
|
|
1001
|
+
return { origin, hostname, path, whole: origin + path + query };
|
|
1002
|
+
}
|
|
1003
|
+
function answerIn(reply) {
|
|
1004
|
+
if (reply.status !== 200) return null;
|
|
1005
|
+
const result = reply.body;
|
|
1006
|
+
if (!result || typeof result !== "object" || !result.route || !isRoutablePath(result.deep_link_path)) {
|
|
1007
|
+
return null;
|
|
1008
|
+
}
|
|
1009
|
+
return result;
|
|
1010
|
+
}
|
|
1011
|
+
function isTolinkuReply(reply) {
|
|
1012
|
+
const body = reply.body;
|
|
1013
|
+
return typeof body === "object" && body !== null && typeof body.error === "string";
|
|
1014
|
+
}
|
|
915
1015
|
var Links = class {
|
|
916
1016
|
constructor(client) {
|
|
917
1017
|
this.client = client;
|
|
@@ -921,30 +1021,63 @@ var Links = class {
|
|
|
921
1021
|
*
|
|
922
1022
|
* `url` is a whole link as the app received it. The question goes to the
|
|
923
1023
|
* link's own host, because that is how the platform knows which Appspace is
|
|
924
|
-
* being asked about
|
|
925
|
-
*
|
|
1024
|
+
* being asked about. If that host is not Tolinku's (no reply, or one that is
|
|
1025
|
+
* not ours), and it is not the API's own host, the question goes once more to
|
|
1026
|
+
* the configured API with the key: a link in an email arrives wrapped in the
|
|
1027
|
+
* email platform's click tracking domain, which knows nothing about routes,
|
|
1028
|
+
* and the API can unwrap it.
|
|
1029
|
+
*
|
|
1030
|
+
* Each question is asked once, with a five second limit. Asking a click
|
|
1031
|
+
* tracking host again would count the email click again.
|
|
926
1032
|
*
|
|
927
1033
|
* Never throws. A link that cannot be resolved, for a bad network or any
|
|
928
1034
|
* other reason, is one the app should fall back to its own handling for, and
|
|
929
1035
|
* an exception in the middle of a cold start is no way to say so.
|
|
930
1036
|
*/
|
|
931
1037
|
async resolve(url) {
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
1038
|
+
try {
|
|
1039
|
+
return await this.resolveLink(url);
|
|
1040
|
+
} catch (err) {
|
|
1041
|
+
debugWarn(`Links.resolve failed: ${err.message}`);
|
|
1042
|
+
return null;
|
|
1043
|
+
}
|
|
1044
|
+
}
|
|
1045
|
+
async resolveLink(url) {
|
|
1046
|
+
const link = normaliseLink(url);
|
|
1047
|
+
if (!link) {
|
|
935
1048
|
debugWarn(`Links.resolve: not an http(s) URL: ${url}`);
|
|
936
1049
|
return null;
|
|
937
1050
|
}
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
1051
|
+
const direct = await this.askOnce(
|
|
1052
|
+
() => this.client.postPublicOnceToOrigin(
|
|
1053
|
+
link.origin,
|
|
941
1054
|
"/v1/api/path",
|
|
942
|
-
{ path:
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
1055
|
+
{ path: link.path, url: link.whole },
|
|
1056
|
+
RESOLVE_TIMEOUT_MS
|
|
1057
|
+
),
|
|
1058
|
+
"Links.resolve"
|
|
1059
|
+
);
|
|
1060
|
+
if (direct) {
|
|
1061
|
+
const found = answerIn(direct);
|
|
1062
|
+
if (found) return found;
|
|
1063
|
+
if (isTolinkuReply(direct)) return null;
|
|
1064
|
+
}
|
|
1065
|
+
const apiHostname = this.client.apiHostname;
|
|
1066
|
+
if (!this.client.hasApiKey || !apiHostname || link.hostname === apiHostname) {
|
|
1067
|
+
return null;
|
|
1068
|
+
}
|
|
1069
|
+
const fallback = await this.askOnce(
|
|
1070
|
+
() => this.client.postOnce("/v1/api/links/resolve", { url: link.whole }, RESOLVE_TIMEOUT_MS),
|
|
1071
|
+
"Links.resolve fallback"
|
|
1072
|
+
);
|
|
1073
|
+
return fallback ? answerIn(fallback) : null;
|
|
1074
|
+
}
|
|
1075
|
+
/** One request; null when there was no reply at all. */
|
|
1076
|
+
async askOnce(request, label) {
|
|
1077
|
+
try {
|
|
1078
|
+
return await request();
|
|
946
1079
|
} catch (err) {
|
|
947
|
-
debugWarn(
|
|
1080
|
+
debugWarn(`${label} failed: ${err.message}`);
|
|
948
1081
|
return null;
|
|
949
1082
|
}
|
|
950
1083
|
}
|