@debros/network-ts-sdk 0.1.4
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 +329 -0
- package/dist/index.d.ts +367 -0
- package/dist/index.js +826 -0
- package/dist/index.js.map +1 -0
- package/package.json +50 -0
- package/src/auth/client.ts +97 -0
- package/src/auth/types.ts +62 -0
- package/src/core/http.ts +158 -0
- package/src/core/ws.ts +182 -0
- package/src/db/client.ts +126 -0
- package/src/db/qb.ts +111 -0
- package/src/db/repository.ts +124 -0
- package/src/db/types.ts +67 -0
- package/src/errors.ts +38 -0
- package/src/index.ts +82 -0
- package/src/network/client.ts +65 -0
- package/src/pubsub/client.ts +142 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) [year] [fullname]
|
|
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,329 @@
|
|
|
1
|
+
# @network/sdk - TypeScript SDK for DeBros Network
|
|
2
|
+
|
|
3
|
+
A modern, isomorphic TypeScript SDK for the DeBros Network gateway. Works seamlessly in both Node.js and browser environments with support for database operations, pub/sub messaging, and network management.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Isomorphic**: Works in Node.js and browsers (uses fetch and isomorphic-ws)
|
|
8
|
+
- **Database ORM-like API**: QueryBuilder, Repository pattern, transactions
|
|
9
|
+
- **Pub/Sub Messaging**: WebSocket subscriptions with automatic reconnection
|
|
10
|
+
- **Authentication**: API key and JWT support with automatic token management
|
|
11
|
+
- **TypeScript First**: Full type safety and IntelliSense
|
|
12
|
+
- **Error Handling**: Unified SDKError with HTTP status and code
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install @network/network-ts-sdk
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Quick Start
|
|
21
|
+
|
|
22
|
+
### Initialize the Client
|
|
23
|
+
|
|
24
|
+
```typescript
|
|
25
|
+
import { createClient } from "@network/sdk";
|
|
26
|
+
|
|
27
|
+
const client = createClient({
|
|
28
|
+
baseURL: "http://localhost:6001",
|
|
29
|
+
apiKey: "ak_your_api_key:namespace",
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
// Or with JWT
|
|
33
|
+
const client = createClient({
|
|
34
|
+
baseURL: "http://localhost:6001",
|
|
35
|
+
jwt: "your_jwt_token",
|
|
36
|
+
});
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### Database Operations
|
|
40
|
+
|
|
41
|
+
#### Create a Table
|
|
42
|
+
|
|
43
|
+
```typescript
|
|
44
|
+
await client.db.createTable(
|
|
45
|
+
"CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT, email TEXT)"
|
|
46
|
+
);
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
#### Insert Data
|
|
50
|
+
|
|
51
|
+
```typescript
|
|
52
|
+
const result = await client.db.exec(
|
|
53
|
+
"INSERT INTO users (name, email) VALUES (?, ?)",
|
|
54
|
+
["Alice", "alice@example.com"]
|
|
55
|
+
);
|
|
56
|
+
console.log(result.last_insert_id);
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
#### Query Data
|
|
60
|
+
|
|
61
|
+
```typescript
|
|
62
|
+
const users = await client.db.query("SELECT * FROM users WHERE email = ?", [
|
|
63
|
+
"alice@example.com",
|
|
64
|
+
]);
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
#### Using QueryBuilder
|
|
68
|
+
|
|
69
|
+
```typescript
|
|
70
|
+
const activeUsers = await client.db
|
|
71
|
+
.createQueryBuilder("users")
|
|
72
|
+
.where("active = ?", [1])
|
|
73
|
+
.orderBy("name DESC")
|
|
74
|
+
.limit(10)
|
|
75
|
+
.getMany();
|
|
76
|
+
|
|
77
|
+
const firstUser = await client.db
|
|
78
|
+
.createQueryBuilder("users")
|
|
79
|
+
.where("id = ?", [1])
|
|
80
|
+
.getOne();
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
#### Using Repository Pattern
|
|
84
|
+
|
|
85
|
+
```typescript
|
|
86
|
+
interface User {
|
|
87
|
+
id?: number;
|
|
88
|
+
name: string;
|
|
89
|
+
email: string;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const repo = client.db.repository<User>("users");
|
|
93
|
+
|
|
94
|
+
// Find
|
|
95
|
+
const users = await repo.find({ active: 1 });
|
|
96
|
+
const user = await repo.findOne({ email: "alice@example.com" });
|
|
97
|
+
|
|
98
|
+
// Save (INSERT or UPDATE)
|
|
99
|
+
const newUser: User = { name: "Bob", email: "bob@example.com" };
|
|
100
|
+
await repo.save(newUser);
|
|
101
|
+
|
|
102
|
+
// Remove
|
|
103
|
+
await repo.remove(newUser);
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
#### Transactions
|
|
107
|
+
|
|
108
|
+
```typescript
|
|
109
|
+
const results = await client.db.transaction([
|
|
110
|
+
{
|
|
111
|
+
kind: "exec",
|
|
112
|
+
sql: "INSERT INTO users (name, email) VALUES (?, ?)",
|
|
113
|
+
args: ["Charlie", "charlie@example.com"],
|
|
114
|
+
},
|
|
115
|
+
{
|
|
116
|
+
kind: "query",
|
|
117
|
+
sql: "SELECT COUNT(*) as count FROM users",
|
|
118
|
+
args: [],
|
|
119
|
+
},
|
|
120
|
+
]);
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
### Pub/Sub Messaging
|
|
124
|
+
|
|
125
|
+
#### Publish a Message
|
|
126
|
+
|
|
127
|
+
```typescript
|
|
128
|
+
await client.pubsub.publish("notifications", "Hello, Network!");
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
#### Subscribe to Topics
|
|
132
|
+
|
|
133
|
+
```typescript
|
|
134
|
+
const subscription = await client.pubsub.subscribe("notifications", {
|
|
135
|
+
onMessage: (msg) => {
|
|
136
|
+
console.log("Received:", msg.data);
|
|
137
|
+
},
|
|
138
|
+
onError: (err) => {
|
|
139
|
+
console.error("Subscription error:", err);
|
|
140
|
+
},
|
|
141
|
+
onClose: () => {
|
|
142
|
+
console.log("Subscription closed");
|
|
143
|
+
},
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
// Later, close the subscription
|
|
147
|
+
subscription.close();
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
#### List Topics
|
|
151
|
+
|
|
152
|
+
```typescript
|
|
153
|
+
const topics = await client.pubsub.topics();
|
|
154
|
+
console.log("Active topics:", topics);
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
### Authentication
|
|
158
|
+
|
|
159
|
+
#### Switch API Key
|
|
160
|
+
|
|
161
|
+
```typescript
|
|
162
|
+
client.auth.setApiKey("ak_new_key:namespace");
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
#### Switch JWT
|
|
166
|
+
|
|
167
|
+
```typescript
|
|
168
|
+
client.auth.setJwt("new_jwt_token");
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
#### Get Current Token
|
|
172
|
+
|
|
173
|
+
```typescript
|
|
174
|
+
const token = client.auth.getToken(); // Returns API key or JWT
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
#### Get Authentication Info
|
|
178
|
+
|
|
179
|
+
```typescript
|
|
180
|
+
const info = await client.auth.whoami();
|
|
181
|
+
console.log(info.authenticated, info.namespace);
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
#### Logout
|
|
185
|
+
|
|
186
|
+
```typescript
|
|
187
|
+
await client.auth.logout();
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
### Network Operations
|
|
191
|
+
|
|
192
|
+
#### Check Health
|
|
193
|
+
|
|
194
|
+
```typescript
|
|
195
|
+
const healthy = await client.network.health();
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
#### Get Network Status
|
|
199
|
+
|
|
200
|
+
```typescript
|
|
201
|
+
const status = await client.network.status();
|
|
202
|
+
console.log(status.healthy, status.peers);
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
#### List Peers
|
|
206
|
+
|
|
207
|
+
```typescript
|
|
208
|
+
const peers = await client.network.peers();
|
|
209
|
+
peers.forEach((peer) => {
|
|
210
|
+
console.log(peer.id, peer.addresses);
|
|
211
|
+
});
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
## Configuration
|
|
215
|
+
|
|
216
|
+
### ClientConfig
|
|
217
|
+
|
|
218
|
+
```typescript
|
|
219
|
+
interface ClientConfig {
|
|
220
|
+
baseURL: string; // Gateway URL
|
|
221
|
+
apiKey?: string; // API key (optional, if using JWT instead)
|
|
222
|
+
jwt?: string; // JWT token (optional, if using API key instead)
|
|
223
|
+
timeout?: number; // Request timeout in ms (default: 30000)
|
|
224
|
+
maxRetries?: number; // Max retry attempts (default: 3)
|
|
225
|
+
retryDelayMs?: number; // Delay between retries (default: 1000)
|
|
226
|
+
storage?: StorageAdapter; // For persisting JWT/API key (default: MemoryStorage)
|
|
227
|
+
wsConfig?: Partial<WSClientConfig>; // WebSocket configuration
|
|
228
|
+
fetch?: typeof fetch; // Custom fetch implementation
|
|
229
|
+
}
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
### Storage Adapters
|
|
233
|
+
|
|
234
|
+
By default, credentials are stored in memory. For browser apps, use localStorage:
|
|
235
|
+
|
|
236
|
+
```typescript
|
|
237
|
+
import { createClient, LocalStorageAdapter } from "@network/sdk";
|
|
238
|
+
|
|
239
|
+
const client = createClient({
|
|
240
|
+
baseURL: "http://localhost:6001",
|
|
241
|
+
storage: new LocalStorageAdapter(),
|
|
242
|
+
apiKey: "ak_your_key:namespace",
|
|
243
|
+
});
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
## Error Handling
|
|
247
|
+
|
|
248
|
+
The SDK throws `SDKError` for all errors:
|
|
249
|
+
|
|
250
|
+
```typescript
|
|
251
|
+
import { SDKError } from "@network/sdk";
|
|
252
|
+
|
|
253
|
+
try {
|
|
254
|
+
await client.db.query("SELECT * FROM nonexistent");
|
|
255
|
+
} catch (error) {
|
|
256
|
+
if (error instanceof SDKError) {
|
|
257
|
+
console.log(error.httpStatus); // e.g., 400
|
|
258
|
+
console.log(error.code); // e.g., "HTTP_400"
|
|
259
|
+
console.log(error.message); // Error message
|
|
260
|
+
console.log(error.details); // Full error response
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
## Browser Usage
|
|
266
|
+
|
|
267
|
+
The SDK works in browsers with minimal setup:
|
|
268
|
+
|
|
269
|
+
```typescript
|
|
270
|
+
// Browser example
|
|
271
|
+
import { createClient } from "@network/sdk";
|
|
272
|
+
|
|
273
|
+
const client = createClient({
|
|
274
|
+
baseURL: "https://gateway.example.com",
|
|
275
|
+
apiKey: "ak_browser_key:my-app",
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
// Use like any other API client
|
|
279
|
+
const data = await client.db.query("SELECT * FROM items");
|
|
280
|
+
```
|
|
281
|
+
|
|
282
|
+
**Note**: For WebSocket connections in browsers with authentication, ensure your gateway supports either header-based auth or query parameter auth.
|
|
283
|
+
|
|
284
|
+
## Testing
|
|
285
|
+
|
|
286
|
+
Run E2E tests against a running gateway:
|
|
287
|
+
|
|
288
|
+
```bash
|
|
289
|
+
# Set environment variables
|
|
290
|
+
export GATEWAY_BASE_URL=http://localhost:6001
|
|
291
|
+
export GATEWAY_API_KEY=ak_test_key:default
|
|
292
|
+
|
|
293
|
+
# Run tests
|
|
294
|
+
npm run test:e2e
|
|
295
|
+
```
|
|
296
|
+
|
|
297
|
+
## Examples
|
|
298
|
+
|
|
299
|
+
See the `tests/e2e/` directory for complete examples of:
|
|
300
|
+
|
|
301
|
+
- Authentication (`auth.test.ts`)
|
|
302
|
+
- Database operations (`db.test.ts`)
|
|
303
|
+
- Transactions (`tx.test.ts`)
|
|
304
|
+
- Pub/Sub messaging (`pubsub.test.ts`)
|
|
305
|
+
- Network operations (`network.test.ts`)
|
|
306
|
+
|
|
307
|
+
## Building
|
|
308
|
+
|
|
309
|
+
```bash
|
|
310
|
+
npm run build
|
|
311
|
+
```
|
|
312
|
+
|
|
313
|
+
Output goes to `dist/` with ESM and type declarations.
|
|
314
|
+
|
|
315
|
+
## Development
|
|
316
|
+
|
|
317
|
+
```bash
|
|
318
|
+
npm run dev # Watch mode
|
|
319
|
+
npm run typecheck # Type checking
|
|
320
|
+
npm run lint # Linting (if configured)
|
|
321
|
+
```
|
|
322
|
+
|
|
323
|
+
## License
|
|
324
|
+
|
|
325
|
+
MIT
|
|
326
|
+
|
|
327
|
+
## Support
|
|
328
|
+
|
|
329
|
+
For issues, questions, or contributions, please open an issue on GitHub or visit [DeBros Network Documentation](https://network.debros.io/docs/).
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,367 @@
|
|
|
1
|
+
import WebSocket from 'isomorphic-ws';
|
|
2
|
+
|
|
3
|
+
interface HttpClientConfig {
|
|
4
|
+
baseURL: string;
|
|
5
|
+
timeout?: number;
|
|
6
|
+
maxRetries?: number;
|
|
7
|
+
retryDelayMs?: number;
|
|
8
|
+
fetch?: typeof fetch;
|
|
9
|
+
}
|
|
10
|
+
declare class HttpClient {
|
|
11
|
+
private baseURL;
|
|
12
|
+
private timeout;
|
|
13
|
+
private maxRetries;
|
|
14
|
+
private retryDelayMs;
|
|
15
|
+
private fetch;
|
|
16
|
+
private apiKey?;
|
|
17
|
+
private jwt?;
|
|
18
|
+
constructor(config: HttpClientConfig);
|
|
19
|
+
setApiKey(apiKey?: string): void;
|
|
20
|
+
setJwt(jwt?: string): void;
|
|
21
|
+
private getAuthHeaders;
|
|
22
|
+
private getAuthToken;
|
|
23
|
+
request<T = any>(method: "GET" | "POST" | "PUT" | "DELETE", path: string, options?: {
|
|
24
|
+
body?: any;
|
|
25
|
+
headers?: Record<string, string>;
|
|
26
|
+
query?: Record<string, string | number | boolean>;
|
|
27
|
+
}): Promise<T>;
|
|
28
|
+
private requestWithRetry;
|
|
29
|
+
get<T = any>(path: string, options?: Omit<Parameters<typeof this.request>[2], "body">): Promise<T>;
|
|
30
|
+
post<T = any>(path: string, body?: any, options?: Omit<Parameters<typeof this.request>[2], "body">): Promise<T>;
|
|
31
|
+
put<T = any>(path: string, body?: any, options?: Omit<Parameters<typeof this.request>[2], "body">): Promise<T>;
|
|
32
|
+
delete<T = any>(path: string, options?: Omit<Parameters<typeof this.request>[2], "body">): Promise<T>;
|
|
33
|
+
getToken(): string | undefined;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
interface AuthConfig {
|
|
37
|
+
apiKey?: string;
|
|
38
|
+
jwt?: string;
|
|
39
|
+
}
|
|
40
|
+
interface WhoAmI {
|
|
41
|
+
address?: string;
|
|
42
|
+
namespace?: string;
|
|
43
|
+
authenticated: boolean;
|
|
44
|
+
}
|
|
45
|
+
interface StorageAdapter {
|
|
46
|
+
get(key: string): Promise<string | null>;
|
|
47
|
+
set(key: string, value: string): Promise<void>;
|
|
48
|
+
clear(): Promise<void>;
|
|
49
|
+
}
|
|
50
|
+
declare class MemoryStorage implements StorageAdapter {
|
|
51
|
+
private storage;
|
|
52
|
+
get(key: string): Promise<string | null>;
|
|
53
|
+
set(key: string, value: string): Promise<void>;
|
|
54
|
+
clear(): Promise<void>;
|
|
55
|
+
}
|
|
56
|
+
declare class LocalStorageAdapter implements StorageAdapter {
|
|
57
|
+
private prefix;
|
|
58
|
+
get(key: string): Promise<string | null>;
|
|
59
|
+
set(key: string, value: string): Promise<void>;
|
|
60
|
+
clear(): Promise<void>;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
declare class AuthClient {
|
|
64
|
+
private httpClient;
|
|
65
|
+
private storage;
|
|
66
|
+
private currentApiKey?;
|
|
67
|
+
private currentJwt?;
|
|
68
|
+
constructor(config: {
|
|
69
|
+
httpClient: HttpClient;
|
|
70
|
+
storage?: StorageAdapter;
|
|
71
|
+
apiKey?: string;
|
|
72
|
+
jwt?: string;
|
|
73
|
+
});
|
|
74
|
+
setApiKey(apiKey: string): void;
|
|
75
|
+
setJwt(jwt: string): void;
|
|
76
|
+
getToken(): string | undefined;
|
|
77
|
+
whoami(): Promise<WhoAmI>;
|
|
78
|
+
refresh(): Promise<string>;
|
|
79
|
+
logout(): Promise<void>;
|
|
80
|
+
clear(): Promise<void>;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
declare class QueryBuilder {
|
|
84
|
+
private httpClient;
|
|
85
|
+
private table;
|
|
86
|
+
private options;
|
|
87
|
+
constructor(httpClient: HttpClient, table: string);
|
|
88
|
+
select(...columns: string[]): this;
|
|
89
|
+
innerJoin(table: string, on: string): this;
|
|
90
|
+
leftJoin(table: string, on: string): this;
|
|
91
|
+
rightJoin(table: string, on: string): this;
|
|
92
|
+
where(expr: string, args?: any[]): this;
|
|
93
|
+
andWhere(expr: string, args?: any[]): this;
|
|
94
|
+
orWhere(expr: string, args?: any[]): this;
|
|
95
|
+
groupBy(...columns: string[]): this;
|
|
96
|
+
orderBy(...columns: string[]): this;
|
|
97
|
+
limit(n: number): this;
|
|
98
|
+
offset(n: number): this;
|
|
99
|
+
getMany<T = any>(ctx?: any): Promise<T[]>;
|
|
100
|
+
getOne<T = any>(ctx?: any): Promise<T | null>;
|
|
101
|
+
count(): Promise<number>;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
interface Entity {
|
|
105
|
+
TableName(): string;
|
|
106
|
+
}
|
|
107
|
+
interface QueryResponse {
|
|
108
|
+
columns?: string[];
|
|
109
|
+
rows?: any[][];
|
|
110
|
+
count?: number;
|
|
111
|
+
items?: any[];
|
|
112
|
+
}
|
|
113
|
+
interface TransactionOp {
|
|
114
|
+
kind: "exec" | "query";
|
|
115
|
+
sql: string;
|
|
116
|
+
args?: any[];
|
|
117
|
+
}
|
|
118
|
+
interface TransactionRequest {
|
|
119
|
+
statements?: string[];
|
|
120
|
+
ops?: TransactionOp[];
|
|
121
|
+
return_results?: boolean;
|
|
122
|
+
}
|
|
123
|
+
interface SelectOptions {
|
|
124
|
+
select?: string[];
|
|
125
|
+
joins?: Array<{
|
|
126
|
+
kind: "INNER" | "LEFT" | "RIGHT" | "FULL";
|
|
127
|
+
table: string;
|
|
128
|
+
on: string;
|
|
129
|
+
}>;
|
|
130
|
+
where?: Array<{
|
|
131
|
+
conj?: "AND" | "OR";
|
|
132
|
+
expr: string;
|
|
133
|
+
args?: any[];
|
|
134
|
+
}>;
|
|
135
|
+
group_by?: string[];
|
|
136
|
+
order_by?: string[];
|
|
137
|
+
limit?: number;
|
|
138
|
+
offset?: number;
|
|
139
|
+
one?: boolean;
|
|
140
|
+
}
|
|
141
|
+
type FindOptions = Omit<SelectOptions, "select" | "joins" | "one">;
|
|
142
|
+
interface ColumnDefinition {
|
|
143
|
+
name: string;
|
|
144
|
+
isPrimaryKey?: boolean;
|
|
145
|
+
isAutoIncrement?: boolean;
|
|
146
|
+
}
|
|
147
|
+
declare function extractTableName(entity: Entity | string): string;
|
|
148
|
+
declare function extractPrimaryKey(entity: any): string | undefined;
|
|
149
|
+
|
|
150
|
+
declare class Repository<T extends Record<string, any>> {
|
|
151
|
+
private httpClient;
|
|
152
|
+
private tableName;
|
|
153
|
+
private primaryKey;
|
|
154
|
+
constructor(httpClient: HttpClient, tableName: string, primaryKey?: string);
|
|
155
|
+
createQueryBuilder(): QueryBuilder;
|
|
156
|
+
find(criteria?: Record<string, any>, options?: FindOptions): Promise<T[]>;
|
|
157
|
+
findOne(criteria: Record<string, any>): Promise<T | null>;
|
|
158
|
+
save(entity: T): Promise<T>;
|
|
159
|
+
remove(entity: T | Record<string, any>): Promise<void>;
|
|
160
|
+
private buildInsertSql;
|
|
161
|
+
private buildInsertArgs;
|
|
162
|
+
private buildUpdateSql;
|
|
163
|
+
private buildUpdateArgs;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
declare class DBClient {
|
|
167
|
+
private httpClient;
|
|
168
|
+
constructor(httpClient: HttpClient);
|
|
169
|
+
/**
|
|
170
|
+
* Execute a write/DDL SQL statement.
|
|
171
|
+
*/
|
|
172
|
+
exec(sql: string, args?: any[]): Promise<{
|
|
173
|
+
rows_affected: number;
|
|
174
|
+
last_insert_id?: number;
|
|
175
|
+
}>;
|
|
176
|
+
/**
|
|
177
|
+
* Execute a SELECT query.
|
|
178
|
+
*/
|
|
179
|
+
query<T = any>(sql: string, args?: any[]): Promise<T[]>;
|
|
180
|
+
/**
|
|
181
|
+
* Find rows with map-based criteria.
|
|
182
|
+
*/
|
|
183
|
+
find<T = any>(table: string, criteria?: Record<string, any>, options?: FindOptions): Promise<T[]>;
|
|
184
|
+
/**
|
|
185
|
+
* Find a single row with map-based criteria.
|
|
186
|
+
*/
|
|
187
|
+
findOne<T = any>(table: string, criteria: Record<string, any>): Promise<T | null>;
|
|
188
|
+
/**
|
|
189
|
+
* Create a fluent QueryBuilder for complex SELECT queries.
|
|
190
|
+
*/
|
|
191
|
+
createQueryBuilder(table: string): QueryBuilder;
|
|
192
|
+
/**
|
|
193
|
+
* Create a Repository for entity-based operations.
|
|
194
|
+
*/
|
|
195
|
+
repository<T extends Record<string, any>>(tableName: string, primaryKey?: string): Repository<T>;
|
|
196
|
+
/**
|
|
197
|
+
* Execute multiple operations atomically.
|
|
198
|
+
*/
|
|
199
|
+
transaction(ops: TransactionOp[], returnResults?: boolean): Promise<any[]>;
|
|
200
|
+
/**
|
|
201
|
+
* Create a table from DDL SQL.
|
|
202
|
+
*/
|
|
203
|
+
createTable(schema: string): Promise<void>;
|
|
204
|
+
/**
|
|
205
|
+
* Drop a table.
|
|
206
|
+
*/
|
|
207
|
+
dropTable(table: string): Promise<void>;
|
|
208
|
+
/**
|
|
209
|
+
* Get current database schema.
|
|
210
|
+
*/
|
|
211
|
+
getSchema(): Promise<any>;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
interface WSClientConfig {
|
|
215
|
+
wsURL: string;
|
|
216
|
+
timeout?: number;
|
|
217
|
+
maxReconnectAttempts?: number;
|
|
218
|
+
reconnectDelayMs?: number;
|
|
219
|
+
heartbeatIntervalMs?: number;
|
|
220
|
+
authMode?: "header" | "query";
|
|
221
|
+
authToken?: string;
|
|
222
|
+
WebSocket?: typeof WebSocket;
|
|
223
|
+
}
|
|
224
|
+
type WSMessageHandler = (data: string) => void;
|
|
225
|
+
type WSErrorHandler = (error: Error) => void;
|
|
226
|
+
type WSCloseHandler = () => void;
|
|
227
|
+
declare class WSClient {
|
|
228
|
+
private url;
|
|
229
|
+
private timeout;
|
|
230
|
+
private maxReconnectAttempts;
|
|
231
|
+
private reconnectDelayMs;
|
|
232
|
+
private heartbeatIntervalMs;
|
|
233
|
+
private authMode;
|
|
234
|
+
private authToken?;
|
|
235
|
+
private WebSocketClass;
|
|
236
|
+
private ws?;
|
|
237
|
+
private reconnectAttempts;
|
|
238
|
+
private heartbeatInterval?;
|
|
239
|
+
private messageHandlers;
|
|
240
|
+
private errorHandlers;
|
|
241
|
+
private closeHandlers;
|
|
242
|
+
private isManuallyClosed;
|
|
243
|
+
constructor(config: WSClientConfig);
|
|
244
|
+
connect(): Promise<void>;
|
|
245
|
+
private buildWSUrl;
|
|
246
|
+
private startHeartbeat;
|
|
247
|
+
private stopHeartbeat;
|
|
248
|
+
private attemptReconnect;
|
|
249
|
+
onMessage(handler: WSMessageHandler): () => boolean;
|
|
250
|
+
onError(handler: WSErrorHandler): () => boolean;
|
|
251
|
+
onClose(handler: WSCloseHandler): () => boolean;
|
|
252
|
+
send(data: string): void;
|
|
253
|
+
close(): void;
|
|
254
|
+
isConnected(): boolean;
|
|
255
|
+
setAuthToken(token?: string): void;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
interface Message {
|
|
259
|
+
data: string;
|
|
260
|
+
topic: string;
|
|
261
|
+
timestamp?: number;
|
|
262
|
+
}
|
|
263
|
+
type MessageHandler = (message: Message) => void;
|
|
264
|
+
type ErrorHandler = (error: Error) => void;
|
|
265
|
+
type CloseHandler = () => void;
|
|
266
|
+
declare class PubSubClient {
|
|
267
|
+
private httpClient;
|
|
268
|
+
private wsConfig;
|
|
269
|
+
constructor(httpClient: HttpClient, wsConfig?: Partial<WSClientConfig>);
|
|
270
|
+
/**
|
|
271
|
+
* Publish a message to a topic.
|
|
272
|
+
*/
|
|
273
|
+
publish(topic: string, data: string | Uint8Array): Promise<void>;
|
|
274
|
+
/**
|
|
275
|
+
* List active topics in the current namespace.
|
|
276
|
+
*/
|
|
277
|
+
topics(): Promise<string[]>;
|
|
278
|
+
/**
|
|
279
|
+
* Subscribe to a topic via WebSocket.
|
|
280
|
+
* Returns a subscription object with event handlers.
|
|
281
|
+
*/
|
|
282
|
+
subscribe(topic: string, handlers?: {
|
|
283
|
+
onMessage?: MessageHandler;
|
|
284
|
+
onError?: ErrorHandler;
|
|
285
|
+
onClose?: CloseHandler;
|
|
286
|
+
}): Promise<Subscription>;
|
|
287
|
+
}
|
|
288
|
+
declare class Subscription {
|
|
289
|
+
private wsClient;
|
|
290
|
+
private topic;
|
|
291
|
+
private messageHandlers;
|
|
292
|
+
private errorHandlers;
|
|
293
|
+
private closeHandlers;
|
|
294
|
+
constructor(wsClient: WSClient, topic: string);
|
|
295
|
+
onMessage(handler: MessageHandler): () => boolean;
|
|
296
|
+
onError(handler: ErrorHandler): () => boolean;
|
|
297
|
+
onClose(handler: CloseHandler): () => boolean;
|
|
298
|
+
close(): void;
|
|
299
|
+
isConnected(): boolean;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
interface PeerInfo {
|
|
303
|
+
id: string;
|
|
304
|
+
addresses: string[];
|
|
305
|
+
lastSeen?: string;
|
|
306
|
+
}
|
|
307
|
+
interface NetworkStatus {
|
|
308
|
+
healthy: boolean;
|
|
309
|
+
peers: number;
|
|
310
|
+
uptime?: number;
|
|
311
|
+
}
|
|
312
|
+
declare class NetworkClient {
|
|
313
|
+
private httpClient;
|
|
314
|
+
constructor(httpClient: HttpClient);
|
|
315
|
+
/**
|
|
316
|
+
* Check gateway health.
|
|
317
|
+
*/
|
|
318
|
+
health(): Promise<boolean>;
|
|
319
|
+
/**
|
|
320
|
+
* Get network status.
|
|
321
|
+
*/
|
|
322
|
+
status(): Promise<NetworkStatus>;
|
|
323
|
+
/**
|
|
324
|
+
* Get connected peers.
|
|
325
|
+
*/
|
|
326
|
+
peers(): Promise<PeerInfo[]>;
|
|
327
|
+
/**
|
|
328
|
+
* Connect to a peer.
|
|
329
|
+
*/
|
|
330
|
+
connect(peerAddr: string): Promise<void>;
|
|
331
|
+
/**
|
|
332
|
+
* Disconnect from a peer.
|
|
333
|
+
*/
|
|
334
|
+
disconnect(peerId: string): Promise<void>;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
declare class SDKError extends Error {
|
|
338
|
+
readonly httpStatus: number;
|
|
339
|
+
readonly code: string;
|
|
340
|
+
readonly details: Record<string, any>;
|
|
341
|
+
constructor(message: string, httpStatus?: number, code?: string, details?: Record<string, any>);
|
|
342
|
+
static fromResponse(status: number, body: any, message?: string): SDKError;
|
|
343
|
+
toJSON(): {
|
|
344
|
+
name: string;
|
|
345
|
+
message: string;
|
|
346
|
+
httpStatus: number;
|
|
347
|
+
code: string;
|
|
348
|
+
details: Record<string, any>;
|
|
349
|
+
};
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
interface ClientConfig extends Omit<HttpClientConfig, "fetch"> {
|
|
353
|
+
apiKey?: string;
|
|
354
|
+
jwt?: string;
|
|
355
|
+
storage?: StorageAdapter;
|
|
356
|
+
wsConfig?: Partial<WSClientConfig>;
|
|
357
|
+
fetch?: typeof fetch;
|
|
358
|
+
}
|
|
359
|
+
interface Client {
|
|
360
|
+
auth: AuthClient;
|
|
361
|
+
db: DBClient;
|
|
362
|
+
pubsub: PubSubClient;
|
|
363
|
+
network: NetworkClient;
|
|
364
|
+
}
|
|
365
|
+
declare function createClient(config: ClientConfig): Client;
|
|
366
|
+
|
|
367
|
+
export { AuthClient, type AuthConfig, type Client, type ClientConfig, type CloseHandler, type ColumnDefinition, DBClient, type Entity, type ErrorHandler, type FindOptions, HttpClient, LocalStorageAdapter, MemoryStorage, type Message, type MessageHandler, NetworkClient, type NetworkStatus, type PeerInfo, PubSubClient, QueryBuilder, type QueryResponse, Repository, SDKError, type SelectOptions, type StorageAdapter, Subscription, type TransactionOp, type TransactionRequest, WSClient, type WhoAmI, createClient, extractPrimaryKey, extractTableName };
|