@zerohash-sdk/crypto-account-link-js 1.3.1 → 1.3.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/dist/index.d.ts +7 -3
- package/dist/index.js +45 -29
- package/dist/index.umd.cjs +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -36,7 +36,7 @@ declare interface BaseConfig<TEvent = AppEvent> extends CommonCallbacks<TEvent>
|
|
|
36
36
|
|
|
37
37
|
/**
|
|
38
38
|
* Theme mode
|
|
39
|
-
* @default '
|
|
39
|
+
* @default 'light'
|
|
40
40
|
*
|
|
41
41
|
* Available themes:
|
|
42
42
|
* - `'auto'` - Automatically detect system preference (light/dark mode)
|
|
@@ -200,7 +200,11 @@ declare abstract class BaseJsSdk<Config extends BaseConfig<never> = BaseConfig>
|
|
|
200
200
|
return import.meta.env['VITE_SCRIPT_URL'] || this.scriptUrls[this.getEnvironment()];
|
|
201
201
|
}
|
|
202
202
|
|
|
203
|
-
|
|
203
|
+
// Route EU partners (region claim in JWT) to EU-hosted assets without
|
|
204
|
+
// changing the public `env` contract — falls back to the configured env
|
|
205
|
+
// when no EU URL is registered for that environment.
|
|
206
|
+
const effectiveEnv = resolveEnvByRegion(this.getEnvironment(), this.config.jwt);
|
|
207
|
+
return this.scriptUrls[effectiveEnv] ?? this.scriptUrls[this.getEnvironment()];
|
|
204
208
|
}
|
|
205
209
|
|
|
206
210
|
private async loadScript() {
|
|
@@ -327,7 +331,7 @@ declare type CommonCallbacks<TEvent = AppEvent> = {
|
|
|
327
331
|
* const cryptoAccountLink = new CryptoAccountLink({
|
|
328
332
|
* jwt: 'your-jwt-token',
|
|
329
333
|
* env: 'prod',
|
|
330
|
-
* theme: '
|
|
334
|
+
* theme: 'light',
|
|
331
335
|
* isPayouts: false,
|
|
332
336
|
* onCompleted: (data) => console.log('Completed', data),
|
|
333
337
|
* onClose: () => console.log('Closed'),
|
package/dist/index.js
CHANGED
|
@@ -1,14 +1,29 @@
|
|
|
1
|
-
const
|
|
2
|
-
|
|
1
|
+
const c = "production", l = "JWT token is required and must be a string.", h = (r) => {
|
|
2
|
+
if (!r || typeof r != "string")
|
|
3
|
+
return null;
|
|
4
|
+
const e = r.split(".");
|
|
5
|
+
if (e.length < 2)
|
|
6
|
+
return null;
|
|
7
|
+
try {
|
|
8
|
+
const n = e[1].replace(/-/g, "+").replace(/_/g, "/"), t = n + "===".slice(0, (4 - n.length % 4) % 4), i = typeof atob < "u" ? atob(t) : Buffer.from(t, "base64").toString("utf-8"), a = JSON.parse(i)?.payload?.region;
|
|
9
|
+
if (typeof a != "string")
|
|
10
|
+
return null;
|
|
11
|
+
const s = a.toLowerCase();
|
|
12
|
+
return s === "us" || s === "eu" ? s : null;
|
|
13
|
+
} catch {
|
|
14
|
+
return null;
|
|
15
|
+
}
|
|
16
|
+
}, p = (r, e) => h(e) !== "eu" ? r : r === "cert" ? "eu-cert" : r === "prod" ? "eu-prod" : r;
|
|
17
|
+
class u {
|
|
3
18
|
config;
|
|
4
19
|
state;
|
|
5
20
|
scriptLoadingPromise;
|
|
6
21
|
constructor(e) {
|
|
7
22
|
if (!e.jwt || typeof e.jwt != "string")
|
|
8
|
-
throw new Error(
|
|
23
|
+
throw new Error(l);
|
|
9
24
|
this.config = {
|
|
10
25
|
...e,
|
|
11
|
-
env: e.env ||
|
|
26
|
+
env: e.env || c,
|
|
12
27
|
theme: e.theme
|
|
13
28
|
}, this.state = {
|
|
14
29
|
initialized: !1,
|
|
@@ -29,10 +44,10 @@ class d {
|
|
|
29
44
|
throw new Error(this.errorMessages.ALREADY_RENDERED);
|
|
30
45
|
try {
|
|
31
46
|
await this.ensureScriptLoaded();
|
|
32
|
-
const
|
|
33
|
-
e.innerHTML = "", e.appendChild(
|
|
34
|
-
} catch (
|
|
35
|
-
throw console.error("Failed to render widget:",
|
|
47
|
+
const n = this.createWebComponent();
|
|
48
|
+
e.innerHTML = "", e.appendChild(n), this.state.container = e, this.state.element = n, this.state.initialized = !0;
|
|
49
|
+
} catch (n) {
|
|
50
|
+
throw console.error("Failed to render widget:", n), n;
|
|
36
51
|
}
|
|
37
52
|
}
|
|
38
53
|
/**
|
|
@@ -43,9 +58,9 @@ class d {
|
|
|
43
58
|
updateConfig(e) {
|
|
44
59
|
if (!this.state.initialized || !this.state.element)
|
|
45
60
|
throw new Error(this.errorMessages.NOT_RENDERED);
|
|
46
|
-
const
|
|
61
|
+
const n = this.state.element;
|
|
47
62
|
Object.entries(e).forEach(([t, i]) => {
|
|
48
|
-
i && (this.config[t] = i,
|
|
63
|
+
i && (this.config[t] = i, n[t] = i);
|
|
49
64
|
});
|
|
50
65
|
}
|
|
51
66
|
/**
|
|
@@ -69,7 +84,7 @@ class d {
|
|
|
69
84
|
return { ...this.config };
|
|
70
85
|
}
|
|
71
86
|
getEnvironment() {
|
|
72
|
-
return this.config.env ||
|
|
87
|
+
return this.config.env || c;
|
|
73
88
|
}
|
|
74
89
|
getScriptId() {
|
|
75
90
|
return `${this.webComponentTag}-script-${this.getEnvironment()}`;
|
|
@@ -81,20 +96,21 @@ class d {
|
|
|
81
96
|
return customElements.get(this.webComponentTag);
|
|
82
97
|
}
|
|
83
98
|
getScriptUrl() {
|
|
84
|
-
|
|
99
|
+
const e = p(this.getEnvironment(), this.config.jwt);
|
|
100
|
+
return this.scriptUrls[e] ?? this.scriptUrls[this.getEnvironment()];
|
|
85
101
|
}
|
|
86
102
|
async loadScript() {
|
|
87
103
|
if (!this.isScriptLoaded()) {
|
|
88
104
|
if (this.scriptLoadingPromise)
|
|
89
105
|
return this.scriptLoadingPromise;
|
|
90
|
-
this.scriptLoadingPromise = new Promise((e,
|
|
106
|
+
this.scriptLoadingPromise = new Promise((e, n) => {
|
|
91
107
|
const t = document.createElement("script");
|
|
92
108
|
t.id = this.getScriptId(), t.src = this.getScriptUrl(), t.type = "module", t.async = !0, t.onload = () => {
|
|
93
109
|
setTimeout(() => {
|
|
94
|
-
this.getWebComponent() ? e() :
|
|
110
|
+
this.getWebComponent() ? e() : n(new Error(this.errorMessages.WEB_COMPONENT_NOT_DEFINED));
|
|
95
111
|
}, 0);
|
|
96
112
|
}, t.onerror = () => {
|
|
97
|
-
this.scriptLoadingPromise = void 0,
|
|
113
|
+
this.scriptLoadingPromise = void 0, n(new Error(`${this.errorMessages.SCRIPT_LOAD_FAILED} (${this.getEnvironment()})`));
|
|
98
114
|
}, document.head.appendChild(t);
|
|
99
115
|
});
|
|
100
116
|
try {
|
|
@@ -107,14 +123,14 @@ class d {
|
|
|
107
123
|
}
|
|
108
124
|
async waitForWebComponent(e = 5e3) {
|
|
109
125
|
if (!this.getWebComponent())
|
|
110
|
-
return new Promise((
|
|
126
|
+
return new Promise((n, t) => {
|
|
111
127
|
const i = setTimeout(() => {
|
|
112
128
|
t(new Error(`Timeout waiting for ${this.webComponentTag} to be defined`));
|
|
113
129
|
}, e);
|
|
114
130
|
customElements.whenDefined(this.webComponentTag).then(() => {
|
|
115
|
-
clearTimeout(i),
|
|
116
|
-
}).catch((
|
|
117
|
-
clearTimeout(i), t(
|
|
131
|
+
clearTimeout(i), n();
|
|
132
|
+
}).catch((o) => {
|
|
133
|
+
clearTimeout(i), t(o);
|
|
118
134
|
});
|
|
119
135
|
});
|
|
120
136
|
}
|
|
@@ -131,16 +147,16 @@ class d {
|
|
|
131
147
|
}
|
|
132
148
|
createWebComponent() {
|
|
133
149
|
const e = document.createElement(this.webComponentTag);
|
|
134
|
-
return Object.entries(this.config).forEach(([
|
|
135
|
-
t && (e[
|
|
150
|
+
return Object.entries(this.config).forEach(([n, t]) => {
|
|
151
|
+
t && (e[n] = t);
|
|
136
152
|
}), e;
|
|
137
153
|
}
|
|
138
154
|
}
|
|
139
|
-
var
|
|
140
|
-
(function(
|
|
141
|
-
|
|
142
|
-
})(
|
|
143
|
-
class
|
|
155
|
+
var d;
|
|
156
|
+
(function(r) {
|
|
157
|
+
r.NETWORK_ERROR = "network_error", r.AUTH_ERROR = "auth_error", r.NOT_FOUND_ERROR = "not_found_error", r.VALIDATION_ERROR = "validation_error", r.SERVER_ERROR = "server_error", r.CLIENT_ERROR = "client_error", r.UNKNOWN_ERROR = "unknown_error";
|
|
158
|
+
})(d || (d = {}));
|
|
159
|
+
class m extends u {
|
|
144
160
|
errorMessages = {
|
|
145
161
|
ALREADY_RENDERED: "CryptoAccountLink widget is already rendered. Call destroy() before rendering again.",
|
|
146
162
|
NOT_RENDERED: "CryptoAccountLink widget is not rendered. Call render() first.",
|
|
@@ -194,7 +210,7 @@ class h extends d {
|
|
|
194
210
|
}
|
|
195
211
|
}
|
|
196
212
|
export {
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
213
|
+
m as CryptoAccountLink,
|
|
214
|
+
d as ErrorCode,
|
|
215
|
+
m as default
|
|
200
216
|
};
|
package/dist/index.umd.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(i,
|
|
1
|
+
(function(i,s){typeof exports=="object"&&typeof module<"u"?s(exports):typeof define=="function"&&define.amd?define(["exports"],s):(i=typeof globalThis<"u"?globalThis:i||self,s(i.CryptoAccountLink={}))})(this,(function(i){"use strict";const s="production",u="JWT token is required and must be a string.",h=n=>{if(!n||typeof n!="string")return null;const e=n.split(".");if(e.length<2)return null;try{const r=e[1].replace(/-/g,"+").replace(/_/g,"/"),t=r+"===".slice(0,(4-r.length%4)%4),o=typeof atob<"u"?atob(t):Buffer.from(t,"base64").toString("utf-8"),l=JSON.parse(o)?.payload?.region;if(typeof l!="string")return null;const c=l.toLowerCase();return c==="us"||c==="eu"?c:null}catch{return null}},p=(n,e)=>h(e)!=="eu"?n:n==="cert"?"eu-cert":n==="prod"?"eu-prod":n;class m{config;state;scriptLoadingPromise;constructor(e){if(!e.jwt||typeof e.jwt!="string")throw new Error(u);this.config={...e,env:e.env||s,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(([t,o])=>{o&&(this.config[t]=o,r[t]=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||s}getScriptId(){return`${this.webComponentTag}-script-${this.getEnvironment()}`}isScriptLoaded(){return!!document.getElementById(this.getScriptId())}getWebComponent(){return customElements.get(this.webComponentTag)}getScriptUrl(){const e=p(this.getEnvironment(),this.config.jwt);return this.scriptUrls[e]??this.scriptUrls[this.getEnvironment()]}async loadScript(){if(!this.isScriptLoaded()){if(this.scriptLoadingPromise)return this.scriptLoadingPromise;this.scriptLoadingPromise=new Promise((e,r)=>{const t=document.createElement("script");t.id=this.getScriptId(),t.src=this.getScriptUrl(),t.type="module",t.async=!0,t.onload=()=>{setTimeout(()=>{this.getWebComponent()?e():r(new Error(this.errorMessages.WEB_COMPONENT_NOT_DEFINED))},0)},t.onerror=()=>{this.scriptLoadingPromise=void 0,r(new Error(`${this.errorMessages.SCRIPT_LOAD_FAILED} (${this.getEnvironment()})`))},document.head.appendChild(t)});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,t)=>{const o=setTimeout(()=>{t(new Error(`Timeout waiting for ${this.webComponentTag} to be defined`))},e);customElements.whenDefined(this.webComponentTag).then(()=>{clearTimeout(o),r()}).catch(d=>{clearTimeout(o),t(d)})})}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,t])=>{t&&(e[r]=t)}),e}}i.ErrorCode=void 0,(function(n){n.NETWORK_ERROR="network_error",n.AUTH_ERROR="auth_error",n.NOT_FOUND_ERROR="not_found_error",n.VALIDATION_ERROR="validation_error",n.SERVER_ERROR="server_error",n.CLIENT_ERROR="client_error",n.UNKNOWN_ERROR="unknown_error"})(i.ErrorCode||(i.ErrorCode={}));class a extends m{errorMessages={ALREADY_RENDERED:"CryptoAccountLink widget is already rendered. Call destroy() before rendering again.",NOT_RENDERED:"CryptoAccountLink widget is not rendered. Call render() first.",INVALID_CONTAINER:"Invalid container element provided.",SCRIPT_LOAD_FAILED:"Failed to load the Connect CryptoAccountLink script.",WEB_COMPONENT_NOT_DEFINED:"Web component is not defined. Script may not be loaded."};scriptUrls={local:"http://localhost:5173/crypto-account-link-web/index.js",dev:"https://connect-sdk.dev.0hash.com/crypto-account-link-web/index.js",cert:"https://sdk.sandbox.connect.xyz/crypto-account-link-web/index.js",prod:"https://sdk.connect.xyz/crypto-account-link-web/index.js",sandbox:"https://sdk.sandbox.connect.xyz/crypto-account-link-web/index.js",production:"https://sdk.connect.xyz/crypto-account-link-web/index.js"};webComponentTag="zerohash-crypto-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()}}i.CryptoAccountLink=a,i.default=a,Object.defineProperties(i,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}));
|