@ipcom/asterisk-ari 0.0.146-beta → 0.0.147

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/esm/index.js CHANGED
@@ -611,7 +611,6 @@ var BaseClient = class {
611
611
  }
612
612
  });
613
613
  this.addInterceptors();
614
- console.log(`BaseClient initialized for ${baseUrl}`);
615
614
  }
616
615
  client;
617
616
  /**
@@ -636,7 +635,6 @@ var BaseClient = class {
636
635
  addInterceptors() {
637
636
  this.client.interceptors.request.use(
638
637
  (config) => {
639
- console.log(`[Request] ${config.method?.toUpperCase()} ${config.url}`);
640
638
  return config;
641
639
  },
642
640
  (error) => {
@@ -647,7 +645,6 @@ var BaseClient = class {
647
645
  );
648
646
  this.client.interceptors.response.use(
649
647
  (response) => {
650
- console.log(`[Response] ${response.status} ${response.config.url}`);
651
648
  return response;
652
649
  },
653
650
  (error) => {
@@ -770,7 +767,6 @@ var BaseClient = class {
770
767
  ...this.client.defaults.headers.common,
771
768
  ...headers
772
769
  };
773
- console.log("Updated client headers");
774
770
  }
775
771
  /**
776
772
  * Gets the current request timeout setting.
@@ -783,7 +779,6 @@ var BaseClient = class {
783
779
  */
784
780
  setTimeout(timeout) {
785
781
  this.client.defaults.timeout = timeout;
786
- console.log(`Updated timeout to ${timeout}ms`);
787
782
  }
788
783
  };
789
784
 
@@ -794,7 +789,7 @@ var Applications = class {
794
789
  }
795
790
  /**
796
791
  * Lists all applications.
797
- *
792
+ *
798
793
  * @returns A promise that resolves to an array of Application objects.
799
794
  * @throws {Error} If the API response is not an array.
800
795
  */
@@ -807,7 +802,7 @@ var Applications = class {
807
802
  }
