@alien_org/react 0.0.11-beta
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 +244 -0
- package/dist/index.cjs +546 -0
- package/dist/index.d.cts +721 -0
- package/dist/index.d.mts +721 -0
- package/dist/index.mjs +516 -0
- package/package.json +49 -0
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,721 @@
|
|
|
1
|
+
import { ReactNode } from "react";
|
|
2
|
+
|
|
3
|
+
//#region ../contract/src/utils.d.ts
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Adds a reqId field to the payload.
|
|
7
|
+
* @schema
|
|
8
|
+
*/
|
|
9
|
+
type WithReqId<T> = T & {
|
|
10
|
+
/**
|
|
11
|
+
* Request identifier.
|
|
12
|
+
* @schema
|
|
13
|
+
*/
|
|
14
|
+
reqId: string;
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* Semantic versioning type.
|
|
18
|
+
* @example
|
|
19
|
+
* type Version = '1.0.0';
|
|
20
|
+
*/
|
|
21
|
+
type Version = `${number}.${number}.${number}`;
|
|
22
|
+
/**
|
|
23
|
+
* Extracts keys, that are present in the type if it is an object.
|
|
24
|
+
* @example
|
|
25
|
+
* type Keys = UnionKeys<{ a: string, b: number }>;
|
|
26
|
+
* // Keys = 'a' | 'b'
|
|
27
|
+
*/
|
|
28
|
+
type UnionKeys<T> = T extends T ? keyof T : never;
|
|
29
|
+
/**
|
|
30
|
+
* Empty object type.
|
|
31
|
+
* @example
|
|
32
|
+
* type Empty = Empty;
|
|
33
|
+
* // Empty = {}
|
|
34
|
+
*/
|
|
35
|
+
type Empty = Record<string, never>;
|
|
36
|
+
//#endregion
|
|
37
|
+
//#region ../contract/src/events/types/payload.d.ts
|
|
38
|
+
/**
|
|
39
|
+
* Creates event payload types.
|
|
40
|
+
*/
|
|
41
|
+
interface CreateEventPayload<Payload = never> {
|
|
42
|
+
payload: Payload;
|
|
43
|
+
}
|
|
44
|
+
//#endregion
|
|
45
|
+
//#region ../contract/src/events/definitions/events.d.ts
|
|
46
|
+
/**
|
|
47
|
+
* Events interface defining all available events and their payloads.
|
|
48
|
+
* @since 0.0.1
|
|
49
|
+
* @schema
|
|
50
|
+
*/
|
|
51
|
+
interface Events {
|
|
52
|
+
/**
|
|
53
|
+
* Miniapp close event, fired by the host app just before the miniapp is closed.
|
|
54
|
+
* @since 0.0.14
|
|
55
|
+
* @schema
|
|
56
|
+
*/
|
|
57
|
+
'miniapp:close': CreateEventPayload<Empty>;
|
|
58
|
+
/**
|
|
59
|
+
* Host app's back button clicked event.
|
|
60
|
+
* @since 0.0.14
|
|
61
|
+
* @schema
|
|
62
|
+
*/
|
|
63
|
+
'host.back.button:clicked': CreateEventPayload<Empty>;
|
|
64
|
+
/**
|
|
65
|
+
* Payment response event.
|
|
66
|
+
*
|
|
67
|
+
* Statuses:
|
|
68
|
+
* - `paid`: Payment successful, `txHash` included
|
|
69
|
+
* - `cancelled`: User manually cancelled/rejected the payment
|
|
70
|
+
* - `failed`: Error occurred (see `errorCode` for details)
|
|
71
|
+
*
|
|
72
|
+
* For instant fulfillment, your backend should fulfill on webhook receipt
|
|
73
|
+
* using the `invoice` from the request.
|
|
74
|
+
*
|
|
75
|
+
* @since 0.0.14
|
|
76
|
+
* @schema
|
|
77
|
+
*/
|
|
78
|
+
'payment:response': CreateEventPayload<WithReqId<{
|
|
79
|
+
/**
|
|
80
|
+
* Payment status.
|
|
81
|
+
* - `paid`: Success
|
|
82
|
+
* - `cancelled`: User rejected
|
|
83
|
+
* - `failed`: Error (check `errorCode`)
|
|
84
|
+
* @since 0.0.14
|
|
85
|
+
* @schema
|
|
86
|
+
*/
|
|
87
|
+
status: 'paid' | 'cancelled' | 'failed';
|
|
88
|
+
/**
|
|
89
|
+
* Transaction hash (present when status is 'paid').
|
|
90
|
+
* @since 0.0.14
|
|
91
|
+
* @schema
|
|
92
|
+
*/
|
|
93
|
+
txHash?: string;
|
|
94
|
+
/**
|
|
95
|
+
* Error code (present when status is 'failed').
|
|
96
|
+
* - `insufficient_balance`: User doesn't have enough tokens
|
|
97
|
+
* - `network_error`: Blockchain network issue
|
|
98
|
+
* - `pre_checkout_rejected`: Backend rejected the payment in pre-checkout
|
|
99
|
+
* - `pre_checkout_timeout`: Backend didn't respond to pre-checkout in time
|
|
100
|
+
* - `unknown`: Unexpected error
|
|
101
|
+
* @since 0.0.14
|
|
102
|
+
* @schema
|
|
103
|
+
*/
|
|
104
|
+
errorCode?: 'insufficient_balance' | 'network_error' | 'pre_checkout_rejected' | 'pre_checkout_timeout' | 'unknown';
|
|
105
|
+
}>>;
|
|
106
|
+
}
|
|
107
|
+
//#endregion
|
|
108
|
+
//#region ../contract/src/events/types/event-types.d.ts
|
|
109
|
+
type EventName = keyof Events;
|
|
110
|
+
type EventPayload<E extends EventName> = Events[E]['payload'];
|
|
111
|
+
//#endregion
|
|
112
|
+
//#region ../contract/src/launch-params.d.ts
|
|
113
|
+
/**
|
|
114
|
+
* Supported platforms for miniapps.
|
|
115
|
+
*/
|
|
116
|
+
declare const PLATFORMS: readonly ["ios", "android"];
|
|
117
|
+
/**
|
|
118
|
+
* Platform the miniapp is running on.
|
|
119
|
+
*/
|
|
120
|
+
type Platform = (typeof PLATFORMS)[number];
|
|
121
|
+
/**
|
|
122
|
+
* Launch parameters injected by the host app.
|
|
123
|
+
*/
|
|
124
|
+
interface LaunchParams {
|
|
125
|
+
/** JWT auth token injected by host app */
|
|
126
|
+
authToken: string | undefined;
|
|
127
|
+
/** Contract version supported by host app (semver) */
|
|
128
|
+
contractVersion: Version | undefined;
|
|
129
|
+
/** Host app version (e.g., '1.2.3') */
|
|
130
|
+
hostAppVersion: string | undefined;
|
|
131
|
+
/** Platform the miniapp is running on */
|
|
132
|
+
platform: Platform | undefined;
|
|
133
|
+
/**
|
|
134
|
+
* Custom start parameter injected by host app.
|
|
135
|
+
* Used for referral codes, campaign tracking, or custom routing.
|
|
136
|
+
*/
|
|
137
|
+
startParam: string | undefined;
|
|
138
|
+
}
|
|
139
|
+
//#endregion
|
|
140
|
+
//#region ../contract/src/methods/types/payload.d.ts
|
|
141
|
+
/**
|
|
142
|
+
* Creates method payload types.
|
|
143
|
+
*/
|
|
144
|
+
interface CreateMethodPayload<Payload = never, VersionedPayload extends UnionKeys<Payload> = never> {
|
|
145
|
+
payload: Payload;
|
|
146
|
+
versionedPayload: VersionedPayload;
|
|
147
|
+
}
|
|
148
|
+
//#endregion
|
|
149
|
+
//#region ../contract/src/methods/definitions/methods.d.ts
|
|
150
|
+
/**
|
|
151
|
+
* Methods interface defining all available methods and their payloads.
|
|
152
|
+
* @schema
|
|
153
|
+
*/
|
|
154
|
+
interface Methods {
|
|
155
|
+
/**
|
|
156
|
+
* Miniapp ready method.
|
|
157
|
+
* Sent by the miniapp to notify the host app that it has loaded and is ready to be displayed.
|
|
158
|
+
* @since 0.0.1
|
|
159
|
+
* @schema
|
|
160
|
+
*/
|
|
161
|
+
'app:ready': CreateMethodPayload<Empty>;
|
|
162
|
+
/**
|
|
163
|
+
* Miniapp close acknowledgment method.
|
|
164
|
+
* Sent by the miniapp to notify the host app that it has completed cleanup and is ready to be closed.
|
|
165
|
+
* Note that if the miniapp takes longer than 10 seconds to close, the host app will force close the miniapp.
|
|
166
|
+
* @since 0.0.14
|
|
167
|
+
* @schema
|
|
168
|
+
*/
|
|
169
|
+
'miniapp:close.ack': CreateMethodPayload<Empty>;
|
|
170
|
+
/**
|
|
171
|
+
* Toggle host app's back button visibility.
|
|
172
|
+
* @since 0.0.14
|
|
173
|
+
* @schema
|
|
174
|
+
*/
|
|
175
|
+
'host.back.button:toggle': CreateMethodPayload<{
|
|
176
|
+
/**
|
|
177
|
+
* Whether to show or hide the back button.
|
|
178
|
+
* @since 0.0.14
|
|
179
|
+
* @schema
|
|
180
|
+
*/
|
|
181
|
+
visible: boolean;
|
|
182
|
+
}>;
|
|
183
|
+
/**
|
|
184
|
+
* Request a payment from the user.
|
|
185
|
+
*
|
|
186
|
+
* The `invoice` field is your order/invoice ID for backend correlation.
|
|
187
|
+
* Your backend receives a webhook when user pays - fulfill the order
|
|
188
|
+
* immediately without waiting for chain confirmation.
|
|
189
|
+
*
|
|
190
|
+
* Optional display fields (`title`, `caption`, `iconUrl`, `quantity`)
|
|
191
|
+
* are shown on the payment approval screen.
|
|
192
|
+
*
|
|
193
|
+
* Set `test: true` for test mode - no real payment is made, but webhooks
|
|
194
|
+
* are fired with `test: true` flag. Use for development and testing.
|
|
195
|
+
*
|
|
196
|
+
* @since 0.0.14
|
|
197
|
+
* @schema
|
|
198
|
+
*/
|
|
199
|
+
'payment:request': CreateMethodPayload<WithReqId<{
|
|
200
|
+
/**
|
|
201
|
+
* The recipient's wallet address.
|
|
202
|
+
* @since 0.0.14
|
|
203
|
+
* @schema
|
|
204
|
+
*/
|
|
205
|
+
recipient: string;
|
|
206
|
+
/**
|
|
207
|
+
* The amount to pay (in token's smallest unit, as string for precision).
|
|
208
|
+
* @since 0.0.14
|
|
209
|
+
* @schema
|
|
210
|
+
*/
|
|
211
|
+
amount: string;
|
|
212
|
+
/**
|
|
213
|
+
* The token identifier (e.g., 'SOL', 'ALIEN', or contract address).
|
|
214
|
+
* @since 0.0.14
|
|
215
|
+
* @schema
|
|
216
|
+
*/
|
|
217
|
+
token: string;
|
|
218
|
+
/**
|
|
219
|
+
* The network for the payment ('solana' or 'alien').
|
|
220
|
+
* @since 0.0.14
|
|
221
|
+
* @schema
|
|
222
|
+
*/
|
|
223
|
+
network: string;
|
|
224
|
+
/**
|
|
225
|
+
* Your order/invoice ID for backend correlation and instant fulfillment.
|
|
226
|
+
* @since 0.0.14
|
|
227
|
+
* @schema
|
|
228
|
+
*/
|
|
229
|
+
invoice: string;
|
|
230
|
+
/**
|
|
231
|
+
* Item title shown on the approval screen.
|
|
232
|
+
* @since 0.0.14
|
|
233
|
+
* @schema
|
|
234
|
+
*/
|
|
235
|
+
title?: string;
|
|
236
|
+
/**
|
|
237
|
+
* Item description/caption shown on the approval screen.
|
|
238
|
+
* @since 0.0.14
|
|
239
|
+
* @schema
|
|
240
|
+
*/
|
|
241
|
+
caption?: string;
|
|
242
|
+
/**
|
|
243
|
+
* Item icon URL shown on the approval screen.
|
|
244
|
+
* @since 0.0.14
|
|
245
|
+
* @schema
|
|
246
|
+
*/
|
|
247
|
+
iconUrl?: string;
|
|
248
|
+
/**
|
|
249
|
+
* Quantity of items being purchased.
|
|
250
|
+
* @since 0.0.14
|
|
251
|
+
* @schema
|
|
252
|
+
*/
|
|
253
|
+
quantity?: number;
|
|
254
|
+
/**
|
|
255
|
+
* Test mode flag. When true, no real payment is processed.
|
|
256
|
+
* The approval screen shows a test indicator, and webhooks
|
|
257
|
+
* include `test: true`. Use for development and testing.
|
|
258
|
+
* @since 0.0.14
|
|
259
|
+
* @schema
|
|
260
|
+
*/
|
|
261
|
+
test?: boolean;
|
|
262
|
+
}>>;
|
|
263
|
+
}
|
|
264
|
+
//#endregion
|
|
265
|
+
//#region ../contract/src/methods/types/method-types.d.ts
|
|
266
|
+
type MethodName = keyof Methods;
|
|
267
|
+
type MethodPayload<M$1 extends MethodName> = Methods[M$1]['payload'];
|
|
268
|
+
//#endregion
|
|
269
|
+
//#region ../contract/src/methods/versions/index.d.ts
|
|
270
|
+
/**
|
|
271
|
+
* Check if a method is supported in a given version.
|
|
272
|
+
*
|
|
273
|
+
* @param method - The method name to check.
|
|
274
|
+
* @param version - The contract version (must be a valid version string, not undefined).
|
|
275
|
+
* @returns `true` if the method is supported in the given version, `false` otherwise.
|
|
276
|
+
*
|
|
277
|
+
* @remarks
|
|
278
|
+
* This function only accepts valid version strings. Version existence checks should be
|
|
279
|
+
* handled at a higher level before calling this function.
|
|
280
|
+
*/
|
|
281
|
+
declare function isMethodSupported(method: MethodName, version: Version): boolean;
|
|
282
|
+
/**
|
|
283
|
+
* Get the minimum version that supports a method.
|
|
284
|
+
* Returns undefined if method not found in any version.
|
|
285
|
+
*/
|
|
286
|
+
declare function getMethodMinVersion(method: MethodName): Version | undefined;
|
|
287
|
+
//#endregion
|
|
288
|
+
//#region ../bridge/src/errors.d.ts
|
|
289
|
+
/**
|
|
290
|
+
* Base class for all bridge-related errors.
|
|
291
|
+
* Allows catching all bridge errors with a single catch block.
|
|
292
|
+
*/
|
|
293
|
+
declare class BridgeError extends Error {
|
|
294
|
+
constructor(message: string);
|
|
295
|
+
}
|
|
296
|
+
/**
|
|
297
|
+
* Thrown when the bridge interface is not available.
|
|
298
|
+
* This occurs when the miniapp is not running in Alien App.
|
|
299
|
+
*/
|
|
300
|
+
declare class BridgeUnavailableError extends BridgeError {
|
|
301
|
+
constructor();
|
|
302
|
+
}
|
|
303
|
+
/**
|
|
304
|
+
* Thrown when window is undefined (e.g., SSR scenarios).
|
|
305
|
+
*/
|
|
306
|
+
declare class BridgeWindowUnavailableError extends BridgeError {
|
|
307
|
+
constructor();
|
|
308
|
+
}
|
|
309
|
+
/**
|
|
310
|
+
* Thrown when a request times out.
|
|
311
|
+
*/
|
|
312
|
+
declare class BridgeTimeoutError extends BridgeError {
|
|
313
|
+
readonly method: string;
|
|
314
|
+
readonly timeout: number;
|
|
315
|
+
constructor(method: string, timeout: number);
|
|
316
|
+
}
|
|
317
|
+
//#endregion
|
|
318
|
+
//#region ../bridge/src/launch-params.d.ts
|
|
319
|
+
declare global {
|
|
320
|
+
interface Window {
|
|
321
|
+
__ALIEN_AUTH_TOKEN__?: string;
|
|
322
|
+
__ALIEN_CONTRACT_VERSION__?: string;
|
|
323
|
+
__ALIEN_HOST_VERSION__?: string;
|
|
324
|
+
__ALIEN_PLATFORM__?: string;
|
|
325
|
+
__ALIEN_START_PARAM__?: string;
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
/**
|
|
329
|
+
* Error thrown when launch params cannot be retrieved.
|
|
330
|
+
*/
|
|
331
|
+
//#endregion
|
|
332
|
+
//#region ../bridge/src/request.d.ts
|
|
333
|
+
interface RequestOptions {
|
|
334
|
+
reqId?: string;
|
|
335
|
+
timeout?: number;
|
|
336
|
+
}
|
|
337
|
+
//#endregion
|
|
338
|
+
//#region ../bridge/src/send.d.ts
|
|
339
|
+
/**
|
|
340
|
+
* Sends a one-way method to the host app without waiting for a response.
|
|
341
|
+
* Use this for fire-and-forget methods like 'app:ready'.
|
|
342
|
+
*
|
|
343
|
+
* @param method - The method name to send
|
|
344
|
+
* @param payload - The method payload
|
|
345
|
+
*
|
|
346
|
+
* @example
|
|
347
|
+
* ```ts
|
|
348
|
+
* import { send } from '@alien_org/bridge';
|
|
349
|
+
*
|
|
350
|
+
* send('app:ready', {});
|
|
351
|
+
* ```
|
|
352
|
+
*/
|
|
353
|
+
declare function send<M$1 extends MethodName>(method: M$1, payload: MethodPayload<M$1>): void;
|
|
354
|
+
//#endregion
|
|
355
|
+
//#region ../bridge/src/transport.d.ts
|
|
356
|
+
interface MiniAppsBridge {
|
|
357
|
+
postMessage(data: string): void;
|
|
358
|
+
}
|
|
359
|
+
declare global {
|
|
360
|
+
interface Window {
|
|
361
|
+
__miniAppsBridge__?: MiniAppsBridge;
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
//#endregion
|
|
365
|
+
//#region src/context.d.ts
|
|
366
|
+
interface AlienContextValue {
|
|
367
|
+
/**
|
|
368
|
+
* Auth token injected by the host app.
|
|
369
|
+
* `undefined` if not available.
|
|
370
|
+
*/
|
|
371
|
+
authToken: string | undefined;
|
|
372
|
+
/**
|
|
373
|
+
* Contract version supported by the host app.
|
|
374
|
+
* `undefined` if not provided (fallback: assume all methods supported).
|
|
375
|
+
*/
|
|
376
|
+
contractVersion: Version | undefined;
|
|
377
|
+
/**
|
|
378
|
+
* Whether the bridge is available (running inside Alien App).
|
|
379
|
+
*/
|
|
380
|
+
isBridgeAvailable: boolean;
|
|
381
|
+
}
|
|
382
|
+
interface AlienProviderProps {
|
|
383
|
+
children: ReactNode;
|
|
384
|
+
}
|
|
385
|
+
/**
|
|
386
|
+
* Provider component that initializes the Alien miniapp context.
|
|
387
|
+
* Must wrap your app to use Alien hooks.
|
|
388
|
+
*
|
|
389
|
+
* @example
|
|
390
|
+
* ```tsx
|
|
391
|
+
* import { AlienProvider } from '@alien_org/react';
|
|
392
|
+
*
|
|
393
|
+
* function App() {
|
|
394
|
+
* return (
|
|
395
|
+
* <AlienProvider>
|
|
396
|
+
* <MyMiniapp />
|
|
397
|
+
* </AlienProvider>
|
|
398
|
+
* );
|
|
399
|
+
* }
|
|
400
|
+
* ```
|
|
401
|
+
*/
|
|
402
|
+
declare function AlienProvider({
|
|
403
|
+
children
|
|
404
|
+
}: AlienProviderProps): ReactNode;
|
|
405
|
+
//#endregion
|
|
406
|
+
//#region src/errors.d.ts
|
|
407
|
+
/**
|
|
408
|
+
* Base class for all React SDK errors.
|
|
409
|
+
*/
|
|
410
|
+
declare class ReactSDKError extends Error {
|
|
411
|
+
constructor(message: string);
|
|
412
|
+
}
|
|
413
|
+
/**
|
|
414
|
+
* Error thrown when a method is not supported by the current contract version.
|
|
415
|
+
*/
|
|
416
|
+
declare class MethodNotSupportedError extends ReactSDKError {
|
|
417
|
+
readonly method: MethodName;
|
|
418
|
+
readonly contractVersion: Version | undefined;
|
|
419
|
+
readonly minVersion: Version | undefined;
|
|
420
|
+
constructor(method: MethodName, contractVersion: Version | undefined, minVersion: Version | undefined);
|
|
421
|
+
}
|
|
422
|
+
//#endregion
|
|
423
|
+
//#region src/hooks/useAlien.d.ts
|
|
424
|
+
/**
|
|
425
|
+
* Hook to access the Alien context.
|
|
426
|
+
* Must be used within an AlienProvider.
|
|
427
|
+
*
|
|
428
|
+
* For additional launch params (platform, startParam, hostAppVersion),
|
|
429
|
+
* use the `useLaunchParams` hook.
|
|
430
|
+
*
|
|
431
|
+
* @example
|
|
432
|
+
* ```tsx
|
|
433
|
+
* const { authToken, contractVersion, isBridgeAvailable } = useAlien();
|
|
434
|
+
* ```
|
|
435
|
+
*/
|
|
436
|
+
declare function useAlien(): AlienContextValue;
|
|
437
|
+
//#endregion
|
|
438
|
+
//#region src/hooks/useEvent.d.ts
|
|
439
|
+
type EventCallback<E extends EventName> = (payload: EventPayload<E>) => void;
|
|
440
|
+
/**
|
|
441
|
+
* Hook to subscribe to bridge events.
|
|
442
|
+
* Automatically handles subscription cleanup on unmount.
|
|
443
|
+
*
|
|
444
|
+
* @param event - The event name to subscribe to.
|
|
445
|
+
* @param callback - The callback to invoke when the event is received.
|
|
446
|
+
*
|
|
447
|
+
* @example
|
|
448
|
+
* ```tsx
|
|
449
|
+
* import { useEvent } from '@alien_org/react';
|
|
450
|
+
*
|
|
451
|
+
* function MyComponent() {
|
|
452
|
+
* useEvent('miniapp:close', () => {
|
|
453
|
+
* // Cleanup before miniapp closes
|
|
454
|
+
* saveState();
|
|
455
|
+
* });
|
|
456
|
+
*
|
|
457
|
+
* useEvent('host.back.button:clicked', () => {
|
|
458
|
+
* // Handle back button press
|
|
459
|
+
* navigateBack();
|
|
460
|
+
* });
|
|
461
|
+
*
|
|
462
|
+
* return <div>Listening for events...</div>;
|
|
463
|
+
* }
|
|
464
|
+
* ```
|
|
465
|
+
*/
|
|
466
|
+
declare function useEvent<E extends EventName>(event: E, callback: EventCallback<E>): void;
|
|
467
|
+
//#endregion
|
|
468
|
+
//#region src/hooks/useIsMethodSupported.d.ts
|
|
469
|
+
interface MethodSupportResult {
|
|
470
|
+
/**
|
|
471
|
+
* Whether the method is supported in the current contract version.
|
|
472
|
+
* Always `true` if no version is provided (fallback behavior).
|
|
473
|
+
*/
|
|
474
|
+
supported: boolean;
|
|
475
|
+
/**
|
|
476
|
+
* The contract version provided by the host app.
|
|
477
|
+
*/
|
|
478
|
+
contractVersion: Version | undefined;
|
|
479
|
+
/**
|
|
480
|
+
* The minimum version that supports this method.
|
|
481
|
+
*/
|
|
482
|
+
minVersion: Version | undefined;
|
|
483
|
+
}
|
|
484
|
+
/**
|
|
485
|
+
* Hook to check if a method is supported by the host app's contract version.
|
|
486
|
+
*
|
|
487
|
+
* @param method - The method name to check.
|
|
488
|
+
* @returns Object with `supported`, `contractVersion`, and `minVersion`.
|
|
489
|
+
*
|
|
490
|
+
* @example
|
|
491
|
+
* ```tsx
|
|
492
|
+
* import { useMethodSupported } from '@alien_org/react';
|
|
493
|
+
*
|
|
494
|
+
* function MyComponent() {
|
|
495
|
+
* const { supported, minVersion } = useMethodSupported('payment:request');
|
|
496
|
+
*
|
|
497
|
+
* if (!supported) {
|
|
498
|
+
* return <div>This feature requires version {minVersion}</div>;
|
|
499
|
+
* }
|
|
500
|
+
*
|
|
501
|
+
* return <div>Feature available!</div>;
|
|
502
|
+
* }
|
|
503
|
+
* ```
|
|
504
|
+
*/
|
|
505
|
+
declare function useIsMethodSupported(method: MethodName): MethodSupportResult;
|
|
506
|
+
//#endregion
|
|
507
|
+
//#region src/hooks/useLaunchParams.d.ts
|
|
508
|
+
/**
|
|
509
|
+
* Hook to get launch params.
|
|
510
|
+
* Returns undefined if params unavailable (use mockLaunchParamsForDev in dev).
|
|
511
|
+
*
|
|
512
|
+
* @example
|
|
513
|
+
* ```tsx
|
|
514
|
+
* import { useLaunchParams } from '@alien_org/react';
|
|
515
|
+
*
|
|
516
|
+
* function MyComponent() {
|
|
517
|
+
* const launchParams = useLaunchParams();
|
|
518
|
+
*
|
|
519
|
+
* if (!launchParams) {
|
|
520
|
+
* return <div>Running outside Alien App</div>;
|
|
521
|
+
* }
|
|
522
|
+
*
|
|
523
|
+
* return <div>Platform: {launchParams.platform}</div>;
|
|
524
|
+
* }
|
|
525
|
+
* ```
|
|
526
|
+
*/
|
|
527
|
+
declare function useLaunchParams(): LaunchParams | undefined;
|
|
528
|
+
//#endregion
|
|
529
|
+
//#region src/hooks/useMethod.d.ts
|
|
530
|
+
interface UseMethodExecuteResult<E extends EventName> {
|
|
531
|
+
data: EventPayload<E> | undefined;
|
|
532
|
+
error: Error | undefined;
|
|
533
|
+
}
|
|
534
|
+
interface UseMethodState<E extends EventName> extends UseMethodExecuteResult<E> {
|
|
535
|
+
isLoading: boolean;
|
|
536
|
+
}
|
|
537
|
+
interface UseMethodOptions {
|
|
538
|
+
/**
|
|
539
|
+
* Whether to check if the method is supported before executing.
|
|
540
|
+
* If unsupported, sets error state with `MethodNotSupportedError`.
|
|
541
|
+
* @default true
|
|
542
|
+
*/
|
|
543
|
+
checkVersion?: boolean;
|
|
544
|
+
}
|
|
545
|
+
interface UseMethodResult<M$1 extends MethodName, E extends EventName> extends UseMethodState<E> {
|
|
546
|
+
execute: (params: Omit<MethodPayload<M$1>, 'reqId'>, options?: RequestOptions) => Promise<UseMethodExecuteResult<E>>;
|
|
547
|
+
reset: () => void;
|
|
548
|
+
/**
|
|
549
|
+
* Whether the method is supported by the current contract version.
|
|
550
|
+
*/
|
|
551
|
+
supported: boolean;
|
|
552
|
+
}
|
|
553
|
+
/**
|
|
554
|
+
* Hook for making bridge requests with loading/error state management.
|
|
555
|
+
*
|
|
556
|
+
* @param method - The method name to call.
|
|
557
|
+
* @param responseEvent - The event name to listen for the response.
|
|
558
|
+
* @param options - Hook options including version checking.
|
|
559
|
+
* @returns Object with `execute`, `reset`, `data`, `error`, `isLoading`, and `supported`.
|
|
560
|
+
*
|
|
561
|
+
* @example
|
|
562
|
+
* ```tsx
|
|
563
|
+
* import { useMethod } from '@alien_org/react';
|
|
564
|
+
*
|
|
565
|
+
* function PayButton() {
|
|
566
|
+
* const { execute, data, error, isLoading, supported } = useMethod(
|
|
567
|
+
* 'payment:request',
|
|
568
|
+
* 'payment:response',
|
|
569
|
+
* );
|
|
570
|
+
*
|
|
571
|
+
* if (!supported) {
|
|
572
|
+
* return <div>This feature is not available</div>;
|
|
573
|
+
* }
|
|
574
|
+
*
|
|
575
|
+
* const handlePay = async () => {
|
|
576
|
+
* // Errors are automatically set in the `error` state - no try/catch needed!
|
|
577
|
+
* const { error, data } = await execute({
|
|
578
|
+
* recipient: 'wallet-123',
|
|
579
|
+
* amount: '100',
|
|
580
|
+
* token: 'SOL',
|
|
581
|
+
* network: 'solana',
|
|
582
|
+
* invoice: 'inv-123',
|
|
583
|
+
* });
|
|
584
|
+
* if (error) {
|
|
585
|
+
* console.error(error);
|
|
586
|
+
* return;
|
|
587
|
+
* }
|
|
588
|
+
* if (data) {
|
|
589
|
+
* console.log('Success:', data);
|
|
590
|
+
* }
|
|
591
|
+
* };
|
|
592
|
+
*
|
|
593
|
+
* if (isLoading) return <button disabled>Loading...</button>;
|
|
594
|
+
* if (error) return <div>Error: {error.message}</div>;
|
|
595
|
+
* if (data) return <div>Payment complete!</div>;
|
|
596
|
+
*
|
|
597
|
+
* return <button onClick={handlePay}>Pay</button>;
|
|
598
|
+
* }
|
|
599
|
+
* ```
|
|
600
|
+
*/
|
|
601
|
+
declare function useMethod<M$1 extends MethodName, E extends EventName>(method: M$1, responseEvent: E, options?: UseMethodOptions): UseMethodResult<M$1, E>;
|
|
602
|
+
//#endregion
|
|
603
|
+
//#region src/hooks/usePayment.d.ts
|
|
604
|
+
type PaymentRequestPayload = MethodPayload<'payment:request'>;
|
|
605
|
+
type PaymentResponsePayload = EventPayload<'payment:response'>;
|
|
606
|
+
/** Payment parameters (without reqId, which is auto-generated). */
|
|
607
|
+
type PaymentParams = Omit<PaymentRequestPayload, 'reqId'>;
|
|
608
|
+
/** Payment response status from the host app. */
|
|
609
|
+
type PaymentResponseStatus = PaymentResponsePayload['status'];
|
|
610
|
+
/** Payment error codes from the host app. */
|
|
611
|
+
type PaymentErrorCode = NonNullable<PaymentResponsePayload['errorCode']>;
|
|
612
|
+
/** Payment status states for the hook. */
|
|
613
|
+
type PaymentStatus = 'idle' | 'loading' | PaymentResponseStatus;
|
|
614
|
+
/**
|
|
615
|
+
* Payment result returned after a payment attempt.
|
|
616
|
+
*/
|
|
617
|
+
interface PaymentResult {
|
|
618
|
+
status: PaymentStatus;
|
|
619
|
+
txHash?: string;
|
|
620
|
+
errorCode?: PaymentErrorCode;
|
|
621
|
+
error?: Error;
|
|
622
|
+
}
|
|
623
|
+
/**
|
|
624
|
+
* Callbacks for payment status changes.
|
|
625
|
+
*/
|
|
626
|
+
interface PaymentCallbacks {
|
|
627
|
+
/** Called when payment succeeds. */
|
|
628
|
+
onPaid?: (txHash: string) => void;
|
|
629
|
+
/** Called when user cancels the payment. */
|
|
630
|
+
onCancelled?: () => void;
|
|
631
|
+
/** Called when payment fails. */
|
|
632
|
+
onFailed?: (errorCode: PaymentErrorCode, error?: Error) => void;
|
|
633
|
+
/** Called on any status change. */
|
|
634
|
+
onStatusChange?: (status: PaymentStatus) => void;
|
|
635
|
+
}
|
|
636
|
+
/**
|
|
637
|
+
* Options for the usePayment hook.
|
|
638
|
+
*/
|
|
639
|
+
interface UsePaymentOptions extends PaymentCallbacks {
|
|
640
|
+
/**
|
|
641
|
+
* Timeout for the payment request in milliseconds.
|
|
642
|
+
* @default 120000 (2 minutes)
|
|
643
|
+
*/
|
|
644
|
+
timeout?: number;
|
|
645
|
+
}
|
|
646
|
+
/**
|
|
647
|
+
* Return type of the usePayment hook.
|
|
648
|
+
*/
|
|
649
|
+
interface UsePaymentReturn {
|
|
650
|
+
/** Current payment status. */
|
|
651
|
+
status: PaymentStatus;
|
|
652
|
+
/** Whether a payment is in progress. */
|
|
653
|
+
isLoading: boolean;
|
|
654
|
+
/** Whether the payment was successful. */
|
|
655
|
+
isPaid: boolean;
|
|
656
|
+
/** Whether the payment was cancelled. */
|
|
657
|
+
isCancelled: boolean;
|
|
658
|
+
/** Whether the payment failed. */
|
|
659
|
+
isFailed: boolean;
|
|
660
|
+
/** Transaction hash (present when paid). */
|
|
661
|
+
txHash?: string;
|
|
662
|
+
/** Error code (present when failed). */
|
|
663
|
+
errorCode?: PaymentErrorCode;
|
|
664
|
+
/** Error object if an error occurred. */
|
|
665
|
+
error?: Error;
|
|
666
|
+
/** Initiate a payment. */
|
|
667
|
+
pay: (params: PaymentParams) => Promise<PaymentResult>;
|
|
668
|
+
/** Reset the payment state to idle. */
|
|
669
|
+
reset: () => void;
|
|
670
|
+
/** Whether the payment method is supported by the host app. */
|
|
671
|
+
supported: boolean;
|
|
672
|
+
}
|
|
673
|
+
/**
|
|
674
|
+
* Hook for handling payments with full state management.
|
|
675
|
+
*
|
|
676
|
+
* Provides an easy-to-use interface for initiating payments and reacting
|
|
677
|
+
* to status changes. Automatically handles loading states, errors, and
|
|
678
|
+
* version checking.
|
|
679
|
+
*
|
|
680
|
+
* @param options - Optional configuration and callbacks.
|
|
681
|
+
* @returns Payment state and methods.
|
|
682
|
+
*
|
|
683
|
+
* @example
|
|
684
|
+
* ```tsx
|
|
685
|
+
* import { usePayment } from '@alien_org/react';
|
|
686
|
+
*
|
|
687
|
+
* function BuyButton({ orderId }: { orderId: string }) {
|
|
688
|
+
* const {
|
|
689
|
+
* pay,
|
|
690
|
+
* isLoading,
|
|
691
|
+
* isPaid,
|
|
692
|
+
* txHash,
|
|
693
|
+
* error,
|
|
694
|
+
* } = usePayment({
|
|
695
|
+
* onPaid: (txHash) => console.log('Paid!', txHash),
|
|
696
|
+
* onCancelled: () => console.log('Cancelled'),
|
|
697
|
+
* onFailed: (code) => console.log('Failed:', code),
|
|
698
|
+
* });
|
|
699
|
+
*
|
|
700
|
+
* const handleBuy = () => pay({
|
|
701
|
+
* recipient: 'wallet-address',
|
|
702
|
+
* amount: '1000000',
|
|
703
|
+
* token: 'SOL',
|
|
704
|
+
* network: 'solana',
|
|
705
|
+
* invoice: orderId,
|
|
706
|
+
* title: 'Premium Plan',
|
|
707
|
+
* });
|
|
708
|
+
*
|
|
709
|
+
* if (isPaid) return <div>Thank you! TX: {txHash}</div>;
|
|
710
|
+
*
|
|
711
|
+
* return (
|
|
712
|
+
* <button onClick={handleBuy} disabled={isLoading}>
|
|
713
|
+
* {isLoading ? 'Processing...' : 'Buy Now'}
|
|
714
|
+
* </button>
|
|
715
|
+
* );
|
|
716
|
+
* }
|
|
717
|
+
* ```
|
|
718
|
+
*/
|
|
719
|
+
declare function usePayment(options?: UsePaymentOptions): UsePaymentReturn;
|
|
720
|
+
//#endregion
|
|
721
|
+
export { AlienProvider, type AlienProviderProps, BridgeError, BridgeTimeoutError, BridgeUnavailableError, BridgeWindowUnavailableError, type EventName, type EventPayload, type MethodName, MethodNotSupportedError, type MethodPayload, type MethodSupportResult, type PaymentCallbacks, type PaymentErrorCode, type PaymentParams, type PaymentResponseStatus, type PaymentResult, type PaymentStatus, ReactSDKError, type RequestOptions, type UseMethodExecuteResult, type UseMethodOptions, type UsePaymentOptions, type UsePaymentReturn, type Version, getMethodMinVersion, isMethodSupported, send, useAlien, useEvent, useIsMethodSupported, useLaunchParams, useMethod, usePayment };
|