@baasix/sdk 0.1.9 → 0.1.11
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 +23 -3
- package/dist/index.cjs +61 -15
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +70 -12
- package/dist/index.d.ts +70 -12
- package/dist/index.js +61 -15
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.cts
CHANGED
|
@@ -1096,6 +1096,28 @@ interface RoomMember {
|
|
|
1096
1096
|
isCreator: boolean;
|
|
1097
1097
|
metadata: Record<string, any>;
|
|
1098
1098
|
}
|
|
1099
|
+
/**
|
|
1100
|
+
* A single message from the room's history buffer, replayed when you join.
|
|
1101
|
+
* Has the same shape as a live {@link RoomMessage} (minus the `room` field).
|
|
1102
|
+
*/
|
|
1103
|
+
interface RoomHistoryMessage {
|
|
1104
|
+
event: string;
|
|
1105
|
+
payload: any;
|
|
1106
|
+
sender: {
|
|
1107
|
+
userId: string | number;
|
|
1108
|
+
socketId: string;
|
|
1109
|
+
};
|
|
1110
|
+
timestamp: string;
|
|
1111
|
+
}
|
|
1112
|
+
/**
|
|
1113
|
+
* Summary of a single custom room returned by {@link RealtimeModule.listRooms}.
|
|
1114
|
+
*/
|
|
1115
|
+
interface RoomInfo {
|
|
1116
|
+
/** Room name (e.g. `"game:lobby"`) */
|
|
1117
|
+
name: string;
|
|
1118
|
+
/** Number of connected members */
|
|
1119
|
+
memberCount: number;
|
|
1120
|
+
}
|
|
1099
1121
|
interface SubscriptionCallback<T = any> {
|
|
1100
1122
|
(payload: SubscriptionPayload<T>): void;
|
|
1101
1123
|
}
|
|
@@ -1275,25 +1297,32 @@ declare class RealtimeModule {
|
|
|
1275
1297
|
* will be visible to all other members via {@link getRoomMembers} and in
|
|
1276
1298
|
* `room:user:joined` events.
|
|
1277
1299
|
*
|
|
1300
|
+
* Returns the room's message history (up to 200 messages) so late joiners
|
|
1301
|
+
* can replay past messages immediately.
|
|
1302
|
+
*
|
|
1278
1303
|
* @param roomName - The room to join.
|
|
1279
1304
|
* @param metadata - Optional key/value pairs stored alongside this member.
|
|
1305
|
+
* @returns An object containing the buffered `history` for the room.
|
|
1280
1306
|
*
|
|
1281
1307
|
* @example
|
|
1282
1308
|
* ```typescript
|
|
1283
|
-
*
|
|
1284
|
-
* await baasix.realtime.joinRoom('game:lobby', {
|
|
1309
|
+
* const { history } = await baasix.realtime.joinRoom('game:lobby', {
|
|
1285
1310
|
* username: 'Alice',
|
|
1286
1311
|
* avatar: 'https://example.com/alice.png',
|
|
1287
|
-
* team: 'blue',
|
|
1288
1312
|
* });
|
|
1289
1313
|
*
|
|
1290
|
-
* //
|
|
1291
|
-
*
|
|
1292
|
-
*
|
|
1314
|
+
* // Render past messages first
|
|
1315
|
+
* history.forEach((msg) => renderMessage(msg));
|
|
1316
|
+
*
|
|
1317
|
+
* // Then listen for new ones
|
|
1318
|
+
* baasix.realtime.onRoomMessage('game:lobby', 'chat', (data) => {
|
|
1319
|
+
* renderMessage(data);
|
|
1293
1320
|
* });
|
|
1294
1321
|
* ```
|
|
1295
1322
|
*/
|
|
1296
|
-
joinRoom(roomName: string, metadata?: Record<string, any>): Promise<
|
|
1323
|
+
joinRoom(roomName: string, metadata?: Record<string, any>): Promise<{
|
|
1324
|
+
history: RoomHistoryMessage[];
|
|
1325
|
+
}>;
|
|
1297
1326
|
/**
|
|
1298
1327
|
* Leave a custom room
|
|
1299
1328
|
*
|
|
@@ -1319,6 +1348,26 @@ declare class RealtimeModule {
|
|
|
1319
1348
|
* ```
|
|
1320
1349
|
*/
|
|
1321
1350
|
getRoomMembers(roomName: string): Promise<RoomMember[]>;
|
|
1351
|
+
/**
|
|
1352
|
+
* List all active custom rooms, optionally filtered by a name prefix.
|
|
1353
|
+
*
|
|
1354
|
+
* Returns every room that currently has at least one member.
|
|
1355
|
+
* You do **not** need to be a member of a room to list it.
|
|
1356
|
+
*
|
|
1357
|
+
* @param prefix - Optional prefix to filter rooms. E.g. `"game:"` returns only
|
|
1358
|
+
* rooms whose name starts with `"game:"`.
|
|
1359
|
+
*
|
|
1360
|
+
* @example
|
|
1361
|
+
* ```typescript
|
|
1362
|
+
* // All active rooms
|
|
1363
|
+
* const rooms = await baasix.realtime.listRooms();
|
|
1364
|
+
* // [{ name: 'game:lobby', memberCount: 4 }, { name: 'chat:general', memberCount: 12 }]
|
|
1365
|
+
*
|
|
1366
|
+
* // Only rooms whose name starts with 'game:'
|
|
1367
|
+
* const gameRooms = await baasix.realtime.listRooms('game:');
|
|
1368
|
+
* ```
|
|
1369
|
+
*/
|
|
1370
|
+
listRooms(prefix?: string): Promise<RoomInfo[]>;
|
|
1322
1371
|
/**
|
|
1323
1372
|
* Kick a user from a custom room. Only the room creator may call this.
|
|
1324
1373
|
*
|
|
@@ -1353,18 +1402,27 @@ declare class RealtimeModule {
|
|
|
1353
1402
|
*/
|
|
1354
1403
|
onRoomCreatorChanged(roomName: string, callback: (data: RoomCreatorChangedEvent) => void): () => void;
|
|
1355
1404
|
/**
|
|
1356
|
-
* Send a message to a room
|
|
1405
|
+
* Send a message to a room.
|
|
1406
|
+
*
|
|
1407
|
+
* By default the message is stored in the room's history buffer so late
|
|
1408
|
+
* joiners can replay it. Pass `{ history: false }` to broadcast without
|
|
1409
|
+
* persisting (e.g. ephemeral cursor positions, typing indicators).
|
|
1357
1410
|
*
|
|
1358
1411
|
* @example
|
|
1359
1412
|
* ```typescript
|
|
1360
|
-
* //
|
|
1413
|
+
* // Persisted — replayed to future joiners
|
|
1361
1414
|
* await baasix.realtime.sendToRoom('game:lobby', 'chat', { text: 'Hello!' });
|
|
1362
1415
|
*
|
|
1363
|
-
* //
|
|
1364
|
-
* await baasix.realtime.sendToRoom('game:
|
|
1416
|
+
* // Ephemeral — broadcast only, never stored in history
|
|
1417
|
+
* await baasix.realtime.sendToRoom('game:lobby', 'typing', { userId }, { history: false });
|
|
1418
|
+
*
|
|
1419
|
+
* // Game move — skips history
|
|
1420
|
+
* await baasix.realtime.sendToRoom('game:123', 'move', { x: 10, y: 20 }, { history: false });
|
|
1365
1421
|
* ```
|
|
1366
1422
|
*/
|
|
1367
|
-
sendToRoom(roomName: string, event: string, payload: any
|
|
1423
|
+
sendToRoom(roomName: string, event: string, payload: any, options?: {
|
|
1424
|
+
history?: boolean;
|
|
1425
|
+
}): Promise<void>;
|
|
1368
1426
|
/**
|
|
1369
1427
|
* Listen for messages in a room with a specific event type
|
|
1370
1428
|
*
|
package/dist/index.d.ts
CHANGED
|
@@ -1096,6 +1096,28 @@ interface RoomMember {
|
|
|
1096
1096
|
isCreator: boolean;
|
|
1097
1097
|
metadata: Record<string, any>;
|
|
1098
1098
|
}
|
|
1099
|
+
/**
|
|
1100
|
+
* A single message from the room's history buffer, replayed when you join.
|
|
1101
|
+
* Has the same shape as a live {@link RoomMessage} (minus the `room` field).
|
|
1102
|
+
*/
|
|
1103
|
+
interface RoomHistoryMessage {
|
|
1104
|
+
event: string;
|
|
1105
|
+
payload: any;
|
|
1106
|
+
sender: {
|
|
1107
|
+
userId: string | number;
|
|
1108
|
+
socketId: string;
|
|
1109
|
+
};
|
|
1110
|
+
timestamp: string;
|
|
1111
|
+
}
|
|
1112
|
+
/**
|
|
1113
|
+
* Summary of a single custom room returned by {@link RealtimeModule.listRooms}.
|
|
1114
|
+
*/
|
|
1115
|
+
interface RoomInfo {
|
|
1116
|
+
/** Room name (e.g. `"game:lobby"`) */
|
|
1117
|
+
name: string;
|
|
1118
|
+
/** Number of connected members */
|
|
1119
|
+
memberCount: number;
|
|
1120
|
+
}
|
|
1099
1121
|
interface SubscriptionCallback<T = any> {
|
|
1100
1122
|
(payload: SubscriptionPayload<T>): void;
|
|
1101
1123
|
}
|
|
@@ -1275,25 +1297,32 @@ declare class RealtimeModule {
|
|
|
1275
1297
|
* will be visible to all other members via {@link getRoomMembers} and in
|
|
1276
1298
|
* `room:user:joined` events.
|
|
1277
1299
|
*
|
|
1300
|
+
* Returns the room's message history (up to 200 messages) so late joiners
|
|
1301
|
+
* can replay past messages immediately.
|
|
1302
|
+
*
|
|
1278
1303
|
* @param roomName - The room to join.
|
|
1279
1304
|
* @param metadata - Optional key/value pairs stored alongside this member.
|
|
1305
|
+
* @returns An object containing the buffered `history` for the room.
|
|
1280
1306
|
*
|
|
1281
1307
|
* @example
|
|
1282
1308
|
* ```typescript
|
|
1283
|
-
*
|
|
1284
|
-
* await baasix.realtime.joinRoom('game:lobby', {
|
|
1309
|
+
* const { history } = await baasix.realtime.joinRoom('game:lobby', {
|
|
1285
1310
|
* username: 'Alice',
|
|
1286
1311
|
* avatar: 'https://example.com/alice.png',
|
|
1287
|
-
* team: 'blue',
|
|
1288
1312
|
* });
|
|
1289
1313
|
*
|
|
1290
|
-
* //
|
|
1291
|
-
*
|
|
1292
|
-
*
|
|
1314
|
+
* // Render past messages first
|
|
1315
|
+
* history.forEach((msg) => renderMessage(msg));
|
|
1316
|
+
*
|
|
1317
|
+
* // Then listen for new ones
|
|
1318
|
+
* baasix.realtime.onRoomMessage('game:lobby', 'chat', (data) => {
|
|
1319
|
+
* renderMessage(data);
|
|
1293
1320
|
* });
|
|
1294
1321
|
* ```
|
|
1295
1322
|
*/
|
|
1296
|
-
joinRoom(roomName: string, metadata?: Record<string, any>): Promise<
|
|
1323
|
+
joinRoom(roomName: string, metadata?: Record<string, any>): Promise<{
|
|
1324
|
+
history: RoomHistoryMessage[];
|
|
1325
|
+
}>;
|
|
1297
1326
|
/**
|
|
1298
1327
|
* Leave a custom room
|
|
1299
1328
|
*
|
|
@@ -1319,6 +1348,26 @@ declare class RealtimeModule {
|
|
|
1319
1348
|
* ```
|
|
1320
1349
|
*/
|
|
1321
1350
|
getRoomMembers(roomName: string): Promise<RoomMember[]>;
|
|
1351
|
+
/**
|
|
1352
|
+
* List all active custom rooms, optionally filtered by a name prefix.
|
|
1353
|
+
*
|
|
1354
|
+
* Returns every room that currently has at least one member.
|
|
1355
|
+
* You do **not** need to be a member of a room to list it.
|
|
1356
|
+
*
|
|
1357
|
+
* @param prefix - Optional prefix to filter rooms. E.g. `"game:"` returns only
|
|
1358
|
+
* rooms whose name starts with `"game:"`.
|
|
1359
|
+
*
|
|
1360
|
+
* @example
|
|
1361
|
+
* ```typescript
|
|
1362
|
+
* // All active rooms
|
|
1363
|
+
* const rooms = await baasix.realtime.listRooms();
|
|
1364
|
+
* // [{ name: 'game:lobby', memberCount: 4 }, { name: 'chat:general', memberCount: 12 }]
|
|
1365
|
+
*
|
|
1366
|
+
* // Only rooms whose name starts with 'game:'
|
|
1367
|
+
* const gameRooms = await baasix.realtime.listRooms('game:');
|
|
1368
|
+
* ```
|
|
1369
|
+
*/
|
|
1370
|
+
listRooms(prefix?: string): Promise<RoomInfo[]>;
|
|
1322
1371
|
/**
|
|
1323
1372
|
* Kick a user from a custom room. Only the room creator may call this.
|
|
1324
1373
|
*
|
|
@@ -1353,18 +1402,27 @@ declare class RealtimeModule {
|
|
|
1353
1402
|
*/
|
|
1354
1403
|
onRoomCreatorChanged(roomName: string, callback: (data: RoomCreatorChangedEvent) => void): () => void;
|
|
1355
1404
|
/**
|
|
1356
|
-
* Send a message to a room
|
|
1405
|
+
* Send a message to a room.
|
|
1406
|
+
*
|
|
1407
|
+
* By default the message is stored in the room's history buffer so late
|
|
1408
|
+
* joiners can replay it. Pass `{ history: false }` to broadcast without
|
|
1409
|
+
* persisting (e.g. ephemeral cursor positions, typing indicators).
|
|
1357
1410
|
*
|
|
1358
1411
|
* @example
|
|
1359
1412
|
* ```typescript
|
|
1360
|
-
* //
|
|
1413
|
+
* // Persisted — replayed to future joiners
|
|
1361
1414
|
* await baasix.realtime.sendToRoom('game:lobby', 'chat', { text: 'Hello!' });
|
|
1362
1415
|
*
|
|
1363
|
-
* //
|
|
1364
|
-
* await baasix.realtime.sendToRoom('game:
|
|
1416
|
+
* // Ephemeral — broadcast only, never stored in history
|
|
1417
|
+
* await baasix.realtime.sendToRoom('game:lobby', 'typing', { userId }, { history: false });
|
|
1418
|
+
*
|
|
1419
|
+
* // Game move — skips history
|
|
1420
|
+
* await baasix.realtime.sendToRoom('game:123', 'move', { x: 10, y: 20 }, { history: false });
|
|
1365
1421
|
* ```
|
|
1366
1422
|
*/
|
|
1367
|
-
sendToRoom(roomName: string, event: string, payload: any
|
|
1423
|
+
sendToRoom(roomName: string, event: string, payload: any, options?: {
|
|
1424
|
+
history?: boolean;
|
|
1425
|
+
}): Promise<void>;
|
|
1368
1426
|
/**
|
|
1369
1427
|
* Listen for messages in a room with a specific event type
|
|
1370
1428
|
*
|
package/dist/index.js
CHANGED
|
@@ -3699,21 +3699,26 @@ var RealtimeModule = class {
|
|
|
3699
3699
|
* will be visible to all other members via {@link getRoomMembers} and in
|
|
3700
3700
|
* `room:user:joined` events.
|
|
3701
3701
|
*
|
|
3702
|
+
* Returns the room's message history (up to 200 messages) so late joiners
|
|
3703
|
+
* can replay past messages immediately.
|
|
3704
|
+
*
|
|
3702
3705
|
* @param roomName - The room to join.
|
|
3703
3706
|
* @param metadata - Optional key/value pairs stored alongside this member.
|
|
3707
|
+
* @returns An object containing the buffered `history` for the room.
|
|
3704
3708
|
*
|
|
3705
3709
|
* @example
|
|
3706
3710
|
* ```typescript
|
|
3707
|
-
*
|
|
3708
|
-
* await baasix.realtime.joinRoom('game:lobby', {
|
|
3711
|
+
* const { history } = await baasix.realtime.joinRoom('game:lobby', {
|
|
3709
3712
|
* username: 'Alice',
|
|
3710
3713
|
* avatar: 'https://example.com/alice.png',
|
|
3711
|
-
* team: 'blue',
|
|
3712
3714
|
* });
|
|
3713
3715
|
*
|
|
3714
|
-
* //
|
|
3715
|
-
*
|
|
3716
|
-
*
|
|
3716
|
+
* // Render past messages first
|
|
3717
|
+
* history.forEach((msg) => renderMessage(msg));
|
|
3718
|
+
*
|
|
3719
|
+
* // Then listen for new ones
|
|
3720
|
+
* baasix.realtime.onRoomMessage('game:lobby', 'chat', (data) => {
|
|
3721
|
+
* renderMessage(data);
|
|
3717
3722
|
* });
|
|
3718
3723
|
* ```
|
|
3719
3724
|
*/
|
|
@@ -3725,7 +3730,7 @@ var RealtimeModule = class {
|
|
|
3725
3730
|
this.socket.emit("room:join", { room: roomName, metadata }, (response) => {
|
|
3726
3731
|
if (response.status === "success") {
|
|
3727
3732
|
this.setupRoomListeners(roomName);
|
|
3728
|
-
resolve();
|
|
3733
|
+
resolve({ history: response.history ?? [] });
|
|
3729
3734
|
} else {
|
|
3730
3735
|
reject(new Error(response.message || "Failed to join room"));
|
|
3731
3736
|
}
|
|
@@ -3784,6 +3789,39 @@ var RealtimeModule = class {
|
|
|
3784
3789
|
});
|
|
3785
3790
|
});
|
|
3786
3791
|
}
|
|
3792
|
+
/**
|
|
3793
|
+
* List all active custom rooms, optionally filtered by a name prefix.
|
|
3794
|
+
*
|
|
3795
|
+
* Returns every room that currently has at least one member.
|
|
3796
|
+
* You do **not** need to be a member of a room to list it.
|
|
3797
|
+
*
|
|
3798
|
+
* @param prefix - Optional prefix to filter rooms. E.g. `"game:"` returns only
|
|
3799
|
+
* rooms whose name starts with `"game:"`.
|
|
3800
|
+
*
|
|
3801
|
+
* @example
|
|
3802
|
+
* ```typescript
|
|
3803
|
+
* // All active rooms
|
|
3804
|
+
* const rooms = await baasix.realtime.listRooms();
|
|
3805
|
+
* // [{ name: 'game:lobby', memberCount: 4 }, { name: 'chat:general', memberCount: 12 }]
|
|
3806
|
+
*
|
|
3807
|
+
* // Only rooms whose name starts with 'game:'
|
|
3808
|
+
* const gameRooms = await baasix.realtime.listRooms('game:');
|
|
3809
|
+
* ```
|
|
3810
|
+
*/
|
|
3811
|
+
async listRooms(prefix) {
|
|
3812
|
+
if (!this.socket?.connected) {
|
|
3813
|
+
throw new Error("Not connected. Call connect() first.");
|
|
3814
|
+
}
|
|
3815
|
+
return new Promise((resolve, reject) => {
|
|
3816
|
+
this.socket.emit("room:list", { prefix: prefix ?? "" }, (response) => {
|
|
3817
|
+
if (response.status === "success") {
|
|
3818
|
+
resolve(response.rooms);
|
|
3819
|
+
} else {
|
|
3820
|
+
reject(new Error(response.message || "Failed to list rooms"));
|
|
3821
|
+
}
|
|
3822
|
+
});
|
|
3823
|
+
});
|
|
3824
|
+
}
|
|
3787
3825
|
/**
|
|
3788
3826
|
* Kick a user from a custom room. Only the room creator may call this.
|
|
3789
3827
|
*
|
|
@@ -3863,25 +3901,33 @@ var RealtimeModule = class {
|
|
|
3863
3901
|
};
|
|
3864
3902
|
}
|
|
3865
3903
|
/**
|
|
3866
|
-
* Send a message to a room
|
|
3867
|
-
*
|
|
3904
|
+
* Send a message to a room.
|
|
3905
|
+
*
|
|
3906
|
+
* By default the message is stored in the room's history buffer so late
|
|
3907
|
+
* joiners can replay it. Pass `{ history: false }` to broadcast without
|
|
3908
|
+
* persisting (e.g. ephemeral cursor positions, typing indicators).
|
|
3909
|
+
*
|
|
3868
3910
|
* @example
|
|
3869
3911
|
* ```typescript
|
|
3870
|
-
* //
|
|
3912
|
+
* // Persisted — replayed to future joiners
|
|
3871
3913
|
* await baasix.realtime.sendToRoom('game:lobby', 'chat', { text: 'Hello!' });
|
|
3872
|
-
*
|
|
3873
|
-
* //
|
|
3874
|
-
* await baasix.realtime.sendToRoom('game:
|
|
3914
|
+
*
|
|
3915
|
+
* // Ephemeral — broadcast only, never stored in history
|
|
3916
|
+
* await baasix.realtime.sendToRoom('game:lobby', 'typing', { userId }, { history: false });
|
|
3917
|
+
*
|
|
3918
|
+
* // Game move — skips history
|
|
3919
|
+
* await baasix.realtime.sendToRoom('game:123', 'move', { x: 10, y: 20 }, { history: false });
|
|
3875
3920
|
* ```
|
|
3876
3921
|
*/
|
|
3877
|
-
async sendToRoom(roomName, event, payload) {
|
|
3922
|
+
async sendToRoom(roomName, event, payload, options = {}) {
|
|
3878
3923
|
if (!this.socket?.connected) {
|
|
3879
3924
|
throw new Error("Not connected. Call connect() first.");
|
|
3880
3925
|
}
|
|
3926
|
+
const history = options.history ?? true;
|
|
3881
3927
|
return new Promise((resolve, reject) => {
|
|
3882
3928
|
this.socket.emit(
|
|
3883
3929
|
"room:message",
|
|
3884
|
-
{ room: roomName, event, payload },
|
|
3930
|
+
{ room: roomName, event, payload, history },
|
|
3885
3931
|
(response) => {
|
|
3886
3932
|
if (response.status === "success") {
|
|
3887
3933
|
resolve();
|