@connect-xyz/auth-js 1.1.0 → 1.3.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/README.md +4 -0
- package/dist/index.d.ts +92 -66
- package/dist/index.js +154 -111
- package/dist/index.umd.cjs +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -56,6 +56,7 @@ import { Auth } from 'https://sdk.connect.xyz/auth-js/index.js';
|
|
|
56
56
|
const auth = new Auth({
|
|
57
57
|
jwt: 'your-jwt-token',
|
|
58
58
|
env: 'production', // or 'sandbox'
|
|
59
|
+
theme: 'auto', // 'auto' (default), 'light', or 'dark'
|
|
59
60
|
onError: ({ error, reason }) => {
|
|
60
61
|
console.error('Auth error:', error, 'Reason:', reason);
|
|
61
62
|
},
|
|
@@ -97,6 +98,7 @@ import { Auth, AuthConfig, ErrorData, DepositData } from '@connect/auth-js';
|
|
|
97
98
|
const config: AuthConfig = {
|
|
98
99
|
jwt: 'your-jwt-token',
|
|
99
100
|
env: 'sandbox',
|
|
101
|
+
theme: 'dark', // 'auto' (default), 'light', or 'dark'
|
|
100
102
|
onError: ({ error, reason }: ErrorData) => {
|
|
101
103
|
// Handle error with type safety
|
|
102
104
|
},
|
|
@@ -116,6 +118,7 @@ const auth = new Auth(config);
|
|
|
116
118
|
| ----------- | --------------------------------- | -------- | -------------- | ---------------------------------------------- |
|
|
117
119
|
| `jwt` | `string` | Yes | - | JWT token for authentication with Connect Auth |
|
|
118
120
|
| `env` | `"production" \| "sandbox"` | No | `"production"` | Target environment |
|
|
121
|
+
| `theme` | `"auto" \| "light" \| "dark"` | No | `"auto"` | Theme mode for the interface |
|
|
119
122
|
| `onError` | `({ errorCode, reason }) => void` | No | - | Callback for error events |
|
|
120
123
|
| `onClose` | `() => void` | No | - | Callback when the widget is closed |
|
|
121
124
|
| `onDeposit` | `({ data }) => void` | No | - | Callback for deposit received |
|
|
@@ -134,6 +137,7 @@ Creates a new Auth instance with the provided configuration.
|
|
|
134
137
|
- `config` (AuthConfig): Configuration object
|
|
135
138
|
- `jwt` (string, required): JWT token for authentication
|
|
136
139
|
- `env` (string, optional): Environment - 'production' (default) or 'sandbox'
|
|
140
|
+
- `theme` (string, optional): Theme mode - 'auto' (default), 'light', or 'dark'
|
|
137
141
|
- `onError` (function, optional): Error callback
|
|
138
142
|
- `onClose` (function, optional): Close callback
|
|
139
143
|
- `onDeposit` (function, optional): Deposit callback
|
package/dist/index.d.ts
CHANGED
|
@@ -44,26 +44,19 @@ declare type AppEvent<TType extends string = string, TData = Record<string, unkn
|
|
|
44
44
|
* auth.destroy();
|
|
45
45
|
* ```
|
|
46
46
|
*/
|
|
47
|
-
declare class Auth {
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
* Load the web component script if not already loaded
|
|
61
|
-
*/
|
|
62
|
-
private ensureScriptLoaded;
|
|
63
|
-
/**
|
|
64
|
-
* Create the web component element
|
|
65
|
-
*/
|
|
66
|
-
private createWebComponent;
|
|
47
|
+
declare class Auth extends BaseJsSdk<AuthConfig> {
|
|
48
|
+
protected errorMessages: {
|
|
49
|
+
readonly ALREADY_RENDERED: "Auth widget is already rendered. Call destroy() before rendering again.";
|
|
50
|
+
readonly NOT_RENDERED: "Auth widget is not rendered. Call render() first.";
|
|
51
|
+
readonly INVALID_CONTAINER: "Invalid container element provided.";
|
|
52
|
+
readonly SCRIPT_LOAD_FAILED: "Failed to load the Connect Auth script.";
|
|
53
|
+
readonly WEB_COMPONENT_NOT_DEFINED: "Web component is not defined. Script may not be loaded.";
|
|
54
|
+
};
|
|
55
|
+
protected scriptUrls: {
|
|
56
|
+
sandbox: string;
|
|
57
|
+
production: string;
|
|
58
|
+
};
|
|
59
|
+
protected webComponentTag: string;
|
|
67
60
|
/**
|
|
68
61
|
* Render the Auth widget to a container element
|
|
69
62
|
* @param container - The container element to render the widget into
|
|
@@ -76,19 +69,19 @@ declare class Auth {
|
|
|
76
69
|
*/
|
|
77
70
|
updateConfig(config: Partial<AuthConfig>): void;
|
|
78
71
|
/**
|
|
79
|
-
*
|
|
72
|
+
* Get the current configuration
|
|
73
|
+
* @returns The current configuration object
|
|
80
74
|
*/
|
|
81
|
-
|
|
75
|
+
getConfig(): Readonly<AuthConfig>;
|
|
82
76
|
/**
|
|
83
77
|
* Check if the Auth widget is currently rendered
|
|
84
78
|
* @returns True if the widget is rendered, false otherwise
|
|
85
79
|
*/
|
|
86
80
|
isRendered(): boolean;
|
|
87
81
|
/**
|
|
88
|
-
*
|
|
89
|
-
* @returns The current configuration object
|
|
82
|
+
* Destroy the Auth widget and clean up resources
|
|
90
83
|
*/
|
|
91
|
-
|
|
84
|
+
destroy(): void;
|
|
92
85
|
}
|
|
93
86
|
export { Auth }
|
|
94
87
|
export default Auth;
|
|
@@ -102,21 +95,34 @@ declare type AuthCallbacks = CommonCallbacks<AuthEvent> & {
|
|
|
102
95
|
onDeposit?: (deposit: DepositCompletedPayload) => void;
|
|
103
96
|
};
|
|
104
97
|
|
|
98
|
+
export declare interface AuthConfig extends BaseConfig<AuthEvent>, AuthCallbacks {
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Auth event structure for auth-specific events
|
|
103
|
+
*/
|
|
104
|
+
declare type AuthEvent = AppEvent<AuthEventType, DepositSubmittedEventData>;
|
|
105
|
+
|
|
105
106
|
/**
|
|
106
|
-
*
|
|
107
|
+
* Auth event type literals
|
|
108
|
+
*/
|
|
109
|
+
declare type AuthEventType = 'deposit.submitted';
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Configuration options for the SDK
|
|
107
113
|
*/
|
|
108
|
-
|
|
114
|
+
declare interface BaseConfig<TEvent = AppEvent> extends CommonCallbacks<TEvent> {
|
|
109
115
|
/**
|
|
110
|
-
* JWT token used for authentication
|
|
116
|
+
* JWT token used for authentication
|
|
111
117
|
*/
|
|
112
118
|
jwt: string;
|
|
113
119
|
/**
|
|
114
|
-
* Target environment
|
|
120
|
+
* Target environment
|
|
115
121
|
* @default 'production'
|
|
116
122
|
*/
|
|
117
123
|
env?: Environment;
|
|
118
124
|
/**
|
|
119
|
-
* Theme mode
|
|
125
|
+
* Theme mode
|
|
120
126
|
* @default 'auto'
|
|
121
127
|
*
|
|
122
128
|
* Available themes:
|
|
@@ -127,28 +133,54 @@ export declare interface AuthConfig extends AuthCallbacks {
|
|
|
127
133
|
theme?: Theme;
|
|
128
134
|
}
|
|
129
135
|
|
|
130
|
-
|
|
131
|
-
* Auth event structure for auth-specific events
|
|
132
|
-
*/
|
|
133
|
-
declare type AuthEvent = AppEvent<AuthEventType, DepositSubmittedEventData>;
|
|
134
|
-
|
|
135
|
-
/**
|
|
136
|
-
* Auth event type literals
|
|
137
|
-
*/
|
|
138
|
-
declare type AuthEventType = 'deposit.submitted';
|
|
136
|
+
declare type BaseErrorMessageKeys = 'ALREADY_RENDERED' | 'NOT_RENDERED' | 'INVALID_CONTAINER' | 'SCRIPT_LOAD_FAILED' | 'WEB_COMPONENT_NOT_DEFINED';
|
|
139
137
|
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
/**
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
138
|
+
declare abstract class BaseJsSdk<Config extends BaseConfig<never> = BaseConfig> {
|
|
139
|
+
private config;
|
|
140
|
+
private state;
|
|
141
|
+
private scriptLoadingPromise?;
|
|
142
|
+
protected abstract errorMessages: Record<BaseErrorMessageKeys, string>;
|
|
143
|
+
protected abstract scriptUrls: Record<Environment, string>;
|
|
144
|
+
protected abstract webComponentTag: string;
|
|
145
|
+
constructor(config: Config);
|
|
146
|
+
/**
|
|
147
|
+
* Render the widget to a container element
|
|
148
|
+
* @param container - The container element to render the widget into
|
|
149
|
+
* @returns Promise that resolves when the widget is rendered
|
|
150
|
+
*/
|
|
151
|
+
render(container: HTMLElement): Promise<void>;
|
|
152
|
+
/**
|
|
153
|
+
* Update the configuration of the widget
|
|
154
|
+
* @param config - Partial configuration to update
|
|
155
|
+
* @returns void
|
|
156
|
+
*/
|
|
157
|
+
updateConfig(config: Partial<Config>): void;
|
|
158
|
+
/**
|
|
159
|
+
* Destroy the widget and clean up resources
|
|
160
|
+
*/
|
|
161
|
+
destroy(): void;
|
|
162
|
+
/**
|
|
163
|
+
* Check if the widget is currently rendered
|
|
164
|
+
* @returns True if the widget is rendered, false otherwise
|
|
165
|
+
*/
|
|
166
|
+
isRendered(): boolean;
|
|
167
|
+
/**
|
|
168
|
+
* Get the current configuration
|
|
169
|
+
* @returns The current configuration object
|
|
170
|
+
*/
|
|
171
|
+
getConfig(): Readonly<Config>;
|
|
172
|
+
private getEnvironment;
|
|
173
|
+
private getScriptId;
|
|
174
|
+
private isScriptLoaded;
|
|
175
|
+
private getWebComponent;
|
|
176
|
+
private getScriptUrl;
|
|
177
|
+
private loadScript;
|
|
178
|
+
private waitForWebComponent;
|
|
179
|
+
/**
|
|
180
|
+
* Load the web component script if not already loaded
|
|
181
|
+
*/
|
|
182
|
+
protected ensureScriptLoaded(): Promise<void>;
|
|
183
|
+
private createWebComponent;
|
|
152
184
|
}
|
|
153
185
|
|
|
154
186
|
/**
|
|
@@ -164,18 +196,7 @@ declare type CommonCallbacks<TEvent = AppEvent> = {
|
|
|
164
196
|
onEvent?: (event: TEvent) => void;
|
|
165
197
|
};
|
|
166
198
|
|
|
167
|
-
|
|
168
|
-
* ConnectAuth element interface
|
|
169
|
-
*/
|
|
170
|
-
export declare interface ConnectAuthElement extends HTMLElement {
|
|
171
|
-
jwt: string;
|
|
172
|
-
env?: Environment;
|
|
173
|
-
theme?: Theme;
|
|
174
|
-
onError?: (errorData: ErrorPayload) => void;
|
|
175
|
-
onClose?: () => void;
|
|
176
|
-
onDeposit?: (depositData: DepositCompletedPayload) => void;
|
|
177
|
-
onEvent?: (eventData: AuthEvent) => void;
|
|
178
|
-
}
|
|
199
|
+
export declare type ConnectAuthElement = SdkElement<AuthConfig>;
|
|
179
200
|
|
|
180
201
|
/**
|
|
181
202
|
* Deposit completed payload structure for deposit callbacks
|
|
@@ -217,14 +238,14 @@ declare type DepositSubmittedEventData = {
|
|
|
217
238
|
};
|
|
218
239
|
|
|
219
240
|
/**
|
|
220
|
-
* Environment configuration for the
|
|
241
|
+
* Environment configuration for the SDK
|
|
221
242
|
*/
|
|
222
243
|
export declare type Environment = 'sandbox' | 'production';
|
|
223
244
|
|
|
224
245
|
/**
|
|
225
246
|
* Generic error codes for all Connect applications
|
|
226
247
|
*/
|
|
227
|
-
declare enum ErrorCode {
|
|
248
|
+
export declare enum ErrorCode {
|
|
228
249
|
/** Network connectivity error */
|
|
229
250
|
NETWORK_ERROR = 'network_error',
|
|
230
251
|
/** Authentication or session expired error */
|
|
@@ -252,7 +273,12 @@ declare type ErrorPayload = {
|
|
|
252
273
|
};
|
|
253
274
|
|
|
254
275
|
/**
|
|
255
|
-
*
|
|
276
|
+
* Generic HTMLElement representing a web component with custom config
|
|
277
|
+
*/
|
|
278
|
+
declare type SdkElement<Config extends BaseConfig<never>> = HTMLElement & Config;
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* Theme configuration for the SDK
|
|
256
282
|
*
|
|
257
283
|
* - `"auto"` - Automatically detect system preference (light/dark mode)
|
|
258
284
|
* - `"light"` - Force light theme
|
package/dist/index.js
CHANGED
|
@@ -1,78 +1,18 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
WEB_COMPONENT_NOT_DEFINED: "Web component is not defined. Script may not be loaded.",
|
|
13
|
-
INVALID_JWT: "JWT token is required and must be a string."
|
|
14
|
-
}, a = /* @__PURE__ */ new Map();
|
|
15
|
-
function f(n) {
|
|
16
|
-
return u[n];
|
|
17
|
-
}
|
|
18
|
-
function h(n) {
|
|
19
|
-
return `${m}-${n}`;
|
|
20
|
-
}
|
|
21
|
-
function w(n) {
|
|
22
|
-
const t = h(n);
|
|
23
|
-
return !!document.getElementById(t);
|
|
24
|
-
}
|
|
25
|
-
async function D(n) {
|
|
26
|
-
const t = h(n);
|
|
27
|
-
if (w(n))
|
|
28
|
-
return Promise.resolve();
|
|
29
|
-
if (a.has(t))
|
|
30
|
-
return a.get(t);
|
|
31
|
-
const e = new Promise((i, s) => {
|
|
32
|
-
const o = document.createElement("script");
|
|
33
|
-
o.id = t, o.src = f(n), o.type = "module", o.async = !0, o.onload = () => {
|
|
34
|
-
setTimeout(() => {
|
|
35
|
-
customElements.get("connect-auth") ? i() : s(new Error(r.WEB_COMPONENT_NOT_DEFINED));
|
|
36
|
-
}, 0);
|
|
37
|
-
}, o.onerror = () => {
|
|
38
|
-
a.delete(t), s(new Error(`${r.SCRIPT_LOAD_FAILED} (${n})`));
|
|
39
|
-
}, document.head.appendChild(o);
|
|
40
|
-
});
|
|
41
|
-
a.set(t, e);
|
|
42
|
-
try {
|
|
43
|
-
await e;
|
|
44
|
-
} catch (i) {
|
|
45
|
-
throw a.delete(t), i;
|
|
46
|
-
}
|
|
47
|
-
return e;
|
|
48
|
-
}
|
|
49
|
-
async function N(n = 5e3) {
|
|
50
|
-
const t = "connect-auth";
|
|
51
|
-
return customElements.get(t) ? Promise.resolve() : new Promise((e, i) => {
|
|
52
|
-
const s = setTimeout(() => {
|
|
53
|
-
i(new Error(`Timeout waiting for ${t} to be defined`));
|
|
54
|
-
}, n);
|
|
55
|
-
customElements.whenDefined(t).then(() => {
|
|
56
|
-
clearTimeout(s), e();
|
|
57
|
-
}).catch((o) => {
|
|
58
|
-
clearTimeout(s), i(o);
|
|
59
|
-
});
|
|
60
|
-
});
|
|
61
|
-
}
|
|
62
|
-
class I {
|
|
63
|
-
/**
|
|
64
|
-
* Create a new Auth instance
|
|
65
|
-
* @param config - Configuration options for the Auth SDK
|
|
66
|
-
*/
|
|
67
|
-
constructor(t) {
|
|
68
|
-
d(this, "config");
|
|
69
|
-
d(this, "state");
|
|
70
|
-
if (!t.jwt || typeof t.jwt != "string")
|
|
71
|
-
throw new Error(r.INVALID_JWT);
|
|
1
|
+
var c = Object.defineProperty;
|
|
2
|
+
var h = (i, e, t) => e in i ? c(i, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : i[e] = t;
|
|
3
|
+
var n = (i, e, t) => h(i, typeof e != "symbol" ? e + "" : e, t);
|
|
4
|
+
const o = "production", l = "JWT token is required and must be a string.";
|
|
5
|
+
class p {
|
|
6
|
+
constructor(e) {
|
|
7
|
+
n(this, "config");
|
|
8
|
+
n(this, "state");
|
|
9
|
+
n(this, "scriptLoadingPromise");
|
|
10
|
+
if (!e.jwt || typeof e.jwt != "string")
|
|
11
|
+
throw new Error(l);
|
|
72
12
|
this.config = {
|
|
73
|
-
...
|
|
74
|
-
env:
|
|
75
|
-
theme:
|
|
13
|
+
...e,
|
|
14
|
+
env: e.env || o,
|
|
15
|
+
theme: e.theme
|
|
76
16
|
}, this.state = {
|
|
77
17
|
initialized: !1,
|
|
78
18
|
scriptLoaded: !1,
|
|
@@ -81,10 +21,105 @@ class I {
|
|
|
81
21
|
};
|
|
82
22
|
}
|
|
83
23
|
/**
|
|
84
|
-
*
|
|
24
|
+
* Render the widget to a container element
|
|
25
|
+
* @param container - The container element to render the widget into
|
|
26
|
+
* @returns Promise that resolves when the widget is rendered
|
|
27
|
+
*/
|
|
28
|
+
async render(e) {
|
|
29
|
+
if (!e || !(e instanceof HTMLElement))
|
|
30
|
+
throw new Error(this.errorMessages.INVALID_CONTAINER);
|
|
31
|
+
if (this.state.initialized)
|
|
32
|
+
throw new Error(this.errorMessages.ALREADY_RENDERED);
|
|
33
|
+
try {
|
|
34
|
+
await this.ensureScriptLoaded();
|
|
35
|
+
const t = this.createWebComponent();
|
|
36
|
+
e.innerHTML = "", e.appendChild(t), this.state.container = e, this.state.element = t, this.state.initialized = !0;
|
|
37
|
+
} catch (t) {
|
|
38
|
+
throw console.error("Failed to render widget:", t), t;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Update the configuration of the widget
|
|
43
|
+
* @param config - Partial configuration to update
|
|
44
|
+
* @returns void
|
|
45
|
+
*/
|
|
46
|
+
updateConfig(e) {
|
|
47
|
+
if (!this.state.initialized || !this.state.element)
|
|
48
|
+
throw new Error(this.errorMessages.NOT_RENDERED);
|
|
49
|
+
const t = this.state.element;
|
|
50
|
+
Object.entries(e).forEach(([r, s]) => {
|
|
51
|
+
s && (this.config[r] = s, t[r] = s);
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Destroy the widget and clean up resources
|
|
56
|
+
*/
|
|
57
|
+
destroy() {
|
|
58
|
+
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);
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Check if the widget is currently rendered
|
|
62
|
+
* @returns True if the widget is rendered, false otherwise
|
|
63
|
+
*/
|
|
64
|
+
isRendered() {
|
|
65
|
+
return this.state.initialized;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Get the current configuration
|
|
69
|
+
* @returns The current configuration object
|
|
85
70
|
*/
|
|
86
|
-
|
|
87
|
-
return this.config
|
|
71
|
+
getConfig() {
|
|
72
|
+
return { ...this.config };
|
|
73
|
+
}
|
|
74
|
+
getEnvironment() {
|
|
75
|
+
return this.config.env || o;
|
|
76
|
+
}
|
|
77
|
+
getScriptId() {
|
|
78
|
+
return `${this.webComponentTag}-script-${this.getEnvironment()}`;
|
|
79
|
+
}
|
|
80
|
+
isScriptLoaded() {
|
|
81
|
+
return !!document.getElementById(this.getScriptId());
|
|
82
|
+
}
|
|
83
|
+
getWebComponent() {
|
|
84
|
+
return customElements.get(this.webComponentTag);
|
|
85
|
+
}
|
|
86
|
+
getScriptUrl() {
|
|
87
|
+
return this.scriptUrls[this.getEnvironment()];
|
|
88
|
+
}
|
|
89
|
+
async loadScript() {
|
|
90
|
+
if (!this.isScriptLoaded()) {
|
|
91
|
+
if (this.scriptLoadingPromise)
|
|
92
|
+
return this.scriptLoadingPromise;
|
|
93
|
+
this.scriptLoadingPromise = new Promise((e, t) => {
|
|
94
|
+
const r = document.createElement("script");
|
|
95
|
+
r.id = this.getScriptId(), r.src = this.getScriptUrl(), r.type = "module", r.async = !0, r.onload = () => {
|
|
96
|
+
setTimeout(() => {
|
|
97
|
+
this.getWebComponent() ? e() : t(new Error(this.errorMessages.WEB_COMPONENT_NOT_DEFINED));
|
|
98
|
+
}, 0);
|
|
99
|
+
}, r.onerror = () => {
|
|
100
|
+
this.scriptLoadingPromise = void 0, t(new Error(`${this.errorMessages.SCRIPT_LOAD_FAILED} (${this.getEnvironment()})`));
|
|
101
|
+
}, document.head.appendChild(r);
|
|
102
|
+
});
|
|
103
|
+
try {
|
|
104
|
+
await this.scriptLoadingPromise;
|
|
105
|
+
} catch (e) {
|
|
106
|
+
throw this.scriptLoadingPromise = void 0, e;
|
|
107
|
+
}
|
|
108
|
+
return this.scriptLoadingPromise;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
async waitForWebComponent(e = 5e3) {
|
|
112
|
+
if (!this.getWebComponent())
|
|
113
|
+
return new Promise((t, r) => {
|
|
114
|
+
const s = setTimeout(() => {
|
|
115
|
+
r(new Error(`Timeout waiting for ${this.webComponentTag} to be defined`));
|
|
116
|
+
}, e);
|
|
117
|
+
customElements.whenDefined(this.webComponentTag).then(() => {
|
|
118
|
+
clearTimeout(s), t();
|
|
119
|
+
}).catch((d) => {
|
|
120
|
+
clearTimeout(s), r(d);
|
|
121
|
+
});
|
|
122
|
+
});
|
|
88
123
|
}
|
|
89
124
|
/**
|
|
90
125
|
* Load the web component script if not already loaded
|
|
@@ -92,68 +127,76 @@ class I {
|
|
|
92
127
|
async ensureScriptLoaded() {
|
|
93
128
|
if (!this.state.scriptLoaded)
|
|
94
129
|
try {
|
|
95
|
-
await
|
|
96
|
-
} catch (
|
|
97
|
-
throw console.error("Failed to load Connect
|
|
130
|
+
await this.loadScript(), await this.waitForWebComponent(), this.state.scriptLoaded = !0;
|
|
131
|
+
} catch (e) {
|
|
132
|
+
throw console.error("Failed to load Connect script:", e), e;
|
|
98
133
|
}
|
|
99
134
|
}
|
|
100
|
-
/**
|
|
101
|
-
* Create the web component element
|
|
102
|
-
*/
|
|
103
135
|
createWebComponent() {
|
|
104
|
-
const
|
|
105
|
-
return
|
|
136
|
+
const e = document.createElement(this.webComponentTag);
|
|
137
|
+
return Object.entries(this.config).forEach(([t, r]) => {
|
|
138
|
+
r && (e[t] = r);
|
|
139
|
+
}), e;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
var a;
|
|
143
|
+
(function(i) {
|
|
144
|
+
i.NETWORK_ERROR = "network_error", i.AUTH_ERROR = "auth_error", i.NOT_FOUND_ERROR = "not_found_error", i.VALIDATION_ERROR = "validation_error", i.SERVER_ERROR = "server_error", i.CLIENT_ERROR = "client_error", i.UNKNOWN_ERROR = "unknown_error";
|
|
145
|
+
})(a || (a = {}));
|
|
146
|
+
class u extends p {
|
|
147
|
+
constructor() {
|
|
148
|
+
super(...arguments);
|
|
149
|
+
n(this, "errorMessages", {
|
|
150
|
+
ALREADY_RENDERED: "Auth widget is already rendered. Call destroy() before rendering again.",
|
|
151
|
+
NOT_RENDERED: "Auth widget is not rendered. Call render() first.",
|
|
152
|
+
INVALID_CONTAINER: "Invalid container element provided.",
|
|
153
|
+
SCRIPT_LOAD_FAILED: "Failed to load the Connect Auth script.",
|
|
154
|
+
WEB_COMPONENT_NOT_DEFINED: "Web component is not defined. Script may not be loaded."
|
|
155
|
+
});
|
|
156
|
+
n(this, "scriptUrls", {
|
|
157
|
+
sandbox: "https://sdk.sandbox.connect.xyz/auth-web/index.js",
|
|
158
|
+
production: "https://sdk.connect.xyz/auth-web/index.js"
|
|
159
|
+
});
|
|
160
|
+
n(this, "webComponentTag", "connect-auth");
|
|
106
161
|
}
|
|
107
162
|
/**
|
|
108
163
|
* Render the Auth widget to a container element
|
|
109
164
|
* @param container - The container element to render the widget into
|
|
110
165
|
* @returns Promise that resolves when the widget is rendered
|
|
111
166
|
*/
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
throw new Error(r.INVALID_CONTAINER);
|
|
115
|
-
if (this.state.initialized)
|
|
116
|
-
throw new Error(r.ALREADY_RENDERED);
|
|
117
|
-
try {
|
|
118
|
-
await this.ensureScriptLoaded();
|
|
119
|
-
const e = this.createWebComponent();
|
|
120
|
-
t.innerHTML = "", t.appendChild(e), this.state.container = t, this.state.element = e, this.state.initialized = !0;
|
|
121
|
-
} catch (e) {
|
|
122
|
-
throw console.error("Failed to render Auth widget:", e), e;
|
|
123
|
-
}
|
|
167
|
+
render(t) {
|
|
168
|
+
return super.render(t);
|
|
124
169
|
}
|
|
125
170
|
/**
|
|
126
171
|
* Update the configuration of the Auth widget
|
|
127
172
|
* @param config - Partial configuration to update
|
|
128
173
|
*/
|
|
129
174
|
updateConfig(t) {
|
|
130
|
-
|
|
131
|
-
throw new Error(r.NOT_RENDERED);
|
|
132
|
-
const e = this.state.element;
|
|
133
|
-
t.jwt && (this.config.jwt = t.jwt, e.jwt = t.jwt), t.theme !== void 0 && (this.config.theme = t.theme, e.theme = t.theme), t.onError !== void 0 && (this.config.onError = t.onError, e.onError = t.onError), t.onClose !== void 0 && (this.config.onClose = t.onClose, e.onClose = t.onClose), t.onDeposit !== void 0 && (this.config.onDeposit = t.onDeposit, e.onDeposit = t.onDeposit), t.onEvent !== void 0 && (this.config.onEvent = t.onEvent, e.onEvent = t.onEvent);
|
|
175
|
+
return super.updateConfig(t);
|
|
134
176
|
}
|
|
135
177
|
/**
|
|
136
|
-
*
|
|
178
|
+
* Get the current configuration
|
|
179
|
+
* @returns The current configuration object
|
|
137
180
|
*/
|
|
138
|
-
|
|
139
|
-
|
|
181
|
+
getConfig() {
|
|
182
|
+
return super.getConfig();
|
|
140
183
|
}
|
|
141
184
|
/**
|
|
142
185
|
* Check if the Auth widget is currently rendered
|
|
143
186
|
* @returns True if the widget is rendered, false otherwise
|
|
144
187
|
*/
|
|
145
188
|
isRendered() {
|
|
146
|
-
return
|
|
189
|
+
return super.isRendered();
|
|
147
190
|
}
|
|
148
191
|
/**
|
|
149
|
-
*
|
|
150
|
-
* @returns The current configuration object
|
|
192
|
+
* Destroy the Auth widget and clean up resources
|
|
151
193
|
*/
|
|
152
|
-
|
|
153
|
-
return
|
|
194
|
+
destroy() {
|
|
195
|
+
return super.destroy();
|
|
154
196
|
}
|
|
155
197
|
}
|
|
156
198
|
export {
|
|
157
|
-
|
|
158
|
-
|
|
199
|
+
u as Auth,
|
|
200
|
+
a as ErrorCode,
|
|
201
|
+
u as default
|
|
159
202
|
};
|
package/dist/index.umd.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(n
|
|
1
|
+
(function(i,n){typeof exports=="object"&&typeof module<"u"?n(exports):typeof define=="function"&&define.amd?define(["exports"],n):(i=typeof globalThis<"u"?globalThis:i||self,n(i.Auth={}))})(this,function(i){"use strict";var l=Object.defineProperty;var m=(i,n,a)=>n in i?l(i,n,{enumerable:!0,configurable:!0,writable:!0,value:a}):i[n]=a;var o=(i,n,a)=>m(i,typeof n!="symbol"?n+"":n,a);const n="production",a="JWT token is required and must be a string.";class c{constructor(e){o(this,"config");o(this,"state");o(this,"scriptLoadingPromise");if(!e.jwt||typeof e.jwt!="string")throw new Error(a);this.config={...e,env:e.env||n,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 t=this.createWebComponent();e.innerHTML="",e.appendChild(t),this.state.container=e,this.state.element=t,this.state.initialized=!0}catch(t){throw console.error("Failed to render widget:",t),t}}updateConfig(e){if(!this.state.initialized||!this.state.element)throw new Error(this.errorMessages.NOT_RENDERED);const t=this.state.element;Object.entries(e).forEach(([r,d])=>{d&&(this.config[r]=d,t[r]=d)})}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||n}getScriptId(){return`${this.webComponentTag}-script-${this.getEnvironment()}`}isScriptLoaded(){return!!document.getElementById(this.getScriptId())}getWebComponent(){return customElements.get(this.webComponentTag)}getScriptUrl(){return this.scriptUrls[this.getEnvironment()]}async loadScript(){if(!this.isScriptLoaded()){if(this.scriptLoadingPromise)return this.scriptLoadingPromise;this.scriptLoadingPromise=new Promise((e,t)=>{const r=document.createElement("script");r.id=this.getScriptId(),r.src=this.getScriptUrl(),r.type="module",r.async=!0,r.onload=()=>{setTimeout(()=>{this.getWebComponent()?e():t(new Error(this.errorMessages.WEB_COMPONENT_NOT_DEFINED))},0)},r.onerror=()=>{this.scriptLoadingPromise=void 0,t(new Error(`${this.errorMessages.SCRIPT_LOAD_FAILED} (${this.getEnvironment()})`))},document.head.appendChild(r)});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((t,r)=>{const d=setTimeout(()=>{r(new Error(`Timeout waiting for ${this.webComponentTag} to be defined`))},e);customElements.whenDefined(this.webComponentTag).then(()=>{clearTimeout(d),t()}).catch(u=>{clearTimeout(d),r(u)})})}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(([t,r])=>{r&&(e[t]=r)}),e}}i.ErrorCode=void 0,function(s){s.NETWORK_ERROR="network_error",s.AUTH_ERROR="auth_error",s.NOT_FOUND_ERROR="not_found_error",s.VALIDATION_ERROR="validation_error",s.SERVER_ERROR="server_error",s.CLIENT_ERROR="client_error",s.UNKNOWN_ERROR="unknown_error"}(i.ErrorCode||(i.ErrorCode={}));class h extends c{constructor(){super(...arguments);o(this,"errorMessages",{ALREADY_RENDERED:"Auth widget is already rendered. Call destroy() before rendering again.",NOT_RENDERED:"Auth widget is not rendered. Call render() first.",INVALID_CONTAINER:"Invalid container element provided.",SCRIPT_LOAD_FAILED:"Failed to load the Connect Auth script.",WEB_COMPONENT_NOT_DEFINED:"Web component is not defined. Script may not be loaded."});o(this,"scriptUrls",{sandbox:"https://sdk.sandbox.connect.xyz/auth-web/index.js",production:"https://sdk.connect.xyz/auth-web/index.js"});o(this,"webComponentTag","connect-auth")}render(t){return super.render(t)}updateConfig(t){return super.updateConfig(t)}getConfig(){return super.getConfig()}isRendered(){return super.isRendered()}destroy(){return super.destroy()}}i.Auth=h,i.default=h,Object.defineProperties(i,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});
|