@ipcom/asterisk-ari 0.0.142 → 0.0.144-beta

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/esm/index.js CHANGED
@@ -598,7 +598,9 @@ var BaseClient = class {
598
598
  this.username = username;
599
599
  this.password = password;
600
600
  if (!/^https?:\/\/.+/.test(baseUrl)) {
601
- throw new Error("Invalid base URL. It must start with http:// or https://");
601
+ throw new Error(
602
+ "Invalid base URL. It must start with http:// or https://"
603
+ );
602
604
  }
603
605
  this.client = axios.create({
604
606
  baseURL: baseUrl,
@@ -792,7 +794,7 @@ var Applications = class {
792
794
  }
793
795
  /**
794
796
  * Lists all applications.
795
- *
797
+ *
796
798
  * @returns A promise that resolves to an array of Application objects.
797
799
  * @throws {Error} If the API response is not an array.
798
800
  */
@@ -805,7 +807,7 @@ var Applications = class {
805
807
  }
806
808
  /**
807
809
  * Retrieves details of a specific application.
808
- *
810
+ *
809
811
  * @param appName - The name of the application to retrieve details for.
810
812
  * @returns A promise that resolves to an ApplicationDetails object.
811
813
  * @throws {Error} If there's an error fetching the application details.
@@ -822,7 +824,7 @@ var Applications = class {
822
824
  }
823
825
  /**
824
826
  * Sends a message to a specific application.
825
- *
827
+ *
826
828
  * @param appName - The name of the application to send the message to.
827
829
  * @param body - The message to be sent, containing an event and optional data.
828
830
  * @returns A promise that resolves when the message is successfully sent.
@@ -915,97 +917,681 @@ var Asterisk = class {
915
917
  };
916
918
 
917
919
  // 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
+ };
918
1231
  var Bridges = class {
919
- constructor(client) {
1232
+ constructor(baseClient, client) {
1233
+ this.baseClient = baseClient;
920
1234
  this.client = client;
921
1235
  }
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
+ }
922
1274
  /**
923
- * Lists all active bridges.
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
+ * }
924
1352
  */
925
1353
  async list() {
926
- return this.client.get("/bridges");
1354
+ return this.baseClient.get("/bridges");
927
1355
  }
928
1356
  /**
929
- * Creates a new bridge.
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.
930
1371
  */
931
1372
  async createBridge(request) {
932
- return this.client.post("/bridges", request);
1373
+ return this.baseClient.post("/bridges", request);
933
1374
  }
934
1375
  /**
935
- * Retrieves details of a specific bridge.
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.
936
1391
  */
937
- async getDetails(bridgeId) {
938
- return this.client.get(`/bridges/${bridgeId}`);
1392
+ async get(bridgeId) {
1393
+ return this.baseClient.get(`/bridges/${bridgeId}`);
939
1394
  }
940
1395
  /**
941
- * Destroys (deletes) a specific bridge.
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.
942
1410
  */
943
1411
  async destroy(bridgeId) {
944
- return this.client.delete(`/bridges/${bridgeId}`);
1412
+ return this.baseClient.delete(`/bridges/${bridgeId}`);
945
1413
  }
946
1414
  /**
947
- * Adds a channel or multiple channels to a bridge.
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.
948
1429
  */
949
1430
  async addChannels(bridgeId, request) {
950
- const queryParams = new URLSearchParams({
1431
+ const queryParams = toQueryParams2({
951
1432
  channel: Array.isArray(request.channel) ? request.channel.join(",") : request.channel,
952
1433
  ...request.role && { role: request.role }
953
- }).toString();
954
- await this.client.post(
1434
+ });
1435
+ await this.baseClient.post(
955
1436
  `/bridges/${bridgeId}/addChannel?${queryParams}`
956
1437
  );
957
1438
  }
958
1439
  /**
959
- * Removes a channel or multiple channels from a bridge.
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.
960
1453
  */
961
1454
  async removeChannels(bridgeId, request) {
962
- const queryParams = new URLSearchParams({
1455
+ const queryParams = toQueryParams2({
963
1456
  channel: Array.isArray(request.channel) ? request.channel.join(",") : request.channel
964
- }).toString();
965
- await this.client.post(
1457
+ });
1458
+ await this.baseClient.post(
966
1459
  `/bridges/${bridgeId}/removeChannel?${queryParams}`
967
1460
  );
968
1461
  }
969
1462
  /**
970
- * Plays media to a bridge.
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.
971
1479
  */
972
1480
  async playMedia(bridgeId, request) {
973
- const queryParams = new URLSearchParams({
1481
+ const queryParams = toQueryParams2({
974
1482
  ...request.lang && { lang: request.lang },
975
1483
  ...request.offsetms && { offsetms: request.offsetms.toString() },
976
1484
  ...request.skipms && { skipms: request.skipms.toString() },
977
1485
  ...request.playbackId && { playbackId: request.playbackId }
978
- }).toString();
979
- return this.client.post(
1486
+ });
1487
+ return this.baseClient.post(
980
1488
  `/bridges/${bridgeId}/play?${queryParams}`,
981
1489
  { media: request.media }
982
1490
  );
983
1491
  }
984
1492
  /**
985
- * Stops media playback on a bridge.
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.
986
1505
  */
987
1506
  async stopPlayback(bridgeId, playbackId) {
988
- await this.client.delete(`/bridges/${bridgeId}/play/${playbackId}`);
1507
+ await this.baseClient.delete(
1508
+ `/bridges/${bridgeId}/play/${playbackId}`
1509
+ );
989
1510
  }
990
1511
  /**
991
- * Sets the video source for a bridge.
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.
992
1524
  */
993
1525
  async setVideoSource(bridgeId, channelId) {
994
- await this.client.post(
995
- `/bridges/${bridgeId}/videoSource?channelId=${encodeURIComponent(channelId)}`
1526
+ const queryParams = toQueryParams2({ channelId });
1527
+ await this.baseClient.post(
1528
+ `/bridges/${bridgeId}/videoSource?${queryParams}`
996
1529
  );
997
1530
  }
998
1531
  /**
999
- * Clears the video source for a bridge.
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.
1000
1545
  */
1001
1546
  async clearVideoSource(bridgeId) {
1002
- await this.client.delete(`/bridges/${bridgeId}/videoSource`);
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);
1003
1589
  }
1004
1590
  };
1005
1591
 
1006
1592
  // src/ari-client/resources/channels.ts
1007
- import { EventEmitter } from "events";
1008
- import { isAxiosError as isAxiosError2 } from "axios";
1593
+ import { EventEmitter as EventEmitter2 } from "events";
1594
+ import { isAxiosError as isAxiosError3 } from "axios";
1009
1595
 
1010
1596
  // node_modules/uuid/dist/esm/stringify.js
1011
1597
  var byteToHex = [];
@@ -1052,16 +1638,9 @@ function v4(options, buf, offset) {
1052
1638
  }
1053
1639
  var v4_default = v4;
1054
1640
 
1055
- // src/ari-client/utils.ts
1056
- function toQueryParams2(options) {
1057
- return new URLSearchParams(
1058
- Object.entries(options).filter(([, value]) => value !== void 0).map(([key, value]) => [key, value])
1059
- ).toString();
1060
- }
1061
-
1062
1641
  // src/ari-client/resources/channels.ts
1063
- var getErrorMessage = (error) => {
1064
- if (isAxiosError2(error)) {
1642
+ var getErrorMessage2 = (error) => {
1643
+ if (isAxiosError3(error)) {
1065
1644
  return error.response?.data?.message || error.message || "An axios error occurred";
1066
1645
  }
1067
1646
  if (error instanceof Error) {
@@ -1076,7 +1655,7 @@ var ChannelInstance = class {
1076
1655
  this.id = channelId || `channel-${Date.now()}`;
1077
1656
  console.log(`Channel instance initialized with ID: ${this.id}`);
1078
1657
  }
1079
- eventEmitter = new EventEmitter();
1658
+ eventEmitter = new EventEmitter2();
1080
1659
  channelData = null;
1081
1660
  id;
1082
1661
  /**
@@ -1165,7 +1744,7 @@ var ChannelInstance = class {
1165
1744
  await this.baseClient.post(`/channels/${this.id}/answer`);
1166
1745
  console.log(`Channel ${this.id} answered`);
1167
1746
  } catch (error) {
1168
- const message = getErrorMessage(error);
1747
+ const message = getErrorMessage2(error);
1169
1748
  console.error(`Error answering channel ${this.id}:`, message);
1170
1749
  throw new Error(`Failed to answer channel: ${message}`);
1171
1750
  }
@@ -1191,7 +1770,7 @@ var ChannelInstance = class {
1191
1770
  );
1192
1771
  return this.channelData;
1193
1772
  } catch (error) {
1194
- const message = getErrorMessage(error);
1773
+ const message = getErrorMessage2(error);
1195
1774
  console.error(`Error originating channel:`, message);
1196
1775
  throw new Error(`Failed to originate channel: ${message}`);
1197
1776
  }
@@ -1216,7 +1795,7 @@ var ChannelInstance = class {
1216
1795
  console.log(`Media playback started on channel ${this.id}`);
1217
1796
  return playback;
1218
1797
  } catch (error) {
1219
- const message = getErrorMessage(error);
1798
+ const message = getErrorMessage2(error);
1220
1799
  console.error(`Error playing media on channel ${this.id}:`, message);
1221
1800
  throw new Error(`Failed to play media: ${message}`);
1222
1801
  }
@@ -1239,7 +1818,7 @@ var ChannelInstance = class {
1239
1818
  console.log(`Retrieved channel details for ${this.id}`);
1240
1819
  return details;
1241
1820
  } catch (error) {
1242
- const message = getErrorMessage(error);
1821
+ const message = getErrorMessage2(error);
1243
1822
  console.error(
1244
1823
  `Error retrieving channel details for ${this.id}:`,
1245
1824
  message
@@ -1484,7 +2063,7 @@ var Channels = class {
1484
2063
  console.log(`Returning existing channel instance: ${id}`);
1485
2064
  return this.channelInstances.get(id);
1486
2065
  } catch (error) {
1487
- const message = getErrorMessage(error);
2066
+ const message = getErrorMessage2(error);
1488
2067
  console.error(`Error creating/retrieving channel instance:`, message);
1489
2068
  throw new Error(`Failed to manage channel instance: ${message}`);
1490
2069
  }
@@ -1498,14 +2077,14 @@ var Channels = class {
1498
2077
  */
1499
2078
  async get(id) {
1500
2079
  try {
1501
- if (id) {
2080
+ if (!id) {
1502
2081
  throw new Error("No channel ID associated with this instance");
1503
2082
  }
1504
2083
  const details = await this.baseClient.get(`/channels/${id}`);
1505
2084
  console.log(`Retrieved channel details for ${id}`);
1506
2085
  return details;
1507
2086
  } catch (error) {
1508
- const message = getErrorMessage(error);
2087
+ const message = getErrorMessage2(error);
1509
2088
  console.error(`Error retrieving channel details for ${id}:`, message);
1510
2089
  throw new Error(`Failed to get channel details: ${message}`);
1511
2090
  }
@@ -1558,7 +2137,7 @@ var Channels = class {
1558
2137
  console.log(`Channel originated successfully with ID: ${channel.id}`);
1559
2138
  return channel;
1560
2139
  } catch (error) {
1561
- const message = getErrorMessage(error);
2140
+ const message = getErrorMessage2(error);
1562
2141
  console.error(`Error originating channel:`, message);
1563
2142
  throw new Error(`Failed to originate channel: ${message}`);
1564
2143
  }
@@ -1575,7 +2154,7 @@ var Channels = class {
1575
2154
  console.log(`Retrieved ${channels.length} active channels`);
1576
2155
  return channels;
1577
2156
  } catch (error) {
1578
- const message = getErrorMessage(error);
2157
+ const message = getErrorMessage2(error);
1579
2158
  console.error(`Error listing channels:`, message);
1580
2159
  throw new Error(`Failed to list channels: ${message}`);
1581
2160
  }
@@ -2002,10 +2581,10 @@ var Endpoints = class {
2002
2581
  };
2003
2582
 
2004
2583
  // src/ari-client/resources/playbacks.ts
2005
- import { EventEmitter as EventEmitter2 } from "events";
2006
- import { isAxiosError as isAxiosError3 } from "axios";
2007
- var getErrorMessage2 = (error) => {
2008
- if (isAxiosError3(error)) {
2584
+ import { EventEmitter as EventEmitter3 } from "events";
2585
+ import { isAxiosError as isAxiosError4 } from "axios";
2586
+ var getErrorMessage3 = (error) => {
2587
+ if (isAxiosError4(error)) {
2009
2588
  return error.response?.data?.message || error.message || "An axios error occurred";
2010
2589
  }
2011
2590
  if (error instanceof Error) {
@@ -2028,7 +2607,7 @@ var PlaybackInstance = class {
2028
2607
  this.id = playbackId;
2029
2608
  console.log(`PlaybackInstance initialized with ID: ${this.id}`);
2030
2609
  }
2031
- eventEmitter = new EventEmitter2();
2610
+ eventEmitter = new EventEmitter3();
2032
2611
  playbackData = null;
2033
2612
  id;
2034
2613
  /**
@@ -2123,7 +2702,7 @@ var PlaybackInstance = class {
2123
2702
  console.log(`Retrieved playback data for ${this.id}`);
2124
2703
  return this.playbackData;
2125
2704
  } catch (error) {
2126
- const message = getErrorMessage2(error);
2705
+ const message = getErrorMessage3(error);
2127
2706
  console.error(`Error retrieving playback data for ${this.id}:`, message);
2128
2707
  throw new Error(`Failed to get playback data: ${message}`);
2129
2708
  }
@@ -2146,7 +2725,7 @@ var PlaybackInstance = class {
2146
2725
  `Operation ${operation} executed successfully on playback ${this.id}`
2147
2726
  );
2148
2727
  } catch (error) {
2149
- const message = getErrorMessage2(error);
2728
+ const message = getErrorMessage3(error);
2150
2729
  console.error(`Error controlling playback ${this.id}:`, message);
2151
2730
  throw new Error(`Failed to control playback: ${message}`);
2152
2731
  }
@@ -2164,7 +2743,7 @@ var PlaybackInstance = class {
2164
2743
  await this.baseClient.delete(`/playbacks/${this.id}`);
2165
2744
  console.log(`Playback ${this.id} stopped successfully`);
2166
2745
  } catch (error) {
2167
- const message = getErrorMessage2(error);
2746
+ const message = getErrorMessage3(error);
2168
2747
  console.error(`Error stopping playback ${this.id}:`, message);
2169
2748
  throw new Error(`Failed to stop playback: ${message}`);
2170
2749
  }
@@ -2224,7 +2803,7 @@ var Playbacks = class {
2224
2803
  console.log(`Returning existing playback instance: ${id}`);
2225
2804
  return this.playbackInstances.get(id);
2226
2805
  } catch (error) {
2227
- const message = getErrorMessage2(error);
2806
+ const message = getErrorMessage3(error);
2228
2807
  console.error(`Error creating/retrieving playback instance:`, message);
2229
2808
  throw new Error(`Failed to manage playback instance: ${message}`);
2230
2809
  }
@@ -2281,7 +2860,7 @@ var Playbacks = class {
2281
2860
  try {
2282
2861
  return await this.baseClient.get(`/playbacks/${playbackId}`);
2283
2862
  } catch (error) {
2284
- const message = getErrorMessage2(error);
2863
+ const message = getErrorMessage3(error);
2285
2864
  console.error(`Error getting playback details ${playbackId}:`, message);
2286
2865
  throw new Error(`Failed to get playback details: ${message}`);
2287
2866
  }
@@ -2301,7 +2880,7 @@ var Playbacks = class {
2301
2880
  await playback.control(operation);
2302
2881
  console.log(`Operation ${operation} executed on playback ${playbackId}`);
2303
2882
  } catch (error) {
2304
- const message = getErrorMessage2(error);
2883
+ const message = getErrorMessage3(error);
2305
2884
  console.error(`Error controlling playback ${playbackId}:`, message);
2306
2885
  throw new Error(`Failed to control playback: ${message}`);
2307
2886
  }
@@ -2320,7 +2899,7 @@ var Playbacks = class {
2320
2899
  await playback.stop();
2321
2900
  console.log(`Playback ${playbackId} stopped`);
2322
2901
  } catch (error) {
2323
- const message = getErrorMessage2(error);
2902
+ const message = getErrorMessage3(error);
2324
2903
  console.error(`Error stopping playback ${playbackId}:`, message);
2325
2904
  throw new Error(`Failed to stop playback: ${message}`);
2326
2905
  }
@@ -2375,12 +2954,12 @@ var Sounds = class {
2375
2954
 
2376
2955
  // src/ari-client/websocketClient.ts
2377
2956
  var import_exponential_backoff = __toESM(require_backoff(), 1);
2378
- import { EventEmitter as EventEmitter3 } from "events";
2957
+ import { EventEmitter as EventEmitter4 } from "events";
2379
2958
  import WebSocket from "ws";
2380
2959
  var DEFAULT_MAX_RECONNECT_ATTEMPTS = 10;
2381
2960
  var DEFAULT_STARTING_DELAY = 500;
2382
2961
  var DEFAULT_MAX_DELAY = 1e4;
2383
- var WebSocketClient = class extends EventEmitter3 {
2962
+ var WebSocketClient = class extends EventEmitter4 {
2384
2963
  /**
2385
2964
  * Creates a new WebSocket client instance.
2386
2965
  *
@@ -2500,10 +3079,18 @@ var WebSocketClient = class extends EventEmitter3 {
2500
3079
  instancePlayback.emitEvent(event);
2501
3080
  event.instancePlayback = instancePlayback;
2502
3081
  }
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
+ }
2503
3087
  this.emit(event.type, event);
2504
3088
  console.log(`Event processed: ${event.type}`);
2505
3089
  } catch (error) {
2506
- console.error("Error processing WebSocket message:", error instanceof Error ? error.message : "Unknown error");
3090
+ console.error(
3091
+ "Error processing WebSocket message:",
3092
+ error instanceof Error ? error.message : "Unknown error"
3093
+ );
2507
3094
  this.emit("error", new Error("Failed to decode WebSocket message"));
2508
3095
  }
2509
3096
  }
@@ -2516,13 +3103,15 @@ var WebSocketClient = class extends EventEmitter3 {
2516
3103
  this.isReconnecting = true;
2517
3104
  console.log("Initiating reconnection attempt...");
2518
3105
  this.removeAllListeners();
2519
- (0, import_exponential_backoff.backOff)(() => this.initializeWebSocket(wsUrl), this.backOffOptions).catch((error) => {
2520
- console.error(
2521
- "Failed to reconnect after multiple attempts:",
2522
- error instanceof Error ? error.message : "Unknown error"
2523
- );
2524
- this.emit("reconnectFailed", error);
2525
- });
3106
+ (0, import_exponential_backoff.backOff)(() => this.initializeWebSocket(wsUrl), this.backOffOptions).catch(
3107
+ (error) => {
3108
+ console.error(
3109
+ "Failed to reconnect after multiple attempts:",
3110
+ error instanceof Error ? error.message : "Unknown error"
3111
+ );
3112
+ this.emit("reconnectFailed", error);
3113
+ }
3114
+ );
2526
3115
  }
2527
3116
  /**
2528
3117
  * Manually closes the WebSocket connection.
@@ -2578,11 +3167,11 @@ var AriClient = class {
2578
3167
  this.baseClient = new BaseClient(baseUrl, config.username, config.password);
2579
3168
  this.channels = new Channels(this.baseClient, this);
2580
3169
  this.playbacks = new Playbacks(this.baseClient, this);
3170
+ this.bridges = new Bridges(this.baseClient, this);
2581
3171
  this.endpoints = new Endpoints(this.baseClient);
2582
3172
  this.applications = new Applications(this.baseClient);
2583
3173
  this.sounds = new Sounds(this.baseClient);
2584
3174
  this.asterisk = new Asterisk(this.baseClient);
2585
- this.bridges = new Bridges(this.baseClient);
2586
3175
  console.log(`ARI Client initialized with base URL: ${baseUrl}`);
2587
3176
  }
2588
3177
  baseClient;
@@ -2698,6 +3287,20 @@ var AriClient = class {
2698
3287
  Playback(playbackId, _app) {
2699
3288
  return this.playbacks.Playback({ id: playbackId });
2700
3289
  }
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
+ }
2701
3304
  /**
2702
3305
  * Gets the current WebSocket connection status.
2703
3306
  *
@@ -2711,6 +3314,7 @@ export {
2711
3314
  Applications,
2712
3315
  AriClient,
2713
3316
  Asterisk,
3317
+ BridgeInstance,
2714
3318
  Bridges,
2715
3319
  ChannelInstance,
2716
3320
  Channels,