@usions/sdk 2.0.1
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 +144 -0
- package/package.json +37 -0
- package/src/browser.js +1793 -0
- package/src/index.js +55 -0
- package/types/index.d.ts +278 -0
package/src/index.js
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @usion/sdk — ES module wrapper for the Usion Mini App SDK
|
|
3
|
+
*
|
|
4
|
+
* Usage:
|
|
5
|
+
* import Usion from '@usion/sdk';
|
|
6
|
+
* // or
|
|
7
|
+
* import { Usion } from '@usion/sdk';
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
// Create a synthetic global for the IIFE to attach to
|
|
11
|
+
const _global = {};
|
|
12
|
+
|
|
13
|
+
// Execute the IIFE SDK with our synthetic global
|
|
14
|
+
(function (global) {
|
|
15
|
+
'use strict';
|
|
16
|
+
|
|
17
|
+
// ---- Begin inlined SDK bootstrap ----
|
|
18
|
+
// We re-export the Usion object that the IIFE creates.
|
|
19
|
+
// Rather than duplicating the full SDK source, we dynamically load it.
|
|
20
|
+
// For the npm module path, consumers import this file;
|
|
21
|
+
// for script-tag usage, they load browser.js directly.
|
|
22
|
+
// ---- End inlined SDK bootstrap ----
|
|
23
|
+
|
|
24
|
+
// Attach a marker so consumers can detect the module version
|
|
25
|
+
global.__USION_SDK_MODULE__ = true;
|
|
26
|
+
})(_global);
|
|
27
|
+
|
|
28
|
+
// Dynamic import approach: read the IIFE SDK and evaluate it
|
|
29
|
+
import { readFileSync } from 'fs';
|
|
30
|
+
import { fileURLToPath } from 'url';
|
|
31
|
+
import { dirname, join } from 'path';
|
|
32
|
+
|
|
33
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
34
|
+
const __dirname = dirname(__filename);
|
|
35
|
+
|
|
36
|
+
// Create a minimal global-like object for the IIFE
|
|
37
|
+
const _sdkGlobal = {};
|
|
38
|
+
const sdkSource = readFileSync(join(__dirname, 'browser.js'), 'utf-8');
|
|
39
|
+
|
|
40
|
+
// Replace the IIFE's global reference to capture the Usion object
|
|
41
|
+
const wrappedSource = sdkSource
|
|
42
|
+
.replace(
|
|
43
|
+
/\}\)\(typeof window !== 'undefined' \? window : this\);?\s*$/,
|
|
44
|
+
'})(_sdkGlobal);'
|
|
45
|
+
);
|
|
46
|
+
|
|
47
|
+
// Execute in a function scope to avoid polluting
|
|
48
|
+
const execFn = new Function('_sdkGlobal', wrappedSource);
|
|
49
|
+
execFn(_sdkGlobal);
|
|
50
|
+
|
|
51
|
+
/** The Usion SDK instance */
|
|
52
|
+
const Usion = _sdkGlobal.Usion;
|
|
53
|
+
|
|
54
|
+
export { Usion };
|
|
55
|
+
export default Usion;
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Usion Mini App SDK v2.0 — TypeScript Declarations
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
// ─── Config & Init ───────────────────────────────────────────────
|
|
6
|
+
|
|
7
|
+
export interface UsionConfig {
|
|
8
|
+
userId?: string;
|
|
9
|
+
userName?: string;
|
|
10
|
+
userAvatar?: string;
|
|
11
|
+
authToken?: string;
|
|
12
|
+
sessionId?: string;
|
|
13
|
+
sessionData?: Record<string, any>;
|
|
14
|
+
balance?: number;
|
|
15
|
+
socketUrl?: string;
|
|
16
|
+
roomId?: string;
|
|
17
|
+
playerIds?: string[];
|
|
18
|
+
serviceId?: string;
|
|
19
|
+
serviceName?: string;
|
|
20
|
+
apiUrl?: string;
|
|
21
|
+
connectionMode?: 'platform' | 'direct';
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// ─── Payment ─────────────────────────────────────────────────────
|
|
25
|
+
|
|
26
|
+
export interface PaymentResponse {
|
|
27
|
+
success: boolean;
|
|
28
|
+
reason?: string;
|
|
29
|
+
newBalance?: number;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// ─── User ────────────────────────────────────────────────────────
|
|
33
|
+
|
|
34
|
+
export interface UserProfile {
|
|
35
|
+
id: string;
|
|
36
|
+
name: string;
|
|
37
|
+
avatar: string;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface UserModule {
|
|
41
|
+
getId(): string | null;
|
|
42
|
+
getName(): string | null;
|
|
43
|
+
getAvatar(): string | null;
|
|
44
|
+
getToken(): string | null;
|
|
45
|
+
getProfile(): Promise<UserProfile>;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// ─── Storage ─────────────────────────────────────────────────────
|
|
49
|
+
|
|
50
|
+
export interface StorageModule {
|
|
51
|
+
get(key: string): Promise<any>;
|
|
52
|
+
set(key: string, value: any): Promise<void>;
|
|
53
|
+
remove(key: string): Promise<void>;
|
|
54
|
+
clear(): Promise<void>;
|
|
55
|
+
keys(): Promise<string[]>;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// ─── Wallet ──────────────────────────────────────────────────────
|
|
59
|
+
|
|
60
|
+
export interface WalletModule {
|
|
61
|
+
getBalance(): Promise<number>;
|
|
62
|
+
hasCredits(amount: number): Promise<boolean>;
|
|
63
|
+
requestPayment(amount: number, reason: string, data?: Record<string, any>): Promise<PaymentResponse>;
|
|
64
|
+
onBalanceChange(callback: (balance: number) => void): void;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// ─── Session ─────────────────────────────────────────────────────
|
|
68
|
+
|
|
69
|
+
export interface SessionModule {
|
|
70
|
+
getId(): string | null;
|
|
71
|
+
getData(key?: string): any;
|
|
72
|
+
setData(keyOrData: string | Record<string, any>, value?: any): void;
|
|
73
|
+
clear(): void;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// ─── Chat ────────────────────────────────────────────────────────
|
|
77
|
+
|
|
78
|
+
export interface ChatCreationResult {
|
|
79
|
+
chatId: string;
|
|
80
|
+
peerName: string;
|
|
81
|
+
peerUsername: string;
|
|
82
|
+
peerAvatar: string;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export interface ChatModule {
|
|
86
|
+
sendMessage(recipientId: string, message: string): Promise<{ success: boolean; reason?: string }>;
|
|
87
|
+
createPersonalChat(peerUserId: string): Promise<ChatCreationResult>;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// ─── Share ────────────────────────────────────────────────────────
|
|
91
|
+
|
|
92
|
+
export interface ShareMediaItem {
|
|
93
|
+
type: 'image' | 'video' | 'audio';
|
|
94
|
+
url: string;
|
|
95
|
+
thumbnailUrl?: string;
|
|
96
|
+
width?: number;
|
|
97
|
+
height?: number;
|
|
98
|
+
duration?: number;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export interface ShareData {
|
|
102
|
+
text?: string;
|
|
103
|
+
audioUrl?: string;
|
|
104
|
+
imageUrl?: string;
|
|
105
|
+
videoUrl?: string;
|
|
106
|
+
thumbnailUrl?: string;
|
|
107
|
+
width?: number;
|
|
108
|
+
height?: number;
|
|
109
|
+
duration?: number;
|
|
110
|
+
media?: ShareMediaItem[];
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// ─── Game ────────────────────────────────────────────────────────
|
|
114
|
+
|
|
115
|
+
export interface GameJoinResult {
|
|
116
|
+
room_id: string;
|
|
117
|
+
player_id: string;
|
|
118
|
+
sequence?: number;
|
|
119
|
+
error?: string;
|
|
120
|
+
message?: string;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export interface ActionResult {
|
|
124
|
+
success: boolean;
|
|
125
|
+
sequence?: number;
|
|
126
|
+
error?: string;
|
|
127
|
+
message?: string;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export interface DirectConnectionConfig {
|
|
131
|
+
roomId?: string;
|
|
132
|
+
serviceId?: string;
|
|
133
|
+
apiUrl?: string;
|
|
134
|
+
token?: string;
|
|
135
|
+
protocolVersion?: string;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export interface PlayerJoinedData {
|
|
139
|
+
room_id: string;
|
|
140
|
+
player_id: string;
|
|
141
|
+
player_ids: string[];
|
|
142
|
+
status?: string;
|
|
143
|
+
game_state?: Record<string, any>;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export interface PlayerLeftData {
|
|
147
|
+
room_id: string;
|
|
148
|
+
player_id: string;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export interface StateUpdateData {
|
|
152
|
+
room_id: string;
|
|
153
|
+
game_state?: Record<string, any>;
|
|
154
|
+
current_turn?: string;
|
|
155
|
+
status?: string;
|
|
156
|
+
sequence?: number;
|
|
157
|
+
[key: string]: any;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export interface GameActionData {
|
|
161
|
+
room_id: string;
|
|
162
|
+
player_id: string;
|
|
163
|
+
action_type: string;
|
|
164
|
+
action_data: Record<string, any>;
|
|
165
|
+
sequence?: number;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
export interface GameFinishedData {
|
|
169
|
+
room_id: string;
|
|
170
|
+
winner_ids: string[];
|
|
171
|
+
reason?: string;
|
|
172
|
+
forfeiter?: string;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
export interface SyncData {
|
|
176
|
+
room_id: string;
|
|
177
|
+
actions: GameActionData[];
|
|
178
|
+
game_state: Record<string, any>;
|
|
179
|
+
sequence: number;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
export interface RematchData {
|
|
183
|
+
room_id: string;
|
|
184
|
+
player_id: string;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
export interface GameModule {
|
|
188
|
+
// Connection
|
|
189
|
+
connect(socketUrl?: string, token?: string): Promise<void>;
|
|
190
|
+
connectDirect(config?: DirectConnectionConfig): Promise<void>;
|
|
191
|
+
disconnect(): void;
|
|
192
|
+
isConnected(): boolean;
|
|
193
|
+
|
|
194
|
+
// Room & actions
|
|
195
|
+
join(roomId?: string): Promise<GameJoinResult>;
|
|
196
|
+
leave(): void;
|
|
197
|
+
action(actionType: string, actionData?: Record<string, any>): Promise<ActionResult>;
|
|
198
|
+
realtime(actionType: string, actionData?: Record<string, any>): void;
|
|
199
|
+
requestSync(lastSequence?: number): void;
|
|
200
|
+
requestRematch(): void;
|
|
201
|
+
forfeit(): Promise<{ success: boolean; error?: string }>;
|
|
202
|
+
|
|
203
|
+
// Event handlers
|
|
204
|
+
onJoined(callback: (data: GameJoinResult) => void): void;
|
|
205
|
+
onPlayerJoined(callback: (data: PlayerJoinedData) => void): void;
|
|
206
|
+
onPlayerLeft(callback: (data: PlayerLeftData) => void): void;
|
|
207
|
+
onStateUpdate(callback: (data: StateUpdateData) => void): void;
|
|
208
|
+
onSync(callback: (data: SyncData) => void): void;
|
|
209
|
+
onAction(callback: (data: GameActionData) => void): void;
|
|
210
|
+
onRealtime(callback: (data: Record<string, any>) => void): void;
|
|
211
|
+
onGameFinished(callback: (data: GameFinishedData) => void): void;
|
|
212
|
+
onGameRestarted(callback: (data: Record<string, any>) => void): void;
|
|
213
|
+
onError(callback: (data: { message: string; code?: string }) => void): void;
|
|
214
|
+
onRematchRequest(callback: (data: RematchData) => void): void;
|
|
215
|
+
onDisconnect(callback: (reason: string) => void): void;
|
|
216
|
+
onReconnect(callback: (attemptNumber: number) => void): void;
|
|
217
|
+
onConnectionError(callback: (error: Error) => void): void;
|
|
218
|
+
|
|
219
|
+
// Generic event listener
|
|
220
|
+
on(event: string, callback: (data: any) => void): void;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// ─── Selection Grid ──────────────────────────────────────────────
|
|
224
|
+
|
|
225
|
+
export interface SelectionGrid {
|
|
226
|
+
getSelected(): string | null;
|
|
227
|
+
clear(): void;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
// ─── Main SDK ────────────────────────────────────────────────────
|
|
231
|
+
|
|
232
|
+
export interface UsionSDK {
|
|
233
|
+
version: string;
|
|
234
|
+
config: UsionConfig;
|
|
235
|
+
|
|
236
|
+
// Lifecycle
|
|
237
|
+
init(callback: (config: UsionConfig) => void): void;
|
|
238
|
+
exit(): void;
|
|
239
|
+
|
|
240
|
+
// Payments
|
|
241
|
+
requestPayment(amount: number, reason: string, data?: Record<string, any>): Promise<PaymentResponse>;
|
|
242
|
+
|
|
243
|
+
// Service communication
|
|
244
|
+
submit(data: Record<string, any>): void;
|
|
245
|
+
error(message: string): void;
|
|
246
|
+
|
|
247
|
+
// Sharing
|
|
248
|
+
share(contentType: 'audio' | 'image' | 'video' | 'text' | 'mixed', data: ShareData): void;
|
|
249
|
+
|
|
250
|
+
// Logging
|
|
251
|
+
log(msg: string): void;
|
|
252
|
+
|
|
253
|
+
// Generic event listener
|
|
254
|
+
on(type: string, callback: (data: any) => void): void;
|
|
255
|
+
|
|
256
|
+
// UI helpers
|
|
257
|
+
setLoading(btn: HTMLElement | string, loading: boolean): void;
|
|
258
|
+
toggle(el: HTMLElement | string, show: boolean): void;
|
|
259
|
+
charCount(input: HTMLElement | string, counter: HTMLElement | string, max: number): void;
|
|
260
|
+
selectionGrid(
|
|
261
|
+
containerSelector: string,
|
|
262
|
+
itemSelector: string,
|
|
263
|
+
onChange: (selected: string, item: HTMLElement) => void
|
|
264
|
+
): SelectionGrid;
|
|
265
|
+
|
|
266
|
+
// Modules
|
|
267
|
+
user: UserModule;
|
|
268
|
+
storage: StorageModule;
|
|
269
|
+
wallet: WalletModule;
|
|
270
|
+
session: SessionModule;
|
|
271
|
+
chat: ChatModule;
|
|
272
|
+
game: GameModule;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
declare const Usion: UsionSDK;
|
|
276
|
+
|
|
277
|
+
export { Usion };
|
|
278
|
+
export default Usion;
|