@ipcom/asterisk-ari 0.0.154 → 0.0.156

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.
@@ -582,6 +582,7 @@ __export(src_exports, {
582
582
  Applications: () => Applications,
583
583
  AriClient: () => AriClient,
584
584
  Asterisk: () => Asterisk,
585
+ BridgeInstance: () => BridgeInstance,
585
586
  Bridges: () => Bridges,
586
587
  ChannelInstance: () => ChannelInstance,
587
588
  Channels: () => Channels,
@@ -809,7 +810,7 @@ var Applications = class {
809
810
  }
810
811
  /**
811
812
  * Lists all applications.
812
- *
813
+ *
813
814
  * @returns A promise that resolves to an array of Application objects.
814
815
  * @throws {Error} If the API response is not an array.
815
816
  */
@@ -822,7 +823,7 @@ var Applications = class {
822
823
  }
823
824
  /**
824
825
  * Retrieves details of a specific application.
825
- *
826
+ *
826
827
  * @param appName - The name of the application to retrieve details for.
827
828
  * @returns A promise that resolves to an ApplicationDetails object.
828
829
  * @throws {Error} If there's an error fetching the application details.
@@ -839,7 +840,7 @@ var Applications = class {
839
840
  }
840
841
  /**
841
842
  * Sends a message to a specific application.
842
- *
843
+ *
843
844
  * @param appName - The name of the application to send the message to.
844
845
  * @param body - The message to be sent, containing an event and optional data.
845
846
  * @returns A promise that resolves when the message is successfully sent.
@@ -932,97 +933,655 @@ var Asterisk = class {
932
933
  };
933
934
 
934
935
  // src/ari-client/resources/bridges.ts
936
+ var import_events = require("events");
937
+ var import_axios2 = require("axios");
938
+
939
+ // src/ari-client/interfaces/events.types.ts
940
+ var bridgeEvents = [
941
+ "BridgeCreated",
942
+ "BridgeDestroyed",
943
+ "BridgeMerged",
944
+ "BridgeBlindTransfer",
945
+ "BridgeAttendedTransfer",
946
+ "BridgeVideoSourceChanged"
947
+ ];
948
+
949
+ // src/ari-client/utils.ts
950
+ function toQueryParams2(options) {
951
+ return new URLSearchParams(
952
+ Object.entries(options).filter(([, value]) => value !== void 0).map(([key, value]) => [key, value])
953
+ ).toString();
954
+ }
955
+
956
+ // src/ari-client/resources/bridges.ts
957
+ var getErrorMessage = (error) => {
958
+ if ((0, import_axios2.isAxiosError)(error)) {
959
+ return error.response?.data?.message || error.message || "Um erro do axios ocorreu";
960
+ }
961
+ if (error instanceof Error) {
962
+ return error.message;
963
+ }
964
+ return "Um erro desconhecido ocorreu";
965
+ };
966
+ var BridgeInstance = class {
967
+ /**
968
+ * Creates a new BridgeInstance.
969
+ *
970
+ * @param client - The AriClient instance for making API calls.
971
+ * @param baseClient - The BaseClient instance for making HTTP requests.
972
+ * @param bridgeId - Optional. The ID of the bridge. If not provided, a new ID will be generated.
973
+ */
974
+ constructor(client, baseClient, bridgeId) {
975
+ this.client = client;
976
+ this.baseClient = baseClient;
977
+ this.id = bridgeId || `bridge-${Date.now()}`;
978
+ }
979
+ eventEmitter = new import_events.EventEmitter();
980
+ bridgeData = null;
981
+ id;
982
+ /**
983
+ * Registers a listener for specific bridge events.
984
+ *
985
+ * @param event - The type of event to listen for.
986
+ * @param listener - The callback function to be called when the event occurs.
987
+ */
988
+ /**
989
+ * Registers a listener for specific bridge events.
990
+ *
991
+ * This method allows you to attach an event listener to the bridge instance for a specific event type.
992
+ * When the specified event occurs, the provided listener function will be called with the event data.
993
+ *
994
+ * @template T - The specific type of WebSocketEvent to listen for.
995
+ * It receives the event data as its parameter.
996
+ * @returns {void}
997
+ *
998
+ * @example
999
+ * bridge.on('BridgeCreated', (event) => {
1000
+ *
1001
+ * });
1002
+ * @param event
1003
+ * @param listener
1004
+ */
1005
+ on(event, listener) {
1006
+ if (!event) {
1007
+ throw new Error("Event type is required");
1008
+ }
1009
+ const wrappedListener = (data) => {
1010
+ if ("bridge" in data && data.bridge?.id === this.id) {
1011
+ listener(data);
1012
+ }
1013
+ };
1014
+ this.eventEmitter.on(event, wrappedListener);
1015
+ }
1016
+ /**
1017
+ * Registers a one-time listener for specific bridge events.
1018
+ *
1019
+ * @param event - The type of event to listen for.
1020
+ * @param listener - The callback function to be called when the event occurs.
1021
+ */
1022
+ once(event, listener) {
1023
+ if (!event) {
1024
+ throw new Error("Event type is required");
1025
+ }
1026
+ const wrappedListener = (data) => {
1027
+ if ("bridge" in data && data.bridge?.id === this.id) {
1028
+ listener(data);
1029
+ }
1030
+ };
1031
+ this.eventEmitter.once(event, wrappedListener);
1032
+ }
1033
+ /**
1034
+ * Removes event listener(s) from the bridge.
1035
+ *
1036
+ * @param event - The type of event to remove listeners for.
1037
+ * @param listener - Optional. The specific listener to remove. If not provided, all listeners for the event will be removed.
1038
+ */
1039
+ off(event, listener) {
1040
+ if (!event) {
1041
+ throw new Error("Event type is required");
1042
+ }
1043
+ if (listener) {
1044
+ this.eventEmitter.off(event, listener);
1045
+ } else {
1046
+ this.eventEmitter.removeAllListeners(event);
1047
+ }
1048
+ }
1049
+ /**
1050
+ * Emits an event if it corresponds to the current bridge.
1051
+ *
1052
+ * @param event - The WebSocketEvent to emit.
1053
+ */
1054
+ emitEvent(event) {
1055
+ if (!event) {
1056
+ console.warn("Invalid event received");
1057
+ return;
1058
+ }
1059
+ if ("bridge" in event && event.bridge?.id === this.id) {
1060
+ this.eventEmitter.emit(event.type, event);
1061
+ }
1062
+ }
1063
+ /**
1064
+ * Removes all event listeners from this bridge instance.
1065
+ */
1066
+ removeAllListeners() {
1067
+ this.eventEmitter.removeAllListeners();
1068
+ }
1069
+ /**
1070
+ * Retrieves the current details of the bridge.
1071
+ *
1072
+ * @returns A Promise that resolves to the Bridge object containing the current details.
1073
+ * @throws An error if the retrieval fails.
1074
+ */
1075
+ async get() {
1076
+ try {
1077
+ if (!this.id) {
1078
+ throw new Error("No bridge associated with this instance");
1079
+ }
1080
+ this.bridgeData = await this.baseClient.get(
1081
+ `/bridges/${this.id}`
1082
+ );
1083
+ return this.bridgeData;
1084
+ } catch (error) {
1085
+ const message = getErrorMessage(error);
1086
+ console.error(`Error retrieving details for bridge ${this.id}:`, message);
1087
+ throw new Error(`Failed to get bridge details: ${message}`);
1088
+ }
1089
+ }
1090
+ /**
1091
+ * Adds channels to the bridge.
1092
+ *
1093
+ * @param request - The AddChannelRequest object containing the channels to add.
1094
+ * @throws An error if the operation fails.
1095
+ */
1096
+ async add(request) {
1097
+ try {
1098
+ const queryParams = toQueryParams2({
1099
+ channel: Array.isArray(request.channel) ? request.channel.join(",") : request.channel,
1100
+ ...request.role && { role: request.role }
1101
+ });
1102
+ await this.baseClient.post(
1103
+ `/bridges/${this.id}/addChannel?${queryParams}`
1104
+ );
1105
+ } catch (error) {
1106
+ const message = getErrorMessage(error);
1107
+ console.error(`Error adding channels to bridge ${this.id}:`, message);
1108
+ throw new Error(`Failed to add channels: ${message}`);
1109
+ }
1110
+ }
1111
+ /**
1112
+ * Removes channels from the bridge.
1113
+ *
1114
+ * @param request - The RemoveChannelRequest object containing the channels to remove.
1115
+ * @throws An error if the operation fails.
1116
+ */
1117
+ async remove(request) {
1118
+ try {
1119
+ const queryParams = toQueryParams2({
1120
+ channel: Array.isArray(request.channel) ? request.channel.join(",") : request.channel
1121
+ });
1122
+ await this.baseClient.post(
1123
+ `/bridges/${this.id}/removeChannel?${queryParams}`
1124
+ );
1125
+ } catch (error) {
1126
+ const message = getErrorMessage(error);
1127
+ console.error(`Error removing channels from bridge ${this.id}:`, message);
1128
+ throw new Error(`Failed to remove channels: ${message}`);
1129
+ }
1130
+ }
1131
+ /**
1132
+ * Plays media on the bridge.
1133
+ *
1134
+ * @param request - The PlayMediaRequest object containing the media details to play.
1135
+ * @returns A Promise that resolves to a BridgePlayback object.
1136
+ * @throws An error if the operation fails.
1137
+ */
1138
+ async playMedia(request) {
1139
+ try {
1140
+ const queryParams = new URLSearchParams({
1141
+ ...request.lang && { lang: request.lang },
1142
+ ...request.offsetms && { offsetms: request.offsetms.toString() },
1143
+ ...request.skipms && { skipms: request.skipms.toString() },
1144
+ ...request.playbackId && { playbackId: request.playbackId }
1145
+ }).toString();
1146
+ const result = await this.baseClient.post(
1147
+ `/bridges/${this.id}/play?${queryParams}`,
1148
+ { media: request.media }
1149
+ );
1150
+ return result;
1151
+ } catch (error) {
1152
+ const message = getErrorMessage(error);
1153
+ console.error(`Error playing media on bridge ${this.id}:`, message);
1154
+ throw new Error(`Failed to play media: ${message}`);
1155
+ }
1156
+ }
1157
+ /**
1158
+ * Stops media playback on the bridge.
1159
+ *
1160
+ * @param playbackId - The ID of the playback to stop.
1161
+ * @throws An error if the operation fails.
1162
+ */
1163
+ async stopPlayback(playbackId) {
1164
+ try {
1165
+ await this.baseClient.delete(
1166
+ `/bridges/${this.id}/play/${playbackId}`
1167
+ );
1168
+ } catch (error) {
1169
+ const message = getErrorMessage(error);
1170
+ console.error(`Error stopping playback on bridge ${this.id}:`, message);
1171
+ throw new Error(`Failed to stop playback: ${message}`);
1172
+ }
1173
+ }
1174
+ /**
1175
+ * Sets the video source for the bridge.
1176
+ *
1177
+ * @param channelId - The ID of the channel to set as the video source.
1178
+ * @throws An error if the operation fails.
1179
+ */
1180
+ async setVideoSource(channelId) {
1181
+ try {
1182
+ await this.baseClient.post(
1183
+ `/bridges/${this.id}/videoSource/${channelId}`
1184
+ );
1185
+ } catch (error) {
1186
+ const message = getErrorMessage(error);
1187
+ console.error(
1188
+ `Error setting video source for bridge ${this.id}:`,
1189
+ message
1190
+ );
1191
+ throw new Error(`Failed to set video source: ${message}`);
1192
+ }
1193
+ }
1194
+ /**
1195
+ * Removes the video source from the bridge.
1196
+ *
1197
+ * @throws An error if the operation fails.
1198
+ */
1199
+ async clearVideoSource() {
1200
+ try {
1201
+ await this.baseClient.delete(`/bridges/${this.id}/videoSource`);
1202
+ } catch (error) {
1203
+ const message = getErrorMessage(error);
1204
+ console.error(
1205
+ `Error removing video source from bridge ${this.id}:`,
1206
+ message
1207
+ );
1208
+ throw new Error(`Failed to remove video source: ${message}`);
1209
+ }
1210
+ }
1211
+ /**
1212
+ * Checks if the bridge has listeners for a specific event.
1213
+ *
1214
+ * @param event - The event type to check for listeners.
1215
+ * @returns A boolean indicating whether there are listeners for the event.
1216
+ */
1217
+ hasListeners(event) {
1218
+ return this.eventEmitter.listenerCount(event) > 0;
1219
+ }
1220
+ /**
1221
+ * Retrieves the current bridge data without making an API call.
1222
+ *
1223
+ * @returns The current Bridge object or null if no data is available.
1224
+ */
1225
+ getCurrentData() {
1226
+ return this.bridgeData;
1227
+ }
1228
+ };
935
1229
  var Bridges = class {
936
- constructor(client) {
1230
+ constructor(baseClient, client) {
1231
+ this.baseClient = baseClient;
937
1232
  this.client = client;
938
1233
  }
1234
+ bridgeInstances = /* @__PURE__ */ new Map();
1235
+ /**
1236
+ * Creates or retrieves a Bridge instance.
1237
+ *
1238
+ * This method manages the creation and retrieval of BridgeInstance objects.
1239
+ * If an ID is provided and an instance with that ID already exists, it returns the existing instance.
1240
+ * If an ID is provided but no instance exists, it creates a new instance with that ID.
1241
+ * If no ID is provided, it creates a new instance with a generated ID.
1242
+ *
1243
+ * @param {Object} params - The parameters for creating or retrieving a Bridge instance.
1244
+ * @param {string} [params.id] - Optional. The ID of the Bridge instance to create or retrieve.
1245
+ *
1246
+ * @returns {BridgeInstance} A BridgeInstance object, either newly created or retrieved from existing instances.
1247
+ *
1248
+ * @throws {Error} If there's an error in creating or retrieving the Bridge instance.
1249
+ */
1250
+ Bridge({ id }) {
1251
+ try {
1252
+ if (!id) {
1253
+ const instance = new BridgeInstance(this.client, this.baseClient);
1254
+ this.bridgeInstances.set(instance.id, instance);
1255
+ return instance;
1256
+ }
1257
+ if (!this.bridgeInstances.has(id)) {
1258
+ const instance = new BridgeInstance(this.client, this.baseClient, id);
1259
+ this.bridgeInstances.set(id, instance);
1260
+ return instance;
1261
+ }
1262
+ return this.bridgeInstances.get(id);
1263
+ } catch (error) {
1264
+ const message = getErrorMessage(error);
1265
+ throw new Error(`Failed to manage bridge instance: ${message}`);
1266
+ }
1267
+ }
1268
+ /**
1269
+ * Removes a bridge instance from the collection of managed bridges.
1270
+ *
1271
+ * This function removes the specified bridge instance, cleans up its event listeners,
1272
+ * and logs the removal. If the bridge instance doesn't exist, it logs a warning.
1273
+ *
1274
+ * @param {string} bridgeId - The unique identifier of the bridge instance to be removed.
1275
+ * @throws {Error} Throws an error if the bridgeId is not provided.
1276
+ * @returns {void}
1277
+ */
1278
+ removeBridgeInstance(bridgeId) {
1279
+ if (!bridgeId) {
1280
+ throw new Error("ID da bridge \xE9 obrigat\xF3rio");
1281
+ }
1282
+ if (this.bridgeInstances.has(bridgeId)) {
1283
+ const instance = this.bridgeInstances.get(bridgeId);
1284
+ instance?.removeAllListeners();
1285
+ this.bridgeInstances.delete(bridgeId);
1286
+ } else {
1287
+ console.warn(`Tentativa de remover inst\xE2ncia inexistente: ${bridgeId}`);
1288
+ }
1289
+ }
1290
+ /**
1291
+ * Propagates a WebSocket event to a specific bridge instance.
1292
+ *
1293
+ * This function checks if the received event is valid and related to a bridge,
1294
+ * then emits the event to the corresponding bridge instance if it exists.
1295
+ *
1296
+ * @param {WebSocketEvent} event - The WebSocket event to be propagated.
1297
+ * This should be an object containing information about the event,
1298
+ * including the bridge ID and event type.
1299
+ *
1300
+ * @returns {void}
1301
+ *
1302
+ * @remarks
1303
+ * - If the event is invalid (null or undefined), a warning is logged and the function returns early.
1304
+ * - The function checks if the event is bridge-related and if the event type is included in the predefined bridge events.
1305
+ * - If a matching bridge instance is found, the event is emitted to that instance.
1306
+ * - If no matching bridge instance is found, a warning is logged.
1307
+ */
1308
+ propagateEventToBridge(event) {
1309
+ if (!event) {
1310
+ console.warn("Evento WebSocket inv\xE1lido recebido");
1311
+ return;
1312
+ }
1313
+ if ("bridge" in event && event.bridge?.id && bridgeEvents.includes(event.type)) {
1314
+ const instance = this.bridgeInstances.get(event.bridge.id);
1315
+ if (instance) {
1316
+ instance.emitEvent(event);
1317
+ } else {
1318
+ console.warn(
1319
+ `Nenhuma inst\xE2ncia encontrada para bridge ${event.bridge.id}`
1320
+ );
1321
+ }
1322
+ }
1323
+ }
939
1324
  /**
940
- * Lists all active bridges.
1325
+ * Lists all active bridges in the system.
1326
+ *
1327
+ * This asynchronous function retrieves a list of all currently active bridges
1328
+ * by making a GET request to the "/bridges" endpoint using the base client.
1329
+ *
1330
+ * @returns {Promise<Bridge[]>} A promise that resolves to an array of Bridge objects.
1331
+ * Each Bridge object represents an active bridge in the system.
1332
+ *
1333
+ * @throws {Error} If there's an error in fetching the bridges or if the request fails.
1334
+ *
1335
+ * @example
1336
+ * try {
1337
+ * const bridges = await bridgesInstance.list();
1338
+ *
1339
+ * } catch (error) {
1340
+ * console.error('Failed to fetch bridges:', error);
1341
+ * }
941
1342
  */
942
1343
  async list() {
943
- return this.client.get("/bridges");
1344
+ return this.baseClient.get("/bridges");
944
1345
  }
945
1346
  /**
946
- * Creates a new bridge.
1347
+ * Creates a new bridge in the system.
1348
+ *
1349
+ * This asynchronous function sends a POST request to create a new bridge
1350
+ * using the provided configuration details.
1351
+ *
1352
+ * @param request - The configuration details for creating the new bridge.
1353
+ * @param request.type - The type of bridge to create (e.g., 'mixing', 'holding').
1354
+ * @param request.name - Optional. A custom name for the bridge.
1355
+ * @param request.bridgeId - Optional. A specific ID for the bridge. If not provided, one will be generated.
1356
+ *
1357
+ * @returns A Promise that resolves to a Bridge object representing the newly created bridge.
1358
+ * The Bridge object contains details such as id, technology, bridge_type, bridge_class, channels, etc.
1359
+ *
1360
+ * @throws Will throw an error if the bridge creation fails or if there's a network issue.
947
1361
  */
948
1362
  async createBridge(request) {
949
- return this.client.post("/bridges", request);
1363
+ return this.baseClient.post("/bridges", request);
950
1364
  }
951
1365
  /**
952
- * Retrieves details of a specific bridge.
1366
+ * Retrieves detailed information about a specific bridge.
1367
+ *
1368
+ * This asynchronous function fetches the complete details of a bridge
1369
+ * identified by its unique ID. It makes a GET request to the ARI endpoint
1370
+ * for the specified bridge.
1371
+ *
1372
+ * @param bridgeId - The unique identifier of the bridge to retrieve details for.
1373
+ * This should be a string that uniquely identifies the bridge in the system.
1374
+ *
1375
+ * @returns A Promise that resolves to a Bridge object containing all the details
1376
+ * of the specified bridge. This includes information such as the bridge's
1377
+ * ID, type, channels, and other relevant properties.
1378
+ *
1379
+ * @throws Will throw an error if the bridge cannot be found, if there's a network issue,
1380
+ * or if the server responds with an error.
953
1381
  */
954
- async getDetails(bridgeId) {
955
- return this.client.get(`/bridges/${bridgeId}`);
1382
+ async get(bridgeId) {
1383
+ return this.baseClient.get(`/bridges/${bridgeId}`);
956
1384
  }
957
1385
  /**
958
- * Destroys (deletes) a specific bridge.
1386
+ * Destroys (deletes) a specific bridge in the system.
1387
+ *
1388
+ * This asynchronous function sends a DELETE request to remove a bridge
1389
+ * identified by its unique ID. Once destroyed, the bridge and all its
1390
+ * associated resources are permanently removed from the system.
1391
+ *
1392
+ * @param bridgeId - The unique identifier of the bridge to be destroyed.
1393
+ * This should be a string that uniquely identifies the bridge in the system.
1394
+ *
1395
+ * @returns A Promise that resolves to void when the bridge is successfully destroyed.
1396
+ * If the operation is successful, the bridge no longer exists in the system.
1397
+ *
1398
+ * @throws Will throw an error if the bridge cannot be found, if there's a network issue,
1399
+ * or if the server responds with an error during the deletion process.
959
1400
  */
960
1401
  async destroy(bridgeId) {
961
- return this.client.delete(`/bridges/${bridgeId}`);
1402
+ return this.baseClient.delete(`/bridges/${bridgeId}`);
962
1403
  }
963
1404
  /**
964
- * Adds a channel or multiple channels to a bridge.
1405
+ * Adds one or more channels to a specified bridge.
1406
+ *
1407
+ * This asynchronous function sends a POST request to add channels to an existing bridge.
1408
+ * It can handle adding a single channel or multiple channels in one operation.
1409
+ *
1410
+ * @param bridgeId - The unique identifier of the bridge to which channels will be added.
1411
+ * @param request - An object containing the details of the channel(s) to be added.
1412
+ * @param request.channel - A single channel ID or an array of channel IDs to add to the bridge.
1413
+ * @param request.role - Optional. Specifies the role of the channel(s) in the bridge.
1414
+ *
1415
+ * @returns A Promise that resolves to void when the operation is successful.
1416
+ *
1417
+ * @throws Will throw an error if the request fails, such as if the bridge doesn't exist
1418
+ * or if there's a network issue.
965
1419
  */
966
1420
  async addChannels(bridgeId, request) {
967
- const queryParams = new URLSearchParams({
1421
+ const queryParams = toQueryParams2({
968
1422
  channel: Array.isArray(request.channel) ? request.channel.join(",") : request.channel,
969
1423
  ...request.role && { role: request.role }
970
- }).toString();
971
- await this.client.post(
1424
+ });
1425
+ await this.baseClient.post(
972
1426
  `/bridges/${bridgeId}/addChannel?${queryParams}`
973
1427
  );
974
1428
  }
975
1429
  /**
976
- * Removes a channel or multiple channels from a bridge.
1430
+ * Removes one or more channels from a specified bridge.
1431
+ *
1432
+ * This asynchronous function sends a POST request to remove channels from an existing bridge.
1433
+ * It can handle removing a single channel or multiple channels in one operation.
1434
+ *
1435
+ * @param bridgeId - The unique identifier of the bridge from which channels will be removed.
1436
+ * @param request - An object containing the details of the channel(s) to be removed.
1437
+ * @param request.channel - A single channel ID or an array of channel IDs to remove from the bridge.
1438
+ *
1439
+ * @returns A Promise that resolves to void when the operation is successful.
1440
+ *
1441
+ * @throws Will throw an error if the request fails, such as if the bridge doesn't exist,
1442
+ * if the channels are not in the bridge, or if there's a network issue.
977
1443
  */
978
1444
  async removeChannels(bridgeId, request) {
979
- const queryParams = new URLSearchParams({
1445
+ const queryParams = toQueryParams2({
980
1446
  channel: Array.isArray(request.channel) ? request.channel.join(",") : request.channel
981
- }).toString();
982
- await this.client.post(
1447
+ });
1448
+ await this.baseClient.post(
983
1449
  `/bridges/${bridgeId}/removeChannel?${queryParams}`
984
1450
  );
985
1451
  }
986
1452
  /**
987
- * Plays media to a bridge.
1453
+ * Plays media on a specified bridge.
1454
+ *
1455
+ * This asynchronous function initiates media playback on a bridge identified by its ID.
1456
+ * It allows for customization of the playback through various options in the request.
1457
+ *
1458
+ * @param bridgeId - The unique identifier of the bridge on which to play the media.
1459
+ * @param request - An object containing the media playback request details.
1460
+ * @param request.media - The media to be played (e.g., sound file, URL).
1461
+ * @param request.lang - Optional. The language of the media content.
1462
+ * @param request.offsetms - Optional. The offset in milliseconds to start playing from.
1463
+ * @param request.skipms - Optional. The number of milliseconds to skip before playing.
1464
+ * @param request.playbackId - Optional. A custom ID for the playback session.
1465
+ *
1466
+ * @returns A Promise that resolves to a BridgePlayback object, containing details about the initiated playback.
1467
+ *
1468
+ * @throws Will throw an error if the playback request fails or if there's a network issue.
988
1469
  */
989
1470
  async playMedia(bridgeId, request) {
990
- const queryParams = new URLSearchParams({
1471
+ const queryParams = toQueryParams2({
991
1472
  ...request.lang && { lang: request.lang },
992
1473
  ...request.offsetms && { offsetms: request.offsetms.toString() },
993
1474
  ...request.skipms && { skipms: request.skipms.toString() },
994
1475
  ...request.playbackId && { playbackId: request.playbackId }
995
- }).toString();
996
- return this.client.post(
1476
+ });
1477
+ return this.baseClient.post(
997
1478
  `/bridges/${bridgeId}/play?${queryParams}`,
998
1479
  { media: request.media }
999
1480
  );
1000
1481
  }
1001
1482
  /**
1002
- * Stops media playback on a bridge.
1483
+ * Stops media playback on a specified bridge.
1484
+ *
1485
+ * This asynchronous function sends a DELETE request to stop the playback of media
1486
+ * on a bridge identified by its ID and a specific playback session.
1487
+ *
1488
+ * @param bridgeId - The unique identifier of the bridge where the playback is to be stopped.
1489
+ * @param playbackId - The unique identifier of the playback session to be stopped.
1490
+ *
1491
+ * @returns A Promise that resolves to void when the playback is successfully stopped.
1492
+ *
1493
+ * @throws Will throw an error if the request fails, such as if the bridge or playback session
1494
+ * doesn't exist, or if there's a network issue.
1003
1495
  */
1004
1496
  async stopPlayback(bridgeId, playbackId) {
1005
- await this.client.delete(`/bridges/${bridgeId}/play/${playbackId}`);
1497
+ await this.baseClient.delete(
1498
+ `/bridges/${bridgeId}/play/${playbackId}`
1499
+ );
1006
1500
  }
1007
1501
  /**
1008
- * Sets the video source for a bridge.
1502
+ * Sets the video source for a specified bridge.
1503
+ *
1504
+ * This asynchronous function configures a channel as the video source for a given bridge.
1505
+ * It sends a POST request to the ARI endpoint to update the bridge's video source.
1506
+ *
1507
+ * @param bridgeId - The unique identifier of the bridge for which to set the video source.
1508
+ * @param channelId - The unique identifier of the channel to be set as the video source.
1509
+ *
1510
+ * @returns A Promise that resolves to void when the video source is successfully set.
1511
+ *
1512
+ * @throws Will throw an error if the request fails, such as if the bridge or channel
1513
+ * doesn't exist, or if there's a network issue.
1009
1514
  */
1010
1515
  async setVideoSource(bridgeId, channelId) {
1011
- await this.client.post(
1012
- `/bridges/${bridgeId}/videoSource?channelId=${encodeURIComponent(channelId)}`
1516
+ const queryParams = toQueryParams2({ channelId });
1517
+ await this.baseClient.post(
1518
+ `/bridges/${bridgeId}/videoSource?${queryParams}`
1013
1519
  );
1014
1520
  }
1015
1521
  /**
1016
- * Clears the video source for a bridge.
1522
+ * Clears the video source for a specified bridge.
1523
+ *
1524
+ * This asynchronous function removes the currently set video source from a bridge.
1525
+ * It sends a DELETE request to the ARI endpoint to clear the video source configuration.
1526
+ *
1527
+ * @param bridgeId - The unique identifier of the bridge from which to clear the video source.
1528
+ * This should be a string that uniquely identifies the bridge in the system.
1529
+ *
1530
+ * @returns A Promise that resolves to void when the video source is successfully cleared.
1531
+ * If the operation is successful, the bridge will no longer have a designated video source.
1532
+ *
1533
+ * @throws Will throw an error if the request fails, such as if the bridge doesn't exist,
1534
+ * if there's no video source set, or if there's a network issue.
1017
1535
  */
1018
1536
  async clearVideoSource(bridgeId) {
1019
- await this.client.delete(`/bridges/${bridgeId}/videoSource`);
1537
+ await this.baseClient.delete(`/bridges/${bridgeId}/videoSource`);
1538
+ }
1539
+ /**
1540
+ * Retrieves the count of active bridge instances.
1541
+ *
1542
+ * This function returns the total number of bridge instances currently
1543
+ * managed by the Bridges class. It provides a quick way to check how many
1544
+ * active bridges are present in the system.
1545
+ *
1546
+ * @returns {number} The count of active bridge instances.
1547
+ */
1548
+ getInstanceCount() {
1549
+ return this.bridgeInstances.size;
1550
+ }
1551
+ /**
1552
+ * Checks if a bridge instance exists in the collection of managed bridges.
1553
+ *
1554
+ * This function verifies whether a bridge instance with the specified ID
1555
+ * is currently being managed by the Bridges class.
1556
+ *
1557
+ * @param bridgeId - The unique identifier of the bridge instance to check.
1558
+ * This should be a string that uniquely identifies the bridge in the system.
1559
+ *
1560
+ * @returns A boolean value indicating whether the bridge instance exists.
1561
+ * Returns true if the bridge instance is found, false otherwise.
1562
+ */
1563
+ hasInstance(bridgeId) {
1564
+ return this.bridgeInstances.has(bridgeId);
1565
+ }
1566
+ /**
1567
+ * Retrieves all active bridge instances currently managed by the Bridges class.
1568
+ *
1569
+ * This method provides a way to access all the BridgeInstance objects that are
1570
+ * currently active and being managed. It returns a new Map to prevent direct
1571
+ * modification of the internal bridgeInstances collection.
1572
+ *
1573
+ * @returns A new Map object containing all active bridge instances, where the keys
1574
+ * are the bridge IDs (strings) and the values are the corresponding
1575
+ * BridgeInstance objects. If no bridges are active, an empty Map is returned.
1576
+ */
1577
+ getAllInstances() {
1578
+ return new Map(this.bridgeInstances);
1020
1579
  }
1021
1580
  };
1022
1581
 
1023
1582
  // src/ari-client/resources/channels.ts
1024
- var import_events = require("events");
1025
- var import_axios2 = require("axios");
1583
+ var import_events3 = require("events");
1584
+ var import_axios3 = require("axios");
1026
1585
 
1027
1586
  // node_modules/uuid/dist/esm/stringify.js
1028
1587
  var byteToHex = [];
@@ -1069,16 +1628,9 @@ function v4(options, buf, offset) {
1069
1628
  }
1070
1629
  var v4_default = v4;
1071
1630
 
1072
- // src/ari-client/utils.ts
1073
- function toQueryParams2(options) {
1074
- return new URLSearchParams(
1075
- Object.entries(options).filter(([, value]) => value !== void 0).map(([key, value]) => [key, value])
1076
- ).toString();
1077
- }
1078
-
1079
1631
  // src/ari-client/resources/channels.ts
1080
- var getErrorMessage = (error) => {
1081
- if ((0, import_axios2.isAxiosError)(error)) {
1632
+ var getErrorMessage2 = (error) => {
1633
+ if ((0, import_axios3.isAxiosError)(error)) {
1082
1634
  return error.response?.data?.message || error.message || "An axios error occurred";
1083
1635
  }
1084
1636
  if (error instanceof Error) {
@@ -1092,7 +1644,7 @@ var ChannelInstance = class {
1092
1644
  this.baseClient = baseClient;
1093
1645
  this.id = channelId || `channel-${Date.now()}`;
1094
1646
  }
1095
- eventEmitter = new import_events.EventEmitter();
1647
+ eventEmitter = new import_events3.EventEmitter();
1096
1648
  channelData = null;
1097
1649
  id;
1098
1650
  /**
@@ -1170,7 +1722,7 @@ var ChannelInstance = class {
1170
1722
  try {
1171
1723
  await this.baseClient.post(`/channels/${this.id}/answer`);
1172
1724
  } catch (error) {
1173
- const message = getErrorMessage(error);
1725
+ const message = getErrorMessage2(error);
1174
1726
  console.error(`Error answering channel ${this.id}:`, message);
1175
1727
  throw new Error(`Failed to answer channel: ${message}`);
1176
1728
  }
@@ -1193,7 +1745,7 @@ var ChannelInstance = class {
1193
1745
  );
1194
1746
  return this.channelData;
1195
1747
  } catch (error) {
1196
- const message = getErrorMessage(error);
1748
+ const message = getErrorMessage2(error);
1197
1749
  console.error(`Error originating channel:`, message);
1198
1750
  throw new Error(`Failed to originate channel: ${message}`);
1199
1751
  }
@@ -1216,7 +1768,7 @@ var ChannelInstance = class {
1216
1768
  );
1217
1769
  return playback;
1218
1770
  } catch (error) {
1219
- const message = getErrorMessage(error);
1771
+ const message = getErrorMessage2(error);
1220
1772
  console.error(`Error playing media on channel ${this.id}:`, message);
1221
1773
  throw new Error(`Failed to play media: ${message}`);
1222
1774
  }
@@ -1238,7 +1790,7 @@ var ChannelInstance = class {
1238
1790
  this.channelData = details;
1239
1791
  return details;
1240
1792
  } catch (error) {
1241
- const message = getErrorMessage(error);
1793
+ const message = getErrorMessage2(error);
1242
1794
  console.error(
1243
1795
  `Error retrieving channel details for ${this.id}:`,
1244
1796
  message
@@ -1479,7 +2031,7 @@ var Channels = class {
1479
2031
  }
1480
2032
  return this.channelInstances.get(id);
1481
2033
  } catch (error) {
1482
- const message = getErrorMessage(error);
2034
+ const message = getErrorMessage2(error);
1483
2035
  console.error(`Error creating/retrieving channel instance:`, message);
1484
2036
  throw new Error(`Failed to manage channel instance: ${message}`);
1485
2037
  }
@@ -1498,7 +2050,7 @@ var Channels = class {
1498
2050
  }
1499
2051
  return await this.baseClient.get(`/channels/${id}`);
1500
2052
  } catch (error) {
1501
- const message = getErrorMessage(error);
2053
+ const message = getErrorMessage2(error);
1502
2054
  console.error(`Error retrieving channel details for ${id}:`, message);
1503
2055
  throw new Error(`Failed to get channel details: ${message}`);
1504
2056
  }
@@ -1545,7 +2097,7 @@ var Channels = class {
1545
2097
  try {
1546
2098
  return await this.baseClient.post("/channels", data);
1547
2099
  } catch (error) {
1548
- const message = getErrorMessage(error);
2100
+ const message = getErrorMessage2(error);
1549
2101
  console.error(`Error originating channel:`, message);
1550
2102
  throw new Error(`Failed to originate channel: ${message}`);
1551
2103
  }
@@ -1561,7 +2113,7 @@ var Channels = class {
1561
2113
  }
1562
2114
  return channels;
1563
2115
  } catch (error) {
1564
- const message = getErrorMessage(error);
2116
+ const message = getErrorMessage2(error);
1565
2117
  console.error(`Error listing channels:`, message);
1566
2118
  throw new Error(`Failed to list channels: ${message}`);
1567
2119
  }
@@ -1988,10 +2540,10 @@ var Endpoints = class {
1988
2540
  };
1989
2541
 
1990
2542
  // src/ari-client/resources/playbacks.ts
1991
- var import_events2 = require("events");
1992
- var import_axios3 = require("axios");
1993
- var getErrorMessage2 = (error) => {
1994
- if ((0, import_axios3.isAxiosError)(error)) {
2543
+ var import_events4 = require("events");
2544
+ var import_axios4 = require("axios");
2545
+ var getErrorMessage3 = (error) => {
2546
+ if ((0, import_axios4.isAxiosError)(error)) {
1995
2547
  return error.response?.data?.message || error.message || "An axios error occurred";
1996
2548
  }
1997
2549
  if (error instanceof Error) {
@@ -2013,7 +2565,7 @@ var PlaybackInstance = class {
2013
2565
  this.playbackId = playbackId;
2014
2566
  this.id = playbackId;
2015
2567
  }
2016
- eventEmitter = new import_events2.EventEmitter();
2568
+ eventEmitter = new import_events4.EventEmitter();
2017
2569
  playbackData = null;
2018
2570
  id;
2019
2571
  /**
@@ -2096,7 +2648,7 @@ var PlaybackInstance = class {
2096
2648
  );
2097
2649
  return this.playbackData;
2098
2650
  } catch (error) {
2099
- const message = getErrorMessage2(error);
2651
+ const message = getErrorMessage3(error);
2100
2652
  console.warn(`Error retrieving playback data for ${this.id}:`, message);
2101
2653
  throw new Error(`Failed to get playback data: ${message}`);
2102
2654
  }
@@ -2116,7 +2668,7 @@ var PlaybackInstance = class {
2116
2668
  `/playbacks/${this.id}/control?operation=${operation}`
2117
2669
  );
2118
2670
  } catch (error) {
2119
- const message = getErrorMessage2(error);
2671
+ const message = getErrorMessage3(error);
2120
2672
  console.warn(`Error controlling playback ${this.id}:`, message);
2121
2673
  throw new Error(`Failed to control playback: ${message}`);
2122
2674
  }
@@ -2133,7 +2685,7 @@ var PlaybackInstance = class {
2133
2685
  try {
2134
2686
  await this.baseClient.delete(`/playbacks/${this.id}`);
2135
2687
  } catch (error) {
2136
- const message = getErrorMessage2(error);
2688
+ const message = getErrorMessage3(error);
2137
2689
  console.warn(`Error stopping playback ${this.id}:`, message);
2138
2690
  throw new Error(`Failed to stop playback: ${message}`);
2139
2691
  }
@@ -2189,7 +2741,7 @@ var Playbacks = class {
2189
2741
  }
2190
2742
  return this.playbackInstances.get(id);
2191
2743
  } catch (error) {
2192
- const message = getErrorMessage2(error);
2744
+ const message = getErrorMessage3(error);
2193
2745
  console.warn(`Error creating/retrieving playback instance:`, message);
2194
2746
  throw new Error(`Failed to manage playback instance: ${message}`);
2195
2747
  }
@@ -2241,7 +2793,7 @@ var Playbacks = class {
2241
2793
  try {
2242
2794
  return await this.baseClient.get(`/playbacks/${playbackId}`);
2243
2795
  } catch (error) {
2244
- const message = getErrorMessage2(error);
2796
+ const message = getErrorMessage3(error);
2245
2797
  console.warn(`Error getting playback details ${playbackId}:`, message);
2246
2798
  throw new Error(`Failed to get playback details: ${message}`);
2247
2799
  }
@@ -2260,7 +2812,7 @@ var Playbacks = class {
2260
2812
  const playback = this.Playback({ id: playbackId });
2261
2813
  await playback.control(operation);
2262
2814
  } catch (error) {
2263
- const message = getErrorMessage2(error);
2815
+ const message = getErrorMessage3(error);
2264
2816
  console.warn(`Error controlling playback ${playbackId}:`, message);
2265
2817
  throw new Error(`Failed to control playback: ${message}`);
2266
2818
  }
@@ -2278,7 +2830,7 @@ var Playbacks = class {
2278
2830
  const playback = this.Playback({ id: playbackId });
2279
2831
  await playback.stop();
2280
2832
  } catch (error) {
2281
- const message = getErrorMessage2(error);
2833
+ const message = getErrorMessage3(error);
2282
2834
  console.warn(`Error stopping playback ${playbackId}:`, message);
2283
2835
  throw new Error(`Failed to stop playback: ${message}`);
2284
2836
  }
@@ -2332,13 +2884,13 @@ var Sounds = class {
2332
2884
  };
2333
2885
 
2334
2886
  // src/ari-client/websocketClient.ts
2335
- var import_events3 = require("events");
2887
+ var import_events5 = require("events");
2336
2888
  var import_exponential_backoff = __toESM(require_backoff(), 1);
2337
2889
  var import_ws = __toESM(require("ws"), 1);
2338
2890
  var DEFAULT_MAX_RECONNECT_ATTEMPTS = 30;
2339
2891
  var DEFAULT_STARTING_DELAY = 500;
2340
2892
  var DEFAULT_MAX_DELAY = 1e4;
2341
- var WebSocketClient = class extends import_events3.EventEmitter {
2893
+ var WebSocketClient = class extends import_events5.EventEmitter {
2342
2894
  /**
2343
2895
  * Creates a new WebSocketClient instance.
2344
2896
  *
@@ -2492,6 +3044,11 @@ var WebSocketClient = class extends import_events3.EventEmitter {
2492
3044
  instancePlayback.emitEvent(event);
2493
3045
  event.instancePlayback = instancePlayback;
2494
3046
  }
3047
+ if ("bridge" in event && event.bridge?.id && this.ariClient) {
3048
+ const instanceBridge = this.ariClient.Bridge(event.bridge.id);
3049
+ instanceBridge.emitEvent(event);
3050
+ event.instanceBridge = instanceBridge;
3051
+ }
2495
3052
  this.emit(event.type, event);
2496
3053
  } catch (error) {
2497
3054
  console.error(
@@ -2602,11 +3159,11 @@ var AriClient = class {
2602
3159
  this.baseClient = new BaseClient(baseUrl, config.username, config.password);
2603
3160
  this.channels = new Channels(this.baseClient, this);
2604
3161
  this.playbacks = new Playbacks(this.baseClient, this);
3162
+ this.bridges = new Bridges(this.baseClient, this);
2605
3163
  this.endpoints = new Endpoints(this.baseClient);
2606
3164
  this.applications = new Applications(this.baseClient);
2607
3165
  this.sounds = new Sounds(this.baseClient);
2608
3166
  this.asterisk = new Asterisk(this.baseClient);
2609
- this.bridges = new Bridges(this.baseClient);
2610
3167
  console.log(`ARI Client initialized with base URL: ${baseUrl}`);
2611
3168
  }
2612
3169
  baseClient;
@@ -2722,6 +3279,20 @@ var AriClient = class {
2722
3279
  Playback(playbackId, _app) {
2723
3280
  return this.playbacks.Playback({ id: playbackId });
2724
3281
  }
3282
+ /**
3283
+ * Creates or retrieves a Bridge instance.
3284
+ *
3285
+ * This function allows you to create a new Bridge instance or retrieve an existing one
3286
+ * based on the provided bridge ID.
3287
+ *
3288
+ * @param {string} [bridgeId] - Optional ID of an existing bridge. If provided, retrieves the
3289
+ * existing bridge with this ID. If omitted, creates a new bridge.
3290
+ * @returns {BridgeInstance} A new or existing Bridge instance that can be used to interact
3291
+ * with the Asterisk bridge.
3292
+ */
3293
+ Bridge(bridgeId) {
3294
+ return this.bridges.Bridge({ id: bridgeId });
3295
+ }
2725
3296
  /**
2726
3297
  * Gets the current WebSocket connection status.
2727
3298
  *
@@ -2736,6 +3307,7 @@ var AriClient = class {
2736
3307
  Applications,
2737
3308
  AriClient,
2738
3309
  Asterisk,
3310
+ BridgeInstance,
2739
3311
  Bridges,
2740
3312
  ChannelInstance,
2741
3313
  Channels,