@usions/sdk 2.0.1 → 2.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.
@@ -0,0 +1,114 @@
1
+ /**
2
+ * Usion SDK Wallet Module — wallet and payment operations
3
+ */
4
+
5
+ import { getNextRequestId } from './core.js';
6
+
7
+ /**
8
+ * @param {object} Usion - Reference to the main Usion object
9
+ */
10
+ export function createWalletModule(Usion) {
11
+ return {
12
+ _balance: null,
13
+ _balanceChangeHandler: null,
14
+
15
+ /**
16
+ * Get current wallet balance
17
+ * @returns {Promise<number>} Balance in credits
18
+ */
19
+ getBalance: function() {
20
+ const self = this;
21
+
22
+ // If we have cached balance, return it
23
+ if (self._balance !== null) {
24
+ return Promise.resolve(self._balance);
25
+ }
26
+
27
+ return Usion._request('GET_BALANCE', {}).then(function(response) {
28
+ self._balance = response.balance;
29
+ return response.balance;
30
+ });
31
+ },
32
+
33
+ /**
34
+ * Check if user has enough credits
35
+ * @param {number} amount - Amount to check
36
+ * @returns {Promise<boolean>}
37
+ */
38
+ hasCredits: function(amount) {
39
+ return this.getBalance().then(function(balance) {
40
+ return balance >= amount;
41
+ });
42
+ },
43
+
44
+ /**
45
+ * Request payment from user with balance check
46
+ * @param {number} amount - Credit amount to charge
47
+ * @param {string} reason - Description shown to user
48
+ * @param {object} data - Optional additional data
49
+ * @returns {Promise} Resolves on payment success, rejects on failure
50
+ */
51
+ requestPayment: function(amount, reason, data) {
52
+ const self = this;
53
+
54
+ return new Promise(function(resolve, reject) {
55
+ const requestId = getNextRequestId();
56
+ const timeoutMs = 60000;
57
+
58
+ // Listen for response
59
+ function handler(event) {
60
+ let response;
61
+ try {
62
+ response = typeof event.data === 'string' ? JSON.parse(event.data) : event.data;
63
+ } catch (e) {
64
+ return;
65
+ }
66
+
67
+ // Only accept responses for this specific payment request.
68
+ if (response._requestId !== requestId) {
69
+ return;
70
+ }
71
+
72
+ if (response.type === 'PAYMENT_SUCCESS') {
73
+ clearTimeout(timer);
74
+ window.removeEventListener('message', handler);
75
+ // Update cached balance
76
+ if (response.newBalance !== undefined) {
77
+ self._balance = response.newBalance;
78
+ } else if (self._balance !== null) {
79
+ self._balance -= amount;
80
+ }
81
+ resolve(response);
82
+ } else if (response.type === 'PAYMENT_FAILED') {
83
+ clearTimeout(timer);
84
+ window.removeEventListener('message', handler);
85
+ reject(new Error(response.reason || 'Payment failed'));
86
+ }
87
+ }
88
+
89
+ window.addEventListener('message', handler);
90
+ const timer = setTimeout(function() {
91
+ window.removeEventListener('message', handler);
92
+ reject(new Error('Payment confirmation timeout'));
93
+ }, timeoutMs);
94
+
95
+ // Send payment request
96
+ Usion._post({
97
+ type: 'PAYMENT_REQUEST',
98
+ _requestId: requestId,
99
+ amount: amount,
100
+ reason: reason,
101
+ data: data
102
+ });
103
+ });
104
+ },
105
+
106
+ /**
107
+ * Listen for balance changes
108
+ * @param {function} callback - Called with new balance
109
+ */
110
+ onBalanceChange: function(callback) {
111
+ this._balanceChangeHandler = callback;
112
+ }
113
+ };
114
+ }
package/types/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Usion Mini App SDK v2.0 — TypeScript Declarations
2
+ * Usion Mini App SDK v2.1 — TypeScript Declarations
3
3
  */
4
4
 
5
5
  // ─── Config & Init ───────────────────────────────────────────────
