@zerohash-sdk/crypto-buy-react 1.1.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 +118 -0
- package/dist/index.d.ts +235 -0
- package/dist/index.js +40 -0
- package/package.json +29 -0
package/README.md
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
# @zerohash-sdk/crypto-buy-react
|
|
2
|
+
|
|
3
|
+
A React SDK that enables frontend React applications to seamlessly integrate with the Connect Crypto Buy product.
|
|
4
|
+
|
|
5
|
+
Connect Crypto Buy provides a secure, customizable flow for purchasing crypto assets directly within your application.
|
|
6
|
+
|
|
7
|
+
## Requirements
|
|
8
|
+
|
|
9
|
+
- React 18.0.0 or higher
|
|
10
|
+
- React DOM 18.0.0 or higher
|
|
11
|
+
|
|
12
|
+
## Installation
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
npm install @zerohash-sdk/crypto-buy-react
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Getting Started
|
|
19
|
+
|
|
20
|
+
### 1. Import the CryptoBuy Component
|
|
21
|
+
|
|
22
|
+
```tsx
|
|
23
|
+
import { CryptoBuy } from '@zerohash-sdk/crypto-buy-react';
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
### 2. Add the CryptoBuy Component to Your App
|
|
27
|
+
|
|
28
|
+
```tsx
|
|
29
|
+
function App() {
|
|
30
|
+
const jwt = 'your-jwt-token'; // Obtain this from your backend
|
|
31
|
+
|
|
32
|
+
return (
|
|
33
|
+
<CryptoBuy
|
|
34
|
+
jwt={jwt}
|
|
35
|
+
env="prod" // or "cert" for testing
|
|
36
|
+
theme="auto" // 'auto' (default), 'light', or 'dark'
|
|
37
|
+
/>
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### 3. Configure Event Callbacks (Optional)
|
|
43
|
+
|
|
44
|
+
```tsx
|
|
45
|
+
function App() {
|
|
46
|
+
const jwt = 'your-jwt-token';
|
|
47
|
+
|
|
48
|
+
return (
|
|
49
|
+
<CryptoBuy
|
|
50
|
+
jwt={jwt}
|
|
51
|
+
env="prod"
|
|
52
|
+
theme="auto"
|
|
53
|
+
onCompleted={({ amountBought, assetSymbol }) => {
|
|
54
|
+
console.log('Buy completed:', amountBought, assetSymbol);
|
|
55
|
+
}}
|
|
56
|
+
onError={({ errorCode, reason }) => {
|
|
57
|
+
console.log('Error:', errorCode, 'Reason:', reason);
|
|
58
|
+
}}
|
|
59
|
+
onClose={() => console.log('Widget closed')}
|
|
60
|
+
onEvent={({ type, data }) => console.log('Event:', type, data)}
|
|
61
|
+
onLoaded={() => console.log('Widget loaded and ready')}
|
|
62
|
+
/>
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Complete Example
|
|
68
|
+
|
|
69
|
+
```tsx
|
|
70
|
+
import React from 'react';
|
|
71
|
+
import { CryptoBuy } from '@zerohash-sdk/crypto-buy-react';
|
|
72
|
+
|
|
73
|
+
function App() {
|
|
74
|
+
const jwt = 'your-jwt-token';
|
|
75
|
+
|
|
76
|
+
return (
|
|
77
|
+
<div className="App">
|
|
78
|
+
<h1>My App</h1>
|
|
79
|
+
|
|
80
|
+
<CryptoBuy
|
|
81
|
+
jwt={jwt}
|
|
82
|
+
env="prod" // 'prod' (default), 'cert', 'dev', or 'local'
|
|
83
|
+
theme="auto" // 'auto' (default), 'light', or 'dark'
|
|
84
|
+
onCompleted={({ amountBought, assetSymbol }) => {
|
|
85
|
+
console.log('Bought:', amountBought, assetSymbol);
|
|
86
|
+
}}
|
|
87
|
+
onError={({ errorCode, reason }) => {
|
|
88
|
+
console.log('Error:', errorCode, 'Reason:', reason);
|
|
89
|
+
}}
|
|
90
|
+
onClose={() => console.log('Widget closed')}
|
|
91
|
+
onEvent={({ type, data }) => console.log('Event type:', type, 'Event data:', data)}
|
|
92
|
+
onLoaded={() => console.log('Widget loaded and ready')}
|
|
93
|
+
/>
|
|
94
|
+
</div>
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export default App;
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
## API Reference
|
|
102
|
+
|
|
103
|
+
### CryptoBuy Component Props
|
|
104
|
+
|
|
105
|
+
| Prop | Type | Required | Default | Description |
|
|
106
|
+
| ------------- | ----------------------------------------- | -------- | -------- | ------------------------------------------------- |
|
|
107
|
+
| `jwt` | `string` | Yes | - | JWT token for authentication with Connect |
|
|
108
|
+
| `env` | `"prod" \| "cert" \| "dev" \| "local"` | No | `"prod"` | Target environment |
|
|
109
|
+
| `theme` | `"auto" \| "light" \| "dark"` | No | `"auto"` | Theme mode for the interface |
|
|
110
|
+
| `onCompleted` | `({ amountBought, assetSymbol }) => void` | No | - | Callback when the buy flow completes successfully |
|
|
111
|
+
| `onError` | `({ errorCode, reason }) => void` | No | - | Callback for error events |
|
|
112
|
+
| `onClose` | `() => void` | No | - | Callback when the widget is closed |
|
|
113
|
+
| `onEvent` | `({ type, data }) => void` | No | - | Callback for general events |
|
|
114
|
+
| `onLoaded` | `() => void` | No | - | Callback when the widget is loaded and ready |
|
|
115
|
+
|
|
116
|
+
## More Information & Support
|
|
117
|
+
|
|
118
|
+
For comprehensive documentation or support about Connect, visit our [Documentation Page](https://docs.zerohash.com/).
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
import { default as default_2 } from 'react';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Generic app event structure
|
|
5
|
+
* @template TType - Event type string
|
|
6
|
+
* @template TData - Event data payload
|
|
7
|
+
*/
|
|
8
|
+
declare type AppEvent<TType extends string = string, TData = Record<string, unknown>> = {
|
|
9
|
+
/** The type of event that occurred */
|
|
10
|
+
type: TType;
|
|
11
|
+
/** Data associated with the event */
|
|
12
|
+
data: TData;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* A React wrapper component for the Connect Crypto Sell product.
|
|
17
|
+
*
|
|
18
|
+
* @param props - Component properties
|
|
19
|
+
* @param jwt - JWT token for authentication (required)
|
|
20
|
+
* @param env - Target environment ("local" | "dev" | "cert" | "prod", defaults to "prod")
|
|
21
|
+
* @param theme - Theme mode ("auto" | "light" | "dark", defaults to "auto")
|
|
22
|
+
* @param onCompleted - Callback invoked when the sell flow is successfully completed
|
|
23
|
+
* @param onError - Callback invoked when an error occurs
|
|
24
|
+
* @param onClose - Callback invoked when the user closes the widget
|
|
25
|
+
* @param onLoaded - Callback invoked when the widget has finished loading
|
|
26
|
+
* @param onEvent - Callback invoked for general application events
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```tsx
|
|
30
|
+
* // Basic usage with production environment (default)
|
|
31
|
+
* <CryptoBuy jwt="your-jwt-token" />
|
|
32
|
+
*
|
|
33
|
+
* // Using cert environment for testing
|
|
34
|
+
* <CryptoBuy jwt="your-jwt-token" env="cert" />
|
|
35
|
+
*
|
|
36
|
+
* // Force dark theme
|
|
37
|
+
* <CryptoBuy jwt="your-jwt-token" theme="dark" />
|
|
38
|
+
*
|
|
39
|
+
* // With callback functions
|
|
40
|
+
* <CryptoBuy
|
|
41
|
+
* jwt="your-jwt-token"
|
|
42
|
+
* theme="auto"
|
|
43
|
+
* onLoaded={() => console.log('Crypto sell loaded and ready')}
|
|
44
|
+
* onCompleted={({ amountSold, assetSymbol }) => console.log('Sold', amountSold, assetSymbol)}
|
|
45
|
+
* onClose={() => console.log('Crypto sell closed')}
|
|
46
|
+
* onError={({ errorCode, reason }) => console.error('Error:', errorCode, reason)}
|
|
47
|
+
* onEvent={(event) => console.log('Event:', event)}
|
|
48
|
+
* />
|
|
49
|
+
* ```
|
|
50
|
+
*
|
|
51
|
+
* @returns React component that renders the Connect Crypto Sell interface
|
|
52
|
+
*/
|
|
53
|
+
export declare const CryptoBuy: default_2.FC<CryptoBuyProps>;
|
|
54
|
+
|
|
55
|
+
declare type CryptoBuyCompletedData = {
|
|
56
|
+
amountBought: string;
|
|
57
|
+
assetSymbol: string;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
declare type CryptoBuyEvent = AppEvent<CryptoBuyEventType>;
|
|
61
|
+
|
|
62
|
+
declare type CryptoBuyEventType = string;
|
|
63
|
+
|
|
64
|
+
declare interface CryptoBuyProps {
|
|
65
|
+
/**
|
|
66
|
+
* JWT token used for authentication with the Connect Crypto Sell service.
|
|
67
|
+
* This token should be obtained from your backend and contain the necessary
|
|
68
|
+
* claims for user identification and authorization.
|
|
69
|
+
*
|
|
70
|
+
* @example "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
|
|
71
|
+
*/
|
|
72
|
+
jwt: string;
|
|
73
|
+
/**
|
|
74
|
+
* Target environment for the Connect Crypto Sell service.
|
|
75
|
+
*
|
|
76
|
+
* @default "prod"
|
|
77
|
+
*
|
|
78
|
+
* Available environments:
|
|
79
|
+
* - `"local"` - Local development environment
|
|
80
|
+
* - `"dev"` - Development environment
|
|
81
|
+
* - `"cert"` - Certificate/staging environment for integration testing
|
|
82
|
+
* - `"prod"` - Live production environment for real applications
|
|
83
|
+
*
|
|
84
|
+
* @example
|
|
85
|
+
* ```tsx
|
|
86
|
+
* // Use production (default)
|
|
87
|
+
* <CryptoBuy jwt={token} />
|
|
88
|
+
*
|
|
89
|
+
* // Use cert for testing
|
|
90
|
+
* <CryptoBuy jwt={token} env="cert" />
|
|
91
|
+
* ```
|
|
92
|
+
*/
|
|
93
|
+
env?: Environment;
|
|
94
|
+
/**
|
|
95
|
+
* Theme mode for the Connect Crypto Sell interface.
|
|
96
|
+
*
|
|
97
|
+
* @default "auto"
|
|
98
|
+
*
|
|
99
|
+
* Available themes:
|
|
100
|
+
* - `"auto"` - Automatically detect system preference (light/dark mode)
|
|
101
|
+
* - `"light"` - Force light theme
|
|
102
|
+
* - `"dark"` - Force dark theme
|
|
103
|
+
*
|
|
104
|
+
* @example
|
|
105
|
+
* ```tsx
|
|
106
|
+
* // Auto-detect system preference (default)
|
|
107
|
+
* <CryptoBuy jwt={token} />
|
|
108
|
+
*
|
|
109
|
+
* // Force dark theme
|
|
110
|
+
* <CryptoBuy jwt={token} theme="dark" />
|
|
111
|
+
* ```
|
|
112
|
+
*/
|
|
113
|
+
theme?: Theme;
|
|
114
|
+
/**
|
|
115
|
+
* Callback invoked when the crypto sell flow is successfully completed.
|
|
116
|
+
*
|
|
117
|
+
* @param data - Details about the completed sell, including amount sold and asset symbol
|
|
118
|
+
*
|
|
119
|
+
* @example
|
|
120
|
+
* ```tsx
|
|
121
|
+
* <CryptoBuy
|
|
122
|
+
* jwt={token}
|
|
123
|
+
* onCompleted={({ amountSold, assetSymbol }) =>
|
|
124
|
+
* console.log(`Sold ${amountSold} ${assetSymbol}`)
|
|
125
|
+
* }
|
|
126
|
+
* />
|
|
127
|
+
* ```
|
|
128
|
+
*/
|
|
129
|
+
onCompleted?: (data: CryptoBuyCompletedData) => void;
|
|
130
|
+
/**
|
|
131
|
+
* Callback invoked when an error occurs during the crypto sell flow.
|
|
132
|
+
*
|
|
133
|
+
* @param error - Error details including error code and reason
|
|
134
|
+
*
|
|
135
|
+
* @example
|
|
136
|
+
* ```tsx
|
|
137
|
+
* <CryptoBuy
|
|
138
|
+
* jwt={token}
|
|
139
|
+
* onError={({ errorCode, reason }) =>
|
|
140
|
+
* console.error('Crypto sell error:', errorCode, reason)
|
|
141
|
+
* }
|
|
142
|
+
* />
|
|
143
|
+
* ```
|
|
144
|
+
*/
|
|
145
|
+
onError?: (error: ErrorPayload) => void;
|
|
146
|
+
/**
|
|
147
|
+
* Callback invoked when the user closes the crypto sell widget.
|
|
148
|
+
*
|
|
149
|
+
* @example
|
|
150
|
+
* ```tsx
|
|
151
|
+
* <CryptoBuy
|
|
152
|
+
* jwt={token}
|
|
153
|
+
* onClose={() => setShowCryptoBuy(false)}
|
|
154
|
+
* />
|
|
155
|
+
* ```
|
|
156
|
+
*/
|
|
157
|
+
onClose?: () => void;
|
|
158
|
+
/**
|
|
159
|
+
* Callback invoked when the crypto sell widget has finished loading and is ready.
|
|
160
|
+
*
|
|
161
|
+
* @example
|
|
162
|
+
* ```tsx
|
|
163
|
+
* <CryptoBuy
|
|
164
|
+
* jwt={token}
|
|
165
|
+
* onLoaded={() => setIsLoading(false)}
|
|
166
|
+
* />
|
|
167
|
+
* ```
|
|
168
|
+
*/
|
|
169
|
+
onLoaded?: () => void;
|
|
170
|
+
/**
|
|
171
|
+
* Callback invoked for general application events during the crypto sell flow.
|
|
172
|
+
*
|
|
173
|
+
* @param event - Event object containing the event type and associated data
|
|
174
|
+
*
|
|
175
|
+
* @example
|
|
176
|
+
* ```tsx
|
|
177
|
+
* <CryptoBuy
|
|
178
|
+
* jwt={token}
|
|
179
|
+
* onEvent={(event) => console.log('Crypto sell event:', event)}
|
|
180
|
+
* />
|
|
181
|
+
* ```
|
|
182
|
+
*/
|
|
183
|
+
onEvent?: (event: CryptoBuyEvent) => void;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Available environments for the Connect Crypto Sell service.
|
|
188
|
+
*
|
|
189
|
+
* - `"local"` - Local development environment (http://localhost:4204)
|
|
190
|
+
* - `"dev"` - Development environment for early-stage testing
|
|
191
|
+
* - `"cert"` - Certificate/staging environment for integration testing
|
|
192
|
+
* - `"prod"` - Live production environment for real applications
|
|
193
|
+
*/
|
|
194
|
+
declare type Environment = 'local' | 'dev' | 'cert' | 'prod';
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Generic error codes for all Connect applications
|
|
198
|
+
*/
|
|
199
|
+
declare enum ErrorCode {
|
|
200
|
+
/** Network connectivity error */
|
|
201
|
+
NETWORK_ERROR = 'network_error',
|
|
202
|
+
/** Authentication or session expired error */
|
|
203
|
+
AUTH_ERROR = 'auth_error',
|
|
204
|
+
/** Resource not found error */
|
|
205
|
+
NOT_FOUND_ERROR = 'not_found_error',
|
|
206
|
+
/** Validation error from user input */
|
|
207
|
+
VALIDATION_ERROR = 'validation_error',
|
|
208
|
+
/** Server error (5xx) */
|
|
209
|
+
SERVER_ERROR = 'server_error',
|
|
210
|
+
/** Client error (4xx) */
|
|
211
|
+
CLIENT_ERROR = 'client_error',
|
|
212
|
+
/** Unknown or unexpected error */
|
|
213
|
+
UNKNOWN_ERROR = 'unknown_error',
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Generic error payload structure for error callbacks
|
|
218
|
+
*/
|
|
219
|
+
declare type ErrorPayload = {
|
|
220
|
+
/** Error code indicating the type of error */
|
|
221
|
+
errorCode: ErrorCode;
|
|
222
|
+
/** Human-readable reason for the error */
|
|
223
|
+
reason: string;
|
|
224
|
+
};
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Theme mode for the Connect Crypto Sell interface.
|
|
228
|
+
*
|
|
229
|
+
* - `"auto"` - Automatically detect system preference (light/dark mode)
|
|
230
|
+
* - `"light"` - Force light theme
|
|
231
|
+
* - `"dark"` - Force dark theme
|
|
232
|
+
*/
|
|
233
|
+
declare type Theme = 'auto' | 'light' | 'dark';
|
|
234
|
+
|
|
235
|
+
export { }
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import a, { useRef as m, useEffect as y } from "react";
|
|
2
|
+
const b = {
|
|
3
|
+
local: "http://localhost:5173/crypto-buy-web/index.js",
|
|
4
|
+
dev: "https://connect-sdk.dev.0hash.com/crypto-buy-web/index.js",
|
|
5
|
+
cert: "https://sdk.sandbox.connect.xyz/crypto-buy-web/index.js",
|
|
6
|
+
prod: "https://sdk.connect.xyz/crypto-buy-web/index.js"
|
|
7
|
+
}, x = "zerohash-crypto-buy-script", l = "zerohash-crypto-buy", I = (c = "prod") => b[c], j = ({
|
|
8
|
+
jwt: c,
|
|
9
|
+
env: r = "prod",
|
|
10
|
+
theme: f,
|
|
11
|
+
onCompleted: o,
|
|
12
|
+
onError: s,
|
|
13
|
+
onClose: n,
|
|
14
|
+
onLoaded: p,
|
|
15
|
+
onEvent: i,
|
|
16
|
+
...h
|
|
17
|
+
}) => {
|
|
18
|
+
const d = m(null);
|
|
19
|
+
return y(() => {
|
|
20
|
+
const t = d.current;
|
|
21
|
+
t && (o && (t.onCompleted = o), s && (t.onError = s), n && (t.onClose = n), p && (t.onLoaded = p), i && (t.onEvent = i));
|
|
22
|
+
}, [o, s, n, p, i]), y(() => {
|
|
23
|
+
const t = I(r), u = `${x}-${r}`;
|
|
24
|
+
if (document.getElementById(u))
|
|
25
|
+
return;
|
|
26
|
+
const e = document.createElement("script");
|
|
27
|
+
e.id = u, e.src = t, e.type = "module", e.async = !0, e.onerror = () => {
|
|
28
|
+
console.error(`Failed to load the script for ${l} from ${r} environment.`);
|
|
29
|
+
}, document.head.appendChild(e);
|
|
30
|
+
}, [r]), a.createElement(l, {
|
|
31
|
+
ref: d,
|
|
32
|
+
jwt: c,
|
|
33
|
+
env: r,
|
|
34
|
+
theme: f,
|
|
35
|
+
...h
|
|
36
|
+
});
|
|
37
|
+
};
|
|
38
|
+
export {
|
|
39
|
+
j as CryptoBuy
|
|
40
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@zerohash-sdk/crypto-buy-react",
|
|
3
|
+
"version": "1.1.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
|
+
"default": "./dist/index.js"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"dist",
|
|
20
|
+
"!**/*.tsbuildinfo"
|
|
21
|
+
],
|
|
22
|
+
"nx": {
|
|
23
|
+
"name": "crypto-buy-react"
|
|
24
|
+
},
|
|
25
|
+
"peerDependencies": {
|
|
26
|
+
"react": ">=18.0.0",
|
|
27
|
+
"react-dom": ">=18.0.0"
|
|
28
|
+
}
|
|
29
|
+
}
|