@ipcom/asterisk-ari 0.0.144 → 0.0.145-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,
@@ -631,6 +632,7 @@ var BaseClient = class {
631
632
  }
632
633
  });
633
634
  this.addInterceptors();
635
+ console.log(`BaseClient initialized for ${baseUrl}`);
634
636
  }
635
637
  client;
636
638
  /**
@@ -655,6 +657,7 @@ var BaseClient = class {
655
657
  addInterceptors() {
656
658
  this.client.interceptors.request.use(
657
659
  (config) => {
660
+ console.log(`[Request] ${config.method?.toUpperCase()} ${config.url}`);
658
661
  return config;
659
662
  },
660
663
  (error) => {
@@ -665,6 +668,7 @@ var BaseClient = class {
665
668
  );
666
669
  this.client.interceptors.response.use(
667
670
  (response) => {
671
+ console.log(`[Response] ${response.status} ${response.config.url}`);
668
672
  return response;
669
673
  },
670
674
  (error) => {
@@ -787,6 +791,7 @@ var BaseClient = class {
787
791
  ...this.client.defaults.headers.common,
788
792
  ...headers
789
793
  };
794
+ console.log("Updated client headers");
790
795
  }
791
796
  /**
792
797
  * Gets the current request timeout setting.
@@ -799,6 +804,7 @@ var BaseClient = class {
799
804
  */
800
805
  setTimeout(timeout) {
801
806
  this.client.defaults.timeout = timeout;
807
+ console.log(`Updated timeout to ${timeout}ms`);
802
808
  }
803
809
  };
804
810
 
@@ -809,7 +815,7 @@ var Applications = class {
809
815
  }
810
816
  /**
811
817
  * Lists all applications.
812
- *
818
+ *
813
819
  * @returns A promise that resolves to an array of Application objects.
814
820
  * @throws {Error} If the API response is not an array.
815
821
  */
@@ -822,7 +828,7 @@ var Applications = class {
822
828
  }
823
829
  /**
824
830
  * Retrieves details of a specific application.
825
- *
831
+ *
826
832
  * @param appName - The name of the application to retrieve details for.
827
833
  * @returns A promise that resolves to an ApplicationDetails object.
828
834
  * @throws {Error} If there's an error fetching the application details.
@@ -839,7 +845,7 @@ var Applications = class {
839
845
  }
840
846
  /**
841
847
  * Sends a message to a specific application.
842
- *
848
+ *
843
849
  * @param appName - The name of the application to send the message to.
844
850
  * @param body - The message to be sent, containing an event and optional data.
845
851
  * @returns A promise that resolves when the message is successfully sent.
@@ -932,97 +938,681 @@ var Asterisk = class {
932
938
  };
933
939
 
934
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
+ };
935
1252
  var Bridges = class {
936
- constructor(client) {
1253
+ constructor(baseClient, client) {
1254
+ this.baseClient = baseClient;
937
1255
  this.client = client;
938
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
+ }
939
1318
  /**
940
- * Lists all active bridges.
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
+ }
1355
+ /**
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
+ * }
941
1373
  */
942
1374
  async list() {
943
- return this.client.get("/bridges");
1375
+ return this.baseClient.get("/bridges");
944
1376
  }
945
1377
  /**
946
- * 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.
947
1392
  */
948
1393
  async createBridge(request) {
949
- return this.client.post("/bridges", request);
1394
+ return this.baseClient.post("/bridges", request);
950
1395
  }
951
1396
  /**
952
- * 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.
953
1412
  */
