capybara-game-sdk 0.0.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,246 @@
1
+ interface GameSDKConfig {
2
+ gameId: string;
3
+ onReady?: () => void;
4
+ devMode?: boolean;
5
+ }
6
+ interface User {
7
+ id: string;
8
+ displayName: string;
9
+ avatar?: string;
10
+ }
11
+ interface CreditBalance {
12
+ credits: number;
13
+ }
14
+ interface PaymentResult {
15
+ success: boolean;
16
+ newBalance: number;
17
+ error?: string;
18
+ }
19
+ interface Transaction {
20
+ amount: number;
21
+ description: string;
22
+ timestamp: number;
23
+ }
24
+
25
+ /**
26
+ * Secure communication bridge between iframe and parent app
27
+ * Uses postMessage with origin verification
28
+ */
29
+ declare class ParentBridge {
30
+ private allowedOrigins;
31
+ private messageHandlers;
32
+ private pendingRequests;
33
+ private requestIdCounter;
34
+ constructor(allowedOrigins?: string[]);
35
+ private setupMessageListener;
36
+ /**
37
+ * Send a message to parent and wait for response
38
+ */
39
+ private sendRequest;
40
+ /**
41
+ * Send a message to parent without expecting a response
42
+ */
43
+ private sendMessage;
44
+ /**
45
+ * Listen for messages from parent
46
+ */
47
+ on(type: string, handler: (data: any) => void): void;
48
+ /**
49
+ * Remove message listener
50
+ */
51
+ off(type: string): void;
52
+ /**
53
+ * Get current authenticated user
54
+ */
55
+ getCurrentUser(): Promise<User | null>;
56
+ /**
57
+ * Request user to login (triggers parent app UI)
58
+ */
59
+ requestLogin(): Promise<void>;
60
+ /**
61
+ * Logout current user
62
+ */
63
+ logout(): Promise<void>;
64
+ /**
65
+ * Get bearer token for authenticated user
66
+ */
67
+ getBearerToken(): Promise<string | null>;
68
+ /**
69
+ * Get user's credit balance
70
+ */
71
+ getBalance(): Promise<CreditBalance>;
72
+ /**
73
+ * Charge credits from user's balance
74
+ */
75
+ chargeCredits(amount: number, description: string): Promise<PaymentResult>;
76
+ /**
77
+ * Notify parent that SDK is ready
78
+ */
79
+ notifyReady(): void;
80
+ }
81
+
82
+ declare class AuthModule {
83
+ private bridge;
84
+ private currentUser;
85
+ private authChangeListeners;
86
+ constructor(bridge: ParentBridge);
87
+ private initialize;
88
+ private notifyAuthChange;
89
+ /**
90
+ * Get the currently authenticated user
91
+ */
92
+ getCurrentUser(): Promise<User | null>;
93
+ /**
94
+ * Get bearer token for the authenticated user
95
+ */
96
+ getBearerToken(): Promise<string | null>;
97
+ /**
98
+ * Request user authentication (triggers parent app UI)
99
+ */
100
+ requestLogin(): Promise<void>;
101
+ /**
102
+ * Logout the current user
103
+ */
104
+ logout(): Promise<void>;
105
+ /**
106
+ * Listen for authentication state changes
107
+ */
108
+ onAuthChange(callback: (user: User | null) => void): () => void;
109
+ /**
110
+ * Check if user is authenticated
111
+ */
112
+ isAuthenticated(): boolean;
113
+ }
114
+
115
+ declare class PaymentsModule {
116
+ private bridge;
117
+ private auth;
118
+ private devMode;
119
+ private currentBalance;
120
+ private balanceChangeListeners;
121
+ private transactions;
122
+ constructor(bridge: ParentBridge, auth: AuthModule, devMode: boolean);
123
+ private initialize;
124
+ private refreshBalance;
125
+ private notifyBalanceChange;
126
+ /**
127
+ * Get the current credit balance
128
+ */
129
+ getBalance(): Promise<CreditBalance>;
130
+ /**
131
+ * Charge credits from the user's balance
132
+ * In dev mode, this is a no-op (free)
133
+ */
134
+ charge(amount: number, description: string): Promise<PaymentResult>;
135
+ /**
136
+ * Listen for balance changes
137
+ */
138
+ onBalanceChange(callback: (balance: CreditBalance) => void): () => void;
139
+ /**
140
+ * Get transaction history
141
+ * Note: This only includes transactions made during the current session
142
+ * For full history, the parent app should provide an API
143
+ */
144
+ getHistory(): Promise<Transaction[]>;
145
+ /**
146
+ * Gift credits to another user (requires server-side implementation)
147
+ */
148
+ giftCredits(userId: string, amount: number, description: string): Promise<PaymentResult>;
149
+ /**
150
+ * Set development mode
151
+ */
152
+ setDevMode(enabled: boolean): void;
153
+ }
154
+
155
+ declare class GameSDK {
156
+ private static instance;
157
+ readonly auth: AuthModule;
158
+ readonly payments: PaymentsModule;
159
+ private bridge;
160
+ private config;
161
+ readonly gameId: string;
162
+ private constructor();
163
+ /**
164
+ * Initialize the Game SDK
165
+ */
166
+ static init(config: GameSDKConfig): Promise<GameSDK>;
167
+ /**
168
+ * Get the current SDK instance
169
+ */
170
+ static getInstance(): GameSDK;
171
+ /**
172
+ * Set development mode
173
+ */
174
+ setDevMode(enabled: boolean): void;
175
+ /**
176
+ * Get SDK configuration
177
+ */
178
+ getConfig(): Readonly<GameSDKConfig>;
179
+ /**
180
+ * Clean up SDK resources
181
+ */
182
+ destroy(): Promise<void>;
183
+ }
184
+
185
+ /**
186
+ * Storage module for persisting game data
187
+ * Data is automatically scoped to userId + gameId
188
+ */
189
+ declare class StorageModule {
190
+ private auth;
191
+ private gameId;
192
+ private apiClient;
193
+ constructor(gameId: string, auth: AuthModule);
194
+ /**
195
+ * Save data to storage
196
+ * Data is automatically scoped to current user and game
197
+ * Maximum size: 5MB
198
+ */
199
+ save<T = any>(data: T): Promise<void>;
200
+ /**
201
+ * Load data from storage
202
+ * Returns null if no data exists
203
+ */
204
+ load<T = any>(): Promise<T | null>;
205
+ /**
206
+ * Clear all stored data for current user and game
207
+ */
208
+ clear(): Promise<void>;
209
+ /**
210
+ * Check if data exists in storage
211
+ */
212
+ exists(): Promise<boolean>;
213
+ /**
214
+ * Get the size of stored data in bytes
215
+ */
216
+ getSize(): Promise<number>;
217
+ }
218
+
219
+ /**
220
+ * Hook to initialize the Game SDK
221
+ */
222
+ declare function useGameSDK(config: GameSDKConfig): {
223
+ sdk: GameSDK | null;
224
+ isReady: boolean;
225
+ error: Error | null;
226
+ };
227
+ /**
228
+ * Hook to access the current user
229
+ */
230
+ declare function useAuth(): {
231
+ user: User | null;
232
+ isLoading: boolean;
233
+ isAuthenticated: boolean;
234
+ login: () => Promise<void>;
235
+ logout: () => Promise<void>;
236
+ };
237
+ /**
238
+ * Hook to access credit balance
239
+ */
240
+ declare function useCredits(): {
241
+ balance: number;
242
+ isLoading: boolean;
243
+ charge: (amount: number, description: string) => Promise<PaymentResult>;
244
+ };
245
+
246
+ export { AuthModule, GameSDK, PaymentsModule, StorageModule, useAuth, useCredits, useGameSDK };
@@ -0,0 +1,246 @@
1
+ interface GameSDKConfig {
2
+ gameId: string;
3
+ onReady?: () => void;
4
+ devMode?: boolean;
5
+ }
6
+ interface User {
7
+ id: string;
8
+ displayName: string;
9
+ avatar?: string;
10
+ }
11
+ interface CreditBalance {
12
+ credits: number;
13
+ }
14
+ interface PaymentResult {
15
+ success: boolean;
16
+ newBalance: number;
17
+ error?: string;
18
+ }
19
+ interface Transaction {
20
+ amount: number;
21
+ description: string;
22
+ timestamp: number;
23
+ }
24
+
25
+ /**
26
+ * Secure communication bridge between iframe and parent app
27
+ * Uses postMessage with origin verification
28
+ */
29
+ declare class ParentBridge {
30
+ private allowedOrigins;
31
+ private messageHandlers;
32
+ private pendingRequests;
33
+ private requestIdCounter;
34
+ constructor(allowedOrigins?: string[]);
35
+ private setupMessageListener;
36
+ /**
37
+ * Send a message to parent and wait for response
38
+ */
39
+ private sendRequest;
40
+ /**
41
+ * Send a message to parent without expecting a response
42
+ */
43
+ private sendMessage;
44
+ /**
45
+ * Listen for messages from parent
46
+ */
47
+ on(type: string, handler: (data: any) => void): void;
48
+ /**
49
+ * Remove message listener
50
+ */
51
+ off(type: string): void;
52
+ /**
53
+ * Get current authenticated user
54
+ */
55
+ getCurrentUser(): Promise<User | null>;
56
+ /**
57
+ * Request user to login (triggers parent app UI)
58
+ */
59
+ requestLogin(): Promise<void>;
60
+ /**
61
+ * Logout current user
62
+ */
63
+ logout(): Promise<void>;
64
+ /**
65
+ * Get bearer token for authenticated user
66
+ */
67
+ getBearerToken(): Promise<string | null>;
68
+ /**
69
+ * Get user's credit balance
70
+ */
71
+ getBalance(): Promise<CreditBalance>;
72
+ /**
73
+ * Charge credits from user's balance
74
+ */
75
+ chargeCredits(amount: number, description: string): Promise<PaymentResult>;
76
+ /**
77
+ * Notify parent that SDK is ready
78
+ */
79
+ notifyReady(): void;
80
+ }
81
+
82
+ declare class AuthModule {
83
+ private bridge;
84
+ private currentUser;
85
+ private authChangeListeners;
86
+ constructor(bridge: ParentBridge);
87
+ private initialize;
88
+ private notifyAuthChange;
89
+ /**
90
+ * Get the currently authenticated user
91
+ */
92
+ getCurrentUser(): Promise<User | null>;
93
+ /**
94
+ * Get bearer token for the authenticated user
95
+ */
96
+ getBearerToken(): Promise<string | null>;
97
+ /**
98
+ * Request user authentication (triggers parent app UI)
99
+ */
100
+ requestLogin(): Promise<void>;
101
+ /**
102
+ * Logout the current user
103
+ */
104
+ logout(): Promise<void>;
105
+ /**
106
+ * Listen for authentication state changes
107
+ */
108
+ onAuthChange(callback: (user: User | null) => void): () => void;
109
+ /**
110
+ * Check if user is authenticated
111
+ */
112
+ isAuthenticated(): boolean;
113
+ }
114
+
115
+ declare class PaymentsModule {
116
+ private bridge;
117
+ private auth;
118
+ private devMode;
119
+ private currentBalance;
120
+ private balanceChangeListeners;
121
+ private transactions;
122
+ constructor(bridge: ParentBridge, auth: AuthModule, devMode: boolean);
123
+ private initialize;
124
+ private refreshBalance;
125
+ private notifyBalanceChange;
126
+ /**
127
+ * Get the current credit balance
128
+ */
129
+ getBalance(): Promise<CreditBalance>;
130
+ /**
131
+ * Charge credits from the user's balance
132
+ * In dev mode, this is a no-op (free)
133
+ */
134
+ charge(amount: number, description: string): Promise<PaymentResult>;
135
+ /**
136
+ * Listen for balance changes
137
+ */
138
+ onBalanceChange(callback: (balance: CreditBalance) => void): () => void;
139
+ /**
140
+ * Get transaction history
141
+ * Note: This only includes transactions made during the current session
142
+ * For full history, the parent app should provide an API
143
+ */
144
+ getHistory(): Promise<Transaction[]>;
145
+ /**
146
+ * Gift credits to another user (requires server-side implementation)
147
+ */
148
+ giftCredits(userId: string, amount: number, description: string): Promise<PaymentResult>;
149
+ /**
150
+ * Set development mode
151
+ */
152
+ setDevMode(enabled: boolean): void;
153
+ }
154
+
155
+ declare class GameSDK {
156
+ private static instance;
157
+ readonly auth: AuthModule;
158
+ readonly payments: PaymentsModule;
159
+ private bridge;
160
+ private config;
161
+ readonly gameId: string;
162
+ private constructor();
163
+ /**
164
+ * Initialize the Game SDK
165
+ */
166
+ static init(config: GameSDKConfig): Promise<GameSDK>;
167
+ /**
168
+ * Get the current SDK instance
169
+ */
170
+ static getInstance(): GameSDK;
171
+ /**
172
+ * Set development mode
173
+ */
174
+ setDevMode(enabled: boolean): void;
175
+ /**
176
+ * Get SDK configuration
177
+ */
178
+ getConfig(): Readonly<GameSDKConfig>;
179
+ /**
180
+ * Clean up SDK resources
181
+ */
182
+ destroy(): Promise<void>;
183
+ }
184
+
185
+ /**
186
+ * Storage module for persisting game data
187
+ * Data is automatically scoped to userId + gameId
188
+ */
189
+ declare class StorageModule {
190
+ private auth;
191
+ private gameId;
192
+ private apiClient;
193
+ constructor(gameId: string, auth: AuthModule);
194
+ /**
195
+ * Save data to storage
196
+ * Data is automatically scoped to current user and game
197
+ * Maximum size: 5MB
198
+ */
199
+ save<T = any>(data: T): Promise<void>;
200
+ /**
201
+ * Load data from storage
202
+ * Returns null if no data exists
203
+ */
204
+ load<T = any>(): Promise<T | null>;
205
+ /**
206
+ * Clear all stored data for current user and game
207
+ */
208
+ clear(): Promise<void>;
209
+ /**
210
+ * Check if data exists in storage
211
+ */
212
+ exists(): Promise<boolean>;
213
+ /**
214
+ * Get the size of stored data in bytes
215
+ */
216
+ getSize(): Promise<number>;
217
+ }
218
+
219
+ /**
220
+ * Hook to initialize the Game SDK
221
+ */
222
+ declare function useGameSDK(config: GameSDKConfig): {
223
+ sdk: GameSDK | null;
224
+ isReady: boolean;
225
+ error: Error | null;
226
+ };
227
+ /**
228
+ * Hook to access the current user
229
+ */
230
+ declare function useAuth(): {
231
+ user: User | null;
232
+ isLoading: boolean;
233
+ isAuthenticated: boolean;
234
+ login: () => Promise<void>;
235
+ logout: () => Promise<void>;
236
+ };
237
+ /**
238
+ * Hook to access credit balance
239
+ */
240
+ declare function useCredits(): {
241
+ balance: number;
242
+ isLoading: boolean;
243
+ charge: (amount: number, description: string) => Promise<PaymentResult>;
244
+ };
245
+
246
+ export { AuthModule, GameSDK, PaymentsModule, StorageModule, useAuth, useCredits, useGameSDK };