@ipcom/asterisk-ari 0.0.143 → 0.0.144-beta

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,
@@ -618,7 +619,9 @@ var BaseClient = class {
618
619
  this.username = username;
619
620
  this.password = password;
620
621
  if (!/^https?:\/\/.+/.test(baseUrl)) {
621
- throw new Error("Invalid base URL. It must start with http:// or https://");
622
+ throw new Error(
623
+ "Invalid base URL. It must start with http:// or https://"
624
+ );
622
625
  }
623
626
  this.client = import_axios.default.create({
624
627
  baseURL: baseUrl,
@@ -812,7 +815,7 @@ var Applications = class {
812
815
  }
813
816
  /**
814
817
  * Lists all applications.
815
- *
818
+ *
816
819
  * @returns A promise that resolves to an array of Application objects.
817
820
  * @throws {Error} If the API response is not an array.
818
821
  */
@@ -825,7 +828,7 @@ var Applications = class {
825
828
  }
826
829
  /**
827
830
  * Retrieves details of a specific application.
828
- *
831
+ *
829
832
  * @param appName - The name of the application to retrieve details for.
830
833
  * @returns A promise that resolves to an ApplicationDetails object.
831
834
  * @throws {Error} If there's an error fetching the application details.
@@ -842,7 +845,7 @@ var Applications = class {
842
845
  }
843
846
  /**
844
847
  * Sends a message to a specific application.
845
- *
848
+ *
846
849
  * @param appName - The name of the application to send the message to.
847
850
  * @param body - The message to be sent, containing an event and optional data.
848
851
  * @returns A promise that resolves when the message is successfully sent.
@@ -935,97 +938,681 @@ var Asterisk = class {
935
938
  };
936
939
 
937
940
  // src/ari-client/resources/bridges.ts
941
+ var import_events = require("events");
942
+ var import_axios2 = require("axios");
943
+
944
+ // src/ari-client/interfaces/events.types.ts
945
+ var bridgeEvents = [
946
+ "BridgeCreated",
947
+ "BridgeDestroyed",
948
+ "BridgeMerged",
949
+ "BridgeBlindTransfer",
950
+ "BridgeAttendedTransfer",
951
+ "BridgeVideoSourceChanged"
952
+ ];
953
+
954
+ // src/ari-client/utils.ts
955
+ function toQueryParams2(options) {
956
+ return new URLSearchParams(
957
+ Object.entries(options).filter(([, value]) => value !== void 0).map(([key, value]) => [key, value])
958
+ ).toString();
959
+ }
960
+
961
+ // src/ari-client/resources/bridges.ts
962
+ var getErrorMessage = (error) => {
963
+ if ((0, import_axios2.isAxiosError)(error)) {
964
+ return error.response?.data?.message || error.message || "Um erro do axios ocorreu";
965
+ }
966
+ if (error instanceof Error) {
967
+ return error.message;
968
+ }
969
+ return "Um erro desconhecido ocorreu";
970
+ };
971
+ var BridgeInstance = class {
972
+ /**
973
+ * Creates a new BridgeInstance.
974
+ *
975
+ * @param client - The AriClient instance for making API calls.
976
+ * @param baseClient - The BaseClient instance for making HTTP requests.
977
+ * @param bridgeId - Optional. The ID of the bridge. If not provided, a new ID will be generated.
978
+ */
979
+ constructor(client, baseClient, bridgeId) {
980
+ this.client = client;
981
+ this.baseClient = baseClient;
982
+ this.id = bridgeId || `bridge-${Date.now()}`;
983
+ console.log(`BridgeInstance inicializada com ID: ${this.id}`);
984
+ }
985
+ eventEmitter = new import_events.EventEmitter();
986
+ bridgeData = null;
987
+ id;
988
+ /**
989
+ * Registers a listener for specific bridge events.
990
+ *
991
+ * @param event - The type of event to listen for.
992
+ * @param listener - The callback function to be called when the event occurs.
993
+ */
994
+ /**
995
+ * Registers a listener for specific bridge events.
996
+ *
997
+ * This method allows you to attach an event listener to the bridge instance for a specific event type.
998
+ * When the specified event occurs, the provided listener function will be called with the event data.
999
+ *
1000
+ * @template T - The specific type of WebSocketEvent to listen for.
1001
+ * It receives the event data as its parameter.
1002
+ * @returns {void}
1003
+ *
1004
+ * @example
1005
+ * bridge.on('BridgeCreated', (event) => {
1006
+ * console.log('Bridge created:', event.bridge.id);
1007
+ * });
1008
+ * @param event
1009
+ * @param listener
1010
+ */
1011
+ on(event, listener) {
1012
+ if (!event) {
1013
+ throw new Error("Event type is required");
1014
+ }
1015
+ const wrappedListener = (data) => {
1016
+ if ("bridge" in data && data.bridge?.id === this.id) {
1017
+ listener(data);
1018
+ }
1019
+ };
1020
+ this.eventEmitter.on(event, wrappedListener);
1021
+ console.log(`Event listener registered for ${event} on bridge ${this.id}`);
1022
+ }
1023
+ /**
1024
+ * Registers a one-time listener for specific bridge events.
1025
+ *
1026
+ * @param event - The type of event to listen for.
1027
+ * @param listener - The callback function to be called when the event occurs.
1028
+ */
1029
+ once(event, listener) {
1030
+ if (!event) {
1031
+ throw new Error("Event type is required");
1032
+ }
1033
+ const wrappedListener = (data) => {
1034
+ if ("bridge" in data && data.bridge?.id === this.id) {
1035
+ listener(data);
1036
+ }
1037
+ };
1038
+ this.eventEmitter.once(event, wrappedListener);
1039
+ console.log(
1040
+ `One-time listener registered for ${event} on bridge ${this.id}`
1041
+ );
1042
+ }
1043
+ /**
1044
+ * Removes event listener(s) from the bridge.
1045
+ *
1046
+ * @param event - The type of event to remove listeners for.
1047
+ * @param listener - Optional. The specific listener to remove. If not provided, all listeners for the event will be removed.
1048
+ */
1049
+ off(event, listener) {
1050
+ if (!event) {
1051
+ throw new Error("Event type is required");
1052
+ }
1053
+ if (listener) {
1054
+ this.eventEmitter.off(event, listener);
1055
+ console.log(
1056
+ `Specific listener removed for ${event} on bridge ${this.id}`
1057
+ );
1058
+ } else {
1059
+ this.eventEmitter.removeAllListeners(event);
1060
+ console.log(`All listeners removed for ${event} on bridge ${this.id}`);
1061
+ }
1062
+ }
1063
+ /**
1064
+ * Emits an event if it corresponds to the current bridge.
1065
+ *
1066
+ * @param event - The WebSocketEvent to emit.
1067
+ */
1068
+ emitEvent(event) {
1069
+ if (!event) {
1070
+ console.warn("Invalid event received");
1071
+ return;
1072
+ }
1073
+ if ("bridge" in event && event.bridge?.id === this.id) {
1074
+ this.eventEmitter.emit(event.type, event);
1075
+ console.log(`Event ${event.type} emitted for bridge ${this.id}`);
1076
+ }
1077
+ }
1078
+ /**
1079
+ * Removes all event listeners from this bridge instance.
1080
+ */
1081
+ removeAllListeners() {
1082
+ this.eventEmitter.removeAllListeners();
1083
+ console.log(`All listeners removed from bridge ${this.id}`);
1084
+ }
1085
+ /**
1086
+ * Retrieves the current details of the bridge.
1087
+ *
1088
+ * @returns A Promise that resolves to the Bridge object containing the current details.
1089
+ * @throws An error if the retrieval fails.
1090
+ */
1091
+ async get() {
1092
+ try {
1093
+ if (!this.id) {
1094
+ throw new Error("No bridge associated with this instance");
1095
+ }
1096
+ this.bridgeData = await this.baseClient.get(
1097
+ `/bridges/${this.id}`
1098
+ );
1099
+ console.log(`Details retrieved for bridge ${this.id}`);
1100
+ return this.bridgeData;
1101
+ } catch (error) {
1102
+ const message = getErrorMessage(error);
1103
+ console.error(`Error retrieving details for bridge ${this.id}:`, message);
1104
+ throw new Error(`Failed to get bridge details: ${message}`);
1105
+ }
1106
+ }
1107
+ /**
1108
+ * Adds channels to the bridge.
1109
+ *
1110
+ * @param request - The AddChannelRequest object containing the channels to add.
1111
+ * @throws An error if the operation fails.
1112
+ */
1113
+ async add(request) {
1114
+ try {
1115
+ const queryParams = toQueryParams2({
1116
+ channel: Array.isArray(request.channel) ? request.channel.join(",") : request.channel,
1117
+ ...request.role && { role: request.role }
1118
+ });
1119
+ await this.baseClient.post(
1120
+ `/bridges/${this.id}/addChannel?${queryParams}`
1121
+ );
1122
+ console.log(`Channels added to bridge ${this.id}`);
1123
+ } catch (error) {
1124
+ const message = getErrorMessage(error);
1125
+ console.error(`Error adding channels to bridge ${this.id}:`, message);
1126
+ throw new Error(`Failed to add channels: ${message}`);
1127
+ }
1128
+ }
1129
+ /**
1130
+ * Removes channels from the bridge.
1131
+ *
1132
+ * @param request - The RemoveChannelRequest object containing the channels to remove.
1133
+ * @throws An error if the operation fails.
1134
+ */
1135
+ async remove(request) {
1136
+ try {
1137
+ const queryParams = toQueryParams2({
1138
+ channel: Array.isArray(request.channel) ? request.channel.join(",") : request.channel
1139
+ });
1140
+ await this.baseClient.post(
1141
+ `/bridges/${this.id}/removeChannel?${queryParams}`
1142
+ );
1143
+ console.log(`Channels removed from bridge ${this.id}`);
1144
+ } catch (error) {
1145
+ const message = getErrorMessage(error);
1146
+ console.error(`Error removing channels from bridge ${this.id}:`, message);
1147
+ throw new Error(`Failed to remove channels: ${message}`);
1148
+ }
1149
+ }
1150
+ /**
1151
+ * Plays media on the bridge.
1152
+ *
1153
+ * @param request - The PlayMediaRequest object containing the media details to play.
1154
+ * @returns A Promise that resolves to a BridgePlayback object.
1155
+ * @throws An error if the operation fails.
1156
+ */
1157
+ async playMedia(request) {
1158
+ try {
1159
+ const queryParams = new URLSearchParams({
1160
+ ...request.lang && { lang: request.lang },
1161
+ ...request.offsetms && { offsetms: request.offsetms.toString() },
1162
+ ...request.skipms && { skipms: request.skipms.toString() },
1163
+ ...request.playbackId && { playbackId: request.playbackId }
1164
+ }).toString();
1165
+ const result = await this.baseClient.post(
1166
+ `/bridges/${this.id}/play?${queryParams}`,
1167
+ { media: request.media }
1168
+ );
1169
+ console.log(`Media playback started on bridge ${this.id}`);
1170
+ return result;
1171
+ } catch (error) {
1172
+ const message = getErrorMessage(error);
1173
+ console.error(`Error playing media on bridge ${this.id}:`, message);
1174
+ throw new Error(`Failed to play media: ${message}`);
1175
+ }
1176
+ }
1177
+ /**
1178
+ * Stops media playback on the bridge.
1179
+ *
1180
+ * @param playbackId - The ID of the playback to stop.
1181
+ * @throws An error if the operation fails.
1182
+ */
1183
+ async stopPlayback(playbackId) {
1184
+ try {
1185
+ await this.baseClient.delete(
1186
+ `/bridges/${this.id}/play/${playbackId}`
1187
+ );
1188
+ console.log(`Playback ${playbackId} stopped on bridge ${this.id}`);
1189
+ } catch (error) {
1190
+ const message = getErrorMessage(error);
1191
+ console.error(`Error stopping playback on bridge ${this.id}:`, message);
1192
+ throw new Error(`Failed to stop playback: ${message}`);
1193
+ }
1194
+ }
1195
+ /**
1196
+ * Sets the video source for the bridge.
1197
+ *
1198
+ * @param channelId - The ID of the channel to set as the video source.
1199
+ * @throws An error if the operation fails.
1200
+ */
1201
+ async setVideoSource(channelId) {
1202
+ try {
1203
+ await this.baseClient.post(
1204
+ `/bridges/${this.id}/videoSource/${channelId}`
1205
+ );
1206
+ console.log(`Video source set for bridge ${this.id}`);
1207
+ } catch (error) {
1208
+ const message = getErrorMessage(error);
1209
+ console.error(
1210
+ `Error setting video source for bridge ${this.id}:`,
1211
+ message
1212
+ );
1213
+ throw new Error(`Failed to set video source: ${message}`);
1214
+ }
1215
+ }
1216
+ /**
1217
+ * Removes the video source from the bridge.
1218
+ *
1219
+ * @throws An error if the operation fails.
1220
+ */
1221
+ async clearVideoSource() {
1222
+ try {
1223
+ await this.baseClient.delete(`/bridges/${this.id}/videoSource`);
1224
+ console.log(`Video source removed from bridge ${this.id}`);
1225
+ } catch (error) {
1226
+ const message = getErrorMessage(error);
1227
+ console.error(
1228
+ `Error removing video source from bridge ${this.id}:`,
1229
+ message
1230
+ );
1231
+ throw new Error(`Failed to remove video source: ${message}`);
1232
+ }
1233
+ }
1234
+ /**
1235
+ * Checks if the bridge has listeners for a specific event.
1236
+ *
1237
+ * @param event - The event type to check for listeners.
1238
+ * @returns A boolean indicating whether there are listeners for the event.
1239
+ */
1240
+ hasListeners(event) {
1241
+ return this.eventEmitter.listenerCount(event) > 0;
1242
+ }
1243
+ /**
1244
+ * Retrieves the current bridge data without making an API call.
1245
+ *
1246
+ * @returns The current Bridge object or null if no data is available.
1247
+ */
1248
+ getCurrentData() {
1249
+ return this.bridgeData;
1250
+ }
1251
+ };
938
1252
  var Bridges = class {
939
- constructor(client) {
1253
+ constructor(baseClient, client) {
1254
+ this.baseClient = baseClient;
940
1255
  this.client = client;
941
1256
  }
1257
+ bridgeInstances = /* @__PURE__ */ new Map();
1258
+ /**
1259
+ * Creates or retrieves a Bridge instance.
1260
+ *
1261
+ * This method manages the creation and retrieval of BridgeInstance objects.
1262
+ * If an ID is provided and an instance with that ID already exists, it returns the existing instance.
1263
+ * If an ID is provided but no instance exists, it creates a new instance with that ID.
1264
+ * If no ID is provided, it creates a new instance with a generated ID.
1265
+ *
1266
+ * @param {Object} params - The parameters for creating or retrieving a Bridge instance.
1267
+ * @param {string} [params.id] - Optional. The ID of the Bridge instance to create or retrieve.
1268
+ *
1269
+ * @returns {BridgeInstance} A BridgeInstance object, either newly created or retrieved from existing instances.
1270
+ *
1271
+ * @throws {Error} If there's an error in creating or retrieving the Bridge instance.
1272
+ */
1273
+ Bridge({ id }) {
1274
+ try {
1275
+ if (!id) {
1276
+ const instance = new BridgeInstance(this.client, this.baseClient);
1277
+ this.bridgeInstances.set(instance.id, instance);
1278
+ console.log(`New bridge instance created with ID: ${instance.id}`);
1279
+ return instance;
1280
+ }
1281
+ if (!this.bridgeInstances.has(id)) {
1282
+ const instance = new BridgeInstance(this.client, this.baseClient, id);
1283
+ this.bridgeInstances.set(id, instance);
1284
+ console.log(`New bridge instance created with provided ID: ${id}`);
1285
+ return instance;
1286
+ }
1287
+ console.log(`Returning existing bridge instance: ${id}`);
1288
+ return this.bridgeInstances.get(id);
1289
+ } catch (error) {
1290
+ const message = getErrorMessage(error);
1291
+ console.error(`Error creating/retrieving bridge instance:`, message);
1292
+ throw new Error(`Failed to manage bridge instance: ${message}`);
1293
+ }
1294
+ }
1295
+ /**
1296
+ * Removes a bridge instance from the collection of managed bridges.
1297
+ *
1298
+ * This function removes the specified bridge instance, cleans up its event listeners,
1299
+ * and logs the removal. If the bridge instance doesn't exist, it logs a warning.
1300
+ *
1301
+ * @param {string} bridgeId - The unique identifier of the bridge instance to be removed.
1302
+ * @throws {Error} Throws an error if the bridgeId is not provided.
1303
+ * @returns {void}
1304
+ */
1305
+ removeBridgeInstance(bridgeId) {
1306
+ if (!bridgeId) {
1307
+ throw new Error("ID da bridge \xE9 obrigat\xF3rio");
1308
+ }
1309
+ if (this.bridgeInstances.has(bridgeId)) {
1310
+ const instance = this.bridgeInstances.get(bridgeId);
1311
+ instance?.removeAllListeners();
1312
+ this.bridgeInstances.delete(bridgeId);
1313
+ console.log(`Inst\xE2ncia de bridge removida: ${bridgeId}`);
1314
+ } else {
1315
+ console.warn(`Tentativa de remover inst\xE2ncia inexistente: ${bridgeId}`);
1316
+ }
1317
+ }
1318
+ /**
1319
+ * Propagates a WebSocket event to a specific bridge instance.
1320
+ *
1321
+ * This function checks if the received event is valid and related to a bridge,
1322
+ * then emits the event to the corresponding bridge instance if it exists.
1323
+ *
1324
+ * @param {WebSocketEvent} event - The WebSocket event to be propagated.
1325
+ * This should be an object containing information about the event,
1326
+ * including the bridge ID and event type.
1327
+ *
1328
+ * @returns {void}
1329
+ *
1330
+ * @remarks
1331
+ * - If the event is invalid (null or undefined), a warning is logged and the function returns early.
1332
+ * - The function checks if the event is bridge-related and if the event type is included in the predefined bridge events.
1333
+ * - If a matching bridge instance is found, the event is emitted to that instance.
1334
+ * - If no matching bridge instance is found, a warning is logged.
1335
+ */
1336
+ propagateEventToBridge(event) {
1337
+ if (!event) {
1338
+ console.warn("Evento WebSocket inv\xE1lido recebido");
1339
+ return;
1340
+ }
1341
+ if ("bridge" in event && event.bridge?.id && bridgeEvents.includes(event.type)) {
1342
+ const instance = this.bridgeInstances.get(event.bridge.id);
1343
+ if (instance) {
1344
+ instance.emitEvent(event);
1345
+ console.log(
1346
+ `Evento propagado para bridge ${event.bridge.id}: ${event.type}`
1347
+ );
1348
+ } else {
1349
+ console.warn(
1350
+ `Nenhuma inst\xE2ncia encontrada para bridge ${event.bridge.id}`
1351
+ );
1352
+ }
1353
+ }
1354
+ }
942
1355
  /**
943
- * Lists all active bridges.
1356
+ * Lists all active bridges in the system.
1357
+ *
1358
+ * This asynchronous function retrieves a list of all currently active bridges
1359
+ * by making a GET request to the "/bridges" endpoint using the base client.
1360
+ *
1361
+ * @returns {Promise<Bridge[]>} A promise that resolves to an array of Bridge objects.
1362
+ * Each Bridge object represents an active bridge in the system.
1363
+ *
1364
+ * @throws {Error} If there's an error in fetching the bridges or if the request fails.
1365
+ *
1366
+ * @example
1367
+ * try {
1368
+ * const bridges = await bridgesInstance.list();
1369
+ * console.log('Active bridges:', bridges);
1370
+ * } catch (error) {
1371
+ * console.error('Failed to fetch bridges:', error);
1372
+ * }
944
1373
  */
945
1374
  async list() {
946
- return this.client.get("/bridges");
1375
+ return this.baseClient.get("/bridges");
947
1376
  }
948
1377
  /**
949
- * Creates a new bridge.
1378
+ * Creates a new bridge in the system.
1379
+ *
1380
+ * This asynchronous function sends a POST request to create a new bridge
1381
+ * using the provided configuration details.
1382
+ *
1383
+ * @param request - The configuration details for creating the new bridge.
1384
+ * @param request.type - The type of bridge to create (e.g., 'mixing', 'holding').
1385
+ * @param request.name - Optional. A custom name for the bridge.
1386
+ * @param request.bridgeId - Optional. A specific ID for the bridge. If not provided, one will be generated.
1387
+ *
1388
+ * @returns A Promise that resolves to a Bridge object representing the newly created bridge.
1389
+ * The Bridge object contains details such as id, technology, bridge_type, bridge_class, channels, etc.
1390
+ *
1391
+ * @throws Will throw an error if the bridge creation fails or if there's a network issue.
950
1392
  */
951
1393
  async createBridge(request) {
952
- return this.client.post("/bridges", request);
1394
+ return this.baseClient.post("/bridges", request);
953
1395
  }
954
1396
  /**
955
- * Retrieves details of a specific bridge.
1397
+ * Retrieves detailed information about a specific bridge.
1398
+ *
1399
+ * This asynchronous function fetches the complete details of a bridge
1400
+ * identified by its unique ID. It makes a GET request to the ARI endpoint
1401
+ * for the specified bridge.
1402
+ *
1403
+ * @param bridgeId - The unique identifier of the bridge to retrieve details for.
1404
+ * This should be a string that uniquely identifies the bridge in the system.
1405
+ *
1406
+ * @returns A Promise that resolves to a Bridge object containing all the details
1407
+ * of the specified bridge. This includes information such as the bridge's
1408
+ * ID, type, channels, and other relevant properties.
1409
+ *
1410
+ * @throws Will throw an error if the bridge cannot be found, if there's a network issue,
1411
+ * or if the server responds with an error.
956
1412
  */
957
- async getDetails(bridgeId) {
958
- return this.client.get(`/bridges/${bridgeId}`);
1413
+ async get(bridgeId) {
1414
+ return this.baseClient.get(`/bridges/${bridgeId}`);
959
1415
  }
960
1416
  /**
961
- * Destroys (deletes) a specific bridge.
1417
+ * Destroys (deletes) a specific bridge in the system.
1418
+ *
1419
+ * This asynchronous function sends a DELETE request to remove a bridge
1420
+ * identified by its unique ID. Once destroyed, the bridge and all its
1421
+ * associated resources are permanently removed from the system.
1422
+ *
1423
+ * @param bridgeId - The unique identifier of the bridge to be destroyed.
1424
+ * This should be a string that uniquely identifies the bridge in the system.
1425
+ *
1426
+ * @returns A Promise that resolves to void when the bridge is successfully destroyed.
1427
+ * If the operation is successful, the bridge no longer exists in the system.
1428
+ *
1429
+ * @throws Will throw an error if the bridge cannot be found, if there's a network issue,
1430
+ * or if the server responds with an error during the deletion process.
962
1431
  */
963
1432
  async destroy(bridgeId) {
964
- return this.client.delete(`/bridges/${bridgeId}`);
1433
+ return this.baseClient.delete(`/bridges/${bridgeId}`);
965
1434
  }
966
1435
  /**
967
- * Adds a channel or multiple channels to a bridge.
1436
+ * Adds one or more channels to a specified bridge.
1437
+ *
1438
+ * This asynchronous function sends a POST request to add channels to an existing bridge.
1439
+ * It can handle adding a single channel or multiple channels in one operation.
1440
+ *
1441
+ * @param bridgeId - The unique identifier of the bridge to which channels will be added.
1442
+ * @param request - An object containing the details of the channel(s) to be added.
1443
+ * @param request.channel - A single channel ID or an array of channel IDs to add to the bridge.
1444
+ * @param request.role - Optional. Specifies the role of the channel(s) in the bridge.
1445
+ *
1446
+ * @returns A Promise that resolves to void when the operation is successful.
1447
+ *
1448
+ * @throws Will throw an error if the request fails, such as if the bridge doesn't exist
1449
+ * or if there's a network issue.
968
1450
  */
969
1451
  async addChannels(bridgeId, request) {
970
- const queryParams = new URLSearchParams({
1452
+ const queryParams = toQueryParams2({
971
1453
  channel: Array.isArray(request.channel) ? request.channel.join(",") : request.channel,
972
1454
  ...request.role && { role: request.role }
973
- }).toString();
974
- await this.client.post(
1455
+ });
1456
+ await this.baseClient.post(
975
1457
  `/bridges/${bridgeId}/addChannel?${queryParams}`
976
1458
  );
977
1459
  }
978
1460
  /**
979
- * Removes a channel or multiple channels from a bridge.
1461
+ * Removes one or more channels from a specified bridge.
1462
+ *
1463
+ * This asynchronous function sends a POST request to remove channels from an existing bridge.
1464
+ * It can handle removing a single channel or multiple channels in one operation.
1465
+ *
1466
+ * @param bridgeId - The unique identifier of the bridge from which channels will be removed.
1467
+ * @param request - An object containing the details of the channel(s) to be removed.
1468
+ * @param request.channel - A single channel ID or an array of channel IDs to remove from the bridge.
1469
+ *
1470
+ * @returns A Promise that resolves to void when the operation is successful.
1471
+ *
1472
+ * @throws Will throw an error if the request fails, such as if the bridge doesn't exist,
1473
+ * if the channels are not in the bridge, or if there's a network issue.
980
1474
  */
981
1475
  async removeChannels(bridgeId, request) {
982
- const queryParams = new URLSearchParams({
1476
+ const queryParams = toQueryParams2({
983
1477
  channel: Array.isArray(request.channel) ? request.channel.join(",") : request.channel
984
- }).toString();
985
- await this.client.post(
1478
+ });
1479
+ await this.baseClient.post(
986
1480
  `/bridges/${bridgeId}/removeChannel?${queryParams}`
987
1481
  );
988
1482
  }
989
1483
  /**
990
- * Plays media to a bridge.
1484
+ * Plays media on a specified bridge.
1485
+ *
1486
+ * This asynchronous function initiates media playback on a bridge identified by its ID.
1487
+ * It allows for customization of the playback through various options in the request.
1488
+ *
1489
+ * @param bridgeId - The unique identifier of the bridge on which to play the media.
1490
+ * @param request - An object containing the media playback request details.
1491
+ * @param request.media - The media to be played (e.g., sound file, URL).
1492
+ * @param request.lang - Optional. The language of the media content.
1493
+ * @param request.offsetms - Optional. The offset in milliseconds to start playing from.
1494
+ * @param request.skipms - Optional. The number of milliseconds to skip before playing.
1495
+ * @param request.playbackId - Optional. A custom ID for the playback session.
1496
+ *
1497
+ * @returns A Promise that resolves to a BridgePlayback object, containing details about the initiated playback.
1498
+ *
1499
+ * @throws Will throw an error if the playback request fails or if there's a network issue.
991
1500
  */
992
1501
  async playMedia(bridgeId, request) {
993
- const queryParams = new URLSearchParams({
1502
+ const queryParams = toQueryParams2({
994
1503
  ...request.lang && { lang: request.lang },
995
1504
  ...request.offsetms && { offsetms: request.offsetms.toString() },
996
1505
  ...request.skipms && { skipms: request.skipms.toString() },
997
1506
  ...request.playbackId && { playbackId: request.playbackId }
998
- }).toString();
999
- return this.client.post(
1507
+ });
1508
+ return this.baseClient.post(
1000
1509
  `/bridges/${bridgeId}/play?${queryParams}`,
1001
1510
  { media: request.media }
1002
1511
  );
1003
1512
  }
1004
1513
  /**
1005
- * Stops media playback on a bridge.
1514
+ * Stops media playback on a specified bridge.
1515
+ *
1516
+ * This asynchronous function sends a DELETE request to stop the playback of media
1517
+ * on a bridge identified by its ID and a specific playback session.
1518
+ *
1519
+ * @param bridgeId - The unique identifier of the bridge where the playback is to be stopped.
1520
+ * @param playbackId - The unique identifier of the playback session to be stopped.
1521
+ *
1522
+ * @returns A Promise that resolves to void when the playback is successfully stopped.
1523
+ *
1524
+ * @throws Will throw an error if the request fails, such as if the bridge or playback session
1525
+ * doesn't exist, or if there's a network issue.
1006
1526
  */
1007
1527
  async stopPlayback(bridgeId, playbackId) {
1008
- await this.client.delete(`/bridges/${bridgeId}/play/${playbackId}`);
1528
+ await this.baseClient.delete(
1529
+ `/bridges/${bridgeId}/play/${playbackId}`
1530
+ );
1009
1531
  }
1010
1532
  /**
1011
- * Sets the video source for a bridge.
1533
+ * Sets the video source for a specified bridge.
1534
+ *
1535
+ * This asynchronous function configures a channel as the video source for a given bridge.
1536
+ * It sends a POST request to the ARI endpoint to update the bridge's video source.
1537
+ *
1538
+ * @param bridgeId - The unique identifier of the bridge for which to set the video source.
1539
+ * @param channelId - The unique identifier of the channel to be set as the video source.
1540
+ *
1541
+ * @returns A Promise that resolves to void when the video source is successfully set.
1542
+ *
1543
+ * @throws Will throw an error if the request fails, such as if the bridge or channel
1544
+ * doesn't exist, or if there's a network issue.
1012
1545
  */
1013
1546
  async setVideoSource(bridgeId, channelId) {
1014
- await this.client.post(
1015
- `/bridges/${bridgeId}/videoSource?channelId=${encodeURIComponent(channelId)}`
1547
+ const queryParams = toQueryParams2({ channelId });
1548
+ await this.baseClient.post(
1549
+ `/bridges/${bridgeId}/videoSource?${queryParams}`
1016
1550
  );
1017
1551
  }
1018
1552
  /**
1019
- * Clears the video source for a bridge.
1553
+ * Clears the video source for a specified bridge.
1554
+ *
1555
+ * This asynchronous function removes the currently set video source from a bridge.
1556
+ * It sends a DELETE request to the ARI endpoint to clear the video source configuration.
1557
+ *
1558
+ * @param bridgeId - The unique identifier of the bridge from which to clear the video source.
1559
+ * This should be a string that uniquely identifies the bridge in the system.
1560
+ *
1561
+ * @returns A Promise that resolves to void when the video source is successfully cleared.
1562
+ * If the operation is successful, the bridge will no longer have a designated video source.
1563
+ *
1564
+ * @throws Will throw an error if the request fails, such as if the bridge doesn't exist,
1565
+ * if there's no video source set, or if there's a network issue.
1020
1566
  */
1021
1567
  async clearVideoSource(bridgeId) {
1022
- await this.client.delete(`/bridges/${bridgeId}/videoSource`);
1568
+ await this.baseClient.delete(`/bridges/${bridgeId}/videoSource`);
1569
+ }
1570
+ /**
1571
+ * Retrieves the count of active bridge instances.
1572
+ *
1573
+ * This function returns the total number of bridge instances currently
1574
+ * managed by the Bridges class. It provides a quick way to check how many
1575
+ * active bridges are present in the system.
1576
+ *
1577
+ * @returns {number} The count of active bridge instances.
1578
+ */
1579
+ getInstanceCount() {
1580
+ return this.bridgeInstances.size;
1581
+ }
1582
+ /**
1583
+ * Checks if a bridge instance exists in the collection of managed bridges.
1584
+ *
1585
+ * This function verifies whether a bridge instance with the specified ID
1586
+ * is currently being managed by the Bridges class.
1587
+ *
1588
+ * @param bridgeId - The unique identifier of the bridge instance to check.
1589
+ * This should be a string that uniquely identifies the bridge in the system.
1590
+ *
1591
+ * @returns A boolean value indicating whether the bridge instance exists.
1592
+ * Returns true if the bridge instance is found, false otherwise.
1593
+ */
1594
+ hasInstance(bridgeId) {
1595
+ return this.bridgeInstances.has(bridgeId);
1596
+ }
1597
+ /**
1598
+ * Retrieves all active bridge instances currently managed by the Bridges class.
1599
+ *
1600
+ * This method provides a way to access all the BridgeInstance objects that are
1601
+ * currently active and being managed. It returns a new Map to prevent direct
1602
+ * modification of the internal bridgeInstances collection.
1603
+ *
1604
+ * @returns A new Map object containing all active bridge instances, where the keys
1605
+ * are the bridge IDs (strings) and the values are the corresponding
1606
+ * BridgeInstance objects. If no bridges are active, an empty Map is returned.
1607
+ */
1608
+ getAllInstances() {
1609
+ return new Map(this.bridgeInstances);
1023
1610
  }
1024
1611
  };
1025
1612
 
1026
1613
  // src/ari-client/resources/channels.ts
1027
- var import_events = require("events");
1028
- var import_axios2 = require("axios");
1614
+ var import_events3 = require("events");
1615
+ var import_axios3 = require("axios");
1029
1616
 
1030
1617
  // node_modules/uuid/dist/esm/stringify.js
1031
1618
  var byteToHex = [];
@@ -1072,16 +1659,9 @@ function v4(options, buf, offset) {
1072
1659
  }
1073
1660
  var v4_default = v4;
1074
1661
 
1075
- // src/ari-client/utils.ts
1076
- function toQueryParams2(options) {
1077
- return new URLSearchParams(
1078
- Object.entries(options).filter(([, value]) => value !== void 0).map(([key, value]) => [key, value])
1079
- ).toString();
1080
- }
1081
-
1082
1662
  // src/ari-client/resources/channels.ts
1083
- var getErrorMessage = (error) => {
1084
- if ((0, import_axios2.isAxiosError)(error)) {
1663
+ var getErrorMessage2 = (error) => {
1664
+ if ((0, import_axios3.isAxiosError)(error)) {
1085
1665
  return error.response?.data?.message || error.message || "An axios error occurred";
1086
1666
  }
1087
1667
  if (error instanceof Error) {
@@ -1096,7 +1676,7 @@ var ChannelInstance = class {
1096
1676
  this.id = channelId || `channel-${Date.now()}`;
1097
1677
  console.log(`Channel instance initialized with ID: ${this.id}`);
1098
1678
  }
1099
- eventEmitter = new import_events.EventEmitter();
1679
+ eventEmitter = new import_events3.EventEmitter();
1100
1680
  channelData = null;
1101
1681
  id;
1102
1682
  /**
@@ -1185,7 +1765,7 @@ var ChannelInstance = class {
1185
1765
  await this.baseClient.post(`/channels/${this.id}/answer`);
1186
1766
  console.log(`Channel ${this.id} answered`);
1187
1767
  } catch (error) {
1188
- const message = getErrorMessage(error);
1768
+ const message = getErrorMessage2(error);
1189
1769
  console.error(`Error answering channel ${this.id}:`, message);
1190
1770
  throw new Error(`Failed to answer channel: ${message}`);
1191
1771
  }
@@ -1211,7 +1791,7 @@ var ChannelInstance = class {
1211
1791
  );
1212
1792
  return this.channelData;
1213
1793
  } catch (error) {
1214
- const message = getErrorMessage(error);
1794
+ const message = getErrorMessage2(error);
1215
1795
  console.error(`Error originating channel:`, message);
1216
1796
  throw new Error(`Failed to originate channel: ${message}`);
1217
1797
  }
@@ -1236,7 +1816,7 @@ var ChannelInstance = class {
1236
1816
  console.log(`Media playback started on channel ${this.id}`);
1237
1817
  return playback;
1238
1818
  } catch (error) {
1239
- const message = getErrorMessage(error);
1819
+ const message = getErrorMessage2(error);
1240
1820
  console.error(`Error playing media on channel ${this.id}:`, message);
1241
1821
  throw new Error(`Failed to play media: ${message}`);
1242
1822
  }
@@ -1259,7 +1839,7 @@ var ChannelInstance = class {
1259
1839
  console.log(`Retrieved channel details for ${this.id}`);
1260
1840
  return details;
1261
1841
  } catch (error) {
1262
- const message = getErrorMessage(error);
1842
+ const message = getErrorMessage2(error);
1263
1843
  console.error(
1264
1844
  `Error retrieving channel details for ${this.id}:`,
1265
1845
  message
@@ -1504,7 +2084,7 @@ var Channels = class {
1504
2084
  console.log(`Returning existing channel instance: ${id}`);
1505
2085
  return this.channelInstances.get(id);
1506
2086
  } catch (error) {
1507
- const message = getErrorMessage(error);
2087
+ const message = getErrorMessage2(error);
1508
2088
  console.error(`Error creating/retrieving channel instance:`, message);
1509
2089
  throw new Error(`Failed to manage channel instance: ${message}`);
1510
2090
  }
@@ -1525,7 +2105,7 @@ var Channels = class {
1525
2105
  console.log(`Retrieved channel details for ${id}`);
1526
2106
  return details;
1527
2107
  } catch (error) {
1528
- const message = getErrorMessage(error);
2108
+ const message = getErrorMessage2(error);
1529
2109
  console.error(`Error retrieving channel details for ${id}:`, message);
1530
2110
  throw new Error(`Failed to get channel details: ${message}`);
1531
2111
  }
@@ -1578,7 +2158,7 @@ var Channels = class {
1578
2158
  console.log(`Channel originated successfully with ID: ${channel.id}`);
1579
2159
  return channel;
1580
2160
  } catch (error) {
1581
- const message = getErrorMessage(error);
2161
+ const message = getErrorMessage2(error);
1582
2162
  console.error(`Error originating channel:`, message);
1583
2163
  throw new Error(`Failed to originate channel: ${message}`);
1584
2164
  }
@@ -1595,7 +2175,7 @@ var Channels = class {
1595
2175
  console.log(`Retrieved ${channels.length} active channels`);
1596
2176
  return channels;
1597
2177
  } catch (error) {
1598
- const message = getErrorMessage(error);
2178
+ const message = getErrorMessage2(error);
1599
2179
  console.error(`Error listing channels:`, message);
1600
2180
  throw new Error(`Failed to list channels: ${message}`);
1601
2181
  }
@@ -2022,10 +2602,10 @@ var Endpoints = class {
2022
2602
  };
2023
2603
 
2024
2604
  // src/ari-client/resources/playbacks.ts
2025
- var import_events2 = require("events");
2026
- var import_axios3 = require("axios");
2027
- var getErrorMessage2 = (error) => {
2028
- if ((0, import_axios3.isAxiosError)(error)) {
2605
+ var import_events4 = require("events");
2606
+ var import_axios4 = require("axios");
2607
+ var getErrorMessage3 = (error) => {
2608
+ if ((0, import_axios4.isAxiosError)(error)) {
2029
2609
  return error.response?.data?.message || error.message || "An axios error occurred";
2030
2610
  }
2031
2611
  if (error instanceof Error) {
@@ -2048,7 +2628,7 @@ var PlaybackInstance = class {
2048
2628
  this.id = playbackId;
2049
2629
  console.log(`PlaybackInstance initialized with ID: ${this.id}`);
2050
2630
  }
2051
- eventEmitter = new import_events2.EventEmitter();
2631
+ eventEmitter = new import_events4.EventEmitter();
2052
2632
  playbackData = null;
2053
2633
  id;
2054
2634
  /**
@@ -2143,7 +2723,7 @@ var PlaybackInstance = class {
2143
2723
  console.log(`Retrieved playback data for ${this.id}`);
2144
2724
  return this.playbackData;
2145
2725
  } catch (error) {
2146
- const message = getErrorMessage2(error);
2726
+ const message = getErrorMessage3(error);
2147
2727
  console.error(`Error retrieving playback data for ${this.id}:`, message);
2148
2728
  throw new Error(`Failed to get playback data: ${message}`);
2149
2729
  }
@@ -2166,7 +2746,7 @@ var PlaybackInstance = class {
2166
2746
  `Operation ${operation} executed successfully on playback ${this.id}`
2167
2747
  );
2168
2748
  } catch (error) {
2169
- const message = getErrorMessage2(error);
2749
+ const message = getErrorMessage3(error);
2170
2750
  console.error(`Error controlling playback ${this.id}:`, message);
2171
2751
  throw new Error(`Failed to control playback: ${message}`);
2172
2752
  }
@@ -2184,7 +2764,7 @@ var PlaybackInstance = class {
2184
2764
  await this.baseClient.delete(`/playbacks/${this.id}`);
2185
2765
  console.log(`Playback ${this.id} stopped successfully`);
2186
2766
  } catch (error) {
2187
- const message = getErrorMessage2(error);
2767
+ const message = getErrorMessage3(error);
2188
2768
  console.error(`Error stopping playback ${this.id}:`, message);
2189
2769
  throw new Error(`Failed to stop playback: ${message}`);
2190
2770
  }
@@ -2244,7 +2824,7 @@ var Playbacks = class {
2244
2824
  console.log(`Returning existing playback instance: ${id}`);
2245
2825
  return this.playbackInstances.get(id);
2246
2826
  } catch (error) {
2247
- const message = getErrorMessage2(error);
2827
+ const message = getErrorMessage3(error);
2248
2828
  console.error(`Error creating/retrieving playback instance:`, message);
2249
2829
  throw new Error(`Failed to manage playback instance: ${message}`);
2250
2830
  }
@@ -2301,7 +2881,7 @@ var Playbacks = class {
2301
2881
  try {
2302
2882
  return await this.baseClient.get(`/playbacks/${playbackId}`);
2303
2883
  } catch (error) {
2304
- const message = getErrorMessage2(error);
2884
+ const message = getErrorMessage3(error);
2305
2885
  console.error(`Error getting playback details ${playbackId}:`, message);
2306
2886
  throw new Error(`Failed to get playback details: ${message}`);
2307
2887
  }
@@ -2321,7 +2901,7 @@ var Playbacks = class {
2321
2901
  await playback.control(operation);
2322
2902
  console.log(`Operation ${operation} executed on playback ${playbackId}`);
2323
2903
  } catch (error) {
2324
- const message = getErrorMessage2(error);
2904
+ const message = getErrorMessage3(error);
2325
2905
  console.error(`Error controlling playback ${playbackId}:`, message);
2326
2906
  throw new Error(`Failed to control playback: ${message}`);
2327
2907
  }
@@ -2340,7 +2920,7 @@ var Playbacks = class {
2340
2920
  await playback.stop();
2341
2921
  console.log(`Playback ${playbackId} stopped`);
2342
2922
  } catch (error) {
2343
- const message = getErrorMessage2(error);
2923
+ const message = getErrorMessage3(error);
2344
2924
  console.error(`Error stopping playback ${playbackId}:`, message);
2345
2925
  throw new Error(`Failed to stop playback: ${message}`);
2346
2926
  }
@@ -2394,13 +2974,13 @@ var Sounds = class {
2394
2974
  };
2395
2975
 
2396
2976
  // src/ari-client/websocketClient.ts
2397
- var import_events3 = require("events");
2977
+ var import_events5 = require("events");
2398
2978
  var import_exponential_backoff = __toESM(require_backoff(), 1);
2399
2979
  var import_ws = __toESM(require("ws"), 1);
2400
2980
  var DEFAULT_MAX_RECONNECT_ATTEMPTS = 10;
2401
2981
  var DEFAULT_STARTING_DELAY = 500;
2402
2982
  var DEFAULT_MAX_DELAY = 1e4;
2403
- var WebSocketClient = class extends import_events3.EventEmitter {
2983
+ var WebSocketClient = class extends import_events5.EventEmitter {
2404
2984
  /**
2405
2985
  * Creates a new WebSocket client instance.
2406
2986
  *
@@ -2520,10 +3100,18 @@ var WebSocketClient = class extends import_events3.EventEmitter {
2520
3100
  instancePlayback.emitEvent(event);
2521
3101
  event.instancePlayback = instancePlayback;
2522
3102
  }
3103
+ if ("bridge" in event && event.bridge?.id && this.ariClient) {
3104
+ const instanceBridge = this.ariClient.Bridge(event.bridge.id);
3105
+ instanceBridge.emitEvent(event);
3106
+ event.instanceBridge = instanceBridge;
3107
+ }
2523
3108
  this.emit(event.type, event);
2524
3109
  console.log(`Event processed: ${event.type}`);
2525
3110
  } catch (error) {
2526
- console.error("Error processing WebSocket message:", error instanceof Error ? error.message : "Unknown error");
3111
+ console.error(
3112
+ "Error processing WebSocket message:",
3113
+ error instanceof Error ? error.message : "Unknown error"
3114
+ );
2527
3115
  this.emit("error", new Error("Failed to decode WebSocket message"));
2528
3116
  }
2529
3117
  }
@@ -2536,13 +3124,15 @@ var WebSocketClient = class extends import_events3.EventEmitter {
2536
3124
  this.isReconnecting = true;
2537
3125
  console.log("Initiating reconnection attempt...");
2538
3126
  this.removeAllListeners();
2539
- (0, import_exponential_backoff.backOff)(() => this.initializeWebSocket(wsUrl), this.backOffOptions).catch((error) => {
2540
- console.error(
2541
- "Failed to reconnect after multiple attempts:",
2542
- error instanceof Error ? error.message : "Unknown error"
2543
- );
2544
- this.emit("reconnectFailed", error);
2545
- });
3127
+ (0, import_exponential_backoff.backOff)(() => this.initializeWebSocket(wsUrl), this.backOffOptions).catch(
3128
+ (error) => {
3129
+ console.error(
3130
+ "Failed to reconnect after multiple attempts:",
3131
+ error instanceof Error ? error.message : "Unknown error"
3132
+ );
3133
+ this.emit("reconnectFailed", error);
3134
+ }
3135
+ );
2546
3136
  }
2547
3137
  /**
2548
3138
  * Manually closes the WebSocket connection.
@@ -2598,11 +3188,11 @@ var AriClient = class {
2598
3188
  this.baseClient = new BaseClient(baseUrl, config.username, config.password);
2599
3189
  this.channels = new Channels(this.baseClient, this);
2600
3190
  this.playbacks = new Playbacks(this.baseClient, this);
3191
+ this.bridges = new Bridges(this.baseClient, this);
2601
3192
  this.endpoints = new Endpoints(this.baseClient);
2602
3193
  this.applications = new Applications(this.baseClient);
2603
3194
  this.sounds = new Sounds(this.baseClient);
2604
3195
  this.asterisk = new Asterisk(this.baseClient);
2605
- this.bridges = new Bridges(this.baseClient);
2606
3196
  console.log(`ARI Client initialized with base URL: ${baseUrl}`);
2607
3197
  }
2608
3198
  baseClient;
@@ -2718,6 +3308,20 @@ var AriClient = class {
2718
3308
  Playback(playbackId, _app) {
2719
3309
  return this.playbacks.Playback({ id: playbackId });
2720
3310
  }
3311
+ /**
3312
+ * Creates or retrieves a Bridge instance.
3313
+ *
3314
+ * This function allows you to create a new Bridge instance or retrieve an existing one
3315
+ * based on the provided bridge ID.
3316
+ *
3317
+ * @param {string} [bridgeId] - Optional ID of an existing bridge. If provided, retrieves the
3318
+ * existing bridge with this ID. If omitted, creates a new bridge.
3319
+ * @returns {BridgeInstance} A new or existing Bridge instance that can be used to interact
3320
+ * with the Asterisk bridge.
3321
+ */
3322
+ Bridge(bridgeId) {
3323
+ return this.bridges.Bridge({ id: bridgeId });
3324
+ }
2721
3325
  /**
2722
3326
  * Gets the current WebSocket connection status.
2723
3327
  *
@@ -2732,6 +3336,7 @@ var AriClient = class {
2732
3336
  Applications,
2733
3337
  AriClient,
2734
3338
  Asterisk,
3339
+ BridgeInstance,
2735
3340
  Bridges,
2736
3341
  ChannelInstance,
2737
3342
  Channels,