mezon-light-sdk 1.0.3 → 1.0.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/README.md +258 -258
- package/package.json +43 -22
- package/dist/api.gen.d.ts +0 -150
- package/dist/api.gen.js +0 -149
- package/dist/client.d.ts +0 -137
- package/dist/client.js +0 -267
- package/dist/constants.d.ts +0 -16
- package/dist/constants.js +0 -19
- package/dist/google/protobuf/struct.d.ts +0 -201
- package/dist/google/protobuf/struct.js +0 -449
- package/dist/google/protobuf/wrappers.d.ts +0 -238
- package/dist/google/protobuf/wrappers.js +0 -506
- package/dist/index.d.ts +0 -5
- package/dist/index.js +0 -20
- package/dist/proto/api.d.ts +0 -24188
- package/dist/proto/api.js +0 -33287
- package/dist/proto/realtime.d.ts +0 -36125
- package/dist/proto/realtime.js +0 -13947
- package/dist/session.d.ts +0 -63
- package/dist/session.js +0 -92
- package/dist/socket.d.ts +0 -135
- package/dist/socket.gen.d.ts +0 -136
- package/dist/socket.gen.js +0 -327
- package/dist/socket.js +0 -249
- package/dist/types.d.ts +0 -44
- package/dist/types.js +0 -2
- package/dist/utils.d.ts +0 -3
- package/dist/utils.js +0 -110
- package/dist/web_socket_adapter_pb.d.ts +0 -69
- package/dist/web_socket_adapter_pb.js +0 -111
package/README.md
CHANGED
|
@@ -1,259 +1,259 @@
|
|
|
1
|
-
# Mezon Light SDK
|
|
2
|
-
|
|
3
|
-
A lightweight SDK for Mezon chat integration, providing simple APIs for authentication, real-time messaging, and direct message management.
|
|
4
|
-
|
|
5
|
-
## Installation
|
|
6
|
-
|
|
7
|
-
```bash
|
|
8
|
-
npm install mezon-light-sdk
|
|
9
|
-
```
|
|
10
|
-
|
|
11
|
-
## Features
|
|
12
|
-
|
|
13
|
-
- 🔐 Simple authentication with ID tokens
|
|
14
|
-
- 💬 Real-time messaging via WebSocket (protobuf-based)
|
|
15
|
-
- 📨 Direct message (DM) and group DM support
|
|
16
|
-
- 🔄 Automatic session management with token refresh
|
|
17
|
-
- 📎 Attachment support for messages
|
|
18
|
-
- 🎯 TypeScript-first with full type definitions
|
|
19
|
-
- ⚡ Exponential backoff for socket connection reliability
|
|
20
|
-
|
|
21
|
-
## Quick Start
|
|
22
|
-
|
|
23
|
-
### 1. Authenticate with ID Token
|
|
24
|
-
|
|
25
|
-
```typescript
|
|
26
|
-
import { LightClient, AuthenticationError } from 'mezon-light-sdk';
|
|
27
|
-
|
|
28
|
-
try {
|
|
29
|
-
const client = await LightClient.authenticate({
|
|
30
|
-
id_token: 'your-id-token',
|
|
31
|
-
user_id: 'user-123',
|
|
32
|
-
username: 'johndoe',
|
|
33
|
-
serverkey: 'your-server-key', // optional, uses DEFAULT_SERVER_KEY
|
|
34
|
-
gateway_url: 'https://gw.mezon.ai' // optional, uses MEZON_GW_URL
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
console.log('Authenticated as:', client.userId);
|
|
38
|
-
} catch (error) {
|
|
39
|
-
if (error instanceof AuthenticationError) {
|
|
40
|
-
console.error('Auth failed:', error.message, error.statusCode);
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
```
|
|
44
|
-
|
|
45
|
-
### 2. Restore Session from Storage
|
|
46
|
-
|
|
47
|
-
After login, persist the session data and restore it later:
|
|
48
|
-
|
|
49
|
-
```typescript
|
|
50
|
-
import { LightClient, SessionError } from 'mezon-light-sdk';
|
|
51
|
-
|
|
52
|
-
// Export session for storage
|
|
53
|
-
const sessionData = client.exportSession();
|
|
54
|
-
// Returns: { token, refresh_token, api_url, user_id }
|
|
55
|
-
localStorage.setItem('mezon_session', JSON.stringify(sessionData));
|
|
56
|
-
|
|
57
|
-
// Later: restore from storage
|
|
58
|
-
try {
|
|
59
|
-
const savedData = JSON.parse(localStorage.getItem('mezon_session')!);
|
|
60
|
-
const client = LightClient.initClient(savedData);
|
|
61
|
-
} catch (error) {
|
|
62
|
-
if (error instanceof SessionError) {
|
|
63
|
-
console.error('Session restore failed:', error.message);
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
```
|
|
67
|
-
|
|
68
|
-
### 3. Session Management
|
|
69
|
-
|
|
70
|
-
Check and refresh the session before connecting:
|
|
71
|
-
|
|
72
|
-
```typescript
|
|
73
|
-
// Check if session token is expired
|
|
74
|
-
if (client.isSessionExpired()) {
|
|
75
|
-
// Check if refresh token is still valid
|
|
76
|
-
if (!client.isRefreshSessionExpired()) {
|
|
77
|
-
await client.refreshSession();
|
|
78
|
-
// Update stored session data
|
|
79
|
-
localStorage.setItem('mezon_session', JSON.stringify(client.exportSession()));
|
|
80
|
-
} else {
|
|
81
|
-
// Both tokens expired - need to re-authenticate
|
|
82
|
-
console.log('Session fully expired, please login again');
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
// Access tokens and session directly if needed
|
|
87
|
-
const token = client.getToken();
|
|
88
|
-
const refreshToken = client.getRefreshToken();
|
|
89
|
-
const session = client.getSession();
|
|
90
|
-
```
|
|
91
|
-
|
|
92
|
-
### 4. Connect to Real-time Socket
|
|
93
|
-
|
|
94
|
-
```typescript
|
|
95
|
-
import { LightSocket, SocketError } from 'mezon-light-sdk';
|
|
96
|
-
|
|
97
|
-
const socket = new LightSocket(client, client.session);
|
|
98
|
-
|
|
99
|
-
await socket.connect({
|
|
100
|
-
onError: (error) => console.error('Socket error:', error),
|
|
101
|
-
onDisconnect: () => console.log('Socket disconnected'),
|
|
102
|
-
verbose: false // set to true for debug logging
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
// Check connection status
|
|
106
|
-
console.log('Connected:', socket.isConnected);
|
|
107
|
-
```
|
|
108
|
-
|
|
109
|
-
### 5. Listen for Messages
|
|
110
|
-
|
|
111
|
-
```typescript
|
|
112
|
-
// Register a message handler (returns unsubscribe function)
|
|
113
|
-
const unsubscribe = socket.onChannelMessage((message) => {
|
|
114
|
-
console.log(`Message from ${message.sender_id}: ${message.content}`);
|
|
115
|
-
console.log('Channel:', message.channel_id);
|
|
116
|
-
console.log('Timestamp:', message.create_time_seconds);
|
|
117
|
-
});
|
|
118
|
-
|
|
119
|
-
// Alternative: use setChannelMessageHandler (does not return unsubscribe)
|
|
120
|
-
socket.setChannelMessageHandler((message) => {
|
|
121
|
-
console.log('Received:', message.content);
|
|
122
|
-
});
|
|
123
|
-
|
|
124
|
-
// Multiple handlers can be registered
|
|
125
|
-
const unsubscribe2 = socket.onChannelMessage((message) => {
|
|
126
|
-
// Another handler for the same messages
|
|
127
|
-
});
|
|
128
|
-
|
|
129
|
-
// Unsubscribe when no longer needed
|
|
130
|
-
unsubscribe();
|
|
131
|
-
unsubscribe2();
|
|
132
|
-
```
|
|
133
|
-
|
|
134
|
-
### 6. Create and Join Channels
|
|
135
|
-
|
|
136
|
-
```typescript
|
|
137
|
-
// Create a DM with a single user
|
|
138
|
-
const dmChannel = await client.createDM('peer-user-id');
|
|
139
|
-
await socket.joinDMChannel(dmChannel.channel_id!);
|
|
140
|
-
|
|
141
|
-
// Create a group DM with multiple users
|
|
142
|
-
const groupDM = await client.createGroupDM(['user-1', 'user-2', 'user-3']);
|
|
143
|
-
await socket.joinGroupChannel(groupDM.channel_id!);
|
|
144
|
-
|
|
145
|
-
// Leave channels
|
|
146
|
-
await socket.leaveDMChannel(dmChannel.channel_id!);
|
|
147
|
-
await socket.leaveGroupChannel(groupDM.channel_id!);
|
|
148
|
-
```
|
|
149
|
-
|
|
150
|
-
### 7. Send Messages
|
|
151
|
-
|
|
152
|
-
```typescript
|
|
153
|
-
// Send a DM message
|
|
154
|
-
await socket.sendDM({
|
|
155
|
-
channelId: 'channel-123',
|
|
156
|
-
content: { t: 'Hello, world!' },
|
|
157
|
-
hideLink: false // optional, whether to hide link previews
|
|
158
|
-
});
|
|
159
|
-
|
|
160
|
-
// Send a group message
|
|
161
|
-
await socket.sendGroup({
|
|
162
|
-
channelId: 'group-channel-456',
|
|
163
|
-
content: { t: 'Hello everyone!' },
|
|
164
|
-
attachments: [
|
|
165
|
-
{
|
|
166
|
-
filename: 'image.png',
|
|
167
|
-
url: 'https://cdn.mezon.ai/path/to/image.png',
|
|
168
|
-
filetype: 'image/png',
|
|
169
|
-
size: 42439,
|
|
170
|
-
width: 716,
|
|
171
|
-
height: 522
|
|
172
|
-
}
|
|
173
|
-
],
|
|
174
|
-
hideLink: true
|
|
175
|
-
});
|
|
176
|
-
```
|
|
177
|
-
|
|
178
|
-
### 8. Disconnect
|
|
179
|
-
|
|
180
|
-
```typescript
|
|
181
|
-
// Disconnect when done
|
|
182
|
-
socket.disconnect();
|
|
183
|
-
```
|
|
184
|
-
|
|
185
|
-
## API Reference
|
|
186
|
-
|
|
187
|
-
### LightClient
|
|
188
|
-
|
|
189
|
-
| Property/Method | Description |
|
|
190
|
-
|-----------------|-------------|
|
|
191
|
-
| `LightClient.authenticate(config)` | Static: Authenticate with ID token |
|
|
192
|
-
| `LightClient.initClient(config)` | Static: Initialize from existing tokens |
|
|
193
|
-
| `client.userId` | Get current user ID |
|
|
194
|
-
| `client.session` | Get underlying Session object |
|
|
195
|
-
| `client.client` | Get underlying MezonApi client |
|
|
196
|
-
| `client.createDM(peerId)` | Create a DM channel with one user |
|
|
197
|
-
| `client.createGroupDM(userIds)` | Create a group DM with multiple users |
|
|
198
|
-
| `client.refreshSession()` | Refresh the session using refresh token |
|
|
199
|
-
| `client.isSessionExpired()` | Check if session token is expired |
|
|
200
|
-
| `client.isRefreshSessionExpired()` | Check if refresh token is expired |
|
|
201
|
-
| `client.getToken()` | Get the current auth token |
|
|
202
|
-
| `client.getRefreshToken()` | Get the refresh token |
|
|
203
|
-
| `client.getSession()` | Get the current session object |
|
|
204
|
-
| `client.exportSession()` | Export session data for persistence |
|
|
205
|
-
| `client.createSocket(verbose?, adapter?, timeout?)` | Create a raw socket instance |
|
|
206
|
-
|
|
207
|
-
### LightSocket
|
|
208
|
-
|
|
209
|
-
| Property/Method | Description |
|
|
210
|
-
|-----------------|-------------|
|
|
211
|
-
| `new LightSocket(client, session)` | Create a new socket instance |
|
|
212
|
-
| `socket.connect(options?)` | Connect to real-time server |
|
|
213
|
-
| `socket.disconnect()` | Disconnect from server |
|
|
214
|
-
| `socket.isConnected` | Check if socket is connected |
|
|
215
|
-
| `socket.socket` | Get underlying Socket (throws if not connected) |
|
|
216
|
-
| `socket.onChannelMessage(handler)` | Register message handler, returns unsubscribe fn |
|
|
217
|
-
| `socket.setChannelMessageHandler(handler)` | Register message handler (alias) |
|
|
218
|
-
| `socket.joinDMChannel(channelId)` | Join a DM channel |
|
|
219
|
-
| `socket.joinGroupChannel(channelId)` | Join a group channel |
|
|
220
|
-
| `socket.leaveDMChannel(channelId)` | Leave a DM channel |
|
|
221
|
-
| `socket.leaveGroupChannel(channelId)` | Leave a group channel |
|
|
222
|
-
| `socket.sendDM(payload)` | Send a DM message |
|
|
223
|
-
| `socket.sendGroup(payload)` | Send a group message |
|
|
224
|
-
| `socket.setErrorHandler(handler)` | Set custom error handler |
|
|
225
|
-
|
|
226
|
-
### Types
|
|
227
|
-
|
|
228
|
-
```typescript
|
|
229
|
-
interface ClientInitConfig {
|
|
230
|
-
token: string; // Auth token
|
|
231
|
-
refresh_token: string; // Refresh token
|
|
232
|
-
api_url: string; // API URL
|
|
233
|
-
user_id: string; // User ID
|
|
234
|
-
serverkey?: string; // Optional server key
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
interface AuthenticateConfig {
|
|
238
|
-
id_token: string; // ID token from provider
|
|
239
|
-
user_id: string; // User ID
|
|
240
|
-
username: string; // Username
|
|
241
|
-
serverkey?: string; // Optional server key
|
|
242
|
-
gateway_url?: string; // Optional gateway URL
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
interface SendMessagePayload {
|
|
246
|
-
channelId: string; // Target channel
|
|
247
|
-
content: unknown; // Message content
|
|
248
|
-
attachments?: ApiMessageAttachment[]; // Optional attachments
|
|
249
|
-
hideLink?: boolean; // Hide link previews
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
interface SocketConnectOptions {
|
|
253
|
-
onError?: (error: unknown) => void; // Error callback
|
|
254
|
-
onDisconnect?: () => void; // Disconnect callback
|
|
255
|
-
verbose?: boolean; // Enable debug logging
|
|
256
|
-
}
|
|
257
|
-
```
|
|
258
|
-
|
|
1
|
+
# Mezon Light SDK
|
|
2
|
+
|
|
3
|
+
A lightweight SDK for Mezon chat integration, providing simple APIs for authentication, real-time messaging, and direct message management.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install mezon-light-sdk
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Features
|
|
12
|
+
|
|
13
|
+
- 🔐 Simple authentication with ID tokens
|
|
14
|
+
- 💬 Real-time messaging via WebSocket (protobuf-based)
|
|
15
|
+
- 📨 Direct message (DM) and group DM support
|
|
16
|
+
- 🔄 Automatic session management with token refresh
|
|
17
|
+
- 📎 Attachment support for messages
|
|
18
|
+
- 🎯 TypeScript-first with full type definitions
|
|
19
|
+
- ⚡ Exponential backoff for socket connection reliability
|
|
20
|
+
|
|
21
|
+
## Quick Start
|
|
22
|
+
|
|
23
|
+
### 1. Authenticate with ID Token
|
|
24
|
+
|
|
25
|
+
```typescript
|
|
26
|
+
import { LightClient, AuthenticationError } from 'mezon-light-sdk';
|
|
27
|
+
|
|
28
|
+
try {
|
|
29
|
+
const client = await LightClient.authenticate({
|
|
30
|
+
id_token: 'your-id-token',
|
|
31
|
+
user_id: 'user-123',
|
|
32
|
+
username: 'johndoe',
|
|
33
|
+
serverkey: 'your-server-key', // optional, uses DEFAULT_SERVER_KEY
|
|
34
|
+
gateway_url: 'https://gw.mezon.ai' // optional, uses MEZON_GW_URL
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
console.log('Authenticated as:', client.userId);
|
|
38
|
+
} catch (error) {
|
|
39
|
+
if (error instanceof AuthenticationError) {
|
|
40
|
+
console.error('Auth failed:', error.message, error.statusCode);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
### 2. Restore Session from Storage
|
|
46
|
+
|
|
47
|
+
After login, persist the session data and restore it later:
|
|
48
|
+
|
|
49
|
+
```typescript
|
|
50
|
+
import { LightClient, SessionError } from 'mezon-light-sdk';
|
|
51
|
+
|
|
52
|
+
// Export session for storage
|
|
53
|
+
const sessionData = client.exportSession();
|
|
54
|
+
// Returns: { token, refresh_token, api_url, user_id }
|
|
55
|
+
localStorage.setItem('mezon_session', JSON.stringify(sessionData));
|
|
56
|
+
|
|
57
|
+
// Later: restore from storage
|
|
58
|
+
try {
|
|
59
|
+
const savedData = JSON.parse(localStorage.getItem('mezon_session')!);
|
|
60
|
+
const client = LightClient.initClient(savedData);
|
|
61
|
+
} catch (error) {
|
|
62
|
+
if (error instanceof SessionError) {
|
|
63
|
+
console.error('Session restore failed:', error.message);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
### 3. Session Management
|
|
69
|
+
|
|
70
|
+
Check and refresh the session before connecting:
|
|
71
|
+
|
|
72
|
+
```typescript
|
|
73
|
+
// Check if session token is expired
|
|
74
|
+
if (client.isSessionExpired()) {
|
|
75
|
+
// Check if refresh token is still valid
|
|
76
|
+
if (!client.isRefreshSessionExpired()) {
|
|
77
|
+
await client.refreshSession();
|
|
78
|
+
// Update stored session data
|
|
79
|
+
localStorage.setItem('mezon_session', JSON.stringify(client.exportSession()));
|
|
80
|
+
} else {
|
|
81
|
+
// Both tokens expired - need to re-authenticate
|
|
82
|
+
console.log('Session fully expired, please login again');
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// Access tokens and session directly if needed
|
|
87
|
+
const token = client.getToken();
|
|
88
|
+
const refreshToken = client.getRefreshToken();
|
|
89
|
+
const session = client.getSession();
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### 4. Connect to Real-time Socket
|
|
93
|
+
|
|
94
|
+
```typescript
|
|
95
|
+
import { LightSocket, SocketError } from 'mezon-light-sdk';
|
|
96
|
+
|
|
97
|
+
const socket = new LightSocket(client, client.session);
|
|
98
|
+
|
|
99
|
+
await socket.connect({
|
|
100
|
+
onError: (error) => console.error('Socket error:', error),
|
|
101
|
+
onDisconnect: () => console.log('Socket disconnected'),
|
|
102
|
+
verbose: false // set to true for debug logging
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
// Check connection status
|
|
106
|
+
console.log('Connected:', socket.isConnected);
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
### 5. Listen for Messages
|
|
110
|
+
|
|
111
|
+
```typescript
|
|
112
|
+
// Register a message handler (returns unsubscribe function)
|
|
113
|
+
const unsubscribe = socket.onChannelMessage((message) => {
|
|
114
|
+
console.log(`Message from ${message.sender_id}: ${message.content}`);
|
|
115
|
+
console.log('Channel:', message.channel_id);
|
|
116
|
+
console.log('Timestamp:', message.create_time_seconds);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
// Alternative: use setChannelMessageHandler (does not return unsubscribe)
|
|
120
|
+
socket.setChannelMessageHandler((message) => {
|
|
121
|
+
console.log('Received:', message.content);
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
// Multiple handlers can be registered
|
|
125
|
+
const unsubscribe2 = socket.onChannelMessage((message) => {
|
|
126
|
+
// Another handler for the same messages
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
// Unsubscribe when no longer needed
|
|
130
|
+
unsubscribe();
|
|
131
|
+
unsubscribe2();
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
### 6. Create and Join Channels
|
|
135
|
+
|
|
136
|
+
```typescript
|
|
137
|
+
// Create a DM with a single user
|
|
138
|
+
const dmChannel = await client.createDM('peer-user-id');
|
|
139
|
+
await socket.joinDMChannel(dmChannel.channel_id!);
|
|
140
|
+
|
|
141
|
+
// Create a group DM with multiple users
|
|
142
|
+
const groupDM = await client.createGroupDM(['user-1', 'user-2', 'user-3']);
|
|
143
|
+
await socket.joinGroupChannel(groupDM.channel_id!);
|
|
144
|
+
|
|
145
|
+
// Leave channels
|
|
146
|
+
await socket.leaveDMChannel(dmChannel.channel_id!);
|
|
147
|
+
await socket.leaveGroupChannel(groupDM.channel_id!);
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
### 7. Send Messages
|
|
151
|
+
|
|
152
|
+
```typescript
|
|
153
|
+
// Send a DM message
|
|
154
|
+
await socket.sendDM({
|
|
155
|
+
channelId: 'channel-123',
|
|
156
|
+
content: { t: 'Hello, world!' },
|
|
157
|
+
hideLink: false // optional, whether to hide link previews
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
// Send a group message
|
|
161
|
+
await socket.sendGroup({
|
|
162
|
+
channelId: 'group-channel-456',
|
|
163
|
+
content: { t: 'Hello everyone!' },
|
|
164
|
+
attachments: [
|
|
165
|
+
{
|
|
166
|
+
filename: 'image.png',
|
|
167
|
+
url: 'https://cdn.mezon.ai/path/to/image.png',
|
|
168
|
+
filetype: 'image/png',
|
|
169
|
+
size: 42439,
|
|
170
|
+
width: 716,
|
|
171
|
+
height: 522
|
|
172
|
+
}
|
|
173
|
+
],
|
|
174
|
+
hideLink: true
|
|
175
|
+
});
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
### 8. Disconnect
|
|
179
|
+
|
|
180
|
+
```typescript
|
|
181
|
+
// Disconnect when done
|
|
182
|
+
socket.disconnect();
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
## API Reference
|
|
186
|
+
|
|
187
|
+
### LightClient
|
|
188
|
+
|
|
189
|
+
| Property/Method | Description |
|
|
190
|
+
|-----------------|-------------|
|
|
191
|
+
| `LightClient.authenticate(config)` | Static: Authenticate with ID token |
|
|
192
|
+
| `LightClient.initClient(config)` | Static: Initialize from existing tokens |
|
|
193
|
+
| `client.userId` | Get current user ID |
|
|
194
|
+
| `client.session` | Get underlying Session object |
|
|
195
|
+
| `client.client` | Get underlying MezonApi client |
|
|
196
|
+
| `client.createDM(peerId)` | Create a DM channel with one user |
|
|
197
|
+
| `client.createGroupDM(userIds)` | Create a group DM with multiple users |
|
|
198
|
+
| `client.refreshSession()` | Refresh the session using refresh token |
|
|
199
|
+
| `client.isSessionExpired()` | Check if session token is expired |
|
|
200
|
+
| `client.isRefreshSessionExpired()` | Check if refresh token is expired |
|
|
201
|
+
| `client.getToken()` | Get the current auth token |
|
|
202
|
+
| `client.getRefreshToken()` | Get the refresh token |
|
|
203
|
+
| `client.getSession()` | Get the current session object |
|
|
204
|
+
| `client.exportSession()` | Export session data for persistence |
|
|
205
|
+
| `client.createSocket(verbose?, adapter?, timeout?)` | Create a raw socket instance |
|
|
206
|
+
|
|
207
|
+
### LightSocket
|
|
208
|
+
|
|
209
|
+
| Property/Method | Description |
|
|
210
|
+
|-----------------|-------------|
|
|
211
|
+
| `new LightSocket(client, session)` | Create a new socket instance |
|
|
212
|
+
| `socket.connect(options?)` | Connect to real-time server |
|
|
213
|
+
| `socket.disconnect()` | Disconnect from server |
|
|
214
|
+
| `socket.isConnected` | Check if socket is connected |
|
|
215
|
+
| `socket.socket` | Get underlying Socket (throws if not connected) |
|
|
216
|
+
| `socket.onChannelMessage(handler)` | Register message handler, returns unsubscribe fn |
|
|
217
|
+
| `socket.setChannelMessageHandler(handler)` | Register message handler (alias) |
|
|
218
|
+
| `socket.joinDMChannel(channelId)` | Join a DM channel |
|
|
219
|
+
| `socket.joinGroupChannel(channelId)` | Join a group channel |
|
|
220
|
+
| `socket.leaveDMChannel(channelId)` | Leave a DM channel |
|
|
221
|
+
| `socket.leaveGroupChannel(channelId)` | Leave a group channel |
|
|
222
|
+
| `socket.sendDM(payload)` | Send a DM message |
|
|
223
|
+
| `socket.sendGroup(payload)` | Send a group message |
|
|
224
|
+
| `socket.setErrorHandler(handler)` | Set custom error handler |
|
|
225
|
+
|
|
226
|
+
### Types
|
|
227
|
+
|
|
228
|
+
```typescript
|
|
229
|
+
interface ClientInitConfig {
|
|
230
|
+
token: string; // Auth token
|
|
231
|
+
refresh_token: string; // Refresh token
|
|
232
|
+
api_url: string; // API URL
|
|
233
|
+
user_id: string; // User ID
|
|
234
|
+
serverkey?: string; // Optional server key
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
interface AuthenticateConfig {
|
|
238
|
+
id_token: string; // ID token from provider
|
|
239
|
+
user_id: string; // User ID
|
|
240
|
+
username: string; // Username
|
|
241
|
+
serverkey?: string; // Optional server key
|
|
242
|
+
gateway_url?: string; // Optional gateway URL
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
interface SendMessagePayload {
|
|
246
|
+
channelId: string; // Target channel
|
|
247
|
+
content: unknown; // Message content
|
|
248
|
+
attachments?: ApiMessageAttachment[]; // Optional attachments
|
|
249
|
+
hideLink?: boolean; // Hide link previews
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
interface SocketConnectOptions {
|
|
253
|
+
onError?: (error: unknown) => void; // Error callback
|
|
254
|
+
onDisconnect?: () => void; // Disconnect callback
|
|
255
|
+
verbose?: boolean; // Enable debug logging
|
|
256
|
+
}
|
|
257
|
+
```
|
|
258
|
+
|
|
259
259
|
---
|
package/package.json
CHANGED
|
@@ -1,22 +1,43 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "mezon-light-sdk",
|
|
3
|
-
"version": "1.0.
|
|
4
|
-
"description": "lightweight SDK for mezon chat.",
|
|
5
|
-
"main": "dist/index.js",
|
|
6
|
-
"
|
|
7
|
-
"
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
"
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "mezon-light-sdk",
|
|
3
|
+
"version": "1.0.4",
|
|
4
|
+
"description": "lightweight SDK for mezon chat.",
|
|
5
|
+
"main": "dist/index.cjs.js",
|
|
6
|
+
"module": "dist/index.esm.mjs",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
"./package.json": "./package.json",
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.esm.mjs",
|
|
13
|
+
"require": "./dist/index.cjs.js"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist"
|
|
18
|
+
],
|
|
19
|
+
"sideEffects": false,
|
|
20
|
+
"scripts": {
|
|
21
|
+
"build": "node build.mjs && tsc --emitDeclarationOnly",
|
|
22
|
+
"build:types": "tsc --emitDeclarationOnly",
|
|
23
|
+
"build:bundle": "node build.mjs"
|
|
24
|
+
},
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"js-base64": "^3.7.8",
|
|
27
|
+
"protobufjs": "^8.0.0"
|
|
28
|
+
},
|
|
29
|
+
"peerDependencies": {
|
|
30
|
+
"long": "^5.0.0"
|
|
31
|
+
},
|
|
32
|
+
"peerDependenciesMeta": {
|
|
33
|
+
"long": {
|
|
34
|
+
"optional": true
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
"author": "mezonai",
|
|
38
|
+
"license": "MIT",
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"esbuild": "^0.24.0",
|
|
41
|
+
"typescript": "^5.4.3"
|
|
42
|
+
}
|
|
43
|
+
}
|