@usions/sdk 2.0.2 → 2.1.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.
@@ -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
+ }
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Browser IIFE build — sets up window.Usion global.
3
+ * Import this module for its side effect only.
4
+ */
5
+ export {};
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[];
@@ -27,6 +30,8 @@ export interface PaymentResponse {
27
30
  success: boolean;
28
31
  reason?: string;
29
32
  newBalance?: number;
33
+ receiptToken?: string;
34
+ transactionId?: string;
30
35
  }
31
36
 
32
37
  // ─── User ────────────────────────────────────────────────────────
@@ -55,6 +60,14 @@ export interface StorageModule {
55
60
  keys(): Promise<string[]>;
56
61
  }
57
62
 
63
+ // ─── File Storage ───────────────────────────────────────────────
64
+
65
+ export interface FileStorageModule {
66
+ set(key: string, base64Data: string, mimeType: string): Promise<void>;
67
+ get(key: string): Promise<{ base64Data: string; mimeType: string } | null>;
68
+ remove(key: string): Promise<void>;
69
+ }
70
+
58
71
  // ─── Wallet ──────────────────────────────────────────────────────
59
72
 
60
73
  export interface WalletModule {
@@ -220,6 +233,37 @@ export interface GameModule {
220
233
  on(event: string, callback: (data: any) => void): void;
221
234
  }
222
235
 
236
+ // ─── Results ────────────────────────────────────────────────────
237
+
238
+ export interface SavedResult {
239
+ id: string;
240
+ data: string;
241
+ metadata?: {
242
+ thumbnail_url?: string;
243
+ title?: string;
244
+ type?: string;
245
+ };
246
+ created_at?: string;
247
+ }
248
+
249
+ // ─── Bot ────────────────────────────────────────────────────────
250
+
251
+ export interface BotMessage {
252
+ id: string;
253
+ content: string;
254
+ content_type?: string;
255
+ components?: any[];
256
+ sender_id: string;
257
+ }
258
+
259
+ export interface BotModule {
260
+ callAction(action: string, data?: Record<string, any>): Promise<any>;
261
+ sendMessage(text: string): void;
262
+ updateContext(ctx: Record<string, any>): void;
263
+ close(result?: any): void;
264
+ onMessage(callback: (message: BotMessage) => void): void;
265
+ }
266
+
223
267
  // ─── Selection Grid ──────────────────────────────────────────────
224
268
 
225
269
  export interface SelectionGrid {
@@ -235,7 +279,11 @@ export interface UsionSDK {
235
279
 
236
280
  // Lifecycle
237
281
  init(callback: (config: UsionConfig) => void): void;
238
- exit(): void;
282
+ exit(options?: { backCount?: number }): void;
283
+
284
+ // Theme & Locale
285
+ getTheme(): 'light' | 'dark';
286
+ getLanguage(): string;
239
287
 
240
288
  // Payments
241
289
  requestPayment(amount: number, reason: string, data?: Record<string, any>): Promise<PaymentResponse>;
@@ -244,6 +292,18 @@ export interface UsionSDK {
244
292
  submit(data: Record<string, any>): void;
245
293
  error(message: string): void;
246
294
 
295
+ // Results (server-side persistence)
296
+ saveResult(data: string, metadata?: { thumbnail_url?: string; title?: string; type?: string }): Promise<SavedResult>;
297
+ deleteResult(resultId: string): Promise<void>;
298
+ getResults(): SavedResult[];
299
+
300
+ // Back button
301
+ claimBackButton(callback: () => void): void;
302
+ releaseBackButton(): void;
303
+
304
+ // Download
305
+ download(url: string, filename?: string): Promise<{ success?: boolean; error?: string }>;
306
+
247
307
  // Sharing
248
308
  share(contentType: 'audio' | 'image' | 'video' | 'text' | 'mixed', data: ShareData): void;
249
309
 
@@ -266,9 +326,11 @@ export interface UsionSDK {
266
326
  // Modules
267
327
  user: UserModule;
268
328
  storage: StorageModule;
329
+ fileStorage: FileStorageModule;
269
330
  wallet: WalletModule;
270
331
  session: SessionModule;
271
332
  chat: ChatModule;
333
+ bot: BotModule;
272
334
  game: GameModule;
273
335
  }
274
336