808
803
  /**
809
804
  * Retrieves details of a specific application.
810
- *
805
+ *
811
806
  * @param appName - The name of the application to retrieve details for.
812
807
  * @returns A promise that resolves to an ApplicationDetails object.
813
808
  * @throws {Error} If there's an error fetching the application details.
@@ -824,7 +819,7 @@ var Applications = class {
824
819
  }
825
820
  /**
826
821
  * Sends a message to a specific application.
827
- *
822
+ *
828
823
  * @param appName - The name of the application to send the message to.
829
824
  * @param body - The message to be sent, containing an event and optional data.
830
825
  * @returns A promise that resolves when the message is successfully sent.
@@ -917,681 +912,97 @@ var Asterisk = class {
917
912
  };
918
913
 
919
914
  // src/ari-client/resources/bridges.ts
920
- import { EventEmitter } from "events";
921
- import { isAxiosError as isAxiosError2 } from "axios";
922
-
923
- // src/ari-client/interfaces/events.types.ts
924
- var bridgeEvents = [
925
- "BridgeCreated",
926
- "BridgeDestroyed",
927
- "BridgeMerged",
928
- "BridgeBlindTransfer",
929
- "BridgeAttendedTransfer",
930
- "BridgeVideoSourceChanged"
931
- ];
932
-
933
- // src/ari-client/utils.ts
934
- function toQueryParams2(options) {
935
- return new URLSearchParams(
936
- Object.entries(options).filter(([, value]) => value !== void 0).map(([key, value]) => [key, value])
937
- ).toString();
938
- }
939
-
940
- // src/ari-client/resources/bridges.ts
941
- var getErrorMessage = (error) => {
942
- if (isAxiosError2(error)) {
943
- return error.response?.data?.message || error.message || "Um erro do axios ocorreu";
944
- }
945
- if (error instanceof Error) {
946
- return error.message;
947
- }
948
- return "Um erro desconhecido ocorreu";
949
- };
950
- var BridgeInstance = class {
951
- /**
952
- * Creates a new BridgeInstance.
953
- *
954
- * @param client - The AriClient instance for making API calls.
955
- * @param baseClient - The BaseClient instance for making HTTP requests.
956
- * @param bridgeId - Optional. The ID of the bridge. If not provided, a new ID will be generated.
957
- */
958
- constructor(client, baseClient, bridgeId) {
959
- this.client = client;
960
- this.baseClient = baseClient;
961
- this.id = bridgeId || `bridge-${Date.now()}`;
962
- console.log(`BridgeInstance inicializada com ID: ${this.id}`);
963
- }
964
- eventEmitter = new EventEmitter();
965
- bridgeData = null;
966
- id;
967
- /**
968
- * Registers a listener for specific bridge events.
969
- *
970
- * @param event - The type of event to listen for.
971
- * @param listener - The callback function to be called when the event occurs.
972
- */
973
- /**
974
- * Registers a listener for specific bridge events.
975
- *
976
- * This method allows you to attach an event listener to the bridge instance for a specific event type.
977
- * When the specified event occurs, the provided listener function will be called with the event data.
978
- *
979
- * @template T - The specific type of WebSocketEvent to listen for.
980
- * It receives the event data as its parameter.
981
- * @returns {void}
982
- *
983
- * @example
984
- * bridge.on('BridgeCreated', (event) => {
985
- * console.log('Bridge created:', event.bridge.id);
986
- * });
987
- * @param event
988
- * @param listener
989
- */
990
- on(event, listener) {
991
- if (!event) {
992
- throw new Error("Event type is required");
993
- }
994
- const wrappedListener = (data) => {
995
- if ("bridge" in data && data.bridge?.id === this.id) {
996
- listener(data);
997
- }
998
- };
999
- this.eventEmitter.on(event, wrappedListener);
1000
- console.log(`Event listener registered for ${event} on bridge ${this.id}`);
1001
- }
1002
- /**
1003
- * Registers a one-time listener for specific bridge events.
1004
- *
1005
- * @param event - The type of event to listen for.
1006
- * @param listener - The callback function to be called when the event occurs.
1007
- */
1008
- once(event, listener) {
1009
- if (!event) {
1010
- throw new Error("Event type is required");
1011
- }
1012
- const wrappedListener = (data) => {
1013
- if ("bridge" in data && data.bridge?.id === this.id) {
1014
- listener(data);
1015
- }
1016
- };
1017
- this.eventEmitter.once(event, wrappedListener);
1018
- console.log(
1019
- `One-time listener registered for ${event} on bridge ${this.id}`
1020
- );
1021
- }
1022
- /**
1023
- * Removes event listener(s) from the bridge.
1024
- *
1025
- * @param event - The type of event to remove listeners for.
1026
- * @param listener - Optional. The specific listener to remove. If not provided, all listeners for the event will be removed.
1027
- */
1028
- off(event, listener) {
1029
- if (!event) {
1030
- throw new Error("Event type is required");
1031
- }
1032
- if (listener) {
1033
- this.eventEmitter.off(event, listener);
1034
- console.log(
1035
- `Specific listener removed for ${event} on bridge ${this.id}`
1036
- );
1037
- } else {
1038
- this.eventEmitter.removeAllListeners(event);
1039
- console.log(`All listeners removed for ${event} on bridge ${this.id}`);
1040
- }
1041
- }
1042
- /**
1043
- * Emits an event if it corresponds to the current bridge.
1044
- *
1045
- * @param event - The WebSocketEvent to emit.
1046
- */
1047
- emitEvent(event) {
1048
- if (!event) {
1049
- console.warn("Invalid event received");
1050
- return;
1051
- }
1052
- if ("bridge" in event && event.bridge?.id === this.id) {
1053
- this.eventEmitter.emit(event.type, event);
1054
- console.log(`Event ${event.type} emitted for bridge ${this.id}`);
1055
- }
1056
- }
1057
- /**
1058
- * Removes all event listeners from this bridge instance.
1059
- */
1060
- removeAllListeners() {
1061
- this.eventEmitter.removeAllListeners();
1062
- console.log(`All listeners removed from bridge ${this.id}`);
1063
- }
1064
- /**
1065
- * Retrieves the current details of the bridge.
1066
- *
1067
- * @returns A Promise that resolves to the Bridge object containing the current details.
1068
- * @throws An error if the retrieval fails.
1069
- */
1070
- async get() {
1071
- try {
1072
- if (!this.id) {
1073
- throw new Error("No bridge associated with this instance");
1074
- }
1075
- this.bridgeData = await this.baseClient.get(
1076
- `/bridges/${this.id}`
1077
- );
1078
- console.log(`Details retrieved for bridge ${this.id}`);
1079
- return this.bridgeData;
1080
- } catch (error) {
1081
- const message = getErrorMessage(error);
1082
- console.error(`Error retrieving details for bridge ${this.id}:`, message);
1083
- throw new Error(`Failed to get bridge details: ${message}`);
1084
- }
1085
- }
1086
- /**
1087
- * Adds channels to the bridge.
1088
- *
1089
- * @param request - The AddChannelRequest object containing the channels to add.
1090
- * @throws An error if the operation fails.
1091
- */
1092
- async add(request) {
1093
- try {
1094
- const queryParams = toQueryParams2({
1095
- channel: Array.isArray(request.channel) ? request.channel.join(",") : request.channel,
1096
- ...request.role && { role: request.role }
1097
- });
1098
- await this.baseClient.post(
1099
- `/bridges/${this.id}/addChannel?${queryParams}`
1100
- );
1101
- console.log(`Channels added to bridge ${this.id}`);
1102
- } catch (error) {
1103
- const message = getErrorMessage(error);
1104
- console.error(`Error adding channels to bridge ${this.id}:`, message);
1105
- throw new Error(`Failed to add channels: ${message}`);
1106
- }
1107
- }
1108
- /**
1109
- * Removes channels from the bridge.
1110
- *
1111
- * @param request - The RemoveChannelRequest object containing the channels to remove.
1112
- * @throws An error if the operation fails.
1113
- */
1114
- async remove(request) {
1115
- try {
1116
- const queryParams = toQueryParams2({
1117
- channel: Array.isArray(request.channel) ? request.channel.join(",") : request.channel
1118
- });
1119
- await this.baseClient.post(
1120
- `/bridges/${this.id}/removeChannel?${queryParams}`
1121
- );
1122
- console.log(`Channels removed from bridge ${this.id}`);
1123
- } catch (error) {
1124
- const message = getErrorMessage(error);
1125
- console.error(`Error removing channels from bridge ${this.id}:`, message);
1126
- throw new Error(`Failed to remove channels: ${message}`);
1127
- }
1128
- }
1129
- /**
1130
- * Plays media on the bridge.
1131
- *
1132
- * @param request - The PlayMediaRequest object containing the media details to play.
1133
- * @returns A Promise that resolves to a BridgePlayback object.
1134
- * @throws An error if the operation fails.
1135
- */
1136
- async playMedia(request) {
1137
- try {
1138
- const queryParams = new URLSearchParams({
1139
- ...request.lang && { lang: request.lang },
1140
- ...request.offsetms && { offsetms: request.offsetms.toString() },
1141
- ...request.skipms && { skipms: request.skipms.toString() },
1142
- ...request.playbackId && { playbackId: request.playbackId }
1143
- }).toString();
1144
- const result = await this.baseClient.post(
1145
- `/bridges/${this.id}/play?${queryParams}`,
1146
- { media: request.media }
1147
- );
1148
- console.log(`Media playback started on bridge ${this.id}`);
1149
- return result;
1150
- } catch (error) {
1151
- const message = getErrorMessage(error);
1152
- console.error(`Error playing media on bridge ${this.id}:`, message);
1153
- throw new Error(`Failed to play media: ${message}`);
1154
- }
1155
- }
1156
- /**
1157
- * Stops media playback on the bridge.
1158
- *
1159
- * @param playbackId - The ID of the playback to stop.
1160
- * @throws An error if the operation fails.
1161
- */
1162
- async stopPlayback(playbackId) {
1163
- try {
1164
- await this.baseClient.delete(
1165
- `/bridges/${this.id}/play/${playbackId}`
1166
- );
1167
- console.log(`Playback ${playbackId} stopped on bridge ${this.id}`);
1168
- } catch (error) {
1169
- const message = getErrorMessage(error);
1170
- console.error(`Error stopping playback on bridge ${this.id}:`, message);
1171
- throw new Error(`Failed to stop playback: ${message}`);
1172
- }
1173
- }
1174
- /**
1175
- * Sets the video source for the bridge.
1176
- *
1177
- * @param channelId - The ID of the channel to set as the video source.
1178
- * @throws An error if the operation fails.
1179
- */
1180
- async setVideoSource(channelId) {
1181
- try {
1182
- await this.baseClient.post(
1183
- `/bridges/${this.id}/videoSource/${channelId}`
1184
- );
1185
- console.log(`Video source set for bridge ${this.id}`);
1186
- } catch (error) {
1187
- const message = getErrorMessage(error);
1188
- console.error(
1189
- `Error setting video source for bridge ${this.id}:`,
1190
- message
1191
- );
1192
- throw new Error(`Failed to set video source: ${message}`);
1193
- }
1194
- }
1195
- /**
1196
- * Removes the video source from the bridge.
1197
- *
1198
- * @throws An error if the operation fails.
1199
- */
1200
- async clearVideoSource() {
1201
- try {
1202
- await this.baseClient.delete(`/bridges/${this.id}/videoSource`);
1203
- console.log(`Video source removed from bridge ${this.id}`);
1204
- } catch (error) {
1205
- const message = getErrorMessage(error);
1206
- console.error(
1207
- `Error removing video source from bridge ${this.id}:`,
1208
- message
1209
- );
1210
- throw new Error(`Failed to remove video source: ${message}`);
1211
- }
1212
- }
1213
- /**
1214
- * Checks if the bridge has listeners for a specific event.
1215
- *
1216
- * @param event - The event type to check for listeners.
1217
- * @returns A boolean indicating whether there are listeners for the event.
1218
- */
1219
- hasListeners(event) {
1220
- return this.eventEmitter.listenerCount(event) > 0;
1221
- }
1222
- /**
1223
- * Retrieves the current bridge data without making an API call.
1224
- *
1225
- * @returns The current Bridge object or null if no data is available.
1226
- */
1227
- getCurrentData() {
1228
- return this.bridgeData;
1229
- }
1230
- };
1231
915
  var Bridges = class {
1232
- constructor(baseClient, client) {
1233
- this.baseClient = baseClient;
916
+ constructor(client) {
1234
917
  this.client = client;
1235
918
  }
1236
- bridgeInstances = /* @__PURE__ */ new Map();
1237
- /**
1238
- * Creates or retrieves a Bridge instance.
1239
- *
1240
- * This method manages the creation and retrieval of BridgeInstance objects.
1241
- * If an ID is provided and an instance with that ID already exists, it returns the existing instance.
1242
- * If an ID is provided but no instance exists, it creates a new instance with that ID.
1243
- * If no ID is provided, it creates a new instance with a generated ID.
1244
- *
1245
- * @param {Object} params - The parameters for creating or retrieving a Bridge instance.
1246
- * @param {string} [params.id] - Optional. The ID of the Bridge instance to create or retrieve.
1247
- *
1248
- * @returns {BridgeInstance} A BridgeInstance object, either newly created or retrieved from existing instances.
1249
- *
1250
- * @throws {Error} If there's an error in creating or retrieving the Bridge instance.
1251
- */
1252
- Bridge({ id }) {
1253
- try {
1254
- if (!id) {
1255
- const instance = new BridgeInstance(this.client, this.baseClient);
1256
- this.bridgeInstances.set(instance.id, instance);
1257
- console.log(`New bridge instance created with ID: ${instance.id}`);
1258
- return instance;
1259
- }
1260
- if (!this.bridgeInstances.has(id)) {
1261
- const instance = new BridgeInstance(this.client, this.baseClient, id);
1262
- this.bridgeInstances.set(id, instance);
1263
- console.log(`New bridge instance created with provided ID: ${id}`);
1264
- return instance;
1265
- }
1266
- console.log(`Returning existing bridge instance: ${id}`);
1267
- return this.bridgeInstances.get(id);
1268
- } catch (error) {
1269
- const message = getErrorMessage(error);
1270
- console.error(`Error creating/retrieving bridge instance:`, message);
1271
- throw new Error(`Failed to manage bridge instance: ${message}`);
1272
- }
1273
- }
1274
919
  /**
1275
- * Removes a bridge instance from the collection of managed bridges.
1276
- *
1277
- * This function removes the specified bridge instance, cleans up its event listeners,
1278
- * and logs the removal. If the bridge instance doesn't exist, it logs a warning.
1279
- *
1280
- * @param {string} bridgeId - The unique identifier of the bridge instance to be removed.
1281
- * @throws {Error} Throws an error if the bridgeId is not provided.
1282
- * @returns {void}
1283
- */
1284
- removeBridgeInstance(bridgeId) {
1285
- if (!bridgeId) {
1286
- throw new Error("ID da bridge \xE9 obrigat\xF3rio");
1287
- }
1288
- if (this.bridgeInstances.has(bridgeId)) {
1289
- const instance = this.bridgeInstances.get(bridgeId);
1290
- instance?.removeAllListeners();
1291
- this.bridgeInstances.delete(bridgeId);
1292
- console.log(`Inst\xE2ncia de bridge removida: ${bridgeId}`);
1293
- } else {
1294
- console.warn(`Tentativa de remover inst\xE2ncia inexistente: ${bridgeId}`);
1295
- }
1296
- }
1297
- /**
1298
- * Propagates a WebSocket event to a specific bridge instance.
1299
- *
1300
- * This function checks if the received event is valid and related to a bridge,
1301
- * then emits the event to the corresponding bridge instance if it exists.
1302
- *
1303
- * @param {WebSocketEvent} event - The WebSocket event to be propagated.
1304
- * This should be an object containing information about the event,
1305
- * including the bridge ID and event type.
1306
- *
1307
- * @returns {void}
1308
- *
1309
- * @remarks
1310
- * - If the event is invalid (null or undefined), a warning is logged and the function returns early.
1311
- * - The function checks if the event is bridge-related and if the event type is included in the predefined bridge events.
1312
- * - If a matching bridge instance is found, the event is emitted to that instance.
1313
- * - If no matching bridge instance is found, a warning is logged.
1314
- */
1315
- propagateEventToBridge(event) {
1316
- if (!event) {
1317
- console.warn("Evento WebSocket inv\xE1lido recebido");
1318
- return;
1319
- }
1320
- if ("bridge" in event && event.bridge?.id && bridgeEvents.includes(event.type)) {
1321
- const instance = this.bridgeInstances.get(event.bridge.id);
1322
- if (instance) {
1323
- instance.emitEvent(event);
1324
- console.log(
1325
- `Evento propagado para bridge ${event.bridge.id}: ${event.type}`
1326
- );
1327
- } else {
1328
- console.warn(
1329
- `Nenhuma inst\xE2ncia encontrada para bridge ${event.bridge.id}`
1330
- );
1331
- }
1332
- }
1333
- }
1334
- /**
1335
- * Lists all active bridges in the system.
1336
- *
1337
- * This asynchronous function retrieves a list of all currently active bridges
1338
- * by making a GET request to the "/bridges" endpoint using the base client.
1339
- *
1340
- * @returns {Promise<Bridge[]>} A promise that resolves to an array of Bridge objects.
1341
- * Each Bridge object represents an active bridge in the system.
1342
- *
1343
- * @throws {Error} If there's an error in fetching the bridges or if the request fails.
1344
- *
1345
- * @example
1346
- * try {
1347
- * const bridges = await bridgesInstance.list();
1348
- * console.log('Active bridges:', bridges);
1349
- * } catch (error) {
1350
- * console.error('Failed to fetch bridges:', error);
1351
- * }
920
+ * Lists all active bridges.
1352
921
  */
1353
922
  async list() {
1354
- return this.baseClient.get("/bridges");
923
+ return this.client.get("/bridges");
1355
924
  }
1356
925
  /**
1357
- * Creates a new bridge in the system.
1358
- *
1359
- * This asynchronous function sends a POST request to create a new bridge
1360
- * using the provided configuration details.
1361
- *
1362
- * @param request - The configuration details for creating the new bridge.
1363
- * @param request.type - The type of bridge to create (e.g., 'mixing', 'holding').
1364
- * @param request.name - Optional. A custom name for the bridge.
1365
- * @param request.bridgeId - Optional. A specific ID for the bridge. If not provided, one will be generated.
1366
- *
1367
- * @returns A Promise that resolves to a Bridge object representing the newly created bridge.
1368
- * The Bridge object contains details such as id, technology, bridge_type, bridge_class, channels, etc.
1369
- *
1370
- * @throws Will throw an error if the bridge creation fails or if there's a network issue.
926
+ * Creates a new bridge.
1371
927
  */
1372
928
  async createBridge(request) {
1373
- return this.baseClient.post("/bridges", request);
929
+ return this.client.post("/bridges", request);
1374
930
  }
1375
931
  /**
1376
- * Retrieves detailed information about a specific bridge.
1377
- *
1378
- * This asynchronous function fetches the complete details of a bridge
1379
- * identified by its unique ID. It makes a GET request to the ARI endpoint
1380
- * for the specified bridge.
1381
- *
1382
- * @param bridgeId - The unique identifier of the bridge to retrieve details for.
1383
- * This should be a string that uniquely identifies the bridge in the system.
1384
- *
1385
- * @returns A Promise that resolves to a Bridge object containing all the details
1386
- * of the specified bridge. This includes information such as the bridge's
1387
- * ID, type, channels, and other relevant properties.
1388
- *
1389
- * @throws Will throw an error if the bridge cannot be found, if there's a network issue,
1390
- * or if the server responds with an error.
932
+ * Retrieves details of a specific bridge.
1391
933
  */
1392
- async get(bridgeId) {
1393
- return this.baseClient.get(`/bridges/${bridgeId}`);
934
+ async getDetails(bridgeId) {
935
+ return this.client.get(`/bridges/${bridgeId}`);
1394
936
  }
1395
937
  /**
1396
- * Destroys (deletes) a specific bridge in the system.
1397
- *
1398
- * This asynchronous function sends a DELETE request to remove a bridge
1399
- * identified by its unique ID. Once destroyed, the bridge and all its
1400
- * associated resources are permanently removed from the system.
1401
- *
1402
- * @param bridgeId - The unique identifier of the bridge to be destroyed.
1403
- * This should be a string that uniquely identifies the bridge in the system.
1404
- *
1405
- * @returns A Promise that resolves to void when the bridge is successfully destroyed.
1406
- * If the operation is successful, the bridge no longer exists in the system.
1407
- *
1408
- * @throws Will throw an error if the bridge cannot be found, if there's a network issue,
1409
- * or if the server responds with an error during the deletion process.
938
+ * Destroys (deletes) a specific bridge.
1410
939
  */
1411
940
  async destroy(bridgeId) {
1412
- return this.baseClient.delete(`/bridges/${bridgeId}`);
941
+ return this.client.delete(`/bridges/${bridgeId}`);
1413
942
  }
1414
943
  /**
1415
- * Adds one or more channels to a specified bridge.
1416
- *
1417
- * This asynchronous function sends a POST request to add channels to an existing bridge.
1418
- * It can handle adding a single channel or multiple channels in one operation.
1419
- *
1420
- * @param bridgeId - The unique identifier of the bridge to which channels will be added.
1421
- * @param request - An object containing the details of the channel(s) to be added.
1422
- * @param request.channel - A single channel ID or an array of channel IDs to add to the bridge.
1423
- * @param request.role - Optional. Specifies the role of the channel(s) in the bridge.
1424
- *
1425
- * @returns A Promise that resolves to void when the operation is successful.
1426
- *
1427
- * @throws Will throw an error if the request fails, such as if the bridge doesn't exist
1428
- * or if there's a network issue.
944
+ * Adds a channel or multiple channels to a bridge.
1429
945
  */
1430
946
  async addChannels(bridgeId, request) {
1431
- const queryParams = toQueryParams2({
947
+ const queryParams = new URLSearchParams({
1432
948
  channel: Array.isArray(request.channel) ? request.channel.join(",") : request.channel,
1433
949
  ...request.role && { role: request.role }
1434
- });
1435
- await this.baseClient.post(
950
+ }).toString();
951
+ await this.client.post(
1436
952
  `/bridges/${bridgeId}/addChannel?${queryParams}`
1437
953
  );
1438
954
  }
1439
955
  /**
1440
- * Removes one or more channels from a specified bridge.
1441
- *
1442
- * This asynchronous function sends a POST request to remove channels from an existing bridge.
1443
- * It can handle removing a single channel or multiple channels in one operation.
1444
- *
1445
- * @param bridgeId - The unique identifier of the bridge from which channels will be removed.
1446
- * @param request - An object containing the details of the channel(s) to be removed.
1447
- * @param request.channel - A single channel ID or an array of channel IDs to remove from the bridge.
1448
- *
1449
- * @returns A Promise that resolves to void when the operation is successful.
1450
- *
1451
- * @throws Will throw an error if the request fails, such as if the bridge doesn't exist,
1452
- * if the channels are not in the bridge, or if there's a network issue.
956
+ * Removes a channel or multiple channels from a bridge.
1453
957
  */
1454
958
  async removeChannels(bridgeId, request) {
1455
- const queryParams = toQueryParams2({
959
+ const queryParams = new URLSearchParams({
1456
960
  channel: Array.isArray(request.channel) ? request.channel.join(",") : request.channel
1457
- });
1458
- await this.baseClient.post(
961
+ }).toString();
962
+ await this.client.post(
1459
963
  `/bridges/${bridgeId}/removeChannel?${queryParams}`
1460
964
  );
1461
965
  }
1462
966
  /**
1463
- * Plays media on a specified bridge.
1464
- *
1465
- * This asynchronous function initiates media playback on a bridge identified by its ID.
1466
- * It allows for customization of the playback through various options in the request.
1467
- *
1468
- * @param bridgeId - The unique identifier of the bridge on which to play the media.
1469
- * @param request - An object containing the media playback request details.
1470
- * @param request.media - The media to be played (e.g., sound file, URL).
1471
- * @param request.lang - Optional. The language of the media content.
1472
- * @param request.offsetms - Optional. The offset in milliseconds to start playing from.
1473
- * @param request.skipms - Optional. The number of milliseconds to skip before playing.
1474
- * @param request.playbackId - Optional. A custom ID for the playback session.
1475
- *
1476
- * @returns A Promise that resolves to a BridgePlayback object, containing details about the initiated playback.
1477
- *
1478
- * @throws Will throw an error if the playback request fails or if there's a network issue.
967
+ * Plays media to a bridge.
1479
968
  */
1480
969
  async playMedia(bridgeId, request) {
1481
- const queryParams = toQueryParams2({
970
+ const queryParams = new URLSearchParams({
1482
971
  ...request.lang && { lang: request.lang },
1483
972
  ...request.offsetms && { offsetms: request.offsetms.toString() },
1484
973
  ...request.skipms && { skipms: request.skipms.toString() },
1485
974
  ...request.playbackId && { playbackId: request.playbackId }
1486
- });
1487
- return this.baseClient.post(
975
+ }).toString();
976
+ return this.client.post(
1488
977
  `/bridges/${bridgeId}/play?${queryParams}`,
1489
978
  { media: request.media }
1490
979
  );
1491
980
  }
1492
981
  /**
1493
- * Stops media playback on a specified bridge.
1494
- *
1495
- * This asynchronous function sends a DELETE request to stop the playback of media
1496
- * on a bridge identified by its ID and a specific playback session.
1497
- *
1498
- * @param bridgeId - The unique identifier of the bridge where the playback is to be stopped.
1499
- * @param playbackId - The unique identifier of the playback session to be stopped.
1500
- *
1501
- * @returns A Promise that resolves to void when the playback is successfully stopped.
1502
- *
1503
- * @throws Will throw an error if the request fails, such as if the bridge or playback session
1504
- * doesn't exist, or if there's a network issue.
982
+ * Stops media playback on a bridge.
1505
983
  */
1506
984
  async stopPlayback(bridgeId, playbackId) {
1507
- await this.baseClient.delete(
1508
- `/bridges/${bridgeId}/play/${playbackId}`
1509
- );
985
+ await this.client.delete(`/bridges/${bridgeId}/play/${playbackId}`);
1510
986
  }
1511
987
  /**
1512
- * Sets the video source for a specified bridge.
1513
- *
1514
- * This asynchronous function configures a channel as the video source for a given bridge.
1515
- * It sends a POST request to the ARI endpoint to update the bridge's video source.
1516
- *
1517
- * @param bridgeId - The unique identifier of the bridge for which to set the video source.
1518
- * @param channelId - The unique identifier of the channel to be set as the video source.
1519
- *
1520
- * @returns A Promise that resolves to void when the video source is successfully set.
1521
- *
1522
- * @throws Will throw an error if the request fails, such as if the bridge or channel
1523
- * doesn't exist, or if there's a network issue.
988
+ * Sets the video source for a bridge.
1524
989
  */
1525
990
  async setVideoSource(bridgeId, channelId) {
1526
- const queryParams = toQueryParams2({ channelId });
1527
- await this.baseClient.post(
1528
- `/bridges/${bridgeId}/videoSource?${queryParams}`
991
+ await this.client.post(
992
+ `/bridges/${bridgeId}/videoSource?channelId=${encodeURIComponent(channelId)}`
1529
993
  );
1530
994
  }
1531
995
  /**
1532
- * Clears the video source for a specified bridge.
1533
- *
1534
- * This asynchronous function removes the currently set video source from a bridge.
1535
- * It sends a DELETE request to the ARI endpoint to clear the video source configuration.
1536
- *
1537
- * @param bridgeId - The unique identifier of the bridge from which to clear the video source.
1538
- * This should be a string that uniquely identifies the bridge in the system.
1539
- *
1540
- * @returns A Promise that resolves to void when the video source is successfully cleared.
1541
- * If the operation is successful, the bridge will no longer have a designated video source.
1542
- *
1543
- * @throws Will throw an error if the request fails, such as if the bridge doesn't exist,
1544
- * if there's no video source set, or if there's a network issue.
996
+ * Clears the video source for a bridge.
1545
997
  */
1546
998
  async clearVideoSource(bridgeId) {
1547
- await this.baseClient.delete(`/bridges/${bridgeId}/videoSource`);
1548
- }
1549
- /**
1550
- * Retrieves the count of active bridge instances.
1551
- *
1552
- * This function returns the total number of bridge instances currently
1553
- * managed by the Bridges class. It provides a quick way to check how many
1554
- * active bridges are present in the system.
1555
- *
1556
- * @returns {number} The count of active bridge instances.
1557
- */
1558
- getInstanceCount() {
1559
- return this.bridgeInstances.size;
1560
- }
1561
- /**
1562
- * Checks if a bridge instance exists in the collection of managed bridges.
1563
- *
1564
- * This function verifies whether a bridge instance with the specified ID
1565
- * is currently being managed by the Bridges class.
1566
- *
1567
- * @param bridgeId - The unique identifier of the bridge instance to check.
1568
- * This should be a string that uniquely identifies the bridge in the system.
1569
- *
1570
- * @returns A boolean value indicating whether the bridge instance exists.
1571
- * Returns true if the bridge instance is found, false otherwise.
1572
- */
1573
- hasInstance(bridgeId) {
1574
- return this.bridgeInstances.has(bridgeId);
1575
- }
1576
- /**
1577
- * Retrieves all active bridge instances currently managed by the Bridges class.
1578
- *
1579
- * This method provides a way to access all the BridgeInstance objects that are
1580
- * currently active and being managed. It returns a new Map to prevent direct
1581
- * modification of the internal bridgeInstances collection.
1582
- *
1583
- * @returns A new Map object containing all active bridge instances, where the keys
1584
- * are the bridge IDs (strings) and the values are the corresponding
1585
- * BridgeInstance objects. If no bridges are active, an empty Map is returned.
1586
- */
1587
- getAllInstances() {
1588
- return new Map(this.bridgeInstances);
999
+ await this.client.delete(`/bridges/${bridgeId}/videoSource`);
1589
1000
  }
1590
1001
  };
1591
1002
 
1592
1003
  // src/ari-client/resources/channels.ts
1593
- import { EventEmitter as EventEmitter2 } from "events";
1594
- import { isAxiosError as isAxiosError3 } from "axios";
1004
+ import { EventEmitter } from "events";
1005
+ import { isAxiosError as isAxiosError2 } from "axios";
1595
1006
 
1596
1007
  // node_modules/uuid/dist/esm/stringify.js
1597
1008
  var byteToHex = [];
@@ -1638,9 +1049,16 @@ function v4(options, buf, offset) {
1638
1049
  }
1639
1050
  var v4_default = v4;
1640
1051
 
1052
+ // src/ari-client/utils.ts
1053
+ function toQueryParams2(options) {
1054
+ return new URLSearchParams(
1055
+ Object.entries(options).filter(([, value]) => value !== void 0).map(([key, value]) => [key, value])
1056
+ ).toString();
1057
+ }
1058
+
1641
1059
  // src/ari-client/resources/channels.ts
1642
- var getErrorMessage2 = (error) => {
1643
- if (isAxiosError3(error)) {
1060
+ var getErrorMessage = (error) => {
1061
+ if (isAxiosError2(error)) {
1644
1062
  return error.response?.data?.message || error.message || "An axios error occurred";
1645
1063
  }
1646
1064
  if (error instanceof Error) {
@@ -1653,9 +1071,8 @@ var ChannelInstance = class {
1653
1071
  this.client = client;
1654
1072
  this.baseClient = baseClient;
1655
1073
  this.id = channelId || `channel-${Date.now()}`;
1656
- console.log(`Channel instance initialized with ID: ${this.id}`);
1657
1074
  }
1658
- eventEmitter = new EventEmitter2();
1075
+ eventEmitter = new EventEmitter();
1659
1076
  channelData = null;
1660
1077
  id;
1661
1078
  /**
@@ -1671,7 +1088,6 @@ var ChannelInstance = class {
1671
1088
  }
1672
1089
  };
1673
1090
  this.eventEmitter.on(event, wrappedListener);
1674
- console.log(`Event listener registered for ${event} on channel ${this.id}`);
1675
1091
  }
1676
1092
  /**
1677
1093
  * Registers a one-time event listener
@@ -1686,9 +1102,6 @@ var ChannelInstance = class {
1686
1102
  }
1687
1103
  };
1688
1104
  this.eventEmitter.once(event, wrappedListener);
1689
- console.log(
1690
- `One-time event listener registered for ${event} on channel ${this.id}`
1691
- );
1692
1105
  }
1693
1106
  /**
1694
1107
  * Removes event listener(s) for a specific WebSocket event type.
@@ -1705,12 +1118,8 @@ var ChannelInstance = class {
1705
1118
  }
1706
1119
  if (listener) {
1707
1120
  this.eventEmitter.off(event, listener);
1708
- console.log(
1709
- `Specific listener removed for ${event} on channel ${this.id}`
1710
- );
1711
1121
  } else {
1712
1122
  this.eventEmitter.removeAllListeners(event);
1713
- console.log(`All listeners removed for ${event} on channel ${this.id}`);
1714
1123
  }
1715
1124
  }
1716
1125
  /**
@@ -1723,7 +1132,6 @@ var ChannelInstance = class {
1723
1132
  }
1724
1133
  if ("channel" in event && event.channel?.id === this.id) {
1725
1134
  this.eventEmitter.emit(event.type, event);
1726
- console.log(`Event ${event.type} emitted for channel ${this.id}`);
1727
1135
  }
1728
1136
  }
1729
1137
  /**
@@ -1733,7 +1141,6 @@ var ChannelInstance = class {
1733
1141
  * @return {void} This method does not return a value.
1734
1142
  */
1735
1143
  removeAllListeners() {
1736
- console.log(`Removendo todos os listeners para o canal ${this.id}`);
1737
1144
  this.eventEmitter.removeAllListeners();
1738
1145
  }
1739
1146
  /**
@@ -1742,9 +1149,8 @@ var ChannelInstance = class {
1742
1149
  async answer() {
1743
1150
  try {
1744
1151
  await this.baseClient.post(`/channels/${this.id}/answer`);
1745
- console.log(`Channel ${this.id} answered`);
1746
1152
  } catch (error) {
1747
- const message = getErrorMessage2(error);
1153
+ const message = getErrorMessage(error);
1748
1154
  console.error(`Error answering channel ${this.id}:`, message);
1749
1155
  throw new Error(`Failed to answer channel: ${message}`);
1750
1156
  }
@@ -1765,12 +1171,9 @@ var ChannelInstance = class {
1765
1171
  "/channels",
1766
1172
  data
1767
1173
  );
1768
- console.log(
1769
- `Channel originated successfully with ID: ${this.channelData.id}`
1770
- );
1771
1174
  return this.channelData;
1772
1175
  } catch (error) {
1773
- const message = getErrorMessage2(error);
1176
+ const message = getErrorMessage(error);
1774
1177
  console.error(`Error originating channel:`, message);
1775
1178
  throw new Error(`Failed to originate channel: ${message}`);
1776
1179
  }
@@ -1783,19 +1186,56 @@ var ChannelInstance = class {
1783
1186
  throw new Error("Media URL is required");
1784
1187
  }
1785
1188
  try {
1786
- if (!this.channelData) {
1787
- console.log("Initializing channel details...");
1788
- this.channelData = await this.getDetails();
1189
+ try {
1190
+ if (!this.channelData) {
1191
+ this.channelData = await this.getDetails();
1192
+ }
1193
+ if (!this.channelData?.id) {
1194
+ throw new Error("Channel ID is not available");
1195
+ }
1196
+ } catch (error) {
1197
+ console.error("Failed to get channel details:", error);
1198
+ throw new Error("Could not initialize channel data");
1789
1199
  }
1790
- const playback = this.client.Playback(playbackId || v4_default());
1791
- await this.baseClient.post(
1792
- `/channels/${this.id}/play/${playback.id}`,
1200
+ const playbackInstance = this.client.Playback(playbackId || v4_default());
1201
+ console.log("Attempting to play media:", {
1202
+ channelId: this.id,
1203
+ playbackId: playbackInstance.id,
1793
1204
  options
1794
- );
1795
- console.log(`Media playback started on channel ${this.id}`);
1796
- return playback;
1205
+ });
1206
+ try {
1207
+ await this.baseClient.post(
1208
+ `/channels/${this.id}/play/${playbackInstance.id}`,
1209
+ options
1210
+ );
1211
+ console.log("Successfully initiated playback:", {
1212
+ channelId: this.id,
1213
+ playbackId: playbackInstance.id
1214
+ });
1215
+ const playbackStatus = await playbackInstance.get().catch((err) => {
1216
+ console.warn("Could not verify playback status:", err);
1217
+ return null;
1218
+ });
1219
+ if (!playbackStatus) {
1220
+ console.warn("Playback was created but status could not be verified");
1221
+ } else {
1222
+ console.log("Playback status:", playbackStatus);
1223
+ }
1224
+ return playbackInstance;
1225
+ } catch (error) {
1226
+ const message = getErrorMessage(error);
1227
+ console.error(
1228
+ `Failed to start playback on channel ${this.id}:`,
1229
+ message
1230
+ );
1231
+ try {
1232
+ await playbackInstance.stop().catch();
1233
+ } catch {
1234
+ }
1235
+ throw new Error(`Failed to start playback: ${message}`);
1236
+ }
1797
1237
  } catch (error) {
1798
- const message = getErrorMessage2(error);
1238
+ const message = getErrorMessage(error);
1799
1239
  console.error(`Error playing media on channel ${this.id}:`, message);
1800
1240
  throw new Error(`Failed to play media: ${message}`);
1801
1241
  }
@@ -1815,10 +1255,9 @@ var ChannelInstance = class {
1815
1255
  `/channels/${this.id}`
1816
1256
  );
1817
1257
  this.channelData = details;
1818
- console.log(`Retrieved channel details for ${this.id}`);
1819
1258
  return details;
1820
1259
  } catch (error) {
1821
- const message = getErrorMessage2(error);
1260
+ const message = getErrorMessage(error);
1822
1261
  console.error(
1823
1262
  `Error retrieving channel details for ${this.id}:`,
1824
1263
  message
@@ -1862,7 +1301,6 @@ var ChannelInstance = class {
1862
1301
  */
1863
1302
  async hangup() {
1864
1303
  if (!this.channelData) {
1865
- console.log("Canal n\xE3o inicializado, buscando detalhes...");
1866
1304
  this.channelData = await this.getDetails();
1867
1305
  }
1868
1306
  if (!this.channelData?.id) {
@@ -2051,19 +1489,16 @@ var Channels = class {
2051
1489
  if (!id) {
2052
1490
  const instance = new ChannelInstance(this.client, this.baseClient);
2053
1491
  this.channelInstances.set(instance.id, instance);
2054
- console.log(`New channel instance created with ID: ${instance.id}`);
2055
1492
  return instance;
2056
1493
  }
2057
1494
  if (!this.channelInstances.has(id)) {
2058
1495
  const instance = new ChannelInstance(this.client, this.baseClient, id);
2059
1496
  this.channelInstances.set(id, instance);
2060
- console.log(`New channel instance created with provided ID: ${id}`);
2061
1497
  return instance;
2062
1498
  }
2063
- console.log(`Returning existing channel instance: ${id}`);
2064
1499
  return this.channelInstances.get(id);
2065
1500
  } catch (error) {
2066
- const message = getErrorMessage2(error);
1501
+ const message = getErrorMessage(error);
2067
1502
  console.error(`Error creating/retrieving channel instance:`, message);
2068
1503
  throw new Error(`Failed to manage channel instance: ${message}`);
2069
1504
  }
@@ -2080,11 +1515,9 @@ var Channels = class {
2080
1515
  if (!id) {
2081
1516
  throw new Error("No channel ID associated with this instance");
2082
1517
  }
2083
- const details = await this.baseClient.get(`/channels/${id}`);
2084
- console.log(`Retrieved channel details for ${id}`);
2085
- return details;
1518
+ return await this.baseClient.get(`/channels/${id}`);
2086
1519
  } catch (error) {
2087
- const message = getErrorMessage2(error);
1520
+ const message = getErrorMessage(error);
2088
1521
  console.error(`Error retrieving channel details for ${id}:`, message);
2089
1522
  throw new Error(`Failed to get channel details: ${message}`);
2090
1523
  }
@@ -2100,7 +1533,6 @@ var Channels = class {
2100
1533
  const instance = this.channelInstances.get(channelId);
2101
1534
  instance?.removeAllListeners();
2102
1535
  this.channelInstances.delete(channelId);
2103
- console.log(`Channel instance removed: ${channelId}`);
2104
1536
  } else {
2105
1537
  console.warn(`Attempt to remove non-existent instance: ${channelId}`);
2106
1538
  }
@@ -2117,9 +1549,6 @@ var Channels = class {
2117
1549
  const instance = this.channelInstances.get(event.channel.id);
2118
1550
  if (instance) {
2119
1551
  instance.emitEvent(event);
2120
- console.log(
2121
- `Event propagated to channel ${event.channel.id}: ${event.type}`
2122
- );
2123
1552
  } else {
2124
1553
  console.warn(`No instance found for channel ${event.channel.id}`);
2125
1554
  }
@@ -2133,11 +1562,9 @@ var Channels = class {
2133
1562
  throw new Error("Endpoint is required for channel origination");
2134
1563
  }
2135
1564
  try {
2136
- const channel = await this.baseClient.post("/channels", data);
2137
- console.log(`Channel originated successfully with ID: ${channel.id}`);
2138
- return channel;
1565
+ return await this.baseClient.post("/channels", data);
2139
1566
  } catch (error) {
2140
- const message = getErrorMessage2(error);
1567
+ const message = getErrorMessage(error);
2141
1568
  console.error(`Error originating channel:`, message);
2142
1569
  throw new Error(`Failed to originate channel: ${message}`);
2143
1570
  }
@@ -2151,10 +1578,9 @@ var Channels = class {
2151
1578
  if (!Array.isArray(channels)) {
2152
1579
  throw new Error("API response for /channels is not an array");
2153
1580
  }
2154
- console.log(`Retrieved ${channels.length} active channels`);
2155
1581
  return channels;
2156
1582
  } catch (error) {
2157
- const message = getErrorMessage2(error);
1583
+ const message = getErrorMessage(error);
2158
1584
  console.error(`Error listing channels:`, message);
2159
1585
  throw new Error(`Failed to list channels: ${message}`);
2160
1586
  }
@@ -2581,10 +2007,10 @@ var Endpoints = class {
2581
2007
  };
2582
2008
 
2583
2009
  // src/ari-client/resources/playbacks.ts
2584
- import { EventEmitter as EventEmitter3 } from "events";
2585
- import { isAxiosError as isAxiosError4 } from "axios";
2586
- var getErrorMessage3 = (error) => {
2587
- if (isAxiosError4(error)) {
2010
+ import { EventEmitter as EventEmitter2 } from "events";
2011
+ import { isAxiosError as isAxiosError3 } from "axios";
2012
+ var getErrorMessage2 = (error) => {
2013
+ if (isAxiosError3(error)) {
2588
2014
  return error.response?.data?.message || error.message || "An axios error occurred";
2589
2015
  }
2590
2016
  if (error instanceof Error) {
@@ -2605,9 +2031,8 @@ var PlaybackInstance = class {
2605
2031
  this.baseClient = baseClient;
2606
2032
  this.playbackId = playbackId;
2607
2033
  this.id = playbackId;
2608
- console.log(`PlaybackInstance initialized with ID: ${this.id}`);
2609
2034
  }
2610
- eventEmitter = new EventEmitter3();
2035
+ eventEmitter = new EventEmitter2();
2611
2036
  playbackData = null;
2612
2037
  id;
2613
2038
  /**
@@ -2626,9 +2051,6 @@ var PlaybackInstance = class {
2626
2051
  }
2627
2052
  };
2628
2053
  this.eventEmitter.on(event, wrappedListener);
2629
- console.log(
2630
- `Event listener registered for ${event} on playback ${this.id}`
2631
- );
2632
2054
  }
2633
2055
  /**
2634
2056
  * Registers a one-time event listener for a specific WebSocket event type.
@@ -2646,9 +2068,6 @@ var PlaybackInstance = class {
2646
2068
  }
2647
2069
  };
2648
2070
  this.eventEmitter.once(event, wrappedListener);
2649
- console.log(
2650
- `One-time event listener registered for ${event} on playback ${this.id}`
2651
- );
2652
2071
  }
2653
2072
  /**
2654
2073
  * Removes event listener(s) for a specific WebSocket event type.
@@ -2662,12 +2081,8 @@ var PlaybackInstance = class {
2662
2081
  }
2663
2082
  if (listener) {
2664
2083
  this.eventEmitter.off(event, listener);
2665
- console.log(
2666
- `Specific listener removed for ${event} on playback ${this.id}`
2667
- );
2668
2084
  } else {
2669
2085
  this.eventEmitter.removeAllListeners(event);
2670
- console.log(`All listeners removed for ${event} on playback ${this.id}`);
2671
2086
  }
2672
2087
  }
2673
2088
  /**
@@ -2682,7 +2097,6 @@ var PlaybackInstance = class {
2682
2097
  }
2683
2098
  if ("playback" in event && event.playback?.id === this.id) {
2684
2099
  this.eventEmitter.emit(event.type, event);
2685
- console.log(`Event ${event.type} emitted for playback ${this.id}`);
2686
2100
  }
2687
2101
  }
2688
2102
  /**
@@ -2699,11 +2113,10 @@ var PlaybackInstance = class {
2699
2113
  this.playbackData = await this.baseClient.get(
2700
2114
  `/playbacks/${this.id}`
2701
2115
  );
2702
- console.log(`Retrieved playback data for ${this.id}`);
2703
2116
  return this.playbackData;
2704
2117
  } catch (error) {
2705
- const message = getErrorMessage3(error);
2706
- console.error(`Error retrieving playback data for ${this.id}:`, message);
2118
+ const message = getErrorMessage2(error);
2119
+ console.warn(`Error retrieving playback data for ${this.id}:`, message);
2707
2120
  throw new Error(`Failed to get playback data: ${message}`);
2708
2121
  }
2709
2122
  }
@@ -2721,12 +2134,9 @@ var PlaybackInstance = class {
2721
2134
  await this.baseClient.post(
2722
2135
  `/playbacks/${this.id}/control?operation=${operation}`
2723
2136
  );
2724
- console.log(
2725
- `Operation ${operation} executed successfully on playback ${this.id}`
2726
- );
2727
2137
  } catch (error) {
2728
- const message = getErrorMessage3(error);
2729
- console.error(`Error controlling playback ${this.id}:`, message);
2138
+ const message = getErrorMessage2(error);
2139
+ console.warn(`Error controlling playback ${this.id}:`, message);
2730
2140
  throw new Error(`Failed to control playback: ${message}`);
2731
2141
  }
2732
2142
  }
@@ -2741,10 +2151,9 @@ var PlaybackInstance = class {
2741
2151
  }
2742
2152
  try {
2743
2153
  await this.baseClient.delete(`/playbacks/${this.id}`);
2744
- console.log(`Playback ${this.id} stopped successfully`);
2745
2154
  } catch (error) {
2746
- const message = getErrorMessage3(error);
2747
- console.error(`Error stopping playback ${this.id}:`, message);
2155
+ const message = getErrorMessage2(error);
2156
+ console.warn(`Error stopping playback ${this.id}:`, message);
2748
2157
  throw new Error(`Failed to stop playback: ${message}`);
2749
2158
  }
2750
2159
  }
@@ -2753,7 +2162,6 @@ var PlaybackInstance = class {
2753
2162
  */
2754
2163
  removeAllListeners() {
2755
2164
  this.eventEmitter.removeAllListeners();
2756
- console.log(`All listeners removed from playback ${this.id}`);
2757
2165
  }
2758
2166
  /**
2759
2167
  * Checks if the playback instance has any listeners for a specific event.
@@ -2791,20 +2199,17 @@ var Playbacks = class {
2791
2199
  if (!id) {
2792
2200
  const instance = new PlaybackInstance(this.client, this.baseClient);
2793
2201
  this.playbackInstances.set(instance.id, instance);
2794
- console.log(`New playback instance created with ID: ${instance.id}`);
2795
2202
  return instance;
2796
2203
  }
2797
2204
  if (!this.playbackInstances.has(id)) {
2798
2205
  const instance = new PlaybackInstance(this.client, this.baseClient, id);
2799
2206
  this.playbackInstances.set(id, instance);
2800
- console.log(`New playback instance created with provided ID: ${id}`);
2801
2207
  return instance;
2802
2208
  }
2803
- console.log(`Returning existing playback instance: ${id}`);
2804
2209
  return this.playbackInstances.get(id);
2805
2210
  } catch (error) {
2806
- const message = getErrorMessage3(error);
2807
- console.error(`Error creating/retrieving playback instance:`, message);
2211
+ const message = getErrorMessage2(error);
2212
+ console.warn(`Error creating/retrieving playback instance:`, message);
2808
2213
  throw new Error(`Failed to manage playback instance: ${message}`);
2809
2214
  }
2810
2215
  }
@@ -2821,7 +2226,6 @@ var Playbacks = class {
2821
2226
  const instance = this.playbackInstances.get(playbackId);
2822
2227
  instance?.removeAllListeners();
2823
2228
  this.playbackInstances.delete(playbackId);
2824
- console.log(`Playback instance removed: ${playbackId}`);
2825
2229
  } else {
2826
2230
  console.warn(`Attempt to remove non-existent instance: ${playbackId}`);
2827
2231
  }
@@ -2832,16 +2236,12 @@ var Playbacks = class {
2832
2236
  */
2833
2237
  propagateEventToPlayback(event) {
2834
2238
  if (!event) {
2835
- console.warn("Invalid WebSocket event received");
2836
2239
  return;
2837
2240
  }
2838
2241
  if ("playback" in event && event.playback?.id) {
2839
2242
  const instance = this.playbackInstances.get(event.playback.id);
2840
2243
  if (instance) {
2841
2244
  instance.emitEvent(event);
2842
- console.log(
2843
- `Event propagated to playback ${event.playback.id}: ${event.type}`
2844
- );
2845
2245
  } else {
2846
2246
  console.warn(`No instance found for playback ${event.playback.id}`);
2847
2247
  }
@@ -2853,15 +2253,15 @@ var Playbacks = class {
2853
2253
  * @returns {Promise<Playback>} Promise resolving to playback details
2854
2254
  * @throws {Error} If the playback ID is invalid or the request fails
2855
2255
  */
2856
- async getDetails(playbackId) {
2256
+ async get(playbackId) {
2857
2257
  if (!playbackId) {
2858
2258
  throw new Error("Playback ID is required");
2859
2259
  }
2860
2260
  try {
2861
2261
  return await this.baseClient.get(`/playbacks/${playbackId}`);
2862
2262
  } catch (error) {
2863
- const message = getErrorMessage3(error);
2864
- console.error(`Error getting playback details ${playbackId}:`, message);
2263
+ const message = getErrorMessage2(error);
2264
+ console.warn(`Error getting playback details ${playbackId}:`, message);
2865
2265
  throw new Error(`Failed to get playback details: ${message}`);
2866
2266
  }
2867
2267
  }
@@ -2878,10 +2278,9 @@ var Playbacks = class {
2878
2278
  try {
2879
2279
  const playback = this.Playback({ id: playbackId });
2880
2280
  await playback.control(operation);
2881
- console.log(`Operation ${operation} executed on playback ${playbackId}`);
2882
2281
  } catch (error) {
2883
- const message = getErrorMessage3(error);
2884
- console.error(`Error controlling playback ${playbackId}:`, message);
2282
+ const message = getErrorMessage2(error);
2283
+ console.warn(`Error controlling playback ${playbackId}:`, message);
2885
2284
  throw new Error(`Failed to control playback: ${message}`);
2886
2285
  }
2887
2286
  }
@@ -2897,10 +2296,9 @@ var Playbacks = class {
2897
2296
  try {
2898
2297
  const playback = this.Playback({ id: playbackId });
2899
2298
  await playback.stop();
2900
- console.log(`Playback ${playbackId} stopped`);
2901
2299
  } catch (error) {
2902
- const message = getErrorMessage3(error);
2903
- console.error(`Error stopping playback ${playbackId}:`, message);
2300
+ const message = getErrorMessage2(error);
2301
+ console.warn(`Error stopping playback ${playbackId}:`, message);
2904
2302
  throw new Error(`Failed to stop playback: ${message}`);
2905
2303
  }
2906
2304
  }
@@ -2954,19 +2352,24 @@ var Sounds = class {
2954
2352
 
2955
2353
  // src/ari-client/websocketClient.ts
2956
2354
  var import_exponential_backoff = __toESM(require_backoff(), 1);
2957
- import { EventEmitter as EventEmitter4 } from "events";
2355
+ import { EventEmitter as EventEmitter3 } from "events";
2958
2356
  import WebSocket from "ws";
2959
2357
  var DEFAULT_MAX_RECONNECT_ATTEMPTS = 10;
2960
2358
  var DEFAULT_STARTING_DELAY = 500;
2961
2359
  var DEFAULT_MAX_DELAY = 1e4;
2962
- var WebSocketClient = class extends EventEmitter4 {
2360
+ var WebSocketClient = class extends EventEmitter3 {
2963
2361
  /**
2964
- * Creates a new WebSocket client instance.
2362
+ * Creates a new WebSocketClient instance.
2363
+ *
2364
+ * This constructor initializes a WebSocketClient with the necessary dependencies and configuration.
2365
+ * It ensures that at least one application name is provided.
2366
+ *
2367
+ * @param baseClient - The BaseClient instance used for basic ARI operations and authentication.
2368
+ * @param apps - An array of application names to connect to via the WebSocket.
2369
+ * @param subscribedEvents - Optional. An array of WebSocketEventTypes to subscribe to. If not provided, all events will be subscribed.
2370
+ * @param ariClient - Optional. The AriClient instance, used for creating Channel and Playback instances when processing events.
2965
2371
  *
2966
- * @param {BaseClient} baseClient - The base client containing connection details
2967
- * @param {string[]} apps - List of applications to connect to
2968
- * @param {WebSocketEventType[]} [subscribedEvents] - Optional list of events to subscribe to
2969
- * @param {AriClient} [ariClient] - Optional ARI client for handling channel and playback events
2372
+ * @throws {Error} Throws an error if the apps array is empty.
2970
2373
  */
2971
2374
  constructor(baseClient, apps, subscribedEvents, ariClient) {
2972
2375
  super();
@@ -2981,6 +2384,8 @@ var WebSocketClient = class extends EventEmitter4 {
2981
2384
  ws;
2982
2385
  isReconnecting = false;
2983
2386
  maxReconnectAttempts = DEFAULT_MAX_RECONNECT_ATTEMPTS;
2387
+ reconnectionAttempts = 0;
2388
+ lastWsUrl = "";
2984
2389
  backOffOptions = {
2985
2390
  numOfAttempts: DEFAULT_MAX_RECONNECT_ATTEMPTS,
2986
2391
  startingDelay: DEFAULT_STARTING_DELAY,
@@ -2997,10 +2402,14 @@ var WebSocketClient = class extends EventEmitter4 {
2997
2402
  }
2998
2403
  };
2999
2404
  /**
3000
- * Establishes a WebSocket connection.
2405
+ * Establishes a WebSocket connection to the Asterisk server.
3001
2406
  *
3002
- * @returns {Promise<void>} Resolves when connection is established
3003
- * @throws {Error} If connection fails
2407
+ * This method constructs the WebSocket URL using the base URL, credentials,
2408
+ * application names, and subscribed events. It then initiates the connection
2409
+ * using the constructed URL.
2410
+ *
2411
+ * @returns A Promise that resolves when the WebSocket connection is successfully established.
2412
+ * @throws Will throw an error if the connection cannot be established.
3004
2413
  */
3005
2414
  async connect() {
3006
2415
  const { baseUrl, username, password } = this.baseClient.getCredentials();
@@ -3015,15 +2424,24 @@ var WebSocketClient = class extends EventEmitter4 {
3015
2424
  } else {
3016
2425
  queryParams.append("subscribeAll", "true");
3017
2426
  }
3018
- const wsUrl = `${protocol}://${encodeURIComponent(username)}:${encodeURIComponent(password)}@${normalizedHost}/ari/events?${queryParams.toString()}`;
2427
+ this.lastWsUrl = `${protocol}://${encodeURIComponent(username)}:${encodeURIComponent(password)}@${normalizedHost}/ari/events?${queryParams.toString()}`;
3019
2428
  console.log("Connecting to WebSocket...");
3020
- return this.initializeWebSocket(wsUrl);
2429
+ return this.initializeWebSocket(this.lastWsUrl);
3021
2430
  }
3022
2431
  /**
3023
- * Initializes WebSocket connection with reconnection logic.
2432
+ * Initializes a WebSocket connection with exponential backoff retry mechanism.
3024
2433
  *
3025
- * @param {string} wsUrl - The WebSocket URL to connect to
3026
- * @returns {Promise<void>} Resolves when connection is established
2434
+ * This method attempts to establish a WebSocket connection to the specified URL.
2435
+ * It sets up event listeners for the WebSocket's 'open', 'message', 'close', and 'error' events.
2436
+ * If the connection is successful, it emits a 'connected' event. If it's a reconnection,
2437
+ * it also emits a 'reconnected' event with the current apps and subscribed events.
2438
+ * In case of connection failure, it uses an exponential backoff strategy to retry.
2439
+ *
2440
+ * @param wsUrl - The WebSocket URL to connect to.
2441
+ * @returns A Promise that resolves when the connection is successfully established,
2442
+ * or rejects if an error occurs during the connection process.
2443
+ * @throws Will throw an error if the WebSocket connection cannot be established
2444
+ * after the maximum number of retry attempts.
3027
2445
  */
3028
2446
  async initializeWebSocket(wsUrl) {
3029
2447
  return (0, import_exponential_backoff.backOff)(async () => {
@@ -3032,7 +2450,14 @@ var WebSocketClient = class extends EventEmitter4 {
3032
2450
  this.ws = new WebSocket(wsUrl);
3033
2451
  this.ws.on("open", () => {
3034
2452
  console.log("WebSocket connection established successfully");
2453
+ if (this.isReconnecting) {
2454
+ this.emit("reconnected", {
2455
+ apps: this.apps,
2456
+ subscribedEvents: this.subscribedEvents
2457
+ });
2458
+ }
3035
2459
  this.isReconnecting = false;
2460
+ this.reconnectionAttempts = 0;
3036
2461
  this.emit("connected");
3037
2462
  resolve();
3038
2463
  });
@@ -3042,13 +2467,13 @@ var WebSocketClient = class extends EventEmitter4 {
3042
2467
  `WebSocket disconnected with code ${code}. Attempting to reconnect...`
3043
2468
  );
3044
2469
  if (!this.isReconnecting) {
3045
- this.reconnect(wsUrl);
2470
+ this.reconnect(this.lastWsUrl);
3046
2471
  }
3047
2472
  });
3048
2473
  this.ws.on("error", (err) => {
3049
2474
  console.error("WebSocket error:", err.message);
3050
2475
  if (!this.isReconnecting) {
3051
- this.reconnect(wsUrl);
2476
+ this.reconnect(this.lastWsUrl);
3052
2477
  }
3053
2478
  reject(err);
3054
2479
  });
@@ -3059,9 +2484,16 @@ var WebSocketClient = class extends EventEmitter4 {
3059
2484
  }, this.backOffOptions);
3060
2485
  }
3061
2486
  /**
3062
- * Processes incoming WebSocket messages.
2487
+ * Handles incoming WebSocket messages by parsing and processing events.
2488
+ *
2489
+ * This method parses the raw message into a WebSocketEvent, filters it based on
2490
+ * subscribed events (if any), processes channel and playback events, and emits
2491
+ * the event to listeners. It also handles any errors that occur during processing.
3063
2492
  *
3064
- * @param {string} rawMessage - The raw message received from WebSocket
2493
+ * @param rawMessage - The raw message string received from the WebSocket connection.
2494
+ * @returns void This method doesn't return a value but emits events.
2495
+ *
2496
+ * @throws Will emit an 'error' event if the message cannot be parsed or processed.
3065
2497
  */
3066
2498
  handleMessage(rawMessage) {
3067
2499
  try {
@@ -3079,13 +2511,7 @@ var WebSocketClient = class extends EventEmitter4 {
3079
2511
  instancePlayback.emitEvent(event);
3080
2512
  event.instancePlayback = instancePlayback;
3081
2513
  }
3082
- if ("bridge" in event && event.bridge?.id && this.ariClient) {
3083
- const instanceBridge = this.ariClient.Bridge(event.bridge.id);
3084
- instanceBridge.emitEvent(event);
3085
- event.instanceBridge = instanceBridge;
3086
- }
3087
2514
  this.emit(event.type, event);
3088
- console.log(`Event processed: ${event.type}`);
3089
2515
  } catch (error) {
3090
2516
  console.error(
3091
2517
  "Error processing WebSocket message:",
@@ -3095,18 +2521,27 @@ var WebSocketClient = class extends EventEmitter4 {
3095
2521
  }
3096
2522
  }
3097
2523
  /**
3098
- * Attempts to reconnect to the WebSocket.
2524
+ * Attempts to reconnect to the WebSocket server using an exponential backoff strategy.
2525
+ *
2526
+ * This method is called when the WebSocket connection is closed unexpectedly.
2527
+ * It increments the reconnection attempt counter, logs the attempt, and uses
2528
+ * the backOff utility to retry the connection with exponential delays between attempts.
3099
2529
  *
3100
- * @param {string} wsUrl - The WebSocket URL to reconnect to
2530
+ * @param wsUrl - The WebSocket URL to reconnect to.
2531
+ * @returns void - This method doesn't return a value.
2532
+ *
2533
+ * @emits reconnectFailed - Emitted if all reconnection attempts fail.
3101
2534
  */
3102
2535
  reconnect(wsUrl) {
3103
2536
  this.isReconnecting = true;
3104
- console.log("Initiating reconnection attempt...");
3105
- this.removeAllListeners();
2537
+ this.reconnectionAttempts++;
2538
+ console.log(
2539
+ `Initiating reconnection attempt #${this.reconnectionAttempts}...`
2540
+ );
3106
2541
  (0, import_exponential_backoff.backOff)(() => this.initializeWebSocket(wsUrl), this.backOffOptions).catch(
3107
2542
  (error) => {
3108
2543
  console.error(
3109
- "Failed to reconnect after multiple attempts:",
2544
+ `Failed to reconnect after ${this.reconnectionAttempts} attempts:`,
3110
2545
  error instanceof Error ? error.message : "Unknown error"
3111
2546
  );
3112
2547
  this.emit("reconnectFailed", error);
@@ -3114,7 +2549,13 @@ var WebSocketClient = class extends EventEmitter4 {
3114
2549
  );
3115
2550
  }
3116
2551
  /**
3117
- * Manually closes the WebSocket connection.
2552
+ * Closes the WebSocket connection if it exists.
2553
+ *
2554
+ * This method attempts to gracefully close the WebSocket connection
2555
+ * and sets the WebSocket instance to undefined. If an error occurs
2556
+ * during the closing process, it will be caught and logged.
2557
+ *
2558
+ * @throws {Error} Logs an error message if closing the WebSocket fails.
3118
2559
  */
3119
2560
  close() {
3120
2561
  try {
@@ -3131,17 +2572,30 @@ var WebSocketClient = class extends EventEmitter4 {
3131
2572
  }
3132
2573
  }
3133
2574
  /**
3134
- * Checks if the WebSocket is currently connected.
2575
+ * Checks if the WebSocket connection is currently open and active.
2576
+ *
2577
+ * This method examines the readyState of the WebSocket instance to determine
2578
+ * if the connection is established and ready for communication.
3135
2579
  *
3136
- * @returns {boolean} True if connected, false otherwise
2580
+ * @returns {boolean} True if the WebSocket connection is open and ready,
2581
+ * false otherwise.
3137
2582
  */
3138
2583
  isConnected() {
3139
2584
  return this.ws?.readyState === WebSocket.OPEN;
3140
2585
  }
3141
2586
  /**
3142
- * Gets the current connection state.
2587
+ * Retrieves the current state of the WebSocket connection.
3143
2588
  *
3144
- * @returns {number} The WebSocket ready state
2589
+ * This method returns the readyState of the WebSocket instance, which
2590
+ * indicates the current state of the connection. If no WebSocket instance
2591
+ * exists, it returns the CLOSED state.
2592
+ *
2593
+ * @returns {number} The current state of the WebSocket connection.
2594
+ * Possible values are:
2595
+ * - WebSocket.CONNECTING (0): The connection is not yet open.
2596
+ * - WebSocket.OPEN (1): The connection is open and ready to communicate.
2597
+ * - WebSocket.CLOSING (2): The connection is in the process of closing.
2598
+ * - WebSocket.CLOSED (3): The connection is closed or couldn't be opened.
3145
2599
  */
3146
2600
  getState() {
3147
2601
  return this.ws?.readyState ?? WebSocket.CLOSED;
@@ -3167,11 +2621,11 @@ var AriClient = class {
3167
2621
  this.baseClient = new BaseClient(baseUrl, config.username, config.password);
3168
2622
  this.channels = new Channels(this.baseClient, this);
3169
2623
  this.playbacks = new Playbacks(this.baseClient, this);
3170
- this.bridges = new Bridges(this.baseClient, this);
3171
2624
  this.endpoints = new Endpoints(this.baseClient);
3172
2625
  this.applications = new Applications(this.baseClient);
3173
2626
  this.sounds = new Sounds(this.baseClient);
3174
2627
  this.asterisk = new Asterisk(this.baseClient);
2628
+ this.bridges = new Bridges(this.baseClient);
3175
2629
  console.log(`ARI Client initialized with base URL: ${baseUrl}`);
3176
2630
  }
3177
2631
  baseClient;
@@ -3287,20 +2741,6 @@ var AriClient = class {
3287
2741
  Playback(playbackId, _app) {
3288
2742
  return this.playbacks.Playback({ id: playbackId });
3289
2743
  }
3290
- /**
3291
- * Creates or retrieves a Bridge instance.
3292
- *
3293
- * This function allows you to create a new Bridge instance or retrieve an existing one
3294
- * based on the provided bridge ID.
3295
- *
3296
- * @param {string} [bridgeId] - Optional ID of an existing bridge. If provided, retrieves the
3297
- * existing bridge with this ID. If omitted, creates a new bridge.
3298
- * @returns {BridgeInstance} A new or existing Bridge instance that can be used to interact
3299
- * with the Asterisk bridge.
3300
- */
3301
- Bridge(bridgeId) {
3302
- return this.bridges.Bridge({ id: bridgeId });
3303
- }
3304
2744
  /**
3305
2745
  * Gets the current WebSocket connection status.
3306
2746
  *
@@ -3314,7 +2754,6 @@ export {
3314
2754
  Applications,
3315
2755
  AriClient,
3316
2756
  Asterisk,
3317
- BridgeInstance,
3318
2757
  Bridges,
3319
2758
  ChannelInstance,
3320
2759
  Channels,