hyperstack-react 0.1.2

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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Hypertek
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,79 @@
1
+ # @hypertek/typescript
2
+
3
+ TypeScript SDK for real-time Solana program data streaming from hyperstack
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @hypertek/typescript
9
+ ```
10
+
11
+ ## Peer Dependencies
12
+
13
+ This package requires:
14
+ - `react` ^18.0.0
15
+ - `zustand` ^4.0.0
16
+
17
+ ## Usage
18
+
19
+ ### Basic Setup
20
+
21
+ ```tsx
22
+ import { HyperstackProvider, useHyperstack, defineStack } from '@hypertek/typescript';
23
+
24
+ const myStack = defineStack({
25
+ // Your stack configuration
26
+ });
27
+
28
+ function App() {
29
+ return (
30
+ <HyperstackProvider config={{ /* your config */ }}>
31
+ <MyComponent />
32
+ </HyperstackProvider>
33
+ );
34
+ }
35
+
36
+ function MyComponent() {
37
+ const stack = useHyperstack(myStack);
38
+ // Use your stack
39
+ }
40
+ ```
41
+
42
+ ### Core Features
43
+
44
+ - **Real-time Data Streaming**: Subscribe to Solana program state changes
45
+ - **React Integration**: Hooks-based API for easy integration with React applications
46
+ - **State Management**: Built-in state management with Zustand
47
+ - **Type Safety**: Full TypeScript support with comprehensive type definitions
48
+ - **View Definitions**: Create state and list views for your data
49
+ - **Transaction Handling**: Define and execute transactions with hooks
50
+
51
+ ### API
52
+
53
+ #### Providers
54
+
55
+ - `HyperstackProvider` - Root provider for Hyperstack configuration
56
+
57
+ #### Hooks
58
+
59
+ - `useHyperstack` - Main hook for accessing stack functionality
60
+ - `useHyperstackContext` - Access the runtime context directly
61
+
62
+ #### Factory Functions
63
+
64
+ - `defineStack` - Define a new stack configuration
65
+ - `createStateView` - Create a state view
66
+ - `createListView` - Create a list view
67
+ - `createRuntime` - Create a runtime instance
68
+
69
+ #### Utilities
70
+
71
+ - `ConnectionManager` - Manage WebSocket connections
72
+
73
+ ## License
74
+
75
+ MIT
76
+
77
+ ## Author
78
+
79
+ HyperTek
@@ -0,0 +1,255 @@
1
+ import React, { ReactNode } from 'react';
2
+ import * as zustand from 'zustand';
3
+
4
+ interface EntityFrame<T = unknown> {
5
+ mode: 'state' | 'kv' | 'append' | 'list';
6
+ entity: string;
7
+ op: 'create' | 'upsert' | 'patch' | 'delete';
8
+ key: string;
9
+ data: T;
10
+ }
11
+ interface Subscription {
12
+ view: string;
13
+ key?: string;
14
+ partition?: string;
15
+ filters?: Record<string, string>;
16
+ }
17
+ type ConnectionState = 'disconnected' | 'connecting' | 'connected' | 'error' | 'reconnecting';
18
+ interface HyperState<T = unknown> {
19
+ connectionState: ConnectionState;
20
+ lastError?: string;
21
+ entities: Map<string, Map<string, T>>;
22
+ recentFrames: EntityFrame<T>[];
23
+ }
24
+ interface HyperSDKConfig {
25
+ websocketUrl?: string;
26
+ reconnectIntervals?: number[];
27
+ maxReconnectAttempts?: number;
28
+ initialSubscriptions?: Subscription[];
29
+ supportsUnsubscribe?: boolean;
30
+ autoSubscribeDefault?: boolean;
31
+ }
32
+ declare const DEFAULT_CONFIG: HyperSDKConfig;
33
+ declare class HyperStreamError extends Error {
34
+ code: string;
35
+ details?: unknown | undefined;
36
+ constructor(message: string, // human-readable error message
37
+ code: string, // machine-readable error code (e.g., 'CONNECTION_FAILED')
38
+ details?: unknown | undefined);
39
+ }
40
+ type ViewMode = 'state' | 'list';
41
+ interface NetworkConfig {
42
+ name: string;
43
+ websocketUrl: string;
44
+ }
45
+ interface ViewDefinition<T = any> {
46
+ mode: ViewMode;
47
+ view: string;
48
+ type: T;
49
+ transform?: (data: any) => T;
50
+ }
51
+ interface TransactionDefinition<TParams = any> {
52
+ build: (params: TParams) => {
53
+ instruction: string;
54
+ params: TParams;
55
+ };
56
+ refresh?: Array<{
57
+ view: string;
58
+ key?: string | ((params: TParams) => string);
59
+ }>;
60
+ }
61
+ interface StackDefinition {
62
+ name: string;
63
+ views: Record<string, any>;
64
+ transactions?: Record<string, TransactionDefinition>;
65
+ helpers?: Record<string, (...args: any[]) => any>;
66
+ }
67
+ interface HyperstackConfig {
68
+ websocketUrl?: string;
69
+ network?: 'devnet' | 'mainnet' | 'localnet' | NetworkConfig;
70
+ apiKey?: string;
71
+ autoConnect?: boolean;
72
+ wallet?: WalletAdapter;
73
+ }
74
+ interface WalletAdapter {
75
+ publicKey: string;
76
+ signAndSend: (transaction: any) => Promise<string>;
77
+ }
78
+ interface ViewHookOptions {
79
+ enabled?: boolean;
80
+ initialData?: any;
81
+ refreshOnReconnect?: boolean;
82
+ }
83
+ interface ViewHookResult<T> {
84
+ data: T | undefined;
85
+ isLoading: boolean;
86
+ error?: Error;
87
+ refresh: () => void;
88
+ }
89
+ interface ListParams {
90
+ key?: string;
91
+ where?: Record<string, any>;
92
+ limit?: number;
93
+ filters?: Record<string, string>;
94
+ }
95
+ interface UseMutationReturn {
96
+ submit: (instructionOrTx: any | any[]) => Promise<string>;
97
+ status: 'idle' | 'pending' | 'success' | 'error';
98
+ error?: string;
99
+ signature?: string;
100
+ reset: () => void;
101
+ }
102
+ interface StateViewHook<T> {
103
+ use: (key: {
104
+ [keyField: string]: string;
105
+ }, options?: ViewHookOptions) => ViewHookResult<T>;
106
+ }
107
+ interface ListViewHook<T> {
108
+ use: (params?: ListParams, options?: ViewHookOptions) => ViewHookResult<T[]>;
109
+ }
110
+
111
+ type FrameHandler = <T>(frame: EntityFrame<T>) => void;
112
+ type StateHandler = (state: ConnectionState, error?: string) => void;
113
+ declare class ConnectionManager {
114
+ private ws;
115
+ private config;
116
+ private reconnectAttempts;
117
+ private reconnectTimeout;
118
+ private pingInterval;
119
+ private currentState;
120
+ private subscriptionQueue;
121
+ private onFrame?;
122
+ private onStateChange?;
123
+ constructor(config?: Partial<HyperSDKConfig>);
124
+ setHandlers(handlers: {
125
+ onFrame?: FrameHandler;
126
+ onStateChange?: StateHandler;
127
+ }): void;
128
+ getState(): ConnectionState;
129
+ getConfig(): HyperSDKConfig;
130
+ connect(): void;
131
+ disconnect(): void;
132
+ updateConfig(newConfig: Partial<HyperSDKConfig>): void;
133
+ subscribe(subscription: Subscription): void;
134
+ unsubscribe(_view: string, _key?: string): void;
135
+ private parseBinaryFrame;
136
+ private updateState;
137
+ private handleReconnect;
138
+ private clearReconnectTimeout;
139
+ private startPingInterval;
140
+ private stopPingInterval;
141
+ }
142
+
143
+ type SubKey = string;
144
+ interface SubscriptionTracker {
145
+ subscription: Subscription;
146
+ refCount: number;
147
+ }
148
+ interface ViewMetadata {
149
+ mode: ViewMode;
150
+ keys: string[];
151
+ lastArgs?: any;
152
+ lastUpdatedAt?: number;
153
+ }
154
+ interface ViewCache {
155
+ [viewPath: string]: ViewMetadata;
156
+ }
157
+ interface HyperStore extends HyperState {
158
+ handleFrame: <T>(frame: EntityFrame<T>) => void;
159
+ _incRef: (subscription: Subscription) => void;
160
+ _decRef: (subscription: Subscription) => void;
161
+ _getRefCount: (subscription: Subscription) => number;
162
+ subscribe: (subscription: Subscription) => void;
163
+ unsubscribe: (entity: string, key?: string) => void;
164
+ connect: () => void;
165
+ disconnect: () => void;
166
+ updateConfig: (config: Partial<HyperSDKConfig>) => void;
167
+ subscriptionRefs: Map<SubKey, SubscriptionTracker>;
168
+ connectionManager: ConnectionManager;
169
+ viewCache: ViewCache;
170
+ }
171
+ declare function createHyperStore(config?: Partial<HyperSDKConfig>): zustand.UseBoundStore<zustand.StoreApi<HyperStore>>;
172
+
173
+ interface SubscriptionHandle {
174
+ view: string;
175
+ key?: string;
176
+ filters?: Record<string, string>;
177
+ unsubscribe: () => void;
178
+ }
179
+ interface HyperstackRuntime {
180
+ store: ReturnType<typeof createHyperStore>;
181
+ connection: ConnectionManager;
182
+ wallet?: WalletAdapter;
183
+ subscribe(view: string, key?: string, filters?: Record<string, string>): SubscriptionHandle;
184
+ unsubscribe(handle: SubscriptionHandle): void;
185
+ }
186
+ declare function createRuntime(config: HyperstackConfig): HyperstackRuntime;
187
+
188
+ interface HyperstackContextValue {
189
+ runtime: HyperstackRuntime;
190
+ config: HyperstackConfig;
191
+ }
192
+ declare function HyperstackProvider({ children, ...config }: HyperstackConfig & {
193
+ children: ReactNode;
194
+ }): React.JSX.Element;
195
+ declare function useHyperstackContext(): HyperstackContextValue;
196
+
197
+ declare function defineStack<TViews extends Record<string, any>, TTxs extends Record<string, TransactionDefinition>, THelpers extends Record<string, (...args: any[]) => any>>(definition: {
198
+ name: string;
199
+ views: TViews;
200
+ transactions?: TTxs;
201
+ helpers?: THelpers;
202
+ }): StackDefinition & {
203
+ views: TViews;
204
+ transactions?: TTxs;
205
+ helpers?: THelpers;
206
+ };
207
+ type InferViewType<T> = T extends ViewDefinition<infer U> ? U : never;
208
+ type BuildViewInterface<TViews extends Record<string, any>> = {
209
+ [K in keyof TViews]: TViews[K] extends {
210
+ state: ViewDefinition<any>;
211
+ list: ViewDefinition<any>;
212
+ } ? {
213
+ state: {
214
+ use: (key: Record<string, string>, options?: ViewHookOptions) => ViewHookResult<InferViewType<TViews[K]['state']>>;
215
+ };
216
+ list: {
217
+ use: (params?: ListParams, options?: ViewHookOptions) => ViewHookResult<InferViewType<TViews[K]['list']>[]>;
218
+ };
219
+ } : TViews[K] extends {
220
+ state: ViewDefinition<any>;
221
+ } ? {
222
+ state: {
223
+ use: (key: Record<string, string>, options?: ViewHookOptions) => ViewHookResult<InferViewType<TViews[K]['state']>>;
224
+ };
225
+ } : TViews[K] extends {
226
+ list: ViewDefinition<any>;
227
+ } ? {
228
+ list: {
229
+ use: (params?: ListParams, options?: ViewHookOptions) => ViewHookResult<InferViewType<TViews[K]['list']>[]>;
230
+ };
231
+ } : never;
232
+ };
233
+ type StackClient<TStack extends StackDefinition> = {
234
+ views: BuildViewInterface<TStack['views']>;
235
+ tx: TStack['transactions'] extends Record<string, TransactionDefinition> ? {
236
+ [K in keyof TStack['transactions']]: TStack['transactions'][K]['build'];
237
+ } & {
238
+ useMutation: () => UseMutationReturn;
239
+ } : {
240
+ useMutation: () => UseMutationReturn;
241
+ };
242
+ helpers: TStack['helpers'] extends Record<string, (...args: any[]) => any> ? TStack['helpers'] : {};
243
+ store: HyperstackRuntime['store'];
244
+ runtime: HyperstackRuntime;
245
+ };
246
+ declare function useHyperstack<TStack extends StackDefinition>(stack: TStack): StackClient<TStack>;
247
+
248
+ interface ViewFactoryOptions<T> {
249
+ transform?: (data: any) => T;
250
+ }
251
+ declare function createStateView<T>(viewPath: string, options?: ViewFactoryOptions<T>): ViewDefinition<T>;
252
+ declare function createListView<T>(viewPath: string, options?: ViewFactoryOptions<T>): ViewDefinition<T>;
253
+
254
+ export { ConnectionManager, DEFAULT_CONFIG, HyperStreamError, HyperstackProvider, createListView, createRuntime, createStateView, defineStack, useHyperstack, useHyperstackContext };
255
+ export type { ConnectionState, EntityFrame, HyperSDKConfig, HyperstackConfig, HyperstackRuntime, ListParams, ListViewHook, NetworkConfig, StackDefinition, StateViewHook, Subscription, SubscriptionHandle, TransactionDefinition, UseMutationReturn, ViewDefinition, ViewHookOptions, ViewHookResult, ViewMode, WalletAdapter };