@laplace.live/event-bridge-sdk 0.0.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 +111 -0
- package/dist/index.js +1 -0
- package/package.json +31 -0
package/README.md
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
# LAPLACE Event Bridge TypeScript SDK
|
|
2
|
+
|
|
3
|
+
A TypeScript SDK for connecting to the LAPLACE Event Bridge server. This SDK allows clients to receive and send events from/to the LAPLACE Event Bridge.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
# Using npm
|
|
9
|
+
npm install @laplace.live/event-bridge
|
|
10
|
+
|
|
11
|
+
# Using Yarn
|
|
12
|
+
yarn add @laplace.live/event-bridge
|
|
13
|
+
|
|
14
|
+
# Using Bun
|
|
15
|
+
bun add @laplace.live/event-bridge
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Usage
|
|
19
|
+
|
|
20
|
+
### Basic Usage
|
|
21
|
+
|
|
22
|
+
```typescript
|
|
23
|
+
import { LaplaceEventClient } from '@laplace.live/event-bridge';
|
|
24
|
+
import type { Message, System } from '@laplace.live/event-types';
|
|
25
|
+
|
|
26
|
+
// Create a new client
|
|
27
|
+
const client = new LaplaceEventClient({
|
|
28
|
+
url: 'ws://localhost:9696',
|
|
29
|
+
token: 'your-auth-token', // Optional
|
|
30
|
+
reconnect: true,
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
// Connect to the bridge
|
|
34
|
+
await client.connect();
|
|
35
|
+
|
|
36
|
+
// Event types are automatically inferred from the event type string!
|
|
37
|
+
client.on('message', (event) => {
|
|
38
|
+
// TypeScript knows 'event' is of type 'Message' here
|
|
39
|
+
console.log(`${event.username}: ${event.message}`);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
// No need for explicit type parameters
|
|
43
|
+
client.on('system', (event) => {
|
|
44
|
+
// TypeScript knows 'event' is of type 'System' here
|
|
45
|
+
console.log(`System: ${event.message}`);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
// Listen for any event
|
|
49
|
+
client.onAny((event) => {
|
|
50
|
+
console.log(`Received event of type: ${event.type}`);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
// Disconnect when done
|
|
54
|
+
client.disconnect();
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### Connection Options
|
|
58
|
+
|
|
59
|
+
```typescript
|
|
60
|
+
interface ConnectionOptions {
|
|
61
|
+
url?: string; // WebSocket URL, default: 'ws://localhost:9696'
|
|
62
|
+
token?: string; // Authentication token, default: ''
|
|
63
|
+
reconnect?: boolean; // Auto reconnect on disconnect, default: true
|
|
64
|
+
reconnectInterval?: number; // Milliseconds between reconnect attempts, default: 3000
|
|
65
|
+
maxReconnectAttempts?: number; // Maximum reconnect attempts, default: 10
|
|
66
|
+
}
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
### API Reference
|
|
70
|
+
|
|
71
|
+
#### Methods
|
|
72
|
+
|
|
73
|
+
- `connect()`: Connect to the event bridge. Returns a Promise that resolves when connected.
|
|
74
|
+
- `disconnect()`: Disconnect from the event bridge.
|
|
75
|
+
- `on(eventType, handler)`: Register an event handler for a specific event type with automatic type inference.
|
|
76
|
+
- `onAny(handler)`: Register a handler for all events.
|
|
77
|
+
- `off(eventType, handler)`: Remove an event handler for a specific event type.
|
|
78
|
+
- `offAny(handler)`: Remove a handler for all events.
|
|
79
|
+
- `isConnectedToBridge()`: Check if the client is connected to the bridge.
|
|
80
|
+
- `getClientId()`: Get the client ID assigned by the bridge.
|
|
81
|
+
- `send(event)`: Send an event to the bridge.
|
|
82
|
+
|
|
83
|
+
## Type Inference
|
|
84
|
+
|
|
85
|
+
The SDK automatically infers the event type based on the event.type property. This means you don't need to use explicit type parameters or type guards when setting up event handlers.
|
|
86
|
+
|
|
87
|
+
For example:
|
|
88
|
+
|
|
89
|
+
```typescript
|
|
90
|
+
// This will infer that 'event' is of type 'Message'
|
|
91
|
+
client.on('message', (event) => {
|
|
92
|
+
// TypeScript knows these properties exist
|
|
93
|
+
console.log(event.username);
|
|
94
|
+
console.log(event.message);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
// This will infer that 'event' is of type 'Gift'
|
|
98
|
+
client.on('gift', (event) => {
|
|
99
|
+
// TypeScript knows these properties exist
|
|
100
|
+
console.log(event.giftName);
|
|
101
|
+
console.log(event.giftCount);
|
|
102
|
+
});
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
## Event Types
|
|
106
|
+
|
|
107
|
+
Event types are imported from `@laplace.live/event-types` package. See the [documentation](https://chat.laplace.live/event-types/index.html) for available event types.
|
|
108
|
+
|
|
109
|
+
## License
|
|
110
|
+
|
|
111
|
+
MIT
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
class A{ws=null;eventHandlers=new Map;anyEventHandlers=[];reconnectTimer=null;reconnectAttempts=0;clientId=null;isConnected=!1;options={url:"ws://localhost:9696",token:"",reconnect:!0,reconnectInterval:3000,maxReconnectAttempts:10};constructor(k={}){this.options={...this.options,...k}}connect(){return new Promise((k,q)=>{try{if(this.ws)this.ws.close();let u=[];if(this.options.token)u.push("laplace-event-bridge-role-client",this.options.token);this.ws=new WebSocket(this.options.url,u),this.ws.onopen=()=>{this.isConnected=!0,this.reconnectAttempts=0,console.log("Connected to LAPLACE Event Bridge"),k()},this.ws.onmessage=(w)=>{try{let z=JSON.parse(w.data);if(z.type==="established"&&z.clientId)this.clientId=z.clientId,console.log(`Connection established with client ID: ${this.clientId}`);this.processEvent(z)}catch(z){console.error("Failed to parse event data:",z)}},this.ws.onerror=(w)=>{console.error("WebSocket error:",w),q(w)},this.ws.onclose=()=>{if(this.isConnected=!1,console.log("Disconnected from LAPLACE Event Bridge"),this.options.reconnect&&this.reconnectAttempts<this.options.maxReconnectAttempts)this.reconnectAttempts++,console.log(`Attempting to reconnect (${this.reconnectAttempts}/${this.options.maxReconnectAttempts})...`),this.reconnectTimer=setTimeout(()=>{this.connect().catch((w)=>{console.error("Reconnection failed:",w)})},this.options.reconnectInterval)}}catch(u){q(u)}})}disconnect(){if(this.reconnectTimer)clearTimeout(this.reconnectTimer),this.reconnectTimer=null;if(this.ws)this.ws.close(),this.ws=null;this.isConnected=!1,this.clientId=null}on(k,q){if(!this.eventHandlers.has(k))this.eventHandlers.set(k,[]);this.eventHandlers.get(k).push(q)}onAny(k){this.anyEventHandlers.push(k)}off(k,q){if(!this.eventHandlers.has(k))return;let u=this.eventHandlers.get(k),w=u.indexOf(q);if(w!==-1)u.splice(w,1);if(u.length===0)this.eventHandlers.delete(k)}offAny(k){let q=this.anyEventHandlers.indexOf(k);if(q!==-1)this.anyEventHandlers.splice(q,1)}isConnectedToBridge(){return this.isConnected}getClientId(){return this.clientId}send(k){if(!this.ws||this.ws.readyState!==WebSocket.OPEN)throw new Error("Not connected to LAPLACE Event Bridge");this.ws.send(JSON.stringify(k))}processEvent(k){if(this.eventHandlers.has(k.type))for(let q of this.eventHandlers.get(k.type))try{q(k)}catch(u){console.error(`Error in event handler for type ${k.type}:`,u)}for(let q of this.anyEventHandlers)try{q(k)}catch(u){console.error("Error in any event handler:",u)}}}export{A as LaplaceEventClient};
|
package/package.json
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@laplace.live/event-bridge-sdk",
|
|
3
|
+
"description": "LAPLACE Event Bridge SDK",
|
|
4
|
+
"version": "0.0.1",
|
|
5
|
+
"module": "index.ts",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "bun build index.ts --outdir dist --target browser --minify"
|
|
9
|
+
},
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"import": "./dist/index.js",
|
|
13
|
+
"types": "./index.ts"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist",
|
|
18
|
+
"src",
|
|
19
|
+
"README.md",
|
|
20
|
+
"LICENSE"
|
|
21
|
+
],
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"@laplace.live/event-types": "^2.0.4"
|
|
24
|
+
},
|
|
25
|
+
"peerDependencies": {
|
|
26
|
+
"@laplace.live/event-types": "^2.0.4"
|
|
27
|
+
},
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"bun-types": "latest"
|
|
30
|
+
}
|
|
31
|
+
}
|