@anonfly/sdk 1.0.1 → 1.0.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/README.md
CHANGED
|
@@ -1,6 +1,21 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<img src="../../docs/images/logo.png" width="96" />
|
|
3
|
+
</p>
|
|
4
|
+
|
|
1
5
|
# @anonfly/sdk
|
|
2
6
|
|
|
3
|
-
|
|
7
|
+
[](https://www.npmjs.com/package/@anonfly/sdk)
|
|
8
|
+
[](https://www.npmjs.com/package/@anonfly/sdk)
|
|
9
|
+
|
|
10
|
+
The core logic for the Anonfly messaging platform. This package provides a low-level, headless SDK for interacting with the Anonfly API and WebSocket services.
|
|
11
|
+
|
|
12
|
+
## Features
|
|
13
|
+
|
|
14
|
+
- **🛡️ Industrial-grade Transport:** Powerful `HttpClient` with middleware support.
|
|
15
|
+
- **⚡ Real-time Engine:** Robust `WebSocketClient` with automatic reconnection.
|
|
16
|
+
- **🔄 Resilience:** Built-in `retryMiddleware` with exponential backoff.
|
|
17
|
+
- **📦 Resource-based API:** Clean access to `rooms`, `messages`, `auth`, and `admin` logic.
|
|
18
|
+
- **✨ Pure TypeScript:** Zero-dependency core logic with full type-safety.
|
|
4
19
|
|
|
5
20
|
## Installation
|
|
6
21
|
|
|
@@ -8,12 +23,87 @@ Core SDK logic for the Anonfly service.
|
|
|
8
23
|
npm install @anonfly/sdk
|
|
9
24
|
```
|
|
10
25
|
|
|
11
|
-
##
|
|
26
|
+
## Usage
|
|
27
|
+
|
|
28
|
+
### Basic Initialization
|
|
29
|
+
|
|
30
|
+
```typescript
|
|
31
|
+
import { Anonfly } from '@anonfly/sdk';
|
|
32
|
+
|
|
33
|
+
const anonfly = new Anonfly({
|
|
34
|
+
apiKey: 'YOUR_API_KEY',
|
|
35
|
+
baseUrl: 'https://api.anonfly.com/v1',
|
|
36
|
+
wsUrl: 'wss://api.anonfly.com', // Optional for real-time
|
|
37
|
+
retries: 3, // Optional: defaults to built-in retry logic
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
// Use resources
|
|
41
|
+
const rooms = await anonfly.rooms.list();
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### Working with Resources
|
|
45
|
+
|
|
46
|
+
The SDK is organized into logical resources:
|
|
47
|
+
|
|
48
|
+
```typescript
|
|
49
|
+
// Authentication
|
|
50
|
+
const session = await anonfly.auth.login(credentials);
|
|
51
|
+
|
|
52
|
+
// Message Management
|
|
53
|
+
const message = await anonfly.messages.send(roomId, {
|
|
54
|
+
content: "Hello World",
|
|
55
|
+
type: "text",
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
// Room Management
|
|
59
|
+
const room = await anonfly.rooms.create({ name: "General" });
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### Advanced: Custom Middleware
|
|
63
|
+
|
|
64
|
+
You can extend the `HttpClient` with custom middleware:
|
|
65
|
+
|
|
66
|
+
```typescript
|
|
67
|
+
anonfly.http.use(async (request, next) => {
|
|
68
|
+
console.log(`Starting request to ${request.url}`);
|
|
69
|
+
const response = await next(request);
|
|
70
|
+
console.log(`Finished request with status ${response.status}`);
|
|
71
|
+
return response;
|
|
72
|
+
});
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
### Real-time with WebSockets
|
|
76
|
+
|
|
77
|
+
```typescript
|
|
78
|
+
if (anonfly.ws) {
|
|
79
|
+
anonfly.ws.subscribe('message:new', (data) => {
|
|
80
|
+
console.log('New message received:', data);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
anonfly.ws.connect();
|
|
84
|
+
}
|
|
85
|
+
```
|
|
12
86
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
87
|
+
## Recipes
|
|
88
|
+
|
|
89
|
+
### Handling Connection Drops
|
|
90
|
+
|
|
91
|
+
The SDK automatically handles reconnections if configured, but you can also listen to events:
|
|
92
|
+
|
|
93
|
+
```typescript
|
|
94
|
+
anonfly.ws?.on('close', () => {
|
|
95
|
+
console.warn('Connection lost. Cleaning up UI state...');
|
|
96
|
+
});
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
## TypeScript Usage
|
|
100
|
+
|
|
101
|
+
`@anonfly/sdk` is written 100% in TypeScript. All types are exported from the main entry point:
|
|
102
|
+
|
|
103
|
+
```typescript
|
|
104
|
+
import { AnonflyConfig, Room, Message } from '@anonfly/sdk';
|
|
105
|
+
```
|
|
16
106
|
|
|
17
107
|
## License
|
|
18
108
|
|
|
19
|
-
MIT
|
|
109
|
+
[MIT](../../LICENSE)
|
|
@@ -13,6 +13,8 @@ export declare class WebSocketClient extends EventEmitter {
|
|
|
13
13
|
connect(): void;
|
|
14
14
|
private reconnect;
|
|
15
15
|
send(data: any): void;
|
|
16
|
+
subscribe(topic: string, handler: (data: any) => void): void;
|
|
17
|
+
unsubscribe(topic: string, handler: (data: any) => void): void;
|
|
16
18
|
disconnect(): void;
|
|
17
19
|
}
|
|
18
20
|
//# sourceMappingURL=WebSocketClient.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"WebSocketClient.d.ts","sourceRoot":"","sources":["../../../src/core/transport/WebSocketClient.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAC;AAGtC,MAAM,WAAW,QAAQ;IACrB,GAAG,EAAE,MAAM,CAAC;IACZ,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,UAAU,CAAC,EAAE,MAAM,CAAC;CACvB;AAED,qBAAa,eAAgB,SAAQ,YAAY;IAIjC,OAAO,CAAC,MAAM;IAH1B,OAAO,CAAC,EAAE,CAA0B;IACpC,OAAO,CAAC,UAAU,CAAK;gBAEH,MAAM,EAAE,QAAQ;IAIpC,OAAO;
|
|
1
|
+
{"version":3,"file":"WebSocketClient.d.ts","sourceRoot":"","sources":["../../../src/core/transport/WebSocketClient.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAC;AAGtC,MAAM,WAAW,QAAQ;IACrB,GAAG,EAAE,MAAM,CAAC;IACZ,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,UAAU,CAAC,EAAE,MAAM,CAAC;CACvB;AAED,qBAAa,eAAgB,SAAQ,YAAY;IAIjC,OAAO,CAAC,MAAM;IAH1B,OAAO,CAAC,EAAE,CAA0B;IACpC,OAAO,CAAC,UAAU,CAAK;gBAEH,MAAM,EAAE,QAAQ;IAIpC,OAAO;IAoCP,OAAO,CAAC,SAAS;IAMjB,IAAI,CAAC,IAAI,EAAE,GAAG;IAQd,SAAS,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC,IAAI,EAAE,GAAG,KAAK,IAAI;IAIrD,WAAW,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC,IAAI,EAAE,GAAG,KAAK,IAAI;IAIvD,UAAU;CAMb"}
|
package/dist/index.cjs
CHANGED
|
@@ -225,6 +225,11 @@ var WebSocketClient = class extends import_events.EventEmitter {
|
|
|
225
225
|
try {
|
|
226
226
|
const message = JSON.parse(data.toString());
|
|
227
227
|
this.emit("message", message);
|
|
228
|
+
if (message.topic) {
|
|
229
|
+
this.emit(message.topic, message);
|
|
230
|
+
} else if (message.chatroomId) {
|
|
231
|
+
this.emit(`room:${message.chatroomId}`, message);
|
|
232
|
+
}
|
|
228
233
|
} catch {
|
|
229
234
|
this.emit("message", data.toString());
|
|
230
235
|
}
|
|
@@ -251,6 +256,12 @@ var WebSocketClient = class extends import_events.EventEmitter {
|
|
|
251
256
|
throw new Error("WebSocket is not connected");
|
|
252
257
|
}
|
|
253
258
|
}
|
|
259
|
+
subscribe(topic, handler) {
|
|
260
|
+
this.on(topic, handler);
|
|
261
|
+
}
|
|
262
|
+
unsubscribe(topic, handler) {
|
|
263
|
+
this.off(topic, handler);
|
|
264
|
+
}
|
|
254
265
|
disconnect() {
|
|
255
266
|
if (this.ws) {
|
|
256
267
|
this.ws.close();
|
package/dist/index.js
CHANGED
|
@@ -188,6 +188,11 @@ var WebSocketClient = class extends EventEmitter {
|
|
|
188
188
|
try {
|
|
189
189
|
const message = JSON.parse(data.toString());
|
|
190
190
|
this.emit("message", message);
|
|
191
|
+
if (message.topic) {
|
|
192
|
+
this.emit(message.topic, message);
|
|
193
|
+
} else if (message.chatroomId) {
|
|
194
|
+
this.emit(`room:${message.chatroomId}`, message);
|
|
195
|
+
}
|
|
191
196
|
} catch {
|
|
192
197
|
this.emit("message", data.toString());
|
|
193
198
|
}
|
|
@@ -214,6 +219,12 @@ var WebSocketClient = class extends EventEmitter {
|
|
|
214
219
|
throw new Error("WebSocket is not connected");
|
|
215
220
|
}
|
|
216
221
|
}
|
|
222
|
+
subscribe(topic, handler) {
|
|
223
|
+
this.on(topic, handler);
|
|
224
|
+
}
|
|
225
|
+
unsubscribe(topic, handler) {
|
|
226
|
+
this.off(topic, handler);
|
|
227
|
+
}
|
|
217
228
|
disconnect() {
|
|
218
229
|
if (this.ws) {
|
|
219
230
|
this.ws.close();
|