@zerohash-sdk/fiat-account-link-js 1.6.0 → 1.6.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/README.md +2 -2
- package/dist/index.d.ts +35 -8
- package/dist/index.js +111 -98
- package/dist/index.umd.cjs +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -21,14 +21,14 @@ import { FiatAccountLink } from '@zerohash-sdk/fiat-account-link-js';
|
|
|
21
21
|
```html
|
|
22
22
|
<script
|
|
23
23
|
type="module"
|
|
24
|
-
src="https://sdk.
|
|
24
|
+
src="https://sdk-cdn.zerohash.com/fiat-account-link-web/index.js"
|
|
25
25
|
></script>
|
|
26
26
|
```
|
|
27
27
|
|
|
28
28
|
Or import directly in your JavaScript code:
|
|
29
29
|
|
|
30
30
|
```javascript
|
|
31
|
-
import { FiatAccountLink } from 'https://sdk.
|
|
31
|
+
import { FiatAccountLink } from 'https://sdk-cdn.zerohash.com/fiat-account-link-web/index.js';
|
|
32
32
|
```
|
|
33
33
|
|
|
34
34
|
## Getting Started
|
package/dist/index.d.ts
CHANGED
|
@@ -309,6 +309,40 @@ declare type CommonCallbacks<TEvent = AppEvent> = {
|
|
|
309
309
|
onEvent?: (event: TEvent) => void;
|
|
310
310
|
/** Called when the widget has loaded and is ready */
|
|
311
311
|
onLoaded?: () => void;
|
|
312
|
+
/**
|
|
313
|
+
* Called when a deposit reaches a terminal state (success/failure/verifying).
|
|
314
|
+
* Optional — only SDKs that drive the integrations deposit flow emit it.
|
|
315
|
+
*/
|
|
316
|
+
onDeposit?: (deposit: DepositCompletedPayload) => void;
|
|
317
|
+
};
|
|
318
|
+
|
|
319
|
+
/**
|
|
320
|
+
* Deposit completed payload — emitted by SDKs that drive a deposit through
|
|
321
|
+
* the integrations flow. Lives on `CommonCallbacks` because both Auth and
|
|
322
|
+
* any SDK embedding `@zerohash/integrations-flow` (e.g. fund with `useAuth`)
|
|
323
|
+
* resolve through the same deposit-status hook.
|
|
324
|
+
*/
|
|
325
|
+
declare type DepositCompletedPayload = {
|
|
326
|
+
data: {
|
|
327
|
+
depositId: string;
|
|
328
|
+
status: DepositStatus;
|
|
329
|
+
assetId: string;
|
|
330
|
+
networkId: string;
|
|
331
|
+
amount?: string;
|
|
332
|
+
accountMatchingValidation?: {
|
|
333
|
+
status: 'PENDING' | 'VALID' | 'INVALID' | 'ERROR';
|
|
334
|
+
reason?: string;
|
|
335
|
+
};
|
|
336
|
+
};
|
|
337
|
+
};
|
|
338
|
+
|
|
339
|
+
/**
|
|
340
|
+
* Deposit status object — shared across SDKs that surface deposit completion.
|
|
341
|
+
*/
|
|
342
|
+
declare type DepositStatus = {
|
|
343
|
+
value: string;
|
|
344
|
+
details: string;
|
|
345
|
+
occurredAt: string;
|
|
312
346
|
};
|
|
313
347
|
|
|
314
348
|
/**
|
|
@@ -374,14 +408,7 @@ declare class FiatAccountLink extends BaseJsSdk<FiatAccountLinkConfig> {
|
|
|
374
408
|
SCRIPT_LOAD_FAILED: string;
|
|
375
409
|
WEB_COMPONENT_NOT_DEFINED: string;
|
|
376
410
|
};
|
|
377
|
-
protected scriptUrls:
|
|
378
|
-
local: string;
|
|
379
|
-
dev: string;
|
|
380
|
-
cert: string;
|
|
381
|
-
prod: string;
|
|
382
|
-
sandbox: string;
|
|
383
|
-
production: string;
|
|
384
|
-
};
|
|
411
|
+
protected scriptUrls: Record<string, string>;
|
|
385
412
|
protected webComponentTag: string;
|
|
386
413
|
render(container: HTMLElement): Promise<void>;
|
|
387
414
|
updateConfig(config: Partial<FiatAccountLinkConfig>): void;
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const
|
|
1
|
+
const T = "production", k = "JWT token is required and must be a string.", F = {
|
|
2
2
|
dev: "https://grafana-faro-collector.dev.0hash.com/collect",
|
|
3
3
|
cert: "https://grafana-faro-collector.cert.zerohash.com/collect",
|
|
4
4
|
prod: "https://grafana-faro-collector.zerohash.com/collect",
|
|
@@ -6,55 +6,55 @@ const C = "production", k = "JWT token is required and must be a string.", z = {
|
|
|
6
6
|
"eu-prod": "https://grafana-faro-collector.zerohash.eu/collect",
|
|
7
7
|
sandbox: "https://grafana-faro-collector.cert.zerohash.com/collect",
|
|
8
8
|
production: "https://grafana-faro-collector.zerohash.com/collect"
|
|
9
|
-
},
|
|
9
|
+
}, M = (t) => {
|
|
10
10
|
if (!t || typeof t != "string")
|
|
11
11
|
return null;
|
|
12
12
|
const e = t.split(".");
|
|
13
13
|
if (e.length < 2)
|
|
14
14
|
return null;
|
|
15
15
|
try {
|
|
16
|
-
const
|
|
17
|
-
if (typeof
|
|
16
|
+
const r = e[1].replace(/-/g, "+").replace(/_/g, "/"), n = r + "===".slice(0, (4 - r.length % 4) % 4), o = typeof atob < "u" ? atob(n) : Buffer.from(n, "base64").toString("utf-8"), i = JSON.parse(o)?.payload?.region;
|
|
17
|
+
if (typeof i != "string")
|
|
18
18
|
return null;
|
|
19
|
-
const
|
|
20
|
-
return
|
|
19
|
+
const s = i.toLowerCase();
|
|
20
|
+
return s === "us" || s === "eu" ? s : null;
|
|
21
21
|
} catch {
|
|
22
22
|
return null;
|
|
23
23
|
}
|
|
24
|
-
},
|
|
24
|
+
}, S = (t, e) => M(e) !== "eu" ? t : t === "cert" ? "eu-cert" : t === "prod" ? "eu-prod" : t, W = (t) => {
|
|
25
25
|
if (!t || typeof t != "string")
|
|
26
26
|
return {};
|
|
27
27
|
const e = t.split(".");
|
|
28
28
|
if (e.length < 2)
|
|
29
29
|
return {};
|
|
30
30
|
try {
|
|
31
|
-
const
|
|
31
|
+
const r = e[1].replace(/-/g, "+").replace(/_/g, "/"), n = r + "===".slice(0, (4 - r.length % 4) % 4), o = typeof atob < "u" ? atob(n) : Buffer.from(n, "base64").toString("utf-8"), a = JSON.parse(o), i = a?.payload ?? {}, s = (c) => typeof c == "string" && c.length > 0 ? c : void 0;
|
|
32
32
|
return {
|
|
33
|
-
participantCode: i
|
|
34
|
-
platformName: i
|
|
35
|
-
platformCode:
|
|
36
|
-
region: i
|
|
33
|
+
participantCode: s(i.participant_code),
|
|
34
|
+
platformName: s(i.platform_name),
|
|
35
|
+
platformCode: s(a.platform_code),
|
|
36
|
+
region: s(i.region)
|
|
37
37
|
};
|
|
38
38
|
} catch {
|
|
39
39
|
return {};
|
|
40
40
|
}
|
|
41
|
-
},
|
|
42
|
-
const
|
|
43
|
-
return t[
|
|
44
|
-
},
|
|
45
|
-
const
|
|
46
|
-
return t[
|
|
47
|
-
},
|
|
48
|
-
},
|
|
41
|
+
}, N = (t, e, r) => {
|
|
42
|
+
const n = S(e, r);
|
|
43
|
+
return t[n] ?? t[e] ?? t.prod;
|
|
44
|
+
}, P = (t, e, r) => {
|
|
45
|
+
const n = S(e, r);
|
|
46
|
+
return t[n] ?? t[e];
|
|
47
|
+
}, w = () => {
|
|
48
|
+
}, U = () => {
|
|
49
49
|
const t = new Uint8Array(8);
|
|
50
50
|
return globalThis.crypto.getRandomValues(t), Array.from(t, (e) => e.toString(16).padStart(2, "0")).join("");
|
|
51
|
-
},
|
|
52
|
-
const
|
|
53
|
-
t.claims.participantCode && (o.participant_code = t.claims.participantCode), t.claims.platformName && (o.platform_name = t.claims.platformName), t.claims.platformCode && (o.platform_code = t.claims.platformCode), t.claims.region && (o.region = t.claims.region),
|
|
51
|
+
}, $ = (t, e) => {
|
|
52
|
+
const r = U(), n = globalThis.__ZH_WEB_SDK_VERSION__, o = {};
|
|
53
|
+
t.claims.participantCode && (o.participant_code = t.claims.participantCode), t.claims.platformName && (o.platform_name = t.claims.platformName), t.claims.platformCode && (o.platform_code = t.claims.platformCode), t.claims.region && (o.region = t.claims.region), n && (o.zh_web_sdk_version = n);
|
|
54
54
|
const a = {
|
|
55
55
|
meta: {
|
|
56
56
|
app: { name: t.appName, version: e ?? "unknown", environment: t.env },
|
|
57
|
-
session: { id:
|
|
57
|
+
session: { id: r, attributes: o },
|
|
58
58
|
browser: typeof navigator < "u" ? { userAgent: navigator.userAgent } : void 0,
|
|
59
59
|
page: typeof window < "u" ? { url: window.location.origin } : void 0
|
|
60
60
|
},
|
|
@@ -73,66 +73,66 @@ const C = "production", k = "JWT token is required and must be a string.", z = {
|
|
|
73
73
|
}
|
|
74
74
|
]
|
|
75
75
|
};
|
|
76
|
-
return { sessionId:
|
|
77
|
-
},
|
|
76
|
+
return { sessionId: r, payload: a };
|
|
77
|
+
}, B = (t, e, r) => {
|
|
78
78
|
if (!t || typeof fetch > "u")
|
|
79
79
|
return;
|
|
80
|
-
const { sessionId:
|
|
80
|
+
const { sessionId: n, payload: o } = $(e, r);
|
|
81
81
|
try {
|
|
82
82
|
fetch(t, {
|
|
83
83
|
method: "POST",
|
|
84
|
-
headers: { "Content-Type": "application/json", "x-faro-session-id":
|
|
84
|
+
headers: { "Content-Type": "application/json", "x-faro-session-id": n },
|
|
85
85
|
body: JSON.stringify(o),
|
|
86
86
|
keepalive: !0
|
|
87
87
|
}).catch(() => {
|
|
88
88
|
});
|
|
89
89
|
} catch {
|
|
90
90
|
}
|
|
91
|
-
},
|
|
92
|
-
const { webComponentTag: e, appName:
|
|
91
|
+
}, V = (t) => {
|
|
92
|
+
const { webComponentTag: e, appName: r, appVersion: n, scriptUrls: o, fallbackScriptUrls: a, collectorUrls: i, env: s, jwt: c, timeoutMs: O = 15e3, onLoad: v, onError: I } = t;
|
|
93
93
|
if (typeof document > "u")
|
|
94
|
-
return
|
|
95
|
-
const
|
|
96
|
-
if (customElements.get(e) || document.getElementById(
|
|
97
|
-
return
|
|
98
|
-
const
|
|
99
|
-
let
|
|
94
|
+
return w;
|
|
95
|
+
const h = `${e}-script-${s}`;
|
|
96
|
+
if (customElements.get(e) || document.getElementById(h))
|
|
97
|
+
return w;
|
|
98
|
+
const A = W(c), D = t.report ?? ((l) => B(i && P(i, s, c), l, n));
|
|
99
|
+
let m = !1, R = 0, g;
|
|
100
100
|
const f = () => {
|
|
101
|
-
|
|
102
|
-
},
|
|
103
|
-
if (
|
|
101
|
+
g !== void 0 && clearTimeout(g);
|
|
102
|
+
}, E = (l, u, d) => {
|
|
103
|
+
if (m)
|
|
104
104
|
return;
|
|
105
|
-
const
|
|
105
|
+
const z = Math.round(performance.now() - R), C = {
|
|
106
106
|
webComponentTag: e,
|
|
107
|
-
appName:
|
|
108
|
-
env:
|
|
109
|
-
scriptUrl:
|
|
107
|
+
appName: r,
|
|
108
|
+
env: s,
|
|
109
|
+
scriptUrl: u,
|
|
110
110
|
reason: l,
|
|
111
|
-
triedFallback:
|
|
112
|
-
elapsedMs:
|
|
113
|
-
claims:
|
|
111
|
+
triedFallback: d,
|
|
112
|
+
elapsedMs: z,
|
|
113
|
+
claims: A
|
|
114
114
|
};
|
|
115
|
-
|
|
116
|
-
const
|
|
117
|
-
if (
|
|
118
|
-
b(
|
|
115
|
+
D(C);
|
|
116
|
+
const _ = d ? void 0 : N(a ?? {}, s, c);
|
|
117
|
+
if (_ && _ !== u) {
|
|
118
|
+
b(_, !0);
|
|
119
119
|
return;
|
|
120
120
|
}
|
|
121
|
-
|
|
122
|
-
}, b = (l,
|
|
123
|
-
f(),
|
|
124
|
-
const
|
|
125
|
-
|
|
121
|
+
m = !0, f(), document.getElementById(h)?.remove(), I?.(C);
|
|
122
|
+
}, b = (l, u) => {
|
|
123
|
+
f(), R = performance.now();
|
|
124
|
+
const d = document.createElement("script");
|
|
125
|
+
d.id = h, d.src = l, d.type = "module", d.async = !0, d.onload = () => {
|
|
126
126
|
setTimeout(() => {
|
|
127
|
-
|
|
127
|
+
m || (customElements.get(e) ? (m = !0, f(), v?.()) : E("not-defined", l, u));
|
|
128
128
|
}, 0);
|
|
129
|
-
},
|
|
130
|
-
},
|
|
131
|
-
return
|
|
132
|
-
|
|
133
|
-
}) :
|
|
134
|
-
},
|
|
135
|
-
class
|
|
129
|
+
}, d.onerror = () => E("network", l, u), g = setTimeout(() => E("timeout", l, u), O), document.getElementById(h)?.remove(), document.head.appendChild(d);
|
|
130
|
+
}, y = N(o, s, c);
|
|
131
|
+
return y ? (b(y, !1), () => {
|
|
132
|
+
m = !0, f();
|
|
133
|
+
}) : w;
|
|
134
|
+
}, j = {};
|
|
135
|
+
class J {
|
|
136
136
|
config;
|
|
137
137
|
state;
|
|
138
138
|
scriptLoadingPromise;
|
|
@@ -141,7 +141,7 @@ class V {
|
|
|
141
141
|
throw new Error(k);
|
|
142
142
|
this.config = {
|
|
143
143
|
...e,
|
|
144
|
-
env: e.env ||
|
|
144
|
+
env: e.env || T,
|
|
145
145
|
theme: e.theme
|
|
146
146
|
}, this.state = {
|
|
147
147
|
initialized: !1,
|
|
@@ -162,10 +162,10 @@ class V {
|
|
|
162
162
|
throw new Error(this.errorMessages.ALREADY_RENDERED);
|
|
163
163
|
try {
|
|
164
164
|
await this.ensureScriptLoaded();
|
|
165
|
-
const
|
|
166
|
-
e.innerHTML = "", e.appendChild(
|
|
167
|
-
} catch (
|
|
168
|
-
throw console.error("Failed to render widget:",
|
|
165
|
+
const r = this.createWebComponent();
|
|
166
|
+
e.innerHTML = "", e.appendChild(r), this.state.container = e, this.state.element = r, this.state.initialized = !0;
|
|
167
|
+
} catch (r) {
|
|
168
|
+
throw console.error("Failed to render widget:", r), r;
|
|
169
169
|
}
|
|
170
170
|
}
|
|
171
171
|
/**
|
|
@@ -176,9 +176,9 @@ class V {
|
|
|
176
176
|
updateConfig(e) {
|
|
177
177
|
if (!this.state.initialized || !this.state.element)
|
|
178
178
|
throw new Error(this.errorMessages.NOT_RENDERED);
|
|
179
|
-
const
|
|
180
|
-
Object.entries(e).forEach(([
|
|
181
|
-
o && (this.config[
|
|
179
|
+
const r = this.state.element;
|
|
180
|
+
Object.entries(e).forEach(([n, o]) => {
|
|
181
|
+
o && (this.config[n] = o, r[n] = o);
|
|
182
182
|
});
|
|
183
183
|
}
|
|
184
184
|
/**
|
|
@@ -202,7 +202,7 @@ class V {
|
|
|
202
202
|
return { ...this.config };
|
|
203
203
|
}
|
|
204
204
|
getEnvironment() {
|
|
205
|
-
return this.config.env ||
|
|
205
|
+
return this.config.env || T;
|
|
206
206
|
}
|
|
207
207
|
getScriptId() {
|
|
208
208
|
return `${this.webComponentTag}-script-${this.getEnvironment()}`;
|
|
@@ -215,7 +215,7 @@ class V {
|
|
|
215
215
|
}
|
|
216
216
|
getEffectiveScriptUrls() {
|
|
217
217
|
if (typeof import.meta < "u") {
|
|
218
|
-
const e =
|
|
218
|
+
const e = j.VITE_SCRIPT_URL;
|
|
219
219
|
if (e)
|
|
220
220
|
return { [this.getEnvironment()]: e };
|
|
221
221
|
}
|
|
@@ -225,17 +225,17 @@ class V {
|
|
|
225
225
|
if (!(this.getWebComponent() || this.isScriptLoaded())) {
|
|
226
226
|
if (this.scriptLoadingPromise)
|
|
227
227
|
return this.scriptLoadingPromise;
|
|
228
|
-
this.scriptLoadingPromise = new Promise((e,
|
|
229
|
-
|
|
228
|
+
this.scriptLoadingPromise = new Promise((e, r) => {
|
|
229
|
+
V({
|
|
230
230
|
webComponentTag: this.webComponentTag,
|
|
231
231
|
appName: this.webComponentTag,
|
|
232
232
|
scriptUrls: this.getEffectiveScriptUrls(),
|
|
233
|
-
collectorUrls:
|
|
233
|
+
collectorUrls: F,
|
|
234
234
|
env: this.getEnvironment(),
|
|
235
235
|
jwt: this.config.jwt,
|
|
236
236
|
onLoad: e,
|
|
237
|
-
onError: (
|
|
238
|
-
|
|
237
|
+
onError: (n) => {
|
|
238
|
+
r(new Error(n.reason === "not-defined" ? this.errorMessages.WEB_COMPONENT_NOT_DEFINED : `${this.errorMessages.SCRIPT_LOAD_FAILED} (${this.getEnvironment()})`));
|
|
239
239
|
}
|
|
240
240
|
});
|
|
241
241
|
});
|
|
@@ -249,14 +249,14 @@ class V {
|
|
|
249
249
|
}
|
|
250
250
|
async waitForWebComponent(e = 5e3) {
|
|
251
251
|
if (!this.getWebComponent())
|
|
252
|
-
return new Promise((
|
|
252
|
+
return new Promise((r, n) => {
|
|
253
253
|
const o = setTimeout(() => {
|
|
254
|
-
|
|
254
|
+
n(new Error(`Timeout waiting for ${this.webComponentTag} to be defined`));
|
|
255
255
|
}, e);
|
|
256
256
|
customElements.whenDefined(this.webComponentTag).then(() => {
|
|
257
|
-
clearTimeout(o),
|
|
257
|
+
clearTimeout(o), r();
|
|
258
258
|
}).catch((a) => {
|
|
259
|
-
clearTimeout(o),
|
|
259
|
+
clearTimeout(o), n(a);
|
|
260
260
|
});
|
|
261
261
|
});
|
|
262
262
|
}
|
|
@@ -273,16 +273,32 @@ class V {
|
|
|
273
273
|
}
|
|
274
274
|
createWebComponent() {
|
|
275
275
|
const e = document.createElement(this.webComponentTag);
|
|
276
|
-
return Object.entries(this.config).forEach(([
|
|
277
|
-
|
|
276
|
+
return Object.entries(this.config).forEach(([r, n]) => {
|
|
277
|
+
n && (e[r] = n);
|
|
278
278
|
}), e;
|
|
279
279
|
}
|
|
280
280
|
}
|
|
281
|
-
|
|
281
|
+
const p = {
|
|
282
|
+
local: "http://localhost:5173",
|
|
283
|
+
dev: "https://sdk-cdn.dev.0hash.com",
|
|
284
|
+
cert: "https://sdk-cdn.cert.zerohash.com",
|
|
285
|
+
prod: "https://sdk-cdn.zerohash.com",
|
|
286
|
+
"eu-cert": "https://sdk-cdn.cert.zerohash.eu",
|
|
287
|
+
"eu-prod": "https://sdk-cdn.zerohash.eu"
|
|
288
|
+
}, x = (t, e, r = {}) => {
|
|
289
|
+
const { eu: n = !1, legacyAliases: o = !1, local: a = p.local } = r, i = (c) => `${c}/${t}/${e}`, s = {
|
|
290
|
+
local: i(a),
|
|
291
|
+
dev: i(p.dev),
|
|
292
|
+
cert: i(p.cert),
|
|
293
|
+
prod: i(p.prod)
|
|
294
|
+
};
|
|
295
|
+
return o && (s.sandbox = i(p.cert), s.production = i(p.prod)), n && (s["eu-cert"] = i(p["eu-cert"]), s["eu-prod"] = i(p["eu-prod"])), s;
|
|
296
|
+
};
|
|
297
|
+
var L;
|
|
282
298
|
(function(t) {
|
|
283
299
|
t.NETWORK_ERROR = "network_error", t.AUTH_ERROR = "auth_error", t.NOT_FOUND_ERROR = "not_found_error", t.VALIDATION_ERROR = "validation_error", t.SERVER_ERROR = "server_error", t.CLIENT_ERROR = "client_error", t.UNKNOWN_ERROR = "unknown_error";
|
|
284
|
-
})(
|
|
285
|
-
class
|
|
300
|
+
})(L || (L = {}));
|
|
301
|
+
class H extends J {
|
|
286
302
|
errorMessages = {
|
|
287
303
|
ALREADY_RENDERED: "FiatAccountLink widget is already rendered. Call destroy() before rendering again.",
|
|
288
304
|
NOT_RENDERED: "FiatAccountLink widget is not rendered. Call render() first.",
|
|
@@ -290,14 +306,11 @@ class $ extends V {
|
|
|
290
306
|
SCRIPT_LOAD_FAILED: "Failed to load the Connect FiatAccountLink script.",
|
|
291
307
|
WEB_COMPONENT_NOT_DEFINED: "Web component is not defined. Script may not be loaded."
|
|
292
308
|
};
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
sandbox: "https://sdk.sandbox.connect.xyz/fiat-account-link-web/index.js",
|
|
299
|
-
production: "https://sdk.connect.xyz/fiat-account-link-web/index.js"
|
|
300
|
-
};
|
|
309
|
+
// This SDK's vite dev server serves on port 4208.
|
|
310
|
+
scriptUrls = x("fiat-account-link-web", "index.js", {
|
|
311
|
+
legacyAliases: !0,
|
|
312
|
+
local: "http://localhost:4208"
|
|
313
|
+
});
|
|
301
314
|
webComponentTag = "zerohash-fiat-account-link";
|
|
302
315
|
render(e) {
|
|
303
316
|
return super.render(e);
|
|
@@ -316,7 +329,7 @@ class $ extends V {
|
|
|
316
329
|
}
|
|
317
330
|
}
|
|
318
331
|
export {
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
332
|
+
L as ErrorCode,
|
|
333
|
+
H as FiatAccountLink,
|
|
334
|
+
H as default
|
|
322
335
|
};
|
package/dist/index.umd.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(
|
|
1
|
+
(function(c,m){typeof exports=="object"&&typeof module<"u"?m(exports):typeof define=="function"&&define.amd?define(["exports"],m):(c=typeof globalThis<"u"?globalThis:c||self,m(c.FiatAccountLink={}))})(this,(function(c){"use strict";var m=typeof document<"u"?document.currentScript:null;const b="production",I="JWT token is required and must be a string.",A={dev:"https://grafana-faro-collector.dev.0hash.com/collect",cert:"https://grafana-faro-collector.cert.zerohash.com/collect",prod:"https://grafana-faro-collector.zerohash.com/collect","eu-cert":"https://grafana-faro-collector.cert.zerohash.eu/collect","eu-prod":"https://grafana-faro-collector.zerohash.eu/collect",sandbox:"https://grafana-faro-collector.cert.zerohash.com/collect",production:"https://grafana-faro-collector.zerohash.com/collect"},D=t=>{if(!t||typeof t!="string")return null;const e=t.split(".");if(e.length<2)return null;try{const r=e[1].replace(/-/g,"+").replace(/_/g,"/"),n=r+"===".slice(0,(4-r.length%4)%4),o=typeof atob<"u"?atob(n):Buffer.from(n,"base64").toString("utf-8"),s=JSON.parse(o)?.payload?.region;if(typeof s!="string")return null;const i=s.toLowerCase();return i==="us"||i==="eu"?i:null}catch{return null}},C=(t,e)=>D(e)!=="eu"?t:t==="cert"?"eu-cert":t==="prod"?"eu-prod":t,k=t=>{if(!t||typeof t!="string")return{};const e=t.split(".");if(e.length<2)return{};try{const r=e[1].replace(/-/g,"+").replace(/_/g,"/"),n=r+"===".slice(0,(4-r.length%4)%4),o=typeof atob<"u"?atob(n):Buffer.from(n,"base64").toString("utf-8"),a=JSON.parse(o),s=a?.payload??{},i=d=>typeof d=="string"&&d.length>0?d:void 0;return{participantCode:i(s.participant_code),platformName:i(s.platform_name),platformCode:i(a.platform_code),region:i(s.region)}}catch{return{}}},T=(t,e,r)=>{const n=C(e,r);return t[n]??t[e]??t.prod},z=(t,e,r)=>{const n=C(e,r);return t[n]??t[e]},E=()=>{},F=()=>{const t=new Uint8Array(8);return globalThis.crypto.getRandomValues(t),Array.from(t,e=>e.toString(16).padStart(2,"0")).join("")},U=(t,e)=>{const r=F(),n=globalThis.__ZH_WEB_SDK_VERSION__,o={};t.claims.participantCode&&(o.participant_code=t.claims.participantCode),t.claims.platformName&&(o.platform_name=t.claims.platformName),t.claims.platformCode&&(o.platform_code=t.claims.platformCode),t.claims.region&&(o.region=t.claims.region),n&&(o.zh_web_sdk_version=n);const a={meta:{app:{name:t.appName,version:e??"unknown",environment:t.env},session:{id:r,attributes:o},browser:typeof navigator<"u"?{userAgent:navigator.userAgent}:void 0,page:typeof window<"u"?{url:window.location.origin}:void 0},logs:[{message:`Failed to load the script for ${t.webComponentTag} from ${t.env} environment.`,level:"error",timestamp:new Date().toISOString(),context:{web_component:t.webComponentTag,script_url:t.scriptUrl,reason:t.reason,tried_fallback:String(t.triedFallback),elapsed_ms:String(t.elapsedMs)}}]};return{sessionId:r,payload:a}},M=(t,e,r)=>{if(!t||typeof fetch>"u")return;const{sessionId:n,payload:o}=U(e,r);try{fetch(t,{method:"POST",headers:{"Content-Type":"application/json","x-faro-session-id":n},body:JSON.stringify(o),keepalive:!0}).catch(()=>{})}catch{}},P=t=>{const{webComponentTag:e,appName:r,appVersion:n,scriptUrls:o,fallbackScriptUrls:a,collectorUrls:s,env:i,jwt:d,timeoutMs:B=15e3,onLoad:V,onError:J}=t;if(typeof document>"u")return E;const g=`${e}-script-${i}`;if(customElements.get(e)||document.getElementById(g))return E;const H=k(d),x=t.report??(u=>M(s&&z(s,i,d),u,n));let h=!1,L=0,R;const _=()=>{R!==void 0&&clearTimeout(R)},w=(u,f,l)=>{if(h)return;const K=Math.round(performance.now()-L),O={webComponentTag:e,appName:r,env:i,scriptUrl:f,reason:u,triedFallback:l,elapsedMs:K,claims:H};x(O);const y=l?void 0:T(a??{},i,d);if(y&&y!==f){S(y,!0);return}h=!0,_(),document.getElementById(g)?.remove(),J?.(O)},S=(u,f)=>{_(),L=performance.now();const l=document.createElement("script");l.id=g,l.src=u,l.type="module",l.async=!0,l.onload=()=>{setTimeout(()=>{h||(customElements.get(e)?(h=!0,_(),V?.()):w("not-defined",u,f))},0)},l.onerror=()=>w("network",u,f),R=setTimeout(()=>w("timeout",u,f),B),document.getElementById(g)?.remove(),document.head.appendChild(l)},v=T(o,i,d);return v?(S(v,!1),()=>{h=!0,_()}):E},W={};class j{config;state;scriptLoadingPromise;constructor(e){if(!e.jwt||typeof e.jwt!="string")throw new Error(I);this.config={...e,env:e.env||b,theme:e.theme},this.state={initialized:!1,scriptLoaded:!1,container:null,element:null}}async render(e){if(!e||!(e instanceof HTMLElement))throw new Error(this.errorMessages.INVALID_CONTAINER);if(this.state.initialized)throw new Error(this.errorMessages.ALREADY_RENDERED);try{await this.ensureScriptLoaded();const r=this.createWebComponent();e.innerHTML="",e.appendChild(r),this.state.container=e,this.state.element=r,this.state.initialized=!0}catch(r){throw console.error("Failed to render widget:",r),r}}updateConfig(e){if(!this.state.initialized||!this.state.element)throw new Error(this.errorMessages.NOT_RENDERED);const r=this.state.element;Object.entries(e).forEach(([n,o])=>{o&&(this.config[n]=o,r[n]=o)})}destroy(){this.state.initialized&&(this.state.element&&this.state.element.parentNode&&this.state.element.parentNode.removeChild(this.state.element),this.state.container&&(this.state.container.innerHTML=""),this.state.container=null,this.state.element=null,this.state.initialized=!1)}isRendered(){return this.state.initialized}getConfig(){return{...this.config}}getEnvironment(){return this.config.env||b}getScriptId(){return`${this.webComponentTag}-script-${this.getEnvironment()}`}isScriptLoaded(){return!!document.getElementById(this.getScriptId())}getWebComponent(){return customElements.get(this.webComponentTag)}getEffectiveScriptUrls(){if(typeof{url:typeof document>"u"&&typeof location>"u"?require("url").pathToFileURL(__filename).href:typeof document>"u"?location.href:m&&m.tagName.toUpperCase()==="SCRIPT"&&m.src||new URL("index.umd.cjs",document.baseURI).href}<"u"){const e=W.VITE_SCRIPT_URL;if(e)return{[this.getEnvironment()]:e}}return this.scriptUrls}async loadScript(){if(!(this.getWebComponent()||this.isScriptLoaded())){if(this.scriptLoadingPromise)return this.scriptLoadingPromise;this.scriptLoadingPromise=new Promise((e,r)=>{P({webComponentTag:this.webComponentTag,appName:this.webComponentTag,scriptUrls:this.getEffectiveScriptUrls(),collectorUrls:A,env:this.getEnvironment(),jwt:this.config.jwt,onLoad:e,onError:n=>{r(new Error(n.reason==="not-defined"?this.errorMessages.WEB_COMPONENT_NOT_DEFINED:`${this.errorMessages.SCRIPT_LOAD_FAILED} (${this.getEnvironment()})`))}})});try{await this.scriptLoadingPromise}catch(e){throw this.scriptLoadingPromise=void 0,e}return this.scriptLoadingPromise}}async waitForWebComponent(e=5e3){if(!this.getWebComponent())return new Promise((r,n)=>{const o=setTimeout(()=>{n(new Error(`Timeout waiting for ${this.webComponentTag} to be defined`))},e);customElements.whenDefined(this.webComponentTag).then(()=>{clearTimeout(o),r()}).catch(a=>{clearTimeout(o),n(a)})})}async ensureScriptLoaded(){if(!this.state.scriptLoaded)try{await this.loadScript(),await this.waitForWebComponent(),this.state.scriptLoaded=!0}catch(e){throw console.error("Failed to load Connect script:",e),e}}createWebComponent(){const e=document.createElement(this.webComponentTag);return Object.entries(this.config).forEach(([r,n])=>{n&&(e[r]=n)}),e}}const p={local:"http://localhost:5173",dev:"https://sdk-cdn.dev.0hash.com",cert:"https://sdk-cdn.cert.zerohash.com",prod:"https://sdk-cdn.zerohash.com","eu-cert":"https://sdk-cdn.cert.zerohash.eu","eu-prod":"https://sdk-cdn.zerohash.eu"},$=(t,e,r={})=>{const{eu:n=!1,legacyAliases:o=!1,local:a=p.local}=r,s=d=>`${d}/${t}/${e}`,i={local:s(a),dev:s(p.dev),cert:s(p.cert),prod:s(p.prod)};return o&&(i.sandbox=s(p.cert),i.production=s(p.prod)),n&&(i["eu-cert"]=s(p["eu-cert"]),i["eu-prod"]=s(p["eu-prod"])),i};c.ErrorCode=void 0,(function(t){t.NETWORK_ERROR="network_error",t.AUTH_ERROR="auth_error",t.NOT_FOUND_ERROR="not_found_error",t.VALIDATION_ERROR="validation_error",t.SERVER_ERROR="server_error",t.CLIENT_ERROR="client_error",t.UNKNOWN_ERROR="unknown_error"})(c.ErrorCode||(c.ErrorCode={}));class N extends j{errorMessages={ALREADY_RENDERED:"FiatAccountLink widget is already rendered. Call destroy() before rendering again.",NOT_RENDERED:"FiatAccountLink widget is not rendered. Call render() first.",INVALID_CONTAINER:"Invalid container element provided.",SCRIPT_LOAD_FAILED:"Failed to load the Connect FiatAccountLink script.",WEB_COMPONENT_NOT_DEFINED:"Web component is not defined. Script may not be loaded."};scriptUrls=$("fiat-account-link-web","index.js",{legacyAliases:!0,local:"http://localhost:4208"});webComponentTag="zerohash-fiat-account-link";render(e){return super.render(e)}updateConfig(e){return super.updateConfig(e)}getConfig(){return super.getConfig()}isRendered(){return super.isRendered()}destroy(){return super.destroy()}}c.FiatAccountLink=N,c.default=N,Object.defineProperties(c,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}));
|