@hyve-sdk/js 1.0.1 → 1.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +177 -14
- package/dist/index.d.mts +59 -2
- package/dist/index.d.ts +59 -2
- package/dist/index.js +91 -7
- package/dist/index.mjs +91 -7
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -1,34 +1,51 @@
|
|
|
1
|
-
# @hyve/
|
|
1
|
+
# @hyve-sdk/js
|
|
2
2
|
|
|
3
3
|
TypeScript SDK for web3 authentication and game integration, providing secure signature verification and utility functions.
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
7
7
|
```bash
|
|
8
|
-
bun add @hyve/
|
|
8
|
+
bun add @hyve-sdk/js
|
|
9
9
|
# or
|
|
10
|
-
npm install @hyve/
|
|
10
|
+
npm install @hyve-sdk/js
|
|
11
11
|
# or
|
|
12
|
-
pnpm add @hyve/
|
|
12
|
+
pnpm add @hyve-sdk/js
|
|
13
13
|
```
|
|
14
14
|
|
|
15
15
|
## Features
|
|
16
16
|
|
|
17
17
|
- **Web3 Authentication**: EVM signature validation and verification
|
|
18
18
|
- **Modern & Legacy Token Support**: Dual authentication token formats
|
|
19
|
-
- **
|
|
19
|
+
- **JWT Authentication**: Support for JWT tokens passed via URL parameters
|
|
20
|
+
- **API Integration**: Authenticated API calls with JWT token support
|
|
21
|
+
- **Inventory Management**: Built-in methods for user inventory operations
|
|
22
|
+
- **Telemetry Tracking**: Session-based analytics and event tracking
|
|
23
|
+
- **Security Utilities**: Domain validation and referrer checking
|
|
20
24
|
- **URL Parameter Parsing**: Easy extraction of authentication parameters
|
|
21
25
|
- **UUID Generation**: Built-in UUID v4 generation
|
|
22
26
|
|
|
23
27
|
## Core Components
|
|
24
28
|
|
|
25
29
|
### HyveClient
|
|
26
|
-
Main client class for SDK operations
|
|
30
|
+
Main client class for SDK operations including authentication, telemetry, and API calls.
|
|
27
31
|
|
|
28
32
|
```typescript
|
|
29
|
-
import { HyveClient } from "@hyve/
|
|
33
|
+
import { HyveClient } from "@hyve-sdk/js";
|
|
30
34
|
|
|
31
|
-
|
|
35
|
+
// Initialize with optional configuration
|
|
36
|
+
const client = new HyveClient({
|
|
37
|
+
apiKey: 'your-api-key', // For telemetry
|
|
38
|
+
isDev: true, // Development mode (default: true)
|
|
39
|
+
apiBaseUrl: 'https://...' // Optional custom API URL
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
// Authenticate from URL parameters
|
|
43
|
+
const authenticated = await client.authenticateFromUrl();
|
|
44
|
+
if (authenticated) {
|
|
45
|
+
console.log('User ID:', client.getUserId());
|
|
46
|
+
console.log('Session ID:', client.getSessionId());
|
|
47
|
+
console.log('Has JWT:', client.hasJwtToken());
|
|
48
|
+
}
|
|
32
49
|
```
|
|
33
50
|
|
|
34
51
|
### Authentication Utilities
|
|
@@ -37,7 +54,7 @@ const client = new HyveClient();
|
|
|
37
54
|
Extract authentication and game parameters from URL:
|
|
38
55
|
|
|
39
56
|
```typescript
|
|
40
|
-
import { parseUrlParams } from "@hyve/
|
|
57
|
+
import { parseUrlParams } from "@hyve-sdk/js";
|
|
41
58
|
|
|
42
59
|
const params = parseUrlParams(window.location.search);
|
|
43
60
|
// Returns:
|
|
@@ -55,7 +72,7 @@ const params = parseUrlParams(window.location.search);
|
|
|
55
72
|
Unified verification supporting both modern and legacy tokens:
|
|
56
73
|
|
|
57
74
|
```typescript
|
|
58
|
-
import { verifyAuthentication } from "@hyve/
|
|
75
|
+
import { verifyAuthentication } from "@hyve-sdk/js";
|
|
59
76
|
|
|
60
77
|
const result = verifyAuthentication({
|
|
61
78
|
hyveToken: params.hyveToken, // Modern token format
|
|
@@ -73,7 +90,7 @@ if (result.isValid) {
|
|
|
73
90
|
Verify modern authentication tokens with expiration:
|
|
74
91
|
|
|
75
92
|
```typescript
|
|
76
|
-
import { verifyHyveToken } from "@hyve/
|
|
93
|
+
import { verifyHyveToken } from "@hyve-sdk/js";
|
|
77
94
|
|
|
78
95
|
// Token format: signature.address.randomBase64.timestamp
|
|
79
96
|
const address = verifyHyveToken(hyveToken, 600); // 600 seconds max age
|
|
@@ -86,7 +103,7 @@ if (address) {
|
|
|
86
103
|
Verify legacy signed messages with metadata:
|
|
87
104
|
|
|
88
105
|
```typescript
|
|
89
|
-
import { handleVerifyMessage, validateSignature } from "@hyve/
|
|
106
|
+
import { handleVerifyMessage, validateSignature } from "@hyve-sdk/js";
|
|
90
107
|
|
|
91
108
|
// Method 1: Verify message with embedded metadata
|
|
92
109
|
const address = handleVerifyMessage(signature, message);
|
|
@@ -101,7 +118,7 @@ const isValid = validateSignature(signature, message, userId);
|
|
|
101
118
|
Check if current domain is allowed:
|
|
102
119
|
|
|
103
120
|
```typescript
|
|
104
|
-
import { isDomainAllowed } from "@hyve/
|
|
121
|
+
import { isDomainAllowed } from "@hyve-sdk/js";
|
|
105
122
|
|
|
106
123
|
// Single domain
|
|
107
124
|
const allowed = isDomainAllowed('example.com', window.location.hostname);
|
|
@@ -121,11 +138,157 @@ const allowed = isDomainAllowed(
|
|
|
121
138
|
Generate random UUID v4:
|
|
122
139
|
|
|
123
140
|
```typescript
|
|
124
|
-
import { generateUUID } from "@hyve/
|
|
141
|
+
import { generateUUID } from "@hyve-sdk/js";
|
|
125
142
|
|
|
126
143
|
const id = generateUUID();
|
|
127
144
|
```
|
|
128
145
|
|
|
146
|
+
## Telemetry & Analytics
|
|
147
|
+
|
|
148
|
+
### Send Telemetry Events
|
|
149
|
+
Track user actions and game events:
|
|
150
|
+
|
|
151
|
+
```typescript
|
|
152
|
+
// Send a custom telemetry event
|
|
153
|
+
await client.sendTelemetry(
|
|
154
|
+
'game', // Event location
|
|
155
|
+
'player', // Event category
|
|
156
|
+
'action', // Event action
|
|
157
|
+
'combat', // Event sub-category (optional)
|
|
158
|
+
'attack', // Event sub-action (optional)
|
|
159
|
+
null, // Event details string (optional)
|
|
160
|
+
{ // Additional data (optional)
|
|
161
|
+
damage: 100,
|
|
162
|
+
targetId: 'enemy-123',
|
|
163
|
+
weaponType: 'sword'
|
|
164
|
+
}
|
|
165
|
+
);
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
**Configuration:**
|
|
169
|
+
- Requires `apiKey` in client configuration
|
|
170
|
+
- Automatically includes `session_id` and `user_id`
|
|
171
|
+
- Additional data is JSON-stringified into `event_details`
|
|
172
|
+
- Supports anonymous telemetry (without authentication)
|
|
173
|
+
|
|
174
|
+
## API Integration
|
|
175
|
+
|
|
176
|
+
### JWT Authentication
|
|
177
|
+
The SDK supports JWT tokens passed via URL parameter `hyve-access`:
|
|
178
|
+
|
|
179
|
+
```typescript
|
|
180
|
+
// URL: https://game.com?hyve-access=your-jwt-token
|
|
181
|
+
|
|
182
|
+
// Authenticate extracts JWT automatically
|
|
183
|
+
await client.authenticateFromUrl();
|
|
184
|
+
|
|
185
|
+
// Check JWT availability
|
|
186
|
+
if (client.hasJwtToken()) {
|
|
187
|
+
const token = client.getJwtToken();
|
|
188
|
+
console.log('JWT token available');
|
|
189
|
+
}
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
### Generic API Calls
|
|
193
|
+
Make authenticated API requests:
|
|
194
|
+
|
|
195
|
+
```typescript
|
|
196
|
+
// GET request
|
|
197
|
+
const userData = await client.callApi('/api/v1/user');
|
|
198
|
+
|
|
199
|
+
// POST request with body
|
|
200
|
+
const result = await client.callApi('/api/v1/game/score', {
|
|
201
|
+
method: 'POST',
|
|
202
|
+
body: JSON.stringify({ score: 1000 })
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
// With TypeScript typing
|
|
206
|
+
interface UserData {
|
|
207
|
+
id: string;
|
|
208
|
+
username: string;
|
|
209
|
+
}
|
|
210
|
+
const user = await client.callApi<UserData>('/api/v1/user');
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
### Inventory Management
|
|
214
|
+
|
|
215
|
+
#### Get User Inventory
|
|
216
|
+
Retrieve all inventory items:
|
|
217
|
+
|
|
218
|
+
```typescript
|
|
219
|
+
const inventory = await client.getInventory();
|
|
220
|
+
|
|
221
|
+
console.log(`Total items: ${inventory.total_count}`);
|
|
222
|
+
inventory.items.forEach(item => {
|
|
223
|
+
console.log(`${item.item_type}: ${item.quantity}x`);
|
|
224
|
+
});
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
**Response Type:**
|
|
228
|
+
```typescript
|
|
229
|
+
interface Inventory {
|
|
230
|
+
items: InventoryItem[];
|
|
231
|
+
total_count: number;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
interface InventoryItem {
|
|
235
|
+
id: string;
|
|
236
|
+
user_id: string;
|
|
237
|
+
item_type: string;
|
|
238
|
+
item_id: string;
|
|
239
|
+
quantity: number;
|
|
240
|
+
metadata?: Record<string, any>;
|
|
241
|
+
created_at: string;
|
|
242
|
+
updated_at: string;
|
|
243
|
+
}
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
#### Get Specific Inventory Item
|
|
247
|
+
Fetch details for a single item:
|
|
248
|
+
|
|
249
|
+
```typescript
|
|
250
|
+
const item = await client.getInventoryItem('item-id-123');
|
|
251
|
+
|
|
252
|
+
console.log(`Item: ${item.item_type}`);
|
|
253
|
+
console.log(`Quantity: ${item.quantity}`);
|
|
254
|
+
if (item.metadata) {
|
|
255
|
+
console.log('Metadata:', item.metadata);
|
|
256
|
+
}
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
**API Endpoints:**
|
|
260
|
+
- `GET /api/v1/inventory` - Get all inventory items
|
|
261
|
+
- `GET /api/v1/inventory/:id` - Get specific item
|
|
262
|
+
|
|
263
|
+
**Requirements:**
|
|
264
|
+
- JWT token must be available (via `hyve-access` URL parameter)
|
|
265
|
+
- User must be authenticated
|
|
266
|
+
|
|
267
|
+
## Client Methods Reference
|
|
268
|
+
|
|
269
|
+
### Authentication
|
|
270
|
+
- `authenticateFromUrl(urlParams?)` - Authenticate from URL parameters
|
|
271
|
+
- `getUserId()` - Get authenticated user ID (address)
|
|
272
|
+
- `getSessionId()` - Get unique session ID
|
|
273
|
+
- `isUserAuthenticated()` - Check if user is authenticated
|
|
274
|
+
- `hasJwtToken()` - Check if JWT token is available
|
|
275
|
+
- `getJwtToken()` - Get JWT token string
|
|
276
|
+
- `logout()` - Clear user authentication
|
|
277
|
+
- `reset()` - Reset client state with new session
|
|
278
|
+
|
|
279
|
+
### API Calls
|
|
280
|
+
- `callApi<T>(endpoint, options?)` - Generic authenticated API call
|
|
281
|
+
- `getInventory()` - Get user's inventory
|
|
282
|
+
- `getInventoryItem(itemId)` - Get specific inventory item
|
|
283
|
+
|
|
284
|
+
### Telemetry
|
|
285
|
+
- `sendTelemetry(location, category, action, subCategory?, subAction?, details?, additionalData?)` - Send analytics event
|
|
286
|
+
- `updateTelemetryConfig(config)` - Update telemetry settings
|
|
287
|
+
|
|
288
|
+
### Configuration
|
|
289
|
+
- `getApiBaseUrl()` - Get current API base URL
|
|
290
|
+
- `updateTelemetryConfig(config)` - Update client configuration
|
|
291
|
+
|
|
129
292
|
## Authentication Flow
|
|
130
293
|
|
|
131
294
|
### Modern Token Flow (Recommended)
|
package/dist/index.d.mts
CHANGED
|
@@ -6,6 +6,8 @@ interface TelemetryConfig {
|
|
|
6
6
|
apiKey?: string;
|
|
7
7
|
/** Environment: true for dev, false for prod. Defaults to true (dev) */
|
|
8
8
|
isDev?: boolean;
|
|
9
|
+
/** Base API URL for all API calls (telemetry and external). Can be set via env. If not provided, defaults based on isDev */
|
|
10
|
+
apiBaseUrl?: string;
|
|
9
11
|
}
|
|
10
12
|
/**
|
|
11
13
|
* Telemetry event data structure
|
|
@@ -35,15 +37,36 @@ interface TelemetryAdditionalData {
|
|
|
35
37
|
/** Optional additional data object (will be JSON stringified and put in event_details) */
|
|
36
38
|
[key: string]: any;
|
|
37
39
|
}
|
|
40
|
+
/**
|
|
41
|
+
* User inventory item
|
|
42
|
+
*/
|
|
43
|
+
interface InventoryItem {
|
|
44
|
+
id: string;
|
|
45
|
+
user_id: string;
|
|
46
|
+
item_type: string;
|
|
47
|
+
item_id: string;
|
|
48
|
+
quantity: number;
|
|
49
|
+
metadata?: Record<string, any>;
|
|
50
|
+
created_at: string;
|
|
51
|
+
updated_at: string;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* User inventory response
|
|
55
|
+
*/
|
|
56
|
+
interface Inventory {
|
|
57
|
+
items: InventoryItem[];
|
|
58
|
+
total_count: number;
|
|
59
|
+
}
|
|
38
60
|
|
|
39
61
|
/**
|
|
40
62
|
* HyveClient provides telemetry and authentication functionality for Hyve games
|
|
41
63
|
*/
|
|
42
64
|
declare class HyveClient {
|
|
43
65
|
private telemetryConfig;
|
|
44
|
-
private
|
|
66
|
+
private apiBaseUrl;
|
|
45
67
|
private sessionId;
|
|
46
68
|
private userId;
|
|
69
|
+
private jwtToken;
|
|
47
70
|
/**
|
|
48
71
|
* Creates a new HyveClient instance
|
|
49
72
|
* @param config Optional telemetry configuration
|
|
@@ -67,6 +90,24 @@ declare class HyveClient {
|
|
|
67
90
|
* @returns Promise resolving to boolean indicating success
|
|
68
91
|
*/
|
|
69
92
|
sendTelemetry(eventLocation: string, eventCategory: string, eventAction: string, eventSubCategory?: string | null, eventSubAction?: string | null, eventDetails?: string | null, additionalData?: TelemetryAdditionalData | null): Promise<boolean>;
|
|
93
|
+
/**
|
|
94
|
+
* Makes an authenticated API call using the JWT token
|
|
95
|
+
* @param endpoint API endpoint path (will be appended to base URL)
|
|
96
|
+
* @param options Fetch options (method, body, etc.)
|
|
97
|
+
* @returns Promise resolving to the API response
|
|
98
|
+
*/
|
|
99
|
+
callApi<T = any>(endpoint: string, options?: RequestInit): Promise<T>;
|
|
100
|
+
/**
|
|
101
|
+
* Gets the user's inventory
|
|
102
|
+
* @returns Promise resolving to the user's inventory
|
|
103
|
+
*/
|
|
104
|
+
getInventory(): Promise<Inventory>;
|
|
105
|
+
/**
|
|
106
|
+
* Gets a specific inventory item by ID
|
|
107
|
+
* @param itemId The inventory item ID
|
|
108
|
+
* @returns Promise resolving to the inventory item details
|
|
109
|
+
*/
|
|
110
|
+
getInventoryItem(itemId: string): Promise<InventoryItem>;
|
|
70
111
|
/**
|
|
71
112
|
* Updates the telemetry configuration
|
|
72
113
|
* @param config New telemetry configuration
|
|
@@ -82,11 +123,26 @@ declare class HyveClient {
|
|
|
82
123
|
* @returns Current session ID
|
|
83
124
|
*/
|
|
84
125
|
getSessionId(): string;
|
|
126
|
+
/**
|
|
127
|
+
* Gets the current JWT token
|
|
128
|
+
* @returns Current JWT token or null if not available
|
|
129
|
+
*/
|
|
130
|
+
getJwtToken(): string | null;
|
|
131
|
+
/**
|
|
132
|
+
* Gets the API base URL
|
|
133
|
+
* @returns API base URL
|
|
134
|
+
*/
|
|
135
|
+
getApiBaseUrl(): string;
|
|
85
136
|
/**
|
|
86
137
|
* Checks if user is authenticated
|
|
87
138
|
* @returns Boolean indicating authentication status
|
|
88
139
|
*/
|
|
89
140
|
isUserAuthenticated(): boolean;
|
|
141
|
+
/**
|
|
142
|
+
* Checks if JWT token is available
|
|
143
|
+
* @returns Boolean indicating if JWT token is present
|
|
144
|
+
*/
|
|
145
|
+
hasJwtToken(): boolean;
|
|
90
146
|
/**
|
|
91
147
|
* Logs out the current user
|
|
92
148
|
*/
|
|
@@ -108,6 +164,7 @@ declare function parseUrlParams(searchParams: URLSearchParams | string): {
|
|
|
108
164
|
gameStartTab: string;
|
|
109
165
|
hyveToken: string;
|
|
110
166
|
platform: string;
|
|
167
|
+
hyveAccess: string;
|
|
111
168
|
};
|
|
112
169
|
/**
|
|
113
170
|
* Validates an EVM signature against a message and user ID
|
|
@@ -158,4 +215,4 @@ declare function generateUUID(): string;
|
|
|
158
215
|
*/
|
|
159
216
|
declare function isDomainAllowed(allowedDomains?: string | string[], hostname?: string): boolean;
|
|
160
217
|
|
|
161
|
-
export { HyveClient, type TelemetryAdditionalData, type TelemetryConfig, type TelemetryEvent, generateUUID, handleVerifyMessage, isDomainAllowed, parseUrlParams, validateSignature, verifyAuthentication, verifyHyveToken };
|
|
218
|
+
export { HyveClient, type Inventory, type InventoryItem, type TelemetryAdditionalData, type TelemetryConfig, type TelemetryEvent, generateUUID, handleVerifyMessage, isDomainAllowed, parseUrlParams, validateSignature, verifyAuthentication, verifyHyveToken };
|
package/dist/index.d.ts
CHANGED
|
@@ -6,6 +6,8 @@ interface TelemetryConfig {
|
|
|
6
6
|
apiKey?: string;
|
|
7
7
|
/** Environment: true for dev, false for prod. Defaults to true (dev) */
|
|
8
8
|
isDev?: boolean;
|
|
9
|
+
/** Base API URL for all API calls (telemetry and external). Can be set via env. If not provided, defaults based on isDev */
|
|
10
|
+
apiBaseUrl?: string;
|
|
9
11
|
}
|
|
10
12
|
/**
|
|
11
13
|
* Telemetry event data structure
|
|
@@ -35,15 +37,36 @@ interface TelemetryAdditionalData {
|
|
|
35
37
|
/** Optional additional data object (will be JSON stringified and put in event_details) */
|
|
36
38
|
[key: string]: any;
|
|
37
39
|
}
|
|
40
|
+
/**
|
|
41
|
+
* User inventory item
|
|
42
|
+
*/
|
|
43
|
+
interface InventoryItem {
|
|
44
|
+
id: string;
|
|
45
|
+
user_id: string;
|
|
46
|
+
item_type: string;
|
|
47
|
+
item_id: string;
|
|
48
|
+
quantity: number;
|
|
49
|
+
metadata?: Record<string, any>;
|
|
50
|
+
created_at: string;
|
|
51
|
+
updated_at: string;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* User inventory response
|
|
55
|
+
*/
|
|
56
|
+
interface Inventory {
|
|
57
|
+
items: InventoryItem[];
|
|
58
|
+
total_count: number;
|
|
59
|
+
}
|
|
38
60
|
|
|
39
61
|
/**
|
|
40
62
|
* HyveClient provides telemetry and authentication functionality for Hyve games
|
|
41
63
|
*/
|
|
42
64
|
declare class HyveClient {
|
|
43
65
|
private telemetryConfig;
|
|
44
|
-
private
|
|
66
|
+
private apiBaseUrl;
|
|
45
67
|
private sessionId;
|
|
46
68
|
private userId;
|
|
69
|
+
private jwtToken;
|
|
47
70
|
/**
|
|
48
71
|
* Creates a new HyveClient instance
|
|
49
72
|
* @param config Optional telemetry configuration
|
|
@@ -67,6 +90,24 @@ declare class HyveClient {
|
|
|
67
90
|
* @returns Promise resolving to boolean indicating success
|
|
68
91
|
*/
|
|
69
92
|
sendTelemetry(eventLocation: string, eventCategory: string, eventAction: string, eventSubCategory?: string | null, eventSubAction?: string | null, eventDetails?: string | null, additionalData?: TelemetryAdditionalData | null): Promise<boolean>;
|
|
93
|
+
/**
|
|
94
|
+
* Makes an authenticated API call using the JWT token
|
|
95
|
+
* @param endpoint API endpoint path (will be appended to base URL)
|
|
96
|
+
* @param options Fetch options (method, body, etc.)
|
|
97
|
+
* @returns Promise resolving to the API response
|
|
98
|
+
*/
|
|
99
|
+
callApi<T = any>(endpoint: string, options?: RequestInit): Promise<T>;
|
|
100
|
+
/**
|
|
101
|
+
* Gets the user's inventory
|
|
102
|
+
* @returns Promise resolving to the user's inventory
|
|
103
|
+
*/
|
|
104
|
+
getInventory(): Promise<Inventory>;
|
|
105
|
+
/**
|
|
106
|
+
* Gets a specific inventory item by ID
|
|
107
|
+
* @param itemId The inventory item ID
|
|
108
|
+
* @returns Promise resolving to the inventory item details
|
|
109
|
+
*/
|
|
110
|
+
getInventoryItem(itemId: string): Promise<InventoryItem>;
|
|
70
111
|
/**
|
|
71
112
|
* Updates the telemetry configuration
|
|
72
113
|
* @param config New telemetry configuration
|
|
@@ -82,11 +123,26 @@ declare class HyveClient {
|
|
|
82
123
|
* @returns Current session ID
|
|
83
124
|
*/
|
|
84
125
|
getSessionId(): string;
|
|
126
|
+
/**
|
|
127
|
+
* Gets the current JWT token
|
|
128
|
+
* @returns Current JWT token or null if not available
|
|
129
|
+
*/
|
|
130
|
+
getJwtToken(): string | null;
|
|
131
|
+
/**
|
|
132
|
+
* Gets the API base URL
|
|
133
|
+
* @returns API base URL
|
|
134
|
+
*/
|
|
135
|
+
getApiBaseUrl(): string;
|
|
85
136
|
/**
|
|
86
137
|
* Checks if user is authenticated
|
|
87
138
|
* @returns Boolean indicating authentication status
|
|
88
139
|
*/
|
|
89
140
|
isUserAuthenticated(): boolean;
|
|
141
|
+
/**
|
|
142
|
+
* Checks if JWT token is available
|
|
143
|
+
* @returns Boolean indicating if JWT token is present
|
|
144
|
+
*/
|
|
145
|
+
hasJwtToken(): boolean;
|
|
90
146
|
/**
|
|
91
147
|
* Logs out the current user
|
|
92
148
|
*/
|
|
@@ -108,6 +164,7 @@ declare function parseUrlParams(searchParams: URLSearchParams | string): {
|
|
|
108
164
|
gameStartTab: string;
|
|
109
165
|
hyveToken: string;
|
|
110
166
|
platform: string;
|
|
167
|
+
hyveAccess: string;
|
|
111
168
|
};
|
|
112
169
|
/**
|
|
113
170
|
* Validates an EVM signature against a message and user ID
|
|
@@ -158,4 +215,4 @@ declare function generateUUID(): string;
|
|
|
158
215
|
*/
|
|
159
216
|
declare function isDomainAllowed(allowedDomains?: string | string[], hostname?: string): boolean;
|
|
160
217
|
|
|
161
|
-
export { HyveClient, type TelemetryAdditionalData, type TelemetryConfig, type TelemetryEvent, generateUUID, handleVerifyMessage, isDomainAllowed, parseUrlParams, validateSignature, verifyAuthentication, verifyHyveToken };
|
|
218
|
+
export { HyveClient, type Inventory, type InventoryItem, type TelemetryAdditionalData, type TelemetryConfig, type TelemetryEvent, generateUUID, handleVerifyMessage, isDomainAllowed, parseUrlParams, validateSignature, verifyAuthentication, verifyHyveToken };
|
package/dist/index.js
CHANGED
|
@@ -41,7 +41,8 @@ function parseUrlParams(searchParams) {
|
|
|
41
41
|
message: params.get("message") || "",
|
|
42
42
|
gameStartTab: params.get("game_start_tab") || "",
|
|
43
43
|
hyveToken: params.get("hyve-token") || "",
|
|
44
|
-
platform: params.get("platform") || ""
|
|
44
|
+
platform: params.get("platform") || "",
|
|
45
|
+
hyveAccess: params.get("hyve-access") || ""
|
|
45
46
|
};
|
|
46
47
|
}
|
|
47
48
|
function validateSignature(signature, message) {
|
|
@@ -186,9 +187,10 @@ function isDomainAllowed(allowedDomains, hostname) {
|
|
|
186
187
|
// src/core/client.ts
|
|
187
188
|
var HyveClient = class {
|
|
188
189
|
telemetryConfig;
|
|
189
|
-
|
|
190
|
+
apiBaseUrl;
|
|
190
191
|
sessionId;
|
|
191
192
|
userId = null;
|
|
193
|
+
jwtToken = null;
|
|
192
194
|
/**
|
|
193
195
|
* Creates a new HyveClient instance
|
|
194
196
|
* @param config Optional telemetry configuration
|
|
@@ -199,10 +201,15 @@ var HyveClient = class {
|
|
|
199
201
|
// Default to dev environment
|
|
200
202
|
...config
|
|
201
203
|
};
|
|
202
|
-
|
|
204
|
+
if (this.telemetryConfig.apiBaseUrl) {
|
|
205
|
+
this.apiBaseUrl = this.telemetryConfig.apiBaseUrl;
|
|
206
|
+
} else {
|
|
207
|
+
this.apiBaseUrl = this.telemetryConfig.isDev ? "https://product-api.dev.hyve.gg" : "https://product-api.prod.hyve.gg";
|
|
208
|
+
}
|
|
203
209
|
this.sessionId = generateUUID();
|
|
204
210
|
console.log("[Hyve SDK] Client initialized with sessionId:", this.sessionId);
|
|
205
|
-
console.log("[Hyve SDK]
|
|
211
|
+
console.log("[Hyve SDK] API Base URL:", this.apiBaseUrl);
|
|
212
|
+
console.log("[Hyve SDK] Environment:", this.telemetryConfig.isDev ? "dev" : "prod");
|
|
206
213
|
}
|
|
207
214
|
/**
|
|
208
215
|
* Authenticates a user from URL parameters
|
|
@@ -212,6 +219,10 @@ var HyveClient = class {
|
|
|
212
219
|
async authenticateFromUrl(urlParams) {
|
|
213
220
|
try {
|
|
214
221
|
const params = urlParams ? parseUrlParams(urlParams) : parseUrlParams(window.location.search);
|
|
222
|
+
if (params.hyveAccess) {
|
|
223
|
+
this.jwtToken = params.hyveAccess;
|
|
224
|
+
console.log("[Hyve SDK] JWT token extracted from hyve-access parameter");
|
|
225
|
+
}
|
|
215
226
|
const authResult = verifyAuthentication({
|
|
216
227
|
hyveToken: params.hyveToken,
|
|
217
228
|
signature: params.signature,
|
|
@@ -270,7 +281,8 @@ var HyveClient = class {
|
|
|
270
281
|
event_details: finalEventDetails
|
|
271
282
|
};
|
|
272
283
|
console.log("[Hyve Telemetry] Sending event:", telemetryEvent);
|
|
273
|
-
const
|
|
284
|
+
const telemetryUrl = `${this.apiBaseUrl}/api/v1/partners/analytics/events`;
|
|
285
|
+
const response = await fetch(telemetryUrl, {
|
|
274
286
|
method: "POST",
|
|
275
287
|
headers: {
|
|
276
288
|
"Content-Type": "application/json",
|
|
@@ -291,6 +303,51 @@ var HyveClient = class {
|
|
|
291
303
|
return false;
|
|
292
304
|
}
|
|
293
305
|
}
|
|
306
|
+
/**
|
|
307
|
+
* Makes an authenticated API call using the JWT token
|
|
308
|
+
* @param endpoint API endpoint path (will be appended to base URL)
|
|
309
|
+
* @param options Fetch options (method, body, etc.)
|
|
310
|
+
* @returns Promise resolving to the API response
|
|
311
|
+
*/
|
|
312
|
+
async callApi(endpoint, options = {}) {
|
|
313
|
+
if (!this.jwtToken) {
|
|
314
|
+
throw new Error("[Hyve SDK] No JWT token available. Call authenticateFromUrl first.");
|
|
315
|
+
}
|
|
316
|
+
try {
|
|
317
|
+
const url = `${this.apiBaseUrl}${endpoint.startsWith("/") ? endpoint : `/${endpoint}`}`;
|
|
318
|
+
const response = await fetch(url, {
|
|
319
|
+
...options,
|
|
320
|
+
headers: {
|
|
321
|
+
"Content-Type": "application/json",
|
|
322
|
+
"Authorization": `Bearer ${this.jwtToken}`,
|
|
323
|
+
...options.headers
|
|
324
|
+
}
|
|
325
|
+
});
|
|
326
|
+
if (!response.ok) {
|
|
327
|
+
const errorText = await response.text();
|
|
328
|
+
throw new Error(`API request failed: ${response.status} ${errorText}`);
|
|
329
|
+
}
|
|
330
|
+
return await response.json();
|
|
331
|
+
} catch (error) {
|
|
332
|
+
console.error("[Hyve SDK] API call failed:", error);
|
|
333
|
+
throw error;
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
/**
|
|
337
|
+
* Gets the user's inventory
|
|
338
|
+
* @returns Promise resolving to the user's inventory
|
|
339
|
+
*/
|
|
340
|
+
async getInventory() {
|
|
341
|
+
return this.callApi("/api/v1/inventory");
|
|
342
|
+
}
|
|
343
|
+
/**
|
|
344
|
+
* Gets a specific inventory item by ID
|
|
345
|
+
* @param itemId The inventory item ID
|
|
346
|
+
* @returns Promise resolving to the inventory item details
|
|
347
|
+
*/
|
|
348
|
+
async getInventoryItem(itemId) {
|
|
349
|
+
return this.callApi(`/api/v1/inventory/${itemId}`);
|
|
350
|
+
}
|
|
294
351
|
/**
|
|
295
352
|
* Updates the telemetry configuration
|
|
296
353
|
* @param config New telemetry configuration
|
|
@@ -300,8 +357,13 @@ var HyveClient = class {
|
|
|
300
357
|
...this.telemetryConfig,
|
|
301
358
|
...config
|
|
302
359
|
};
|
|
303
|
-
|
|
304
|
-
|
|
360
|
+
if (config.apiBaseUrl !== void 0) {
|
|
361
|
+
this.apiBaseUrl = config.apiBaseUrl;
|
|
362
|
+
} else if (config.isDev !== void 0) {
|
|
363
|
+
this.apiBaseUrl = config.isDev ? "https://product-api.dev.hyve.gg" : "https://product-api.prod.hyve.gg";
|
|
364
|
+
}
|
|
365
|
+
console.log("[Hyve SDK] Config updated");
|
|
366
|
+
console.log("[Hyve SDK] API Base URL:", this.apiBaseUrl);
|
|
305
367
|
}
|
|
306
368
|
/**
|
|
307
369
|
* Gets the current user ID
|
|
@@ -317,6 +379,20 @@ var HyveClient = class {
|
|
|
317
379
|
getSessionId() {
|
|
318
380
|
return this.sessionId;
|
|
319
381
|
}
|
|
382
|
+
/**
|
|
383
|
+
* Gets the current JWT token
|
|
384
|
+
* @returns Current JWT token or null if not available
|
|
385
|
+
*/
|
|
386
|
+
getJwtToken() {
|
|
387
|
+
return this.jwtToken;
|
|
388
|
+
}
|
|
389
|
+
/**
|
|
390
|
+
* Gets the API base URL
|
|
391
|
+
* @returns API base URL
|
|
392
|
+
*/
|
|
393
|
+
getApiBaseUrl() {
|
|
394
|
+
return this.apiBaseUrl;
|
|
395
|
+
}
|
|
320
396
|
/**
|
|
321
397
|
* Checks if user is authenticated
|
|
322
398
|
* @returns Boolean indicating authentication status
|
|
@@ -324,11 +400,19 @@ var HyveClient = class {
|
|
|
324
400
|
isUserAuthenticated() {
|
|
325
401
|
return this.userId !== null;
|
|
326
402
|
}
|
|
403
|
+
/**
|
|
404
|
+
* Checks if JWT token is available
|
|
405
|
+
* @returns Boolean indicating if JWT token is present
|
|
406
|
+
*/
|
|
407
|
+
hasJwtToken() {
|
|
408
|
+
return this.jwtToken !== null;
|
|
409
|
+
}
|
|
327
410
|
/**
|
|
328
411
|
* Logs out the current user
|
|
329
412
|
*/
|
|
330
413
|
logout() {
|
|
331
414
|
this.userId = null;
|
|
415
|
+
this.jwtToken = null;
|
|
332
416
|
console.log("[Hyve SDK] User logged out");
|
|
333
417
|
}
|
|
334
418
|
/**
|
package/dist/index.mjs
CHANGED
|
@@ -8,7 +8,8 @@ function parseUrlParams(searchParams) {
|
|
|
8
8
|
message: params.get("message") || "",
|
|
9
9
|
gameStartTab: params.get("game_start_tab") || "",
|
|
10
10
|
hyveToken: params.get("hyve-token") || "",
|
|
11
|
-
platform: params.get("platform") || ""
|
|
11
|
+
platform: params.get("platform") || "",
|
|
12
|
+
hyveAccess: params.get("hyve-access") || ""
|
|
12
13
|
};
|
|
13
14
|
}
|
|
14
15
|
function validateSignature(signature, message) {
|
|
@@ -153,9 +154,10 @@ function isDomainAllowed(allowedDomains, hostname) {
|
|
|
153
154
|
// src/core/client.ts
|
|
154
155
|
var HyveClient = class {
|
|
155
156
|
telemetryConfig;
|
|
156
|
-
|
|
157
|
+
apiBaseUrl;
|
|
157
158
|
sessionId;
|
|
158
159
|
userId = null;
|
|
160
|
+
jwtToken = null;
|
|
159
161
|
/**
|
|
160
162
|
* Creates a new HyveClient instance
|
|
161
163
|
* @param config Optional telemetry configuration
|
|
@@ -166,10 +168,15 @@ var HyveClient = class {
|
|
|
166
168
|
// Default to dev environment
|
|
167
169
|
...config
|
|
168
170
|
};
|
|
169
|
-
|
|
171
|
+
if (this.telemetryConfig.apiBaseUrl) {
|
|
172
|
+
this.apiBaseUrl = this.telemetryConfig.apiBaseUrl;
|
|
173
|
+
} else {
|
|
174
|
+
this.apiBaseUrl = this.telemetryConfig.isDev ? "https://product-api.dev.hyve.gg" : "https://product-api.prod.hyve.gg";
|
|
175
|
+
}
|
|
170
176
|
this.sessionId = generateUUID();
|
|
171
177
|
console.log("[Hyve SDK] Client initialized with sessionId:", this.sessionId);
|
|
172
|
-
console.log("[Hyve SDK]
|
|
178
|
+
console.log("[Hyve SDK] API Base URL:", this.apiBaseUrl);
|
|
179
|
+
console.log("[Hyve SDK] Environment:", this.telemetryConfig.isDev ? "dev" : "prod");
|
|
173
180
|
}
|
|
174
181
|
/**
|
|
175
182
|
* Authenticates a user from URL parameters
|
|
@@ -179,6 +186,10 @@ var HyveClient = class {
|
|
|
179
186
|
async authenticateFromUrl(urlParams) {
|
|
180
187
|
try {
|
|
181
188
|
const params = urlParams ? parseUrlParams(urlParams) : parseUrlParams(window.location.search);
|
|
189
|
+
if (params.hyveAccess) {
|
|
190
|
+
this.jwtToken = params.hyveAccess;
|
|
191
|
+
console.log("[Hyve SDK] JWT token extracted from hyve-access parameter");
|
|
192
|
+
}
|
|
182
193
|
const authResult = verifyAuthentication({
|
|
183
194
|
hyveToken: params.hyveToken,
|
|
184
195
|
signature: params.signature,
|
|
@@ -237,7 +248,8 @@ var HyveClient = class {
|
|
|
237
248
|
event_details: finalEventDetails
|
|
238
249
|
};
|
|
239
250
|
console.log("[Hyve Telemetry] Sending event:", telemetryEvent);
|
|
240
|
-
const
|
|
251
|
+
const telemetryUrl = `${this.apiBaseUrl}/api/v1/partners/analytics/events`;
|
|
252
|
+
const response = await fetch(telemetryUrl, {
|
|
241
253
|
method: "POST",
|
|
242
254
|
headers: {
|
|
243
255
|
"Content-Type": "application/json",
|
|
@@ -258,6 +270,51 @@ var HyveClient = class {
|
|
|
258
270
|
return false;
|
|
259
271
|
}
|
|
260
272
|
}
|
|
273
|
+
/**
|
|
274
|
+
* Makes an authenticated API call using the JWT token
|
|
275
|
+
* @param endpoint API endpoint path (will be appended to base URL)
|
|
276
|
+
* @param options Fetch options (method, body, etc.)
|
|
277
|
+
* @returns Promise resolving to the API response
|
|
278
|
+
*/
|
|
279
|
+
async callApi(endpoint, options = {}) {
|
|
280
|
+
if (!this.jwtToken) {
|
|
281
|
+
throw new Error("[Hyve SDK] No JWT token available. Call authenticateFromUrl first.");
|
|
282
|
+
}
|
|
283
|
+
try {
|
|
284
|
+
const url = `${this.apiBaseUrl}${endpoint.startsWith("/") ? endpoint : `/${endpoint}`}`;
|
|
285
|
+
const response = await fetch(url, {
|
|
286
|
+
...options,
|
|
287
|
+
headers: {
|
|
288
|
+
"Content-Type": "application/json",
|
|
289
|
+
"Authorization": `Bearer ${this.jwtToken}`,
|
|
290
|
+
...options.headers
|
|
291
|
+
}
|
|
292
|
+
});
|
|
293
|
+
if (!response.ok) {
|
|
294
|
+
const errorText = await response.text();
|
|
295
|
+
throw new Error(`API request failed: ${response.status} ${errorText}`);
|
|
296
|
+
}
|
|
297
|
+
return await response.json();
|
|
298
|
+
} catch (error) {
|
|
299
|
+
console.error("[Hyve SDK] API call failed:", error);
|
|
300
|
+
throw error;
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
/**
|
|
304
|
+
* Gets the user's inventory
|
|
305
|
+
* @returns Promise resolving to the user's inventory
|
|
306
|
+
*/
|
|
307
|
+
async getInventory() {
|
|
308
|
+
return this.callApi("/api/v1/inventory");
|
|
309
|
+
}
|
|
310
|
+
/**
|
|
311
|
+
* Gets a specific inventory item by ID
|
|
312
|
+
* @param itemId The inventory item ID
|
|
313
|
+
* @returns Promise resolving to the inventory item details
|
|
314
|
+
*/
|
|
315
|
+
async getInventoryItem(itemId) {
|
|
316
|
+
return this.callApi(`/api/v1/inventory/${itemId}`);
|
|
317
|
+
}
|
|
261
318
|
/**
|
|
262
319
|
* Updates the telemetry configuration
|
|
263
320
|
* @param config New telemetry configuration
|
|
@@ -267,8 +324,13 @@ var HyveClient = class {
|
|
|
267
324
|
...this.telemetryConfig,
|
|
268
325
|
...config
|
|
269
326
|
};
|
|
270
|
-
|
|
271
|
-
|
|
327
|
+
if (config.apiBaseUrl !== void 0) {
|
|
328
|
+
this.apiBaseUrl = config.apiBaseUrl;
|
|
329
|
+
} else if (config.isDev !== void 0) {
|
|
330
|
+
this.apiBaseUrl = config.isDev ? "https://product-api.dev.hyve.gg" : "https://product-api.prod.hyve.gg";
|
|
331
|
+
}
|
|
332
|
+
console.log("[Hyve SDK] Config updated");
|
|
333
|
+
console.log("[Hyve SDK] API Base URL:", this.apiBaseUrl);
|
|
272
334
|
}
|
|
273
335
|
/**
|
|
274
336
|
* Gets the current user ID
|
|
@@ -284,6 +346,20 @@ var HyveClient = class {
|
|
|
284
346
|
getSessionId() {
|
|
285
347
|
return this.sessionId;
|
|
286
348
|
}
|
|
349
|
+
/**
|
|
350
|
+
* Gets the current JWT token
|
|
351
|
+
* @returns Current JWT token or null if not available
|
|
352
|
+
*/
|
|
353
|
+
getJwtToken() {
|
|
354
|
+
return this.jwtToken;
|
|
355
|
+
}
|
|
356
|
+
/**
|
|
357
|
+
* Gets the API base URL
|
|
358
|
+
* @returns API base URL
|
|
359
|
+
*/
|
|
360
|
+
getApiBaseUrl() {
|
|
361
|
+
return this.apiBaseUrl;
|
|
362
|
+
}
|
|
287
363
|
/**
|
|
288
364
|
* Checks if user is authenticated
|
|
289
365
|
* @returns Boolean indicating authentication status
|
|
@@ -291,11 +367,19 @@ var HyveClient = class {
|
|
|
291
367
|
isUserAuthenticated() {
|
|
292
368
|
return this.userId !== null;
|
|
293
369
|
}
|
|
370
|
+
/**
|
|
371
|
+
* Checks if JWT token is available
|
|
372
|
+
* @returns Boolean indicating if JWT token is present
|
|
373
|
+
*/
|
|
374
|
+
hasJwtToken() {
|
|
375
|
+
return this.jwtToken !== null;
|
|
376
|
+
}
|
|
294
377
|
/**
|
|
295
378
|
* Logs out the current user
|
|
296
379
|
*/
|
|
297
380
|
logout() {
|
|
298
381
|
this.userId = null;
|
|
382
|
+
this.jwtToken = null;
|
|
299
383
|
console.log("[Hyve SDK] User logged out");
|
|
300
384
|
}
|
|
301
385
|
/**
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hyve-sdk/js",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.2",
|
|
4
4
|
"description": "Hyve SDK - TypeScript wrapper for Hyve game server integration",
|
|
5
5
|
"private": false,
|
|
6
6
|
"publishConfig": {
|
|
7
|
-
"access": "
|
|
7
|
+
"access": "public",
|
|
8
8
|
"registry": "https://registry.npmjs.org/"
|
|
9
9
|
},
|
|
10
10
|
"main": "dist/index.js",
|
|
@@ -51,7 +51,7 @@
|
|
|
51
51
|
"lint": "eslint . --max-warnings 0",
|
|
52
52
|
"check-types": "tsc --noEmit",
|
|
53
53
|
"build": "tsup src/index.ts --format cjs,esm --dts --clean",
|
|
54
|
-
"publish:npm": "pnpm publish --access
|
|
55
|
-
"publish:dry-run": "pnpm publish --dry-run --access
|
|
54
|
+
"publish:npm": "pnpm publish --access public --no-git-checks",
|
|
55
|
+
"publish:dry-run": "pnpm publish --dry-run --access public --no-git-checks"
|
|
56
56
|
}
|
|
57
57
|
}
|