@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/README.md
ADDED
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
# @alien_org/react
|
|
2
|
+
|
|
3
|
+
React bindings for the Alien miniapp SDK. This is the primary interface for developers.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
bun add @alien_org/react
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick Start
|
|
12
|
+
|
|
13
|
+
Wrap your app with `AlienProvider`:
|
|
14
|
+
|
|
15
|
+
```tsx
|
|
16
|
+
import { AlienProvider } from '@alien_org/react';
|
|
17
|
+
|
|
18
|
+
function App() {
|
|
19
|
+
return (
|
|
20
|
+
<AlienProvider>
|
|
21
|
+
<MyMiniapp />
|
|
22
|
+
</AlienProvider>
|
|
23
|
+
);
|
|
24
|
+
}
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Hooks
|
|
28
|
+
|
|
29
|
+
### useAlien
|
|
30
|
+
|
|
31
|
+
Access the Alien context (auth token, contract version, bridge availability):
|
|
32
|
+
|
|
33
|
+
```tsx
|
|
34
|
+
import { useAlien } from '@alien_org/react';
|
|
35
|
+
|
|
36
|
+
function MyComponent() {
|
|
37
|
+
const { authToken, contractVersion, isBridgeAvailable } = useAlien();
|
|
38
|
+
|
|
39
|
+
if (!isBridgeAvailable) {
|
|
40
|
+
return <div>Please open in Alien App</div>;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return <div>Running v{contractVersion} in Alien App!</div>;
|
|
44
|
+
}
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### useLaunchParams
|
|
48
|
+
|
|
49
|
+
Get all launch parameters injected by the host app:
|
|
50
|
+
|
|
51
|
+
```tsx
|
|
52
|
+
import { useLaunchParams } from '@alien_org/react';
|
|
53
|
+
|
|
54
|
+
function MyComponent() {
|
|
55
|
+
const launchParams = useLaunchParams();
|
|
56
|
+
|
|
57
|
+
if (!launchParams) {
|
|
58
|
+
return <div>Running outside Alien App</div>;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return <div>Platform: {launchParams.platform}</div>;
|
|
62
|
+
}
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
### useIsMethodSupported
|
|
66
|
+
|
|
67
|
+
Check if a method is supported by the host:
|
|
68
|
+
|
|
69
|
+
```tsx
|
|
70
|
+
import { useIsMethodSupported } from '@alien_org/react';
|
|
71
|
+
|
|
72
|
+
function MyComponent() {
|
|
73
|
+
const { supported, minVersion } = useIsMethodSupported('payment:request');
|
|
74
|
+
|
|
75
|
+
if (!supported) {
|
|
76
|
+
return <div>Please update to v{minVersion}</div>;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return <div>Feature available!</div>;
|
|
80
|
+
}
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
### useEvent
|
|
84
|
+
|
|
85
|
+
Subscribe to bridge events:
|
|
86
|
+
|
|
87
|
+
```tsx
|
|
88
|
+
import { useEvent } from '@alien_org/react';
|
|
89
|
+
|
|
90
|
+
function MyComponent() {
|
|
91
|
+
// Handle miniapp close (cleanup before close)
|
|
92
|
+
useEvent('miniapp:close', () => {
|
|
93
|
+
saveState();
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
// Handle back button
|
|
97
|
+
useEvent('host.back.button:clicked', () => {
|
|
98
|
+
navigateBack();
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
return <div>Listening...</div>;
|
|
102
|
+
}
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
### useMethod
|
|
106
|
+
|
|
107
|
+
Make bridge requests with state management and version checking:
|
|
108
|
+
|
|
109
|
+
```tsx
|
|
110
|
+
import { useMethod } from '@alien_org/react';
|
|
111
|
+
|
|
112
|
+
function PayButton() {
|
|
113
|
+
const { execute, data, error, isLoading, supported, reset } = useMethod(
|
|
114
|
+
'payment:request',
|
|
115
|
+
'payment:response',
|
|
116
|
+
);
|
|
117
|
+
|
|
118
|
+
if (!supported) {
|
|
119
|
+
return <div>Payment not supported</div>;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const handlePay = async () => {
|
|
123
|
+
const { error, data } = await execute({
|
|
124
|
+
recipient: 'wallet-123',
|
|
125
|
+
amount: '100',
|
|
126
|
+
token: 'SOL',
|
|
127
|
+
network: 'solana',
|
|
128
|
+
invoice: 'inv-123',
|
|
129
|
+
});
|
|
130
|
+
if (error) {
|
|
131
|
+
console.error(error);
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
console.log('Payment status:', data.status, data.txHash);
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
if (isLoading) return <button disabled>Processing...</button>;
|
|
138
|
+
if (error) return <div>Error: {error.message}</div>;
|
|
139
|
+
if (data) return <div>Status: {data.status}</div>;
|
|
140
|
+
|
|
141
|
+
return <button onClick={handlePay}>Pay</button>;
|
|
142
|
+
}
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
Disable version checking if needed:
|
|
146
|
+
|
|
147
|
+
```tsx
|
|
148
|
+
const { execute } = useMethod('payment:request', 'payment:response', { checkVersion: false });
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
### usePayment
|
|
152
|
+
|
|
153
|
+
Handle payments with full state management:
|
|
154
|
+
|
|
155
|
+
```tsx
|
|
156
|
+
import { usePayment } from '@alien_org/react';
|
|
157
|
+
|
|
158
|
+
function BuyButton({ orderId }: { orderId: string }) {
|
|
159
|
+
const {
|
|
160
|
+
pay,
|
|
161
|
+
isLoading,
|
|
162
|
+
isPaid,
|
|
163
|
+
isCancelled,
|
|
164
|
+
isFailed,
|
|
165
|
+
txHash,
|
|
166
|
+
errorCode,
|
|
167
|
+
error,
|
|
168
|
+
reset,
|
|
169
|
+
supported,
|
|
170
|
+
} = usePayment({
|
|
171
|
+
timeout: 120000, // 2 minutes (default)
|
|
172
|
+
onPaid: (txHash) => console.log('Paid!', txHash),
|
|
173
|
+
onCancelled: () => console.log('Cancelled'),
|
|
174
|
+
onFailed: (code, error) => console.log('Failed:', code, error),
|
|
175
|
+
onStatusChange: (status) => console.log('Status:', status),
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
const handleBuy = () => pay({
|
|
179
|
+
recipient: 'wallet-address',
|
|
180
|
+
amount: '1000000',
|
|
181
|
+
token: 'SOL',
|
|
182
|
+
network: 'solana',
|
|
183
|
+
invoice: orderId,
|
|
184
|
+
title: 'Premium Plan',
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
if (isPaid) return <div>Thank you! TX: {txHash}</div>;
|
|
188
|
+
|
|
189
|
+
return (
|
|
190
|
+
<button onClick={handleBuy} disabled={isLoading}>
|
|
191
|
+
{isLoading ? 'Processing...' : 'Buy Now'}
|
|
192
|
+
</button>
|
|
193
|
+
);
|
|
194
|
+
}
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
## Re-exports
|
|
198
|
+
|
|
199
|
+
The package re-exports utilities from `@alien_org/contract` and `@alien_org/bridge`:
|
|
200
|
+
|
|
201
|
+
```tsx
|
|
202
|
+
import {
|
|
203
|
+
// From @alien_org/bridge
|
|
204
|
+
send,
|
|
205
|
+
type RequestOptions,
|
|
206
|
+
|
|
207
|
+
// From @alien_org/contract
|
|
208
|
+
isMethodSupported,
|
|
209
|
+
getMethodMinVersion,
|
|
210
|
+
type MethodName,
|
|
211
|
+
type MethodPayload,
|
|
212
|
+
type EventName,
|
|
213
|
+
type EventPayload,
|
|
214
|
+
type Version,
|
|
215
|
+
} from '@alien_org/react';
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
## Error Handling
|
|
219
|
+
|
|
220
|
+
Bridge errors are caught and set in error state rather than throwing, allowing development outside Alien App.
|
|
221
|
+
|
|
222
|
+
### Error Types
|
|
223
|
+
|
|
224
|
+
```tsx
|
|
225
|
+
import {
|
|
226
|
+
// React SDK errors
|
|
227
|
+
ReactSDKError, // Base class for React SDK errors
|
|
228
|
+
MethodNotSupportedError, // Method not supported by contract version
|
|
229
|
+
|
|
230
|
+
// Bridge errors (re-exported from @alien_org/bridge)
|
|
231
|
+
BridgeError, // Base class for bridge errors
|
|
232
|
+
BridgeUnavailableError, // Bridge not available
|
|
233
|
+
BridgeTimeoutError, // Request timed out
|
|
234
|
+
BridgeWindowUnavailableError, // Window undefined (SSR)
|
|
235
|
+
} from '@alien_org/react';
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
## Development Mode
|
|
239
|
+
|
|
240
|
+
When running outside Alien App, the SDK will:
|
|
241
|
+
|
|
242
|
+
- Warn that the bridge is not available (does not throw)
|
|
243
|
+
- Handle errors gracefully by setting error state
|
|
244
|
+
- Allow your app to render and function (though bridge communication won't work)
|