@usearete/react 0.1.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.
- package/LICENSE +21 -0
- package/README.md +108 -0
- package/dist/index.d.ts +211 -0
- package/dist/index.esm.js +10001 -0
- package/dist/index.esm.js.map +1 -0
- package/dist/index.js +10033 -0
- package/dist/index.js.map +1 -0
- package/package.json +65 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Arete
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
# Arete React SDK
|
|
2
|
+
|
|
3
|
+
React SDK for real-time Solana program data streaming from Arete.
|
|
4
|
+
|
|
5
|
+
Built on top of [`@usearete/sdk`](https://www.npmjs.com/package/@usearete/sdk), the pure TypeScript core SDK.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @usearete/react
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
> **Not using React?** Use [`@usearete/sdk`](../core/README.md) directly for Vue, Svelte, Node.js, or vanilla JavaScript.
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
### Basic Setup
|
|
18
|
+
|
|
19
|
+
```tsx
|
|
20
|
+
import { AreteProvider, useArete, defineStack } from '@usearete/react';
|
|
21
|
+
|
|
22
|
+
const myStack = defineStack({
|
|
23
|
+
// Your stack configuration
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
function App() {
|
|
27
|
+
return (
|
|
28
|
+
<AreteProvider config={{ /* your config */ }}>
|
|
29
|
+
<MyComponent />
|
|
30
|
+
</AreteProvider>
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function MyComponent() {
|
|
35
|
+
const stack = useArete(myStack);
|
|
36
|
+
// Use your stack
|
|
37
|
+
}
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
### Core Features
|
|
41
|
+
|
|
42
|
+
- **Real-time Data Streaming**: Subscribe to Solana program state changes
|
|
43
|
+
- **React Integration**: Hooks-based API for easy integration with React applications
|
|
44
|
+
- **State Management**: Built-in state management with Zustand
|
|
45
|
+
- **Type Safety**: Full TypeScript support with comprehensive type definitions
|
|
46
|
+
- **View Definitions**: Create state and list views for your data
|
|
47
|
+
- **Transaction Handling**: Define and execute transactions with hooks
|
|
48
|
+
- **Single Item Queries**: Type-safe single item fetching with `take: 1` or `useOne()`
|
|
49
|
+
|
|
50
|
+
### API
|
|
51
|
+
|
|
52
|
+
#### Providers
|
|
53
|
+
|
|
54
|
+
- `AreteProvider` - Root provider for Arete configuration
|
|
55
|
+
|
|
56
|
+
#### Hooks
|
|
57
|
+
|
|
58
|
+
- `useArete` - Main hook for accessing stack functionality
|
|
59
|
+
- `useAreteContext` - Access the runtime context directly
|
|
60
|
+
|
|
61
|
+
#### View Methods
|
|
62
|
+
|
|
63
|
+
- `.use()` - Subscribe to view data (returns `T[]` for lists, `T` for state)
|
|
64
|
+
- `.use({ take: 1 })` - Subscribe to single item with type narrowing (returns `T | undefined`)
|
|
65
|
+
- `.useOne()` - Convenience method for single item queries (returns `T | undefined`)
|
|
66
|
+
|
|
67
|
+
#### Factory Functions
|
|
68
|
+
|
|
69
|
+
- `defineStack` - Define a new stack configuration
|
|
70
|
+
- `createStateView` - Create a state view
|
|
71
|
+
- `createListView` - Create a list view
|
|
72
|
+
- `createRuntime` - Create a runtime instance
|
|
73
|
+
|
|
74
|
+
#### Utilities
|
|
75
|
+
|
|
76
|
+
- `ConnectionManager` - Manage WebSocket connections
|
|
77
|
+
|
|
78
|
+
## Relationship with @usearete/sdk
|
|
79
|
+
|
|
80
|
+
This package depends on and re-exports the core `@usearete/sdk` package. The core SDK provides:
|
|
81
|
+
|
|
82
|
+
- `Arete` - Main client class
|
|
83
|
+
- `ConnectionManager` - WebSocket connection handling
|
|
84
|
+
- `EntityStore` - State management
|
|
85
|
+
- AsyncIterable-based streaming APIs
|
|
86
|
+
|
|
87
|
+
The React SDK adds:
|
|
88
|
+
|
|
89
|
+
- `AreteProvider` - React context provider
|
|
90
|
+
- `useArete` - Main hook for accessing stacks
|
|
91
|
+
- `useConnectionState` - Connection monitoring hook
|
|
92
|
+
- `defineStack`, `createStateView`, `createListView` - React-friendly factories
|
|
93
|
+
|
|
94
|
+
If you need low-level access, you can import directly from the core:
|
|
95
|
+
|
|
96
|
+
```typescript
|
|
97
|
+
import { Arete, ConnectionManager } from '@usearete/react';
|
|
98
|
+
// or
|
|
99
|
+
import { Arete, ConnectionManager } from '@usearete/sdk';
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
## License
|
|
103
|
+
|
|
104
|
+
MIT
|
|
105
|
+
|
|
106
|
+
## Author
|
|
107
|
+
|
|
108
|
+
Arete Team
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
import React, { ReactNode } from 'react';
|
|
2
|
+
import * as _usearete_sdk from '@usearete/sdk';
|
|
3
|
+
import { WalletAdapter, Schema, StackDefinition, ConnectionState, Arete, InstructionExecutorOptions, ExecutionResult, InstructionExecutor, StorageAdapter, StorageAdapterConfig, UpdateCallback, RichUpdateCallback, Update, RichUpdate, ViewSortConfig, ViewGroup, ViewDef, InstructionHandler } from '@usearete/sdk';
|
|
4
|
+
export { AccountCategory, AccountMeta, AccountResolutionOptions, AccountResolutionResult, Arete, AreteError, AreteOptions, AreteOptionsWithStorage, ArgSchema, ArgType, BuiltInstruction, ConfirmationLevel, ConnectionManager, ConnectionState, DEFAULT_CONFIG, DEFAULT_MAX_ENTRIES_PER_VIEW, EntityFrame, ErrorMetadata, ExecuteOptions, ExecutionResult, Frame, FrameMode, FrameOp, FrameProcessor, FrameProcessorConfig, InstructionDefinition, InstructionExecutor, InstructionExecutorOptions, InstructionHandler, MemoryAdapter, PdaConfig, PdaSeed, ProgramError, ResolvedAccount, ResolvedAccounts, RichUpdate, RichUpdateCallback, Schema, SnapshotEntity, SnapshotFrame, StackDefinition, StorageAdapter, StorageAdapterConfig, Subscription, SubscriptionRegistry, Update, UpdateCallback, ViewDef, ViewGroup, WalletAdapter, WalletConnectOptions, WalletState, createInstructionExecutor, createPublicKeySeed, createSeed, derivePda, executeInstruction, formatProgramError, isSnapshotFrame, isValidFrame, parseFrame, parseFrameFromBlob, parseInstructionError, resolveAccounts, serializeInstructionData, validateAccountResolution, waitForConfirmation } from '@usearete/sdk';
|
|
5
|
+
import { UseBoundStore, StoreApi } from 'zustand';
|
|
6
|
+
|
|
7
|
+
type ViewMode = 'state' | 'list';
|
|
8
|
+
interface TransactionDefinition<TParams = unknown> {
|
|
9
|
+
build: (params: TParams) => {
|
|
10
|
+
instruction: string;
|
|
11
|
+
params: TParams;
|
|
12
|
+
};
|
|
13
|
+
refresh?: Array<{
|
|
14
|
+
view: string;
|
|
15
|
+
key?: string | ((params: TParams) => string);
|
|
16
|
+
}>;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Global configuration for AreteProvider.
|
|
20
|
+
*
|
|
21
|
+
* Note: WebSocket URL is no longer configured here. The URL is:
|
|
22
|
+
* 1. Embedded in the stack definition (stack.url)
|
|
23
|
+
* 2. Optionally overridden per-hook via useArete(stack, { url: '...' })
|
|
24
|
+
*/
|
|
25
|
+
interface AreteConfig {
|
|
26
|
+
autoConnect?: boolean;
|
|
27
|
+
wallet?: WalletAdapter;
|
|
28
|
+
reconnectIntervals?: number[];
|
|
29
|
+
maxReconnectAttempts?: number;
|
|
30
|
+
maxEntriesPerView?: number | null;
|
|
31
|
+
flushIntervalMs?: number;
|
|
32
|
+
/** Authentication configuration */
|
|
33
|
+
auth?: _usearete_sdk.AuthConfig;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Options for useArete hook
|
|
37
|
+
*/
|
|
38
|
+
interface UseAreteOptions {
|
|
39
|
+
/** Override the stack's embedded URL (useful for local development) */
|
|
40
|
+
url?: string;
|
|
41
|
+
}
|
|
42
|
+
interface ViewHookOptions<TSchema = unknown> {
|
|
43
|
+
enabled?: boolean;
|
|
44
|
+
initialData?: unknown;
|
|
45
|
+
refreshOnReconnect?: boolean;
|
|
46
|
+
/** Schema to validate entities. Returns undefined if validation fails. */
|
|
47
|
+
schema?: Schema<TSchema>;
|
|
48
|
+
/** Whether to include initial snapshot (defaults to true) */
|
|
49
|
+
withSnapshot?: boolean;
|
|
50
|
+
/** Cursor for resuming from a specific point (_seq value) */
|
|
51
|
+
after?: string;
|
|
52
|
+
/** Maximum number of entities to include in snapshot */
|
|
53
|
+
snapshotLimit?: number;
|
|
54
|
+
}
|
|
55
|
+
interface ViewHookResult<T> {
|
|
56
|
+
data: T | undefined;
|
|
57
|
+
isLoading: boolean;
|
|
58
|
+
error?: Error;
|
|
59
|
+
refresh: () => void;
|
|
60
|
+
}
|
|
61
|
+
interface ListParamsBase<TSchema = unknown> {
|
|
62
|
+
key?: string;
|
|
63
|
+
where?: Record<string, unknown>;
|
|
64
|
+
limit?: number;
|
|
65
|
+
filters?: Record<string, string>;
|
|
66
|
+
skip?: number;
|
|
67
|
+
/** Schema to validate/filter entities. Only entities passing safeParse will be returned. */
|
|
68
|
+
schema?: Schema<TSchema>;
|
|
69
|
+
/** Whether to include initial snapshot (defaults to true) */
|
|
70
|
+
withSnapshot?: boolean;
|
|
71
|
+
/** Cursor for resuming from a specific point (_seq value) */
|
|
72
|
+
after?: string;
|
|
73
|
+
/** Maximum number of entities to include in snapshot */
|
|
74
|
+
snapshotLimit?: number;
|
|
75
|
+
}
|
|
76
|
+
interface ListParamsSingle<TSchema = unknown> extends ListParamsBase<TSchema> {
|
|
77
|
+
take: 1;
|
|
78
|
+
}
|
|
79
|
+
interface ListParamsMultiple<TSchema = unknown> extends ListParamsBase<TSchema> {
|
|
80
|
+
take?: number;
|
|
81
|
+
}
|
|
82
|
+
type ListParams<TSchema = unknown> = ListParamsSingle<TSchema> | ListParamsMultiple<TSchema>;
|
|
83
|
+
interface UseMutationReturn {
|
|
84
|
+
submit: (instructionOrTx: unknown | unknown[]) => Promise<string>;
|
|
85
|
+
status: 'idle' | 'pending' | 'success' | 'error';
|
|
86
|
+
error?: string;
|
|
87
|
+
signature?: string;
|
|
88
|
+
reset: () => void;
|
|
89
|
+
}
|
|
90
|
+
interface StateViewHook<T> {
|
|
91
|
+
use: (key: {
|
|
92
|
+
[keyField: string]: string;
|
|
93
|
+
}, options?: ViewHookOptions) => ViewHookResult<T>;
|
|
94
|
+
}
|
|
95
|
+
interface ListViewHook<T> {
|
|
96
|
+
use<TSchema = T>(params: ListParamsSingle<TSchema>, options?: ViewHookOptions): ViewHookResult<TSchema | undefined>;
|
|
97
|
+
use<TSchema = T>(params?: ListParamsMultiple<TSchema>, options?: ViewHookOptions): ViewHookResult<TSchema[]>;
|
|
98
|
+
useOne: <TSchema = T>(params?: Omit<ListParamsBase<TSchema>, 'take'>, options?: ViewHookOptions) => ViewHookResult<TSchema | undefined>;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
interface AreteContextValue {
|
|
102
|
+
getOrCreateClient: <TStack extends StackDefinition>(stack: TStack, urlOverride?: string) => Promise<Arete<TStack>>;
|
|
103
|
+
getClient: <TStack extends StackDefinition>(stack: TStack | undefined) => Arete<TStack> | null;
|
|
104
|
+
subscribeToClientChanges: (callback: () => void) => () => void;
|
|
105
|
+
config: AreteConfig;
|
|
106
|
+
}
|
|
107
|
+
declare function AreteProvider({ children, fallback, ...config }: AreteConfig & {
|
|
108
|
+
children: ReactNode;
|
|
109
|
+
fallback?: ReactNode;
|
|
110
|
+
}): React.JSX.Element;
|
|
111
|
+
declare function useAreteContext(): AreteContextValue;
|
|
112
|
+
declare function useConnectionState(stack?: StackDefinition): ConnectionState;
|
|
113
|
+
declare function useView<T>(stack: StackDefinition, viewPath: string): T[];
|
|
114
|
+
declare function useEntity<T>(stack: StackDefinition, viewPath: string, key: string): T | null;
|
|
115
|
+
|
|
116
|
+
type MutationStatus = 'idle' | 'pending' | 'success' | 'error';
|
|
117
|
+
interface UseMutationOptions extends InstructionExecutorOptions {
|
|
118
|
+
onSuccess?: (result: ExecutionResult) => void;
|
|
119
|
+
onError?: (error: Error) => void;
|
|
120
|
+
}
|
|
121
|
+
interface UseMutationResult {
|
|
122
|
+
submit: (args: Record<string, unknown>, options?: Partial<UseMutationOptions>) => Promise<ExecutionResult>;
|
|
123
|
+
status: MutationStatus;
|
|
124
|
+
error: string | null;
|
|
125
|
+
signature: string | null;
|
|
126
|
+
isLoading: boolean;
|
|
127
|
+
reset: () => void;
|
|
128
|
+
}
|
|
129
|
+
declare function useInstructionMutation(execute: InstructionExecutor): UseMutationResult;
|
|
130
|
+
|
|
131
|
+
interface ZustandState {
|
|
132
|
+
entities: Map<string, Map<string, unknown>>;
|
|
133
|
+
viewConfigs: Map<string, ViewSortConfig>;
|
|
134
|
+
connectionState: ConnectionState;
|
|
135
|
+
lastError?: string;
|
|
136
|
+
}
|
|
137
|
+
interface ZustandActions {
|
|
138
|
+
_set: <T>(viewPath: string, key: string, data: T) => void;
|
|
139
|
+
_delete: (viewPath: string, key: string) => void;
|
|
140
|
+
_clear: (viewPath?: string) => void;
|
|
141
|
+
_setConnectionState: (state: ConnectionState, error?: string) => void;
|
|
142
|
+
_setViewConfig: (viewPath: string, config: ViewSortConfig) => void;
|
|
143
|
+
}
|
|
144
|
+
type AreteStore = ZustandState & ZustandActions;
|
|
145
|
+
declare class ZustandAdapter implements StorageAdapter {
|
|
146
|
+
private updateCallbacks;
|
|
147
|
+
private richUpdateCallbacks;
|
|
148
|
+
private accessOrder;
|
|
149
|
+
readonly store: UseBoundStore<StoreApi<AreteStore>>;
|
|
150
|
+
constructor(_config?: StorageAdapterConfig);
|
|
151
|
+
get<T>(viewPath: string, key: string): T | null;
|
|
152
|
+
getAll<T>(viewPath: string): T[];
|
|
153
|
+
getAllSync<T>(viewPath: string): T[] | undefined;
|
|
154
|
+
getSync<T>(viewPath: string, key: string): T | null | undefined;
|
|
155
|
+
has(viewPath: string, key: string): boolean;
|
|
156
|
+
keys(viewPath: string): string[];
|
|
157
|
+
size(viewPath: string): number;
|
|
158
|
+
set<T>(viewPath: string, key: string, data: T): void;
|
|
159
|
+
delete(viewPath: string, key: string): void;
|
|
160
|
+
clear(viewPath?: string): void;
|
|
161
|
+
evictOldest(viewPath: string): string | undefined;
|
|
162
|
+
onUpdate(callback: UpdateCallback): () => void;
|
|
163
|
+
onRichUpdate(callback: RichUpdateCallback): () => void;
|
|
164
|
+
notifyUpdate<T>(viewPath: string, key: string, update: Update<T>): void;
|
|
165
|
+
notifyRichUpdate<T>(viewPath: string, key: string, update: RichUpdate<T>): void;
|
|
166
|
+
setConnectionState(state: ConnectionState, error?: string): void;
|
|
167
|
+
setViewConfig(viewPath: string, config: ViewSortConfig): void;
|
|
168
|
+
getViewConfig(viewPath: string): ViewSortConfig | undefined;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
type ViewHookForDef<TDef> = TDef extends ViewDef<infer T, 'state'> ? {
|
|
172
|
+
use: <TSchema = T>(key?: Record<string, string>, options?: ViewHookOptions<TSchema>) => ViewHookResult<TSchema>;
|
|
173
|
+
} : TDef extends ViewDef<infer T, 'list'> ? {
|
|
174
|
+
use: {
|
|
175
|
+
<TSchema = T>(params: ListParamsSingle<TSchema>, options?: ViewHookOptions<TSchema>): ViewHookResult<TSchema | undefined>;
|
|
176
|
+
<TSchema = T>(params?: ListParamsMultiple<TSchema>, options?: ViewHookOptions<TSchema>): ViewHookResult<TSchema[]>;
|
|
177
|
+
};
|
|
178
|
+
useOne: <TSchema = T>(params?: Omit<ListParamsBase<TSchema>, 'take'>, options?: ViewHookOptions<TSchema>) => ViewHookResult<TSchema | undefined>;
|
|
179
|
+
} : TDef extends ViewDef<infer T, 'state' | 'list'> ? {
|
|
180
|
+
use: {
|
|
181
|
+
<TSchema = T>(params: ListParamsSingle<TSchema>, options?: ViewHookOptions<TSchema>): ViewHookResult<TSchema | undefined>;
|
|
182
|
+
<TSchema = T>(params?: ListParamsMultiple<TSchema> | Record<string, string>, options?: ViewHookOptions<TSchema>): ViewHookResult<TSchema | TSchema[]>;
|
|
183
|
+
};
|
|
184
|
+
useOne: <TSchema = T>(params?: Omit<ListParamsBase<TSchema>, 'take'>, options?: ViewHookOptions<TSchema>) => ViewHookResult<TSchema | undefined>;
|
|
185
|
+
} : never;
|
|
186
|
+
type BuildViewInterface<TViews extends Record<string, ViewGroup>> = {
|
|
187
|
+
[K in keyof TViews]: {
|
|
188
|
+
[SubK in keyof TViews[K] as TViews[K][SubK] extends ViewDef<unknown, ViewMode> ? SubK : never]: ViewHookForDef<TViews[K][SubK]>;
|
|
189
|
+
};
|
|
190
|
+
};
|
|
191
|
+
type InstructionHook = {
|
|
192
|
+
useMutation: () => UseMutationResult;
|
|
193
|
+
execute: InstructionExecutor;
|
|
194
|
+
};
|
|
195
|
+
type BuildInstructionInterface<TInstructions extends Record<string, InstructionHandler> | undefined> = TInstructions extends Record<string, InstructionHandler> ? {
|
|
196
|
+
[K in keyof TInstructions]: InstructionHook;
|
|
197
|
+
} : {};
|
|
198
|
+
type StackClient<TStack extends StackDefinition> = {
|
|
199
|
+
views: BuildViewInterface<TStack['views']>;
|
|
200
|
+
instructions: BuildInstructionInterface<TStack['instructions']>;
|
|
201
|
+
zustandStore: UseBoundStore<StoreApi<AreteStore>>;
|
|
202
|
+
client: Arete<TStack>;
|
|
203
|
+
connectionState: ConnectionState;
|
|
204
|
+
isConnected: boolean;
|
|
205
|
+
isLoading: boolean;
|
|
206
|
+
error: Error | null;
|
|
207
|
+
};
|
|
208
|
+
declare function useArete<TStack extends StackDefinition>(stack: TStack, options?: UseAreteOptions): StackClient<TStack>;
|
|
209
|
+
|
|
210
|
+
export { AreteProvider, ZustandAdapter, useArete, useAreteContext, useConnectionState, useEntity, useInstructionMutation, useView };
|
|
211
|
+
export type { AreteConfig, AreteStore, ListParams, ListParamsBase, ListParamsMultiple, ListParamsSingle, ListViewHook, MutationStatus, StateViewHook, TransactionDefinition, UseAreteOptions, UseMutationOptions, UseMutationResult, UseMutationReturn, ViewHookOptions, ViewHookResult, ViewMode };
|