@zerohash-sdk/crypto-sell-js 1.2.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 +252 -0
- package/dist/index.d.ts +482 -0
- package/dist/index.js +200 -0
- package/dist/index.umd.cjs +1 -0
- package/package.json +26 -0
package/README.md
ADDED
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
# @zerohash-sdk/crypto-sell-js
|
|
2
|
+
|
|
3
|
+
A JavaScript SDK that enables frontend applications to seamlessly integrate with the Connect Crypto Sell product.
|
|
4
|
+
|
|
5
|
+
Connect Crypto Sell provides a secure, customizable flow for selling crypto assets directly within your application.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
### Via NPM (recommended)
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install @zerohash-sdk/crypto-sell-js
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
```javascript
|
|
16
|
+
import { CryptoSell } from '@zerohash-sdk/crypto-sell-js';
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
### Via CDN
|
|
20
|
+
|
|
21
|
+
You can import the script in your HTML file and use the `CryptoSell` class provided by it.
|
|
22
|
+
|
|
23
|
+
```html
|
|
24
|
+
<script
|
|
25
|
+
type="module"
|
|
26
|
+
src="https://sdk.connect.xyz/crypto-sell-web/index.js"
|
|
27
|
+
></script>
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Or import directly in your JavaScript code:
|
|
31
|
+
|
|
32
|
+
```javascript
|
|
33
|
+
import { CryptoSell } from 'https://sdk.connect.xyz/crypto-sell-web/index.js';
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## Getting Started
|
|
37
|
+
|
|
38
|
+
Follow these simple steps to integrate Connect Crypto Sell into your frontend application:
|
|
39
|
+
|
|
40
|
+
### 1. Import the CryptoSell module
|
|
41
|
+
|
|
42
|
+
```javascript
|
|
43
|
+
import { CryptoSell } from '@zerohash-sdk/crypto-sell-js';
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
### 1.1 Or import via CDN
|
|
47
|
+
|
|
48
|
+
```javascript
|
|
49
|
+
import { CryptoSell } from 'https://sdk.connect.xyz/crypto-sell-web/index.js';
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### 2. Initialize and render the widget
|
|
53
|
+
|
|
54
|
+
```javascript
|
|
55
|
+
// Create a CryptoSell instance with configuration
|
|
56
|
+
const cryptoSell = new CryptoSell({
|
|
57
|
+
jwt: 'your-jwt-token', // Obtain this from your backend
|
|
58
|
+
env: 'prod', // or 'cert' for testing
|
|
59
|
+
theme: 'auto', // 'auto' (default), 'light', or 'dark'
|
|
60
|
+
onCompleted: ({ amountSold, assetSymbol }) => {
|
|
61
|
+
console.log('Sell completed:', amountSold, assetSymbol);
|
|
62
|
+
},
|
|
63
|
+
onError: ({ errorCode, reason }) => {
|
|
64
|
+
console.error('Crypto sell error:', errorCode, 'Reason:', reason);
|
|
65
|
+
},
|
|
66
|
+
onClose: () => {
|
|
67
|
+
console.log('Crypto sell widget closed');
|
|
68
|
+
},
|
|
69
|
+
onEvent: ({ type, data }) => {
|
|
70
|
+
console.log('Event received:', type, data);
|
|
71
|
+
},
|
|
72
|
+
onLoaded: () => {
|
|
73
|
+
console.log('Crypto sell widget loaded and ready');
|
|
74
|
+
},
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
// Render the widget to a container element
|
|
78
|
+
const container = document.getElementById('crypto-sell-container');
|
|
79
|
+
await cryptoSell.render(container);
|
|
80
|
+
|
|
81
|
+
// Update configuration dynamically
|
|
82
|
+
cryptoSell.updateConfig({
|
|
83
|
+
jwt: 'new-jwt-token',
|
|
84
|
+
theme: 'dark',
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
// Check if rendered
|
|
88
|
+
if (cryptoSell.isRendered()) {
|
|
89
|
+
console.log('Widget is active');
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// Clean up when done
|
|
93
|
+
cryptoSell.destroy();
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
### 2.1 Using TypeScript (optional)
|
|
97
|
+
|
|
98
|
+
```typescript
|
|
99
|
+
import { CryptoSell, CryptoSellConfig } from '@zerohash-sdk/crypto-sell-js';
|
|
100
|
+
|
|
101
|
+
const config: CryptoSellConfig = {
|
|
102
|
+
jwt: 'your-jwt-token',
|
|
103
|
+
env: 'cert',
|
|
104
|
+
theme: 'dark',
|
|
105
|
+
onCompleted: ({ amountSold, assetSymbol }) => {
|
|
106
|
+
console.log(`Sold ${amountSold} ${assetSymbol}`);
|
|
107
|
+
},
|
|
108
|
+
onError: ({ errorCode, reason }) => {
|
|
109
|
+
console.error(errorCode, reason);
|
|
110
|
+
},
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
const cryptoSell = new CryptoSell(config);
|
|
114
|
+
await cryptoSell.render(document.getElementById('crypto-sell-container')!);
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
## API Reference
|
|
118
|
+
|
|
119
|
+
### Configuration
|
|
120
|
+
|
|
121
|
+
| Prop | Type | Required | Default | Description |
|
|
122
|
+
| ------------- | --------------------------------------- | -------- | -------- | -------------------------------------------------- |
|
|
123
|
+
| `jwt` | `string` | Yes | - | JWT token for authentication with Connect |
|
|
124
|
+
| `env` | `"prod" \| "cert" \| "dev" \| "local"` | No | `"prod"` | Target environment |
|
|
125
|
+
| `theme` | `"auto" \| "light" \| "dark"` | No | `"auto"` | Theme mode for the interface |
|
|
126
|
+
| `onCompleted` | `({ amountSold, assetSymbol }) => void` | No | - | Callback when the sell flow completes successfully |
|
|
127
|
+
| `onError` | `({ errorCode, reason }) => void` | No | - | Callback for error events |
|
|
128
|
+
| `onClose` | `() => void` | No | - | Callback when the widget is closed |
|
|
129
|
+
| `onEvent` | `({ type, data }) => void` | No | - | Callback for general events |
|
|
130
|
+
| `onLoaded` | `() => void` | No | - | Callback when the widget is loaded and ready |
|
|
131
|
+
|
|
132
|
+
### Constructor
|
|
133
|
+
|
|
134
|
+
```javascript
|
|
135
|
+
new CryptoSell(config: CryptoSellConfig)
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
Creates a new CryptoSell instance with the provided configuration.
|
|
139
|
+
|
|
140
|
+
### Methods
|
|
141
|
+
|
|
142
|
+
#### `render(container: HTMLElement): Promise<void>`
|
|
143
|
+
|
|
144
|
+
Renders the Crypto Sell widget to the specified container element.
|
|
145
|
+
|
|
146
|
+
```javascript
|
|
147
|
+
await cryptoSell.render(document.getElementById('crypto-sell-container'));
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
#### `updateConfig(config: Partial<CryptoSellConfig>): void`
|
|
151
|
+
|
|
152
|
+
Updates the configuration of an already rendered widget.
|
|
153
|
+
|
|
154
|
+
```javascript
|
|
155
|
+
cryptoSell.updateConfig({
|
|
156
|
+
jwt: 'new-token',
|
|
157
|
+
theme: 'dark',
|
|
158
|
+
});
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
#### `destroy(): void`
|
|
162
|
+
|
|
163
|
+
Removes the widget from the DOM and cleans up resources.
|
|
164
|
+
|
|
165
|
+
```javascript
|
|
166
|
+
cryptoSell.destroy();
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
#### `isRendered(): boolean`
|
|
170
|
+
|
|
171
|
+
Returns whether the widget is currently rendered.
|
|
172
|
+
|
|
173
|
+
```javascript
|
|
174
|
+
if (cryptoSell.isRendered()) {
|
|
175
|
+
// Widget is active
|
|
176
|
+
}
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
#### `getConfig(): CryptoSellConfig`
|
|
180
|
+
|
|
181
|
+
Returns a copy of the current configuration.
|
|
182
|
+
|
|
183
|
+
```javascript
|
|
184
|
+
const config = cryptoSell.getConfig();
|
|
185
|
+
console.log('Current JWT:', config.jwt);
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
## Callback Functions
|
|
189
|
+
|
|
190
|
+
### onCompleted
|
|
191
|
+
|
|
192
|
+
Called when the sell flow completes successfully.
|
|
193
|
+
|
|
194
|
+
```javascript
|
|
195
|
+
onCompleted: ({ amountSold, assetSymbol }) => {
|
|
196
|
+
// amountSold: Quantity sold as a string
|
|
197
|
+
// assetSymbol: Symbol of the asset sold (e.g. 'BTC.BITCOIN')
|
|
198
|
+
};
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
### onError
|
|
202
|
+
|
|
203
|
+
Called when an error occurs in the Crypto Sell widget.
|
|
204
|
+
|
|
205
|
+
```javascript
|
|
206
|
+
onError: ({ errorCode, reason }) => {
|
|
207
|
+
// errorCode: Error code ('network_error', 'auth_error', 'server_error', etc.)
|
|
208
|
+
// reason: Human-readable error description
|
|
209
|
+
};
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
### onClose
|
|
213
|
+
|
|
214
|
+
Called when the Crypto Sell widget is closed by the user.
|
|
215
|
+
|
|
216
|
+
```javascript
|
|
217
|
+
onClose: () => {
|
|
218
|
+
// Handle close event
|
|
219
|
+
};
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
### onEvent
|
|
223
|
+
|
|
224
|
+
Called for general events from the Crypto Sell widget.
|
|
225
|
+
|
|
226
|
+
```javascript
|
|
227
|
+
onEvent: ({ type, data }) => {
|
|
228
|
+
// type: Event type string
|
|
229
|
+
// data: Event-specific data object
|
|
230
|
+
};
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
### onLoaded
|
|
234
|
+
|
|
235
|
+
Called when the Crypto Sell widget has fully loaded and is ready for user interaction.
|
|
236
|
+
|
|
237
|
+
```javascript
|
|
238
|
+
onLoaded: () => {
|
|
239
|
+
// Widget is fully loaded and ready
|
|
240
|
+
};
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
## Browser Support
|
|
244
|
+
|
|
245
|
+
- Chrome/Edge 90+
|
|
246
|
+
- Firefox 88+
|
|
247
|
+
- Safari 14+
|
|
248
|
+
- All modern browsers with Web Components support
|
|
249
|
+
|
|
250
|
+
## More Information & Support
|
|
251
|
+
|
|
252
|
+
For comprehensive documentation or support about Connect, visit our [Documentation Page](https://docs.zerohash.com/).
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,482 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Zerohash Crypto Sell JavaScript SDK
|
|
3
|
+
*
|
|
4
|
+
* A programmatic JavaScript SDK for integrating the Crypto Sell widget
|
|
5
|
+
* into web applications without requiring HTML custom elements.
|
|
6
|
+
*
|
|
7
|
+
* @packageDocumentation
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Generic app event structure
|
|
12
|
+
* @template TType - Event type string
|
|
13
|
+
* @template TData - Event data payload
|
|
14
|
+
*/
|
|
15
|
+
declare type AppEvent<TType extends string = string, TData = Record<string, unknown>> = {
|
|
16
|
+
/** The type of event that occurred */
|
|
17
|
+
type: TType;
|
|
18
|
+
/** Data associated with the event */
|
|
19
|
+
data: TData;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Configuration options for the SDK
|
|
24
|
+
*/
|
|
25
|
+
declare interface BaseConfig<TEvent = AppEvent> extends CommonCallbacks<TEvent> {
|
|
26
|
+
/**
|
|
27
|
+
* JWT token used for authentication
|
|
28
|
+
*/
|
|
29
|
+
jwt: string;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Target environment
|
|
33
|
+
* @default 'production'
|
|
34
|
+
*/
|
|
35
|
+
env?: Environment;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Theme mode
|
|
39
|
+
* @default 'auto'
|
|
40
|
+
*
|
|
41
|
+
* Available themes:
|
|
42
|
+
* - `'auto'` - Automatically detect system preference (light/dark mode)
|
|
43
|
+
* - `'light'` - Force light theme
|
|
44
|
+
* - `'dark'` - Force dark theme
|
|
45
|
+
*/
|
|
46
|
+
theme?: Theme;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
declare type BaseErrorMessageKeys =
|
|
50
|
+
| 'ALREADY_RENDERED'
|
|
51
|
+
| 'NOT_RENDERED'
|
|
52
|
+
| 'INVALID_CONTAINER'
|
|
53
|
+
| 'SCRIPT_LOAD_FAILED'
|
|
54
|
+
| 'WEB_COMPONENT_NOT_DEFINED';
|
|
55
|
+
|
|
56
|
+
declare abstract class BaseJsSdk<Config extends BaseConfig<never> = BaseConfig> {
|
|
57
|
+
private config: Config;
|
|
58
|
+
private state: SdkState;
|
|
59
|
+
private scriptLoadingPromise?: Promise<void>;
|
|
60
|
+
|
|
61
|
+
protected abstract errorMessages: Record<BaseErrorMessageKeys, string>;
|
|
62
|
+
protected abstract scriptUrls: Record<string, string>;
|
|
63
|
+
protected abstract webComponentTag: string;
|
|
64
|
+
|
|
65
|
+
constructor(config: Config) {
|
|
66
|
+
if (!config.jwt || typeof config.jwt !== 'string') {
|
|
67
|
+
throw new Error(INVALID_JWT_ERROR_MESSAGE);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
this.config = {
|
|
71
|
+
...config,
|
|
72
|
+
env: config.env || DEFAULT_ENVIRONMENT,
|
|
73
|
+
theme: config.theme,
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
this.state = {
|
|
77
|
+
initialized: false,
|
|
78
|
+
scriptLoaded: false,
|
|
79
|
+
container: null,
|
|
80
|
+
element: null,
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Render the widget to a container element
|
|
86
|
+
* @param container - The container element to render the widget into
|
|
87
|
+
* @returns Promise that resolves when the widget is rendered
|
|
88
|
+
*/
|
|
89
|
+
async render(container: HTMLElement): Promise<void> {
|
|
90
|
+
// Validate container
|
|
91
|
+
if (!container || !(container instanceof HTMLElement)) {
|
|
92
|
+
throw new Error(this.errorMessages.INVALID_CONTAINER);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// Check if already rendered
|
|
96
|
+
if (this.state.initialized) {
|
|
97
|
+
throw new Error(this.errorMessages.ALREADY_RENDERED);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
try {
|
|
101
|
+
// Ensure script is loaded
|
|
102
|
+
await this.ensureScriptLoaded();
|
|
103
|
+
|
|
104
|
+
// Create and append the web component
|
|
105
|
+
const element = this.createWebComponent();
|
|
106
|
+
|
|
107
|
+
// Clear the container and append the element
|
|
108
|
+
container.innerHTML = '';
|
|
109
|
+
container.appendChild(element);
|
|
110
|
+
|
|
111
|
+
// Update state
|
|
112
|
+
this.state.container = container;
|
|
113
|
+
this.state.element = element;
|
|
114
|
+
this.state.initialized = true;
|
|
115
|
+
} catch (error) {
|
|
116
|
+
console.error('Failed to render widget:', error);
|
|
117
|
+
throw error;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Update the configuration of the widget
|
|
123
|
+
* @param config - Partial configuration to update
|
|
124
|
+
* @returns void
|
|
125
|
+
*/
|
|
126
|
+
updateConfig(config: Partial<Config>): void {
|
|
127
|
+
if (!this.state.initialized || !this.state.element) {
|
|
128
|
+
throw new Error(this.errorMessages.NOT_RENDERED);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const element = this.state.element as SdkElement<Config>;
|
|
132
|
+
|
|
133
|
+
Object.entries(config).forEach(([key, value]) => {
|
|
134
|
+
if (value) {
|
|
135
|
+
this.config[key as keyof Config] = value;
|
|
136
|
+
element[key as keyof Config] = value;
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Destroy the widget and clean up resources
|
|
143
|
+
*/
|
|
144
|
+
destroy(): void {
|
|
145
|
+
if (!this.state.initialized) {
|
|
146
|
+
return; // Nothing to destroy
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// Remove the element from the container
|
|
150
|
+
if (this.state.element && this.state.element.parentNode) {
|
|
151
|
+
this.state.element.parentNode.removeChild(this.state.element);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// Clear the container
|
|
155
|
+
if (this.state.container) {
|
|
156
|
+
this.state.container.innerHTML = '';
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// Reset state
|
|
160
|
+
this.state.container = null;
|
|
161
|
+
this.state.element = null;
|
|
162
|
+
this.state.initialized = false;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Check if the widget is currently rendered
|
|
167
|
+
* @returns True if the widget is rendered, false otherwise
|
|
168
|
+
*/
|
|
169
|
+
isRendered(): boolean {
|
|
170
|
+
return this.state.initialized;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Get the current configuration
|
|
175
|
+
* @returns The current configuration object
|
|
176
|
+
*/
|
|
177
|
+
getConfig(): Readonly<Config> {
|
|
178
|
+
return { ...this.config };
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
private getEnvironment(): Environment {
|
|
182
|
+
return this.config.env || DEFAULT_ENVIRONMENT;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
private getScriptId() {
|
|
186
|
+
return `${this.webComponentTag}-script-${this.getEnvironment()}`;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
private isScriptLoaded() {
|
|
190
|
+
return !!document.getElementById(this.getScriptId());
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
private getWebComponent() {
|
|
194
|
+
return customElements.get(this.webComponentTag);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
private getScriptUrl() {
|
|
198
|
+
// Support local development with Vite
|
|
199
|
+
if (typeof import.meta !== 'undefined' && import.meta.env?.['VITE_INTERNAL_BUILD'] === 'true') {
|
|
200
|
+
return import.meta.env['VITE_SCRIPT_URL'] || this.scriptUrls[this.getEnvironment()];
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
return this.scriptUrls[this.getEnvironment()];
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
private async loadScript() {
|
|
207
|
+
if (this.isScriptLoaded()) {
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
if (this.scriptLoadingPromise) {
|
|
212
|
+
return this.scriptLoadingPromise;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
this.scriptLoadingPromise = new Promise<void>((resolve, reject) => {
|
|
216
|
+
const script = document.createElement('script');
|
|
217
|
+
script.id = this.getScriptId();
|
|
218
|
+
script.src = this.getScriptUrl();
|
|
219
|
+
script.type = 'module';
|
|
220
|
+
script.async = true;
|
|
221
|
+
|
|
222
|
+
script.onload = () => {
|
|
223
|
+
setTimeout(() => {
|
|
224
|
+
if (this.getWebComponent()) {
|
|
225
|
+
resolve();
|
|
226
|
+
} else {
|
|
227
|
+
reject(new Error(this.errorMessages.WEB_COMPONENT_NOT_DEFINED));
|
|
228
|
+
}
|
|
229
|
+
}, 0);
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
script.onerror = () => {
|
|
233
|
+
this.scriptLoadingPromise = undefined;
|
|
234
|
+
reject(new Error(`${this.errorMessages.SCRIPT_LOAD_FAILED} (${this.getEnvironment()})`));
|
|
235
|
+
};
|
|
236
|
+
|
|
237
|
+
document.head.appendChild(script);
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
try {
|
|
241
|
+
await this.scriptLoadingPromise;
|
|
242
|
+
} catch (error) {
|
|
243
|
+
this.scriptLoadingPromise = undefined;
|
|
244
|
+
throw error;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
return this.scriptLoadingPromise;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
private async waitForWebComponent(timeout = 5000) {
|
|
251
|
+
if (this.getWebComponent()) {
|
|
252
|
+
return;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
return new Promise<void>((resolve, reject) => {
|
|
256
|
+
const timeoutId = setTimeout(() => {
|
|
257
|
+
reject(new Error(`Timeout waiting for ${this.webComponentTag} to be defined`));
|
|
258
|
+
}, timeout);
|
|
259
|
+
|
|
260
|
+
customElements
|
|
261
|
+
.whenDefined(this.webComponentTag)
|
|
262
|
+
.then(() => {
|
|
263
|
+
clearTimeout(timeoutId);
|
|
264
|
+
resolve();
|
|
265
|
+
})
|
|
266
|
+
.catch((error) => {
|
|
267
|
+
clearTimeout(timeoutId);
|
|
268
|
+
reject(error);
|
|
269
|
+
});
|
|
270
|
+
});
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* Load the web component script if not already loaded
|
|
275
|
+
*/
|
|
276
|
+
protected async ensureScriptLoaded(): Promise<void> {
|
|
277
|
+
if (this.state.scriptLoaded) {
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
try {
|
|
282
|
+
await this.loadScript();
|
|
283
|
+
await this.waitForWebComponent();
|
|
284
|
+
this.state.scriptLoaded = true;
|
|
285
|
+
} catch (error) {
|
|
286
|
+
console.error('Failed to load Connect script:', error);
|
|
287
|
+
throw error;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
private createWebComponent() {
|
|
292
|
+
const element = document.createElement(this.webComponentTag) as SdkElement<Config>;
|
|
293
|
+
|
|
294
|
+
Object.entries(this.config).forEach(([key, value]) => {
|
|
295
|
+
if (value) {
|
|
296
|
+
element[key as keyof Config] = value;
|
|
297
|
+
}
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
return element;
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
* Common callback function types shared across all Connect apps
|
|
306
|
+
* @template TEvent - The event type for onEvent callback
|
|
307
|
+
*/
|
|
308
|
+
declare type CommonCallbacks<TEvent = AppEvent> = {
|
|
309
|
+
/** Called when an error occurs */
|
|
310
|
+
onError?: (error: ErrorPayload) => void;
|
|
311
|
+
/** Called when the component is closed */
|
|
312
|
+
onClose?: () => void;
|
|
313
|
+
/** Called when a general event occurs */
|
|
314
|
+
onEvent?: (event: TEvent) => void;
|
|
315
|
+
/** Called when the widget has loaded and is ready */
|
|
316
|
+
onLoaded?: () => void;
|
|
317
|
+
};
|
|
318
|
+
|
|
319
|
+
/**
|
|
320
|
+
* CryptoSell SDK class for programmatic control of the Connect Crypto Sell widget.
|
|
321
|
+
*
|
|
322
|
+
* Renders the `zerohash-crypto-sell` web component, which handles the iframe
|
|
323
|
+
* and postMessage communication internally.
|
|
324
|
+
*
|
|
325
|
+
* @example
|
|
326
|
+
* ```javascript
|
|
327
|
+
* const cryptoSell = new CryptoSell({
|
|
328
|
+
* jwt: 'your-jwt-token',
|
|
329
|
+
* env: 'prod',
|
|
330
|
+
* theme: 'auto',
|
|
331
|
+
* onCompleted: ({ amountSold, assetSymbol }) => console.log('Sold', amountSold, assetSymbol),
|
|
332
|
+
* onClose: () => console.log('Closed'),
|
|
333
|
+
* onError: ({ errorCode, reason }) => console.error(errorCode, reason),
|
|
334
|
+
* });
|
|
335
|
+
*
|
|
336
|
+
* // Render to a container
|
|
337
|
+
* await cryptoSell.render(document.getElementById('crypto-sell-container'));
|
|
338
|
+
*
|
|
339
|
+
* // Update configuration
|
|
340
|
+
* cryptoSell.updateConfig({ jwt: 'new-token', theme: 'dark' });
|
|
341
|
+
*
|
|
342
|
+
* // Clean up
|
|
343
|
+
* cryptoSell.destroy();
|
|
344
|
+
* ```
|
|
345
|
+
*/
|
|
346
|
+
declare class CryptoSell extends BaseJsSdk<CryptoSellConfig> {
|
|
347
|
+
protected errorMessages: {
|
|
348
|
+
ALREADY_RENDERED: string;
|
|
349
|
+
NOT_RENDERED: string;
|
|
350
|
+
INVALID_CONTAINER: string;
|
|
351
|
+
SCRIPT_LOAD_FAILED: string;
|
|
352
|
+
WEB_COMPONENT_NOT_DEFINED: string;
|
|
353
|
+
};
|
|
354
|
+
protected scriptUrls: {
|
|
355
|
+
local: string;
|
|
356
|
+
dev: string;
|
|
357
|
+
cert: string;
|
|
358
|
+
prod: string;
|
|
359
|
+
sandbox: string;
|
|
360
|
+
production: string;
|
|
361
|
+
};
|
|
362
|
+
protected webComponentTag: string;
|
|
363
|
+
/**
|
|
364
|
+
* Render the CryptoSell widget to a container element
|
|
365
|
+
* @param container - The container element to render the widget into
|
|
366
|
+
* @returns Promise that resolves when the widget is rendered
|
|
367
|
+
*/
|
|
368
|
+
render(container: HTMLElement): Promise<void>;
|
|
369
|
+
/**
|
|
370
|
+
* Update the configuration of the CryptoSell widget
|
|
371
|
+
* @param config - Partial configuration to update
|
|
372
|
+
*/
|
|
373
|
+
updateConfig(config: Partial<CryptoSellConfig>): void;
|
|
374
|
+
/**
|
|
375
|
+
* Get the current configuration
|
|
376
|
+
* @returns The current configuration object
|
|
377
|
+
*/
|
|
378
|
+
getConfig(): Readonly<CryptoSellConfig>;
|
|
379
|
+
/**
|
|
380
|
+
* Check if the CryptoSell widget is currently rendered
|
|
381
|
+
* @returns True if the widget is rendered, false otherwise
|
|
382
|
+
*/
|
|
383
|
+
isRendered(): boolean;
|
|
384
|
+
/**
|
|
385
|
+
* Destroy the CryptoSell widget and clean up resources
|
|
386
|
+
*/
|
|
387
|
+
destroy(): void;
|
|
388
|
+
}
|
|
389
|
+
export { CryptoSell }
|
|
390
|
+
export default CryptoSell;
|
|
391
|
+
|
|
392
|
+
declare type CryptoSellCallbacks = CommonCallbacks<CryptoSellEvent> & {
|
|
393
|
+
/** Called when the sell is successfully executed */
|
|
394
|
+
onCompleted?: (data: CryptoSellCompletedData) => void;
|
|
395
|
+
};
|
|
396
|
+
|
|
397
|
+
declare type CryptoSellCompletedData = {
|
|
398
|
+
/** Symbol of the asset that was sold (e.g. 'BTC.BITCOIN') */
|
|
399
|
+
assetSymbol: string;
|
|
400
|
+
/** Quantity sold as a string */
|
|
401
|
+
amountSold: string;
|
|
402
|
+
};
|
|
403
|
+
|
|
404
|
+
export declare interface CryptoSellConfig extends BaseConfig<CryptoSellEvent>, CryptoSellCallbacks {
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
/**
|
|
408
|
+
* Crypto sell event structure
|
|
409
|
+
*/
|
|
410
|
+
declare type CryptoSellEvent = AppEvent<CryptoSellEventType>;
|
|
411
|
+
|
|
412
|
+
/**
|
|
413
|
+
* Crypto sell event type literals
|
|
414
|
+
*/
|
|
415
|
+
declare type CryptoSellEventType = string;
|
|
416
|
+
|
|
417
|
+
/**
|
|
418
|
+
* Environment configuration for the SDK
|
|
419
|
+
*/
|
|
420
|
+
export declare type Environment = 'local' | 'dev' | 'cert' | 'prod' | 'sandbox' | 'production';
|
|
421
|
+
|
|
422
|
+
/**
|
|
423
|
+
* Generic error codes for all Connect applications
|
|
424
|
+
*/
|
|
425
|
+
export declare enum ErrorCode {
|
|
426
|
+
/** Network connectivity error */
|
|
427
|
+
NETWORK_ERROR = 'network_error',
|
|
428
|
+
/** Authentication or session expired error */
|
|
429
|
+
AUTH_ERROR = 'auth_error',
|
|
430
|
+
/** Resource not found error */
|
|
431
|
+
NOT_FOUND_ERROR = 'not_found_error',
|
|
432
|
+
/** Validation error from user input */
|
|
433
|
+
VALIDATION_ERROR = 'validation_error',
|
|
434
|
+
/** Server error (5xx) */
|
|
435
|
+
SERVER_ERROR = 'server_error',
|
|
436
|
+
/** Client error (4xx) */
|
|
437
|
+
CLIENT_ERROR = 'client_error',
|
|
438
|
+
/** Unknown or unexpected error */
|
|
439
|
+
UNKNOWN_ERROR = 'unknown_error',
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
/**
|
|
443
|
+
* Generic error payload structure for error callbacks
|
|
444
|
+
*/
|
|
445
|
+
declare type ErrorPayload = {
|
|
446
|
+
/** Error code indicating the type of error */
|
|
447
|
+
errorCode: ErrorCode;
|
|
448
|
+
/** Human-readable reason for the error */
|
|
449
|
+
reason: string;
|
|
450
|
+
};
|
|
451
|
+
|
|
452
|
+
/**
|
|
453
|
+
* Generic HTMLElement representing a web component with custom config
|
|
454
|
+
*/
|
|
455
|
+
declare type SdkElement<Config extends BaseConfig<never>> = HTMLElement & Config;
|
|
456
|
+
|
|
457
|
+
/**
|
|
458
|
+
* Internal state of the SDK
|
|
459
|
+
*/
|
|
460
|
+
declare interface SdkState {
|
|
461
|
+
/** Whether the SDK is initialized */
|
|
462
|
+
initialized: boolean;
|
|
463
|
+
/** Whether the web component script is loaded */
|
|
464
|
+
scriptLoaded: boolean;
|
|
465
|
+
/** The container element where the widget is rendered */
|
|
466
|
+
container: HTMLElement | null;
|
|
467
|
+
/** The web component element */
|
|
468
|
+
element: HTMLElement | null;
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
/**
|
|
472
|
+
* Theme configuration for the SDK
|
|
473
|
+
*
|
|
474
|
+
* - `"auto"` - Automatically detect system preference (light/dark mode)
|
|
475
|
+
* - `"light"` - Force light theme
|
|
476
|
+
* - `"dark"` - Force dark theme
|
|
477
|
+
*/
|
|
478
|
+
declare type Theme = 'auto' | 'light' | 'dark';
|
|
479
|
+
|
|
480
|
+
export declare type ZerohashCryptoSellElement = SdkElement<CryptoSellConfig>;
|
|
481
|
+
|
|
482
|
+
export { }
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
const s = "production", d = "JWT token is required and must be a string.";
|
|
2
|
+
class c {
|
|
3
|
+
config;
|
|
4
|
+
state;
|
|
5
|
+
scriptLoadingPromise;
|
|
6
|
+
constructor(e) {
|
|
7
|
+
if (!e.jwt || typeof e.jwt != "string")
|
|
8
|
+
throw new Error(d);
|
|
9
|
+
this.config = {
|
|
10
|
+
...e,
|
|
11
|
+
env: e.env || s,
|
|
12
|
+
theme: e.theme
|
|
13
|
+
}, this.state = {
|
|
14
|
+
initialized: !1,
|
|
15
|
+
scriptLoaded: !1,
|
|
16
|
+
container: null,
|
|
17
|
+
element: null
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Render the widget to a container element
|
|
22
|
+
* @param container - The container element to render the widget into
|
|
23
|
+
* @returns Promise that resolves when the widget is rendered
|
|
24
|
+
*/
|
|
25
|
+
async render(e) {
|
|
26
|
+
if (!e || !(e instanceof HTMLElement))
|
|
27
|
+
throw new Error(this.errorMessages.INVALID_CONTAINER);
|
|
28
|
+
if (this.state.initialized)
|
|
29
|
+
throw new Error(this.errorMessages.ALREADY_RENDERED);
|
|
30
|
+
try {
|
|
31
|
+
await this.ensureScriptLoaded();
|
|
32
|
+
const r = this.createWebComponent();
|
|
33
|
+
e.innerHTML = "", e.appendChild(r), this.state.container = e, this.state.element = r, this.state.initialized = !0;
|
|
34
|
+
} catch (r) {
|
|
35
|
+
throw console.error("Failed to render widget:", r), r;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Update the configuration of the widget
|
|
40
|
+
* @param config - Partial configuration to update
|
|
41
|
+
* @returns void
|
|
42
|
+
*/
|
|
43
|
+
updateConfig(e) {
|
|
44
|
+
if (!this.state.initialized || !this.state.element)
|
|
45
|
+
throw new Error(this.errorMessages.NOT_RENDERED);
|
|
46
|
+
const r = this.state.element;
|
|
47
|
+
Object.entries(e).forEach(([t, n]) => {
|
|
48
|
+
n && (this.config[t] = n, r[t] = n);
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Destroy the widget and clean up resources
|
|
53
|
+
*/
|
|
54
|
+
destroy() {
|
|
55
|
+
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);
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Check if the widget is currently rendered
|
|
59
|
+
* @returns True if the widget is rendered, false otherwise
|
|
60
|
+
*/
|
|
61
|
+
isRendered() {
|
|
62
|
+
return this.state.initialized;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Get the current configuration
|
|
66
|
+
* @returns The current configuration object
|
|
67
|
+
*/
|
|
68
|
+
getConfig() {
|
|
69
|
+
return { ...this.config };
|
|
70
|
+
}
|
|
71
|
+
getEnvironment() {
|
|
72
|
+
return this.config.env || s;
|
|
73
|
+
}
|
|
74
|
+
getScriptId() {
|
|
75
|
+
return `${this.webComponentTag}-script-${this.getEnvironment()}`;
|
|
76
|
+
}
|
|
77
|
+
isScriptLoaded() {
|
|
78
|
+
return !!document.getElementById(this.getScriptId());
|
|
79
|
+
}
|
|
80
|
+
getWebComponent() {
|
|
81
|
+
return customElements.get(this.webComponentTag);
|
|
82
|
+
}
|
|
83
|
+
getScriptUrl() {
|
|
84
|
+
return this.scriptUrls[this.getEnvironment()];
|
|
85
|
+
}
|
|
86
|
+
async loadScript() {
|
|
87
|
+
if (!this.isScriptLoaded()) {
|
|
88
|
+
if (this.scriptLoadingPromise)
|
|
89
|
+
return this.scriptLoadingPromise;
|
|
90
|
+
this.scriptLoadingPromise = new Promise((e, r) => {
|
|
91
|
+
const t = document.createElement("script");
|
|
92
|
+
t.id = this.getScriptId(), t.src = this.getScriptUrl(), t.type = "module", t.async = !0, t.onload = () => {
|
|
93
|
+
setTimeout(() => {
|
|
94
|
+
this.getWebComponent() ? e() : r(new Error(this.errorMessages.WEB_COMPONENT_NOT_DEFINED));
|
|
95
|
+
}, 0);
|
|
96
|
+
}, t.onerror = () => {
|
|
97
|
+
this.scriptLoadingPromise = void 0, r(new Error(`${this.errorMessages.SCRIPT_LOAD_FAILED} (${this.getEnvironment()})`));
|
|
98
|
+
}, document.head.appendChild(t);
|
|
99
|
+
});
|
|
100
|
+
try {
|
|
101
|
+
await this.scriptLoadingPromise;
|
|
102
|
+
} catch (e) {
|
|
103
|
+
throw this.scriptLoadingPromise = void 0, e;
|
|
104
|
+
}
|
|
105
|
+
return this.scriptLoadingPromise;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
async waitForWebComponent(e = 5e3) {
|
|
109
|
+
if (!this.getWebComponent())
|
|
110
|
+
return new Promise((r, t) => {
|
|
111
|
+
const n = setTimeout(() => {
|
|
112
|
+
t(new Error(`Timeout waiting for ${this.webComponentTag} to be defined`));
|
|
113
|
+
}, e);
|
|
114
|
+
customElements.whenDefined(this.webComponentTag).then(() => {
|
|
115
|
+
clearTimeout(n), r();
|
|
116
|
+
}).catch((a) => {
|
|
117
|
+
clearTimeout(n), t(a);
|
|
118
|
+
});
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Load the web component script if not already loaded
|
|
123
|
+
*/
|
|
124
|
+
async ensureScriptLoaded() {
|
|
125
|
+
if (!this.state.scriptLoaded)
|
|
126
|
+
try {
|
|
127
|
+
await this.loadScript(), await this.waitForWebComponent(), this.state.scriptLoaded = !0;
|
|
128
|
+
} catch (e) {
|
|
129
|
+
throw console.error("Failed to load Connect script:", e), e;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
createWebComponent() {
|
|
133
|
+
const e = document.createElement(this.webComponentTag);
|
|
134
|
+
return Object.entries(this.config).forEach(([r, t]) => {
|
|
135
|
+
t && (e[r] = t);
|
|
136
|
+
}), e;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
var o;
|
|
140
|
+
(function(i) {
|
|
141
|
+
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";
|
|
142
|
+
})(o || (o = {}));
|
|
143
|
+
class l extends c {
|
|
144
|
+
errorMessages = {
|
|
145
|
+
ALREADY_RENDERED: "CryptoSell widget is already rendered. Call destroy() before rendering again.",
|
|
146
|
+
NOT_RENDERED: "CryptoSell widget is not rendered. Call render() first.",
|
|
147
|
+
INVALID_CONTAINER: "Invalid container element provided.",
|
|
148
|
+
SCRIPT_LOAD_FAILED: "Failed to load the Connect CryptoSell script.",
|
|
149
|
+
WEB_COMPONENT_NOT_DEFINED: "Web component is not defined. Script may not be loaded."
|
|
150
|
+
};
|
|
151
|
+
scriptUrls = {
|
|
152
|
+
local: "http://localhost:5173/crypto-sell-web/index.js",
|
|
153
|
+
dev: "https://connect-sdk.dev.0hash.com/crypto-sell-web/index.js",
|
|
154
|
+
cert: "https://sdk.sandbox.connect.xyz/crypto-sell-web/index.js",
|
|
155
|
+
prod: "https://sdk.connect.xyz/crypto-sell-web/index.js",
|
|
156
|
+
sandbox: "https://sdk.sandbox.connect.xyz/crypto-sell-web/index.js",
|
|
157
|
+
production: "https://sdk.connect.xyz/crypto-sell-web/index.js"
|
|
158
|
+
};
|
|
159
|
+
webComponentTag = "zerohash-crypto-sell";
|
|
160
|
+
/**
|
|
161
|
+
* Render the CryptoSell widget to a container element
|
|
162
|
+
* @param container - The container element to render the widget into
|
|
163
|
+
* @returns Promise that resolves when the widget is rendered
|
|
164
|
+
*/
|
|
165
|
+
render(e) {
|
|
166
|
+
return super.render(e);
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Update the configuration of the CryptoSell widget
|
|
170
|
+
* @param config - Partial configuration to update
|
|
171
|
+
*/
|
|
172
|
+
updateConfig(e) {
|
|
173
|
+
return super.updateConfig(e);
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Get the current configuration
|
|
177
|
+
* @returns The current configuration object
|
|
178
|
+
*/
|
|
179
|
+
getConfig() {
|
|
180
|
+
return super.getConfig();
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Check if the CryptoSell widget is currently rendered
|
|
184
|
+
* @returns True if the widget is rendered, false otherwise
|
|
185
|
+
*/
|
|
186
|
+
isRendered() {
|
|
187
|
+
return super.isRendered();
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Destroy the CryptoSell widget and clean up resources
|
|
191
|
+
*/
|
|
192
|
+
destroy() {
|
|
193
|
+
return super.destroy();
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
export {
|
|
197
|
+
l as CryptoSell,
|
|
198
|
+
o as ErrorCode,
|
|
199
|
+
l as default
|
|
200
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
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.CryptoSell={}))})(this,(function(i){"use strict";const s="production",a="JWT token is required and must be a string.";class c{config;state;scriptLoadingPromise;constructor(e){if(!e.jwt||typeof e.jwt!="string")throw new Error(a);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(){return 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(l=>{clearTimeout(o),t(l)})})}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 d extends c{errorMessages={ALREADY_RENDERED:"CryptoSell widget is already rendered. Call destroy() before rendering again.",NOT_RENDERED:"CryptoSell widget is not rendered. Call render() first.",INVALID_CONTAINER:"Invalid container element provided.",SCRIPT_LOAD_FAILED:"Failed to load the Connect CryptoSell script.",WEB_COMPONENT_NOT_DEFINED:"Web component is not defined. Script may not be loaded."};scriptUrls={local:"http://localhost:5173/crypto-sell-web/index.js",dev:"https://connect-sdk.dev.0hash.com/crypto-sell-web/index.js",cert:"https://sdk.sandbox.connect.xyz/crypto-sell-web/index.js",prod:"https://sdk.connect.xyz/crypto-sell-web/index.js",sandbox:"https://sdk.sandbox.connect.xyz/crypto-sell-web/index.js",production:"https://sdk.connect.xyz/crypto-sell-web/index.js"};webComponentTag="zerohash-crypto-sell";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.CryptoSell=d,i.default=d,Object.defineProperties(i,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}));
|
package/package.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@zerohash-sdk/crypto-sell-js",
|
|
3
|
+
"version": "1.2.0",
|
|
4
|
+
"private": false,
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"license": "MIT",
|
|
10
|
+
"exports": {
|
|
11
|
+
"./package.json": "./package.json",
|
|
12
|
+
".": {
|
|
13
|
+
"types": "./dist/index.d.ts",
|
|
14
|
+
"import": "./dist/index.js",
|
|
15
|
+
"require": "./dist/index.umd.js",
|
|
16
|
+
"default": "./dist/index.js"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"dist",
|
|
21
|
+
"!**/*.tsbuildinfo"
|
|
22
|
+
],
|
|
23
|
+
"nx": {
|
|
24
|
+
"name": "crypto-sell-js"
|
|
25
|
+
}
|
|
26
|
+
}
|