@calimero-network/mero-react 1.0.0-beta.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.
package/README.md ADDED
@@ -0,0 +1,220 @@
1
+ # @calimero-network/mero-react
2
+
3
+ React bindings for [MeroJs](https://github.com/calimero-network/mero-js) - the official Calimero Network SDK.
4
+
5
+ ## Features
6
+
7
+ - **MeroProvider** - React Context provider that manages MeroJs instance
8
+ - **useMero** - Hook to access MeroJs and authentication state
9
+ - **ConnectButton** - Ready-to-use connection button component
10
+ - **LoginModal** - Modal for node selection (local/remote)
11
+ - **localStorage TokenStorage** - Built-in token persistence
12
+
13
+ ## Installation
14
+
15
+ ```bash
16
+ npm install @calimero-network/mero-react @calimero-network/mero-js
17
+ # or
18
+ pnpm add @calimero-network/mero-react @calimero-network/mero-js
19
+ ```
20
+
21
+ ## Quick Start
22
+
23
+ ```tsx
24
+ import { MeroProvider, ConnectButton, useMero, AppMode } from '@calimero-network/mero-react';
25
+
26
+ function App() {
27
+ return (
28
+ <MeroProvider
29
+ mode={AppMode.SingleContext}
30
+ packageName="my-app"
31
+ >
32
+ <MyApp />
33
+ </MeroProvider>
34
+ );
35
+ }
36
+
37
+ function MyApp() {
38
+ const { mero, isAuthenticated, isLoading } = useMero();
39
+
40
+ if (isLoading) {
41
+ return <div>Loading...</div>;
42
+ }
43
+
44
+ return (
45
+ <div>
46
+ <ConnectButton />
47
+
48
+ {isAuthenticated && mero && (
49
+ <Dashboard mero={mero} />
50
+ )}
51
+ </div>
52
+ );
53
+ }
54
+
55
+ function Dashboard({ mero }) {
56
+ const [contexts, setContexts] = useState([]);
57
+
58
+ useEffect(() => {
59
+ // Access MeroJs APIs through the mero instance
60
+ mero.admin.contexts.listContexts()
61
+ .then(response => setContexts(response.contexts));
62
+ }, [mero]);
63
+
64
+ return (
65
+ <ul>
66
+ {contexts.map(ctx => (
67
+ <li key={ctx.contextId}>{ctx.contextId}</li>
68
+ ))}
69
+ </ul>
70
+ );
71
+ }
72
+ ```
73
+
74
+ ## Provider Configuration
75
+
76
+ ```tsx
77
+ <MeroProvider
78
+ // Required: Application mode
79
+ mode={AppMode.SingleContext | AppMode.MultiContext | AppMode.Admin}
80
+
81
+ // Package-based (recommended)
82
+ packageName="@my-org/my-app"
83
+ packageVersion="1.0.0" // optional, defaults to latest
84
+ registryUrl="https://registry.calimero.network" // optional
85
+
86
+ // OR Legacy: Application ID
87
+ applicationId="app-hash-id"
88
+ applicationPath="/my-app"
89
+
90
+ // Optional
91
+ eventStreamMode={EventStreamMode.WebSocket | EventStreamMode.SSE}
92
+ timeoutMs={30000}
93
+ >
94
+ {children}
95
+ </MeroProvider>
96
+ ```
97
+
98
+ ## Application Modes
99
+
100
+ | Mode | Permissions | Use Case |
101
+ |------|-------------|----------|
102
+ | `SingleContext` | `context:execute` | Apps that work with one context |
103
+ | `MultiContext` | `context:create`, `context:list`, `context:execute` | Apps managing multiple contexts |
104
+ | `Admin` | `admin` | Admin dashboards, dev tools |
105
+
106
+ ## useMero Hook
107
+
108
+ ```tsx
109
+ const {
110
+ mero, // MeroJs instance (null if not connected)
111
+ isAuthenticated, // Whether user is logged in
112
+ isOnline, // Whether connection is healthy
113
+ isLoading, // Initial loading state
114
+ nodeUrl, // Current node URL
115
+ applicationId, // Resolved application ID
116
+ connectToNode, // Connect to a node URL and start auth
117
+ logout, // Clear session
118
+ } = useMero();
119
+ ```
120
+
121
+ ## Connect Button
122
+
123
+ ```tsx
124
+ import { ConnectButton, ConnectionType } from '@calimero-network/mero-react';
125
+
126
+ // Default: shows local/remote options
127
+ <ConnectButton />
128
+
129
+ // Only remote
130
+ <ConnectButton connectionType={ConnectionType.Remote} />
131
+
132
+ // Only local
133
+ <ConnectButton connectionType={ConnectionType.Local} />
134
+
135
+ // Custom URL (skip modal)
136
+ <ConnectButton connectionType={{ type: ConnectionType.Custom, url: 'https://my-node.com' }} />
137
+ ```
138
+
139
+ ## Styling
140
+
141
+ The components come with default styles. You can override them by:
142
+
143
+ 1. **CSS Variables**:
144
+ ```css
145
+ :root {
146
+ --mero-bg: #1a1a2e;
147
+ --mero-text: #eaeaea;
148
+ --mero-accent: #7b68ee;
149
+ --mero-success: #a8e640;
150
+ --mero-error: #ff4d4d;
151
+ /* ... see styles.css for all variables */
152
+ }
153
+ ```
154
+
155
+ 2. **Custom Classes**:
156
+ ```tsx
157
+ <ConnectButton className="my-custom-button" />
158
+ ```
159
+
160
+ 3. **Replace Components**: Create your own using `useMero()` hook.
161
+
162
+ ## Storage
163
+
164
+ Built-in localStorage utilities:
165
+
166
+ ```tsx
167
+ import {
168
+ localStorageTokenStorage,
169
+ getNodeUrl,
170
+ setNodeUrl,
171
+ getApplicationId,
172
+ clearAllStorage,
173
+ } from '@calimero-network/mero-react';
174
+
175
+ // localStorageTokenStorage implements MeroJs TokenStorage interface
176
+ // It's automatically used by MeroProvider
177
+ ```
178
+
179
+ ## Accessing MeroJs APIs
180
+
181
+ All MeroJs APIs are available through the `mero` instance:
182
+
183
+ ```tsx
184
+ const { mero } = useMero();
185
+
186
+ // Admin APIs
187
+ await mero.admin.applications.listApplications();
188
+ await mero.admin.contexts.createContext({ applicationId, ... });
189
+ await mero.admin.blobs.uploadBlob(file);
190
+
191
+ // Auth APIs
192
+ await mero.auth.getHealth();
193
+ await mero.auth.refreshToken();
194
+
195
+ // RPC
196
+ await mero.rpc.execute({ contextId, method, args, executorPublicKey });
197
+
198
+ // WebSocket subscriptions
199
+ mero.ws.subscribe(contextId, onEvent);
200
+
201
+ // SSE subscriptions
202
+ mero.sse.subscribe(contextId, onEvent);
203
+ ```
204
+
205
+ ## TypeScript
206
+
207
+ Full TypeScript support:
208
+
209
+ ```tsx
210
+ import type {
211
+ MeroContextValue,
212
+ MeroProviderConfig,
213
+ AppContext,
214
+ ExecutionResult,
215
+ } from '@calimero-network/mero-react';
216
+ ```
217
+
218
+ ## License
219
+
220
+ MIT