954
- async getDetails(bridgeId) {
955
- return this.client.get(`/bridges/${bridgeId}`);
1413
+ async get(bridgeId) {
1414
+ return this.baseClient.get(`/bridges/${bridgeId}`);
956
1415
  }
957
1416
  /**
958
- * 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.
959
1431
  */
960
1432
  async destroy(bridgeId) {
961
- return this.client.delete(`/bridges/${bridgeId}`);
1433
+ return this.baseClient.delete(`/bridges/${bridgeId}`);
962
1434
  }
963
1435
  /**
964
- * 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.
965
1450
  */
966
1451
  async addChannels(bridgeId, request) {
967
- const queryParams = new URLSearchParams({
1452
+ const queryParams = toQueryParams2({
968
1453
  channel: Array.isArray(request.channel) ? request.channel.join(",") : request.channel,
969
1454
  ...request.role && { role: request.role }
970
- }).toString();
971
- await this.client.post(
1455
+ });
1456
+ await this.baseClient.post(
972
1457
  `/bridges/${bridgeId}/addChannel?${queryParams}`
973
1458
  );
974
1459
  }
975
1460
  /**
976
- * 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.
977
1474
  */
978
1475
  async removeChannels(bridgeId, request) {
979
- const queryParams = new URLSearchParams({
1476
+ const queryParams = toQueryParams2({
980
1477
  channel: Array.isArray(request.channel) ? request.channel.join(",") : request.channel
981
- }).toString();
982
- await this.client.post(
1478
+ });
1479
+ await this.baseClient.post(
983
1480
  `/bridges/${bridgeId}/removeChannel?${queryParams}`
984
1481
  );
985
1482
  }
986
1483
  /**
987
- * 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.
988
1500
  */
989
1501
  async playMedia(bridgeId, request) {
990
- const queryParams = new URLSearchParams({
1502
+ const queryParams = toQueryParams2({
991
1503
  ...request.lang && { lang: request.lang },
992
1504
  ...request.offsetms && { offsetms: request.offsetms.toString() },
993
1505
  ...request.skipms && { skipms: request.skipms.toString() },
994
1506
  ...request.playbackId && { playbackId: request.playbackId }
995
- }).toString();
996
- return this.client.post(
1507
+ });
1508
+ return this.baseClient.post(
997
1509
  `/bridges/${bridgeId}/play?${queryParams}`,
998
1510
  { media: request.media }
999
1511
  );
1000
1512
  }
1001
1513
  /**
1002
- * 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.
1003
1526
  */
1004
1527
  async stopPlayback(bridgeId, playbackId) {
1005
- await this.client.delete(`/bridges/${bridgeId}/play/${playbackId}`);
1528
+ await this.baseClient.delete(
1529
+ `/bridges/${bridgeId}/play/${playbackId}`
1530
+ );
1006
1531
  }
1007
1532
  /**
1008
- * 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.
1009
1545
  */
1010
1546
  async setVideoSource(bridgeId, channelId) {
1011
- await this.client.post(
1012
- `/bridges/${bridgeId}/videoSource?channelId=${encodeURIComponent(channelId)}`
1547
+ const queryParams = toQueryParams2({ channelId });
1548
+ await this.baseClient.post(
1549
+ `/bridges/${bridgeId}/videoSource?${queryParams}`
1013
1550
  );
1014
1551
  }
1015
1552
  /**
1016
- * 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.
1017
1566
  */
1018
1567
  async clearVideoSource(bridgeId) {
1019
- 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);
1020
1610
  }
1021
1611
  };
1022
1612
 
1023
1613
  // src/ari-client/resources/channels.ts
1024
- var import_events = require("events");
1025
- var import_axios2 = require("axios");
1614
+ var import_events3 = require("events");
1615
+ var import_axios3 = require("axios");
1026
1616
 
1027
1617
  // node_modules/uuid/dist/esm/stringify.js
1028
1618
  var byteToHex = [];
@@ -1069,16 +1659,9 @@ function v4(options, buf, offset) {
1069
1659
  }
1070
1660
  var v4_default = v4;
1071
1661
 
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
1662
  // src/ari-client/resources/channels.ts
1080
- var getErrorMessage = (error) => {
1081
- if ((0, import_axios2.isAxiosError)(error)) {
1663
+ var getErrorMessage2 = (error) => {
1664
+ if ((0, import_axios3.isAxiosError)(error)) {
1082
1665
  return error.response?.data?.message || error.message || "An axios error occurred";
1083
1666
  }
1084
1667
  if (error instanceof Error) {
@@ -1091,8 +1674,9 @@ var ChannelInstance = class {
1091
1674
  this.client = client;
1092
1675
  this.baseClient = baseClient;
1093
1676
  this.id = channelId || `channel-${Date.now()}`;
1677
+ console.log(`Channel instance initialized with ID: ${this.id}`);
1094
1678
  }
1095
- eventEmitter = new import_events.EventEmitter();
1679
+ eventEmitter = new import_events3.EventEmitter();
1096
1680
  channelData = null;
1097
1681
  id;
1098
1682
  /**
@@ -1108,6 +1692,7 @@ var ChannelInstance = class {
1108
1692
  }
1109
1693
  };
1110
1694
  this.eventEmitter.on(event, wrappedListener);
1695
+ console.log(`Event listener registered for ${event} on channel ${this.id}`);
1111
1696
  }
1112
1697
  /**
1113
1698
  * Registers a one-time event listener
@@ -1122,6 +1707,9 @@ var ChannelInstance = class {
1122
1707
  }
1123
1708
  };
1124
1709
  this.eventEmitter.once(event, wrappedListener);
1710
+ console.log(
1711
+ `One-time event listener registered for ${event} on channel ${this.id}`
1712
+ );
1125
1713
  }
1126
1714
  /**
1127
1715
  * Removes event listener(s) for a specific WebSocket event type.
@@ -1138,8 +1726,12 @@ var ChannelInstance = class {
1138
1726
  }
1139
1727
  if (listener) {
1140
1728
  this.eventEmitter.off(event, listener);
1729
+ console.log(
1730
+ `Specific listener removed for ${event} on channel ${this.id}`
1731
+ );
1141
1732
  } else {
1142
1733
  this.eventEmitter.removeAllListeners(event);
1734
+ console.log(`All listeners removed for ${event} on channel ${this.id}`);
1143
1735
  }
1144
1736
  }
1145
1737
  /**
@@ -1152,6 +1744,7 @@ var ChannelInstance = class {
1152
1744
  }
1153
1745
  if ("channel" in event && event.channel?.id === this.id) {
1154
1746
  this.eventEmitter.emit(event.type, event);
1747
+ console.log(`Event ${event.type} emitted for channel ${this.id}`);
1155
1748
  }
1156
1749
  }
1157
1750
  /**
@@ -1161,6 +1754,7 @@ var ChannelInstance = class {
1161
1754
  * @return {void} This method does not return a value.
1162
1755
  */
1163
1756
  removeAllListeners() {
1757
+ console.log(`Removendo todos os listeners para o canal ${this.id}`);
1164
1758
  this.eventEmitter.removeAllListeners();
1165
1759
  }
1166
1760
  /**
@@ -1169,8 +1763,9 @@ var ChannelInstance = class {
1169
1763
  async answer() {
1170
1764
  try {
1171
1765
  await this.baseClient.post(`/channels/${this.id}/answer`);
1766
+ console.log(`Channel ${this.id} answered`);
1172
1767
  } catch (error) {
1173
- const message = getErrorMessage(error);
1768
+ const message = getErrorMessage2(error);
1174
1769
  console.error(`Error answering channel ${this.id}:`, message);
1175
1770
  throw new Error(`Failed to answer channel: ${message}`);
1176
1771
  }
@@ -1191,9 +1786,12 @@ var ChannelInstance = class {
1191
1786
  "/channels",
1192
1787
  data
1193
1788
  );
1789
+ console.log(
1790
+ `Channel originated successfully with ID: ${this.channelData.id}`
1791
+ );
1194
1792
  return this.channelData;
1195
1793
  } catch (error) {
1196
- const message = getErrorMessage(error);
1794
+ const message = getErrorMessage2(error);
1197
1795
  console.error(`Error originating channel:`, message);
1198
1796
  throw new Error(`Failed to originate channel: ${message}`);
1199
1797
  }
@@ -1207,6 +1805,7 @@ var ChannelInstance = class {
1207
1805
  }
1208
1806
  try {
1209
1807
  if (!this.channelData) {
1808
+ console.log("Initializing channel details...");
1210
1809
  this.channelData = await this.getDetails();
1211
1810
  }
1212
1811
  const playback = this.client.Playback(playbackId || v4_default());
@@ -1214,9 +1813,10 @@ var ChannelInstance = class {
1214
1813
  `/channels/${this.id}/play/${playback.id}`,
1215
1814
  options
1216
1815
  );
1816
+ console.log(`Media playback started on channel ${this.id}`);
1217
1817
  return playback;
1218
1818
  } catch (error) {
1219
- const message = getErrorMessage(error);
1819
+ const message = getErrorMessage2(error);
1220
1820
  console.error(`Error playing media on channel ${this.id}:`, message);
1221
1821
  throw new Error(`Failed to play media: ${message}`);
1222
1822
  }
@@ -1236,9 +1836,10 @@ var ChannelInstance = class {
1236
1836
  `/channels/${this.id}`
1237
1837
  );
1238
1838
  this.channelData = details;
1839
+ console.log(`Retrieved channel details for ${this.id}`);
1239
1840
  return details;
1240
1841
  } catch (error) {
1241
- const message = getErrorMessage(error);
1842
+ const message = getErrorMessage2(error);
1242
1843
  console.error(
1243
1844
  `Error retrieving channel details for ${this.id}:`,
1244
1845
  message
@@ -1282,6 +1883,7 @@ var ChannelInstance = class {
1282
1883
  */
1283
1884
  async hangup() {
1284
1885
  if (!this.channelData) {
1886
+ console.log("Canal n\xE3o inicializado, buscando detalhes...");
1285
1887
  this.channelData = await this.getDetails();
1286
1888
  }
1287
1889
  if (!this.channelData?.id) {
@@ -1470,16 +2072,19 @@ var Channels = class {
1470
2072
  if (!id) {
1471
2073
  const instance = new ChannelInstance(this.client, this.baseClient);
1472
2074
  this.channelInstances.set(instance.id, instance);
2075
+ console.log(`New channel instance created with ID: ${instance.id}`);
1473
2076
  return instance;
1474
2077
  }
1475
2078
  if (!this.channelInstances.has(id)) {
1476
2079
  const instance = new ChannelInstance(this.client, this.baseClient, id);
1477
2080
  this.channelInstances.set(id, instance);
2081
+ console.log(`New channel instance created with provided ID: ${id}`);
1478
2082
  return instance;
1479
2083
  }
2084
+ console.log(`Returning existing channel instance: ${id}`);
1480
2085
  return this.channelInstances.get(id);
1481
2086
  } catch (error) {
1482
- const message = getErrorMessage(error);
2087
+ const message = getErrorMessage2(error);
1483
2088
  console.error(`Error creating/retrieving channel instance:`, message);
1484
2089
  throw new Error(`Failed to manage channel instance: ${message}`);
1485
2090
  }
@@ -1496,9 +2101,11 @@ var Channels = class {
1496
2101
  if (!id) {
1497
2102
  throw new Error("No channel ID associated with this instance");
1498
2103
  }
1499
- return await this.baseClient.get(`/channels/${id}`);
2104
+ const details = await this.baseClient.get(`/channels/${id}`);
2105
+ console.log(`Retrieved channel details for ${id}`);
2106
+ return details;
1500
2107
  } catch (error) {
1501
- const message = getErrorMessage(error);
2108
+ const message = getErrorMessage2(error);
1502
2109
  console.error(`Error retrieving channel details for ${id}:`, message);
1503
2110
  throw new Error(`Failed to get channel details: ${message}`);
1504
2111
  }
@@ -1514,6 +2121,7 @@ var Channels = class {
1514
2121
  const instance = this.channelInstances.get(channelId);
1515
2122
  instance?.removeAllListeners();
1516
2123
  this.channelInstances.delete(channelId);
2124
+ console.log(`Channel instance removed: ${channelId}`);
1517
2125
  } else {
1518
2126
  console.warn(`Attempt to remove non-existent instance: ${channelId}`);
1519
2127
  }
@@ -1530,6 +2138,9 @@ var Channels = class {
1530
2138
  const instance = this.channelInstances.get(event.channel.id);
1531
2139
  if (instance) {
1532
2140
  instance.emitEvent(event);
2141
+ console.log(
2142
+ `Event propagated to channel ${event.channel.id}: ${event.type}`
2143
+ );
1533
2144
  } else {
1534
2145
  console.warn(`No instance found for channel ${event.channel.id}`);
1535
2146
  }
@@ -1543,9 +2154,11 @@ var Channels = class {
1543
2154
  throw new Error("Endpoint is required for channel origination");
1544
2155
  }
1545
2156
  try {
1546
- return await this.baseClient.post("/channels", data);
2157
+ const channel = await this.baseClient.post("/channels", data);
2158
+ console.log(`Channel originated successfully with ID: ${channel.id}`);
2159
+ return channel;
1547
2160
  } catch (error) {
1548
- const message = getErrorMessage(error);
2161
+ const message = getErrorMessage2(error);
1549
2162
  console.error(`Error originating channel:`, message);
1550
2163
  throw new Error(`Failed to originate channel: ${message}`);
1551
2164
  }
@@ -1559,9 +2172,10 @@ var Channels = class {
1559
2172
  if (!Array.isArray(channels)) {
1560
2173
  throw new Error("API response for /channels is not an array");
1561
2174
  }
2175
+ console.log(`Retrieved ${channels.length} active channels`);
1562
2176
  return channels;
1563
2177
  } catch (error) {
1564
- const message = getErrorMessage(error);
2178
+ const message = getErrorMessage2(error);
1565
2179
  console.error(`Error listing channels:`, message);
1566
2180
  throw new Error(`Failed to list channels: ${message}`);
1567
2181
  }
@@ -1988,10 +2602,10 @@ var Endpoints = class {
1988
2602
  };
1989
2603
 
1990
2604
  // 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)) {
2605
+ var import_events4 = require("events");
2606
+ var import_axios4 = require("axios");
2607
+ var getErrorMessage3 = (error) => {
2608
+ if ((0, import_axios4.isAxiosError)(error)) {
1995
2609
  return error.response?.data?.message || error.message || "An axios error occurred";
1996
2610
  }
1997
2611
  if (error instanceof Error) {
@@ -2012,8 +2626,9 @@ var PlaybackInstance = class {
2012
2626
  this.baseClient = baseClient;
2013
2627
  this.playbackId = playbackId;
2014
2628
  this.id = playbackId;
2629
+ console.log(`PlaybackInstance initialized with ID: ${this.id}`);
2015
2630
  }
2016
- eventEmitter = new import_events2.EventEmitter();
2631
+ eventEmitter = new import_events4.EventEmitter();
2017
2632
  playbackData = null;
2018
2633
  id;
2019
2634
  /**
@@ -2032,6 +2647,9 @@ var PlaybackInstance = class {
2032
2647
  }
2033
2648
  };
2034
2649
  this.eventEmitter.on(event, wrappedListener);
2650
+ console.log(
2651
+ `Event listener registered for ${event} on playback ${this.id}`
2652
+ );
2035
2653
  }
2036
2654
  /**
2037
2655
  * Registers a one-time event listener for a specific WebSocket event type.
@@ -2049,6 +2667,9 @@ var PlaybackInstance = class {
2049
2667
  }
2050
2668
  };
2051
2669
  this.eventEmitter.once(event, wrappedListener);
2670
+ console.log(
2671
+ `One-time event listener registered for ${event} on playback ${this.id}`
2672
+ );
2052
2673
  }
2053
2674
  /**
2054
2675
  * Removes event listener(s) for a specific WebSocket event type.
@@ -2062,8 +2683,12 @@ var PlaybackInstance = class {
2062
2683
  }
2063
2684
  if (listener) {
2064
2685
  this.eventEmitter.off(event, listener);
2686
+ console.log(
2687
+ `Specific listener removed for ${event} on playback ${this.id}`
2688
+ );
2065
2689
  } else {
2066
2690
  this.eventEmitter.removeAllListeners(event);
2691
+ console.log(`All listeners removed for ${event} on playback ${this.id}`);
2067
2692
  }
2068
2693
  }
2069
2694
  /**
@@ -2078,6 +2703,7 @@ var PlaybackInstance = class {
2078
2703
  }
2079
2704
  if ("playback" in event && event.playback?.id === this.id) {
2080
2705
  this.eventEmitter.emit(event.type, event);
2706
+ console.log(`Event ${event.type} emitted for playback ${this.id}`);
2081
2707
  }
2082
2708
  }
2083
2709
  /**
@@ -2094,9 +2720,10 @@ var PlaybackInstance = class {
2094
2720
  this.playbackData = await this.baseClient.get(
2095
2721
  `/playbacks/${this.id}`
2096
2722
  );
2723
+ console.log(`Retrieved playback data for ${this.id}`);
2097
2724
  return this.playbackData;
2098
2725
  } catch (error) {
2099
- const message = getErrorMessage2(error);
2726
+ const message = getErrorMessage3(error);
2100
2727
  console.error(`Error retrieving playback data for ${this.id}:`, message);
2101
2728
  throw new Error(`Failed to get playback data: ${message}`);
2102
2729
  }
@@ -2115,8 +2742,11 @@ var PlaybackInstance = class {
2115
2742
  await this.baseClient.post(
2116
2743
  `/playbacks/${this.id}/control?operation=${operation}`
2117
2744
  );
2745
+ console.log(
2746
+ `Operation ${operation} executed successfully on playback ${this.id}`
2747
+ );
2118
2748
  } catch (error) {
2119
- const message = getErrorMessage2(error);
2749
+ const message = getErrorMessage3(error);
2120
2750
  console.error(`Error controlling playback ${this.id}:`, message);
2121
2751
  throw new Error(`Failed to control playback: ${message}`);
2122
2752
  }
@@ -2132,8 +2762,9 @@ var PlaybackInstance = class {
2132
2762
  }
2133
2763
  try {
2134
2764
  await this.baseClient.delete(`/playbacks/${this.id}`);
2765
+ console.log(`Playback ${this.id} stopped successfully`);
2135
2766
  } catch (error) {
2136
- const message = getErrorMessage2(error);
2767
+ const message = getErrorMessage3(error);
2137
2768
  console.error(`Error stopping playback ${this.id}:`, message);
2138
2769
  throw new Error(`Failed to stop playback: ${message}`);
2139
2770
  }
@@ -2143,6 +2774,7 @@ var PlaybackInstance = class {
2143
2774
  */
2144
2775
  removeAllListeners() {
2145
2776
  this.eventEmitter.removeAllListeners();
2777
+ console.log(`All listeners removed from playback ${this.id}`);
2146
2778
  }
2147
2779
  /**
2148
2780
  * Checks if the playback instance has any listeners for a specific event.
@@ -2180,16 +2812,19 @@ var Playbacks = class {
2180
2812
  if (!id) {
2181
2813
  const instance = new PlaybackInstance(this.client, this.baseClient);
2182
2814
  this.playbackInstances.set(instance.id, instance);
2815
+ console.log(`New playback instance created with ID: ${instance.id}`);
2183
2816
  return instance;
2184
2817
  }
2185
2818
  if (!this.playbackInstances.has(id)) {
2186
2819
  const instance = new PlaybackInstance(this.client, this.baseClient, id);
2187
2820
  this.playbackInstances.set(id, instance);
2821
+ console.log(`New playback instance created with provided ID: ${id}`);
2188
2822
  return instance;
2189
2823
  }
2824
+ console.log(`Returning existing playback instance: ${id}`);
2190
2825
  return this.playbackInstances.get(id);
2191
2826
  } catch (error) {
2192
- const message = getErrorMessage2(error);
2827
+ const message = getErrorMessage3(error);
2193
2828
  console.error(`Error creating/retrieving playback instance:`, message);
2194
2829
  throw new Error(`Failed to manage playback instance: ${message}`);
2195
2830
  }
@@ -2207,6 +2842,7 @@ var Playbacks = class {
2207
2842
  const instance = this.playbackInstances.get(playbackId);
2208
2843
  instance?.removeAllListeners();
2209
2844
  this.playbackInstances.delete(playbackId);
2845
+ console.log(`Playback instance removed: ${playbackId}`);
2210
2846
  } else {
2211
2847
  console.warn(`Attempt to remove non-existent instance: ${playbackId}`);
2212
2848
  }
@@ -2217,12 +2853,16 @@ var Playbacks = class {
2217
2853
  */
2218
2854
  propagateEventToPlayback(event) {
2219
2855
  if (!event) {
2856
+ console.warn("Invalid WebSocket event received");
2220
2857
  return;
2221
2858
  }
2222
2859
  if ("playback" in event && event.playback?.id) {
2223
2860
  const instance = this.playbackInstances.get(event.playback.id);
2224
2861
  if (instance) {
2225
2862
  instance.emitEvent(event);
2863
+ console.log(
2864
+ `Event propagated to playback ${event.playback.id}: ${event.type}`
2865
+ );
2226
2866
  } else {
2227
2867
  console.warn(`No instance found for playback ${event.playback.id}`);
2228
2868
  }
@@ -2241,7 +2881,7 @@ var Playbacks = class {
2241
2881
  try {
2242
2882
  return await this.baseClient.get(`/playbacks/${playbackId}`);
2243
2883
  } catch (error) {
2244
- const message = getErrorMessage2(error);
2884
+ const message = getErrorMessage3(error);
2245
2885
  console.error(`Error getting playback details ${playbackId}:`, message);
2246
2886
  throw new Error(`Failed to get playback details: ${message}`);
2247
2887
  }
@@ -2259,8 +2899,9 @@ var Playbacks = class {
2259
2899
  try {
2260
2900
  const playback = this.Playback({ id: playbackId });
2261
2901
  await playback.control(operation);
2902
+ console.log(`Operation ${operation} executed on playback ${playbackId}`);
2262
2903
  } catch (error) {
2263
- const message = getErrorMessage2(error);
2904
+ const message = getErrorMessage3(error);
2264
2905
  console.error(`Error controlling playback ${playbackId}:`, message);
2265
2906
  throw new Error(`Failed to control playback: ${message}`);
2266
2907
  }
@@ -2277,8 +2918,9 @@ var Playbacks = class {
2277
2918
  try {
2278
2919
  const playback = this.Playback({ id: playbackId });
2279
2920
  await playback.stop();
2921
+ console.log(`Playback ${playbackId} stopped`);
2280
2922
  } catch (error) {
2281
- const message = getErrorMessage2(error);
2923
+ const message = getErrorMessage3(error);
2282
2924
  console.error(`Error stopping playback ${playbackId}:`, message);
2283
2925
  throw new Error(`Failed to stop playback: ${message}`);
2284
2926
  }
@@ -2332,13 +2974,13 @@ var Sounds = class {
2332
2974
  };
2333
2975
 
2334
2976
  // src/ari-client/websocketClient.ts
2335
- var import_events3 = require("events");
2977
+ var import_events5 = require("events");
2336
2978
  var import_exponential_backoff = __toESM(require_backoff(), 1);
2337
2979
  var import_ws = __toESM(require("ws"), 1);
2338
2980
  var DEFAULT_MAX_RECONNECT_ATTEMPTS = 10;
2339
2981
  var DEFAULT_STARTING_DELAY = 500;
2340
2982
  var DEFAULT_MAX_DELAY = 1e4;
2341
- var WebSocketClient = class extends import_events3.EventEmitter {
2983
+ var WebSocketClient = class extends import_events5.EventEmitter {
2342
2984
  /**
2343
2985
  * Creates a new WebSocket client instance.
2344
2986
  *
@@ -2458,7 +3100,13 @@ var WebSocketClient = class extends import_events3.EventEmitter {
2458
3100
  instancePlayback.emitEvent(event);
2459
3101
  event.instancePlayback = instancePlayback;
2460
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
+ }
2461
3108
  this.emit(event.type, event);
3109
+ console.log(`Event processed: ${event.type}`);
2462
3110
  } catch (error) {
2463
3111
  console.error(
2464
3112
  "Error processing WebSocket message:",
@@ -2540,11 +3188,11 @@ var AriClient = class {
2540
3188
  this.baseClient = new BaseClient(baseUrl, config.username, config.password);
2541
3189
  this.channels = new Channels(this.baseClient, this);
2542
3190
  this.playbacks = new Playbacks(this.baseClient, this);
3191
+ this.bridges = new Bridges(this.baseClient, this);
2543
3192
  this.endpoints = new Endpoints(this.baseClient);
2544
3193
  this.applications = new Applications(this.baseClient);
2545
3194
  this.sounds = new Sounds(this.baseClient);
2546
3195
  this.asterisk = new Asterisk(this.baseClient);
2547
- this.bridges = new Bridges(this.baseClient);
2548
3196
  console.log(`ARI Client initialized with base URL: ${baseUrl}`);
2549
3197
  }
2550
3198
  baseClient;
@@ -2660,6 +3308,20 @@ var AriClient = class {
2660
3308
  Playback(playbackId, _app) {
2661
3309
  return this.playbacks.Playback({ id: playbackId });
2662
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
+ }
2663
3325
  /**
2664
3326
  * Gets the current WebSocket connection status.
2665
3327
  *
@@ -2674,6 +3336,7 @@ var AriClient = class {
2674
3336
  Applications,
2675
3337
  AriClient,
2676
3338
  Asterisk,
3339
+ BridgeInstance,
2677
3340
  Bridges,
2678
3341
  ChannelInstance,
2679
3342
  Channels,