@@ -12,6 +12,9 @@ export interface UsionConfig {
12
12
  sessionId?: string;
13
13
  sessionData?: Record<string, any>;
14
14
  balance?: number;
15
+ results?: SavedResult[];
16
+ theme?: 'light' | 'dark';
17
+ language?: string;
15
18
  socketUrl?: string;
16
19
  roomId?: string;
17
20
  playerIds?: string[];
@@ -55,6 +58,14 @@ export interface StorageModule {
55
58
  keys(): Promise<string[]>;
56
59
  }
57
60
 
61
+ // ─── File Storage ───────────────────────────────────────────────
62
+
63
+ export interface FileStorageModule {
64
+ set(key: string, base64Data: string, mimeType: string): Promise<void>;
65
+ get(key: string): Promise<{ base64Data: string; mimeType: string } | null>;
66
+ remove(key: string): Promise<void>;
67
+ }
68
+
58
69
  // ─── Wallet ──────────────────────────────────────────────────────
59
70
 
60
71
  export interface WalletModule {
@@ -220,6 +231,37 @@ export interface GameModule {
220
231
  on(event: string, callback: (data: any) => void): void;
221
232
  }
222
233
 
234
+ // ─── Results ────────────────────────────────────────────────────
235
+
236
+ export interface SavedResult {
237
+ id: string;
238
+ data: string;
239
+ metadata?: {
240
+ thumbnail_url?: string;
241
+ title?: string;
242
+ type?: string;
243
+ };
244
+ created_at?: string;
245
+ }
246
+
247
+ // ─── Bot ────────────────────────────────────────────────────────
248
+
249
+ export interface BotMessage {
250
+ id: string;
251
+ content: string;
252
+ content_type?: string;
253
+ components?: any[];
254
+ sender_id: string;
255
+ }
256
+
257
+ export interface BotModule {
258
+ callAction(action: string, data?: Record<string, any>): Promise<any>;
259
+ sendMessage(text: string): void;
260
+ updateContext(ctx: Record<string, any>): void;
261
+ close(result?: any): void;
262
+ onMessage(callback: (message: BotMessage) => void): void;
263
+ }
264
+
223
265
  // ─── Selection Grid ──────────────────────────────────────────────
224
266
 
225
267
  export interface SelectionGrid {
@@ -235,7 +277,11 @@ export interface UsionSDK {
235
277
 
236
278
  // Lifecycle
237
279
  init(callback: (config: UsionConfig) => void): void;
238
- exit(): void;
280
+ exit(options?: { backCount?: number }): void;
281
+
282
+ // Theme & Locale
283
+ getTheme(): 'light' | 'dark';
284
+ getLanguage(): string;
239
285
 
240
286
  // Payments
241
287
  requestPayment(amount: number, reason: string, data?: Record<string, any>): Promise<PaymentResponse>;
@@ -244,6 +290,18 @@ export interface UsionSDK {
244
290
  submit(data: Record<string, any>): void;
245
291
  error(message: string): void;
246
292
 
293
+ // Results (server-side persistence)
294
+ saveResult(data: string, metadata?: { thumbnail_url?: string; title?: string; type?: string }): Promise<SavedResult>;
295
+ deleteResult(resultId: string): Promise<void>;
296
+ getResults(): SavedResult[];
297
+
298
+ // Back button
299
+ claimBackButton(callback: () => void): void;
300
+ releaseBackButton(): void;
301
+
302
+ // Download
303
+ download(url: string, filename?: string): Promise<{ success?: boolean; error?: string }>;
304
+
247
305
  // Sharing
248
306
  share(contentType: 'audio' | 'image' | 'video' | 'text' | 'mixed', data: ShareData): void;
249
307
 
@@ -266,9 +324,11 @@ export interface UsionSDK {
266
324
  // Modules
267
325
  user: UserModule;
268
326
  storage: StorageModule;
327
+ fileStorage: FileStorageModule;
269
328
  wallet: WalletModule;
270
329
  session: SessionModule;
271
330
  chat: ChatModule;
331
+ bot: BotModule;
272
332
  game: GameModule;
273
333
  }
274
334