@ipcom/asterisk-ari 0.0.19 → 0.0.20

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.
@@ -644,8 +644,8 @@ var Applications = class {
644
644
  }
645
645
  /**
646
646
  * Lists all applications.
647
- *
648
- * @returns A promise that resolves to an array of Application objects representing all registered applications.
647
+ *
648
+ * @returns A promise that resolves to an array of Application objects.
649
649
  * @throws {Error} If the API response is not an array.
650
650
  */
651
651
  async list() {
@@ -657,19 +657,27 @@ var Applications = class {
657
657
  }
658
658
  /**
659
659
  * Retrieves details of a specific application.
660
- *
661
- * @param appName - The unique name of the application.
662
- * @returns A promise that resolves to an ApplicationDetails object containing the details of the specified application.
660
+ *
661
+ * @param appName - The name of the application to retrieve details for.
662
+ * @returns A promise that resolves to an ApplicationDetails object.
663
+ * @throws {Error} If there's an error fetching the application details.
663
664
  */
664
665
  async getDetails(appName) {
665
- return this.client.get(`/applications/${appName}`);
666
+ try {
667
+ return await this.client.get(
668
+ `/applications/${appName}`
669
+ );
670
+ } catch (error) {
671
+ console.error(`Erro ao obter detalhes do aplicativo ${appName}:`, error);
672
+ throw error;
673
+ }
666
674
  }
667
675
  /**
668
676
  * Sends a message to a specific application.
669
- *
670
- * @param appName - The unique name of the application.
671
- * @param body - The message body to send.
672
- * @returns A promise that resolves when the message is sent successfully.
677
+ *
678
+ * @param appName - The name of the application to send the message to.
679
+ * @param body - The message to be sent, containing an event and optional data.
680
+ * @returns A promise that resolves when the message is successfully sent.
673
681
  */
674
682
  async sendMessage(appName, body) {
675
683
  await this.client.post(`/applications/${appName}/messages`, body);
@@ -1206,13 +1214,17 @@ var WebSocketClient = class {
1206
1214
  throw new Error("WebSocket n\xE3o est\xE1 conectado.");
1207
1215
  }
1208
1216
  if (event === "message") {
1209
- this.ws.on(event, (data) => {
1217
+ this.ws.on(event, (rawData) => {
1210
1218
  try {
1211
- const decodedData = JSON.parse(data.toString());
1212
- callback(decodedData);
1219
+ const decodedData = JSON.parse(rawData.toString());
1220
+ if (decodedData?.type) {
1221
+ const matchedEvent = decodedData;
1222
+ callback(matchedEvent);
1223
+ } else {
1224
+ console.warn("Mensagem sem tipo:", decodedData);
1225
+ }
1213
1226
  } catch (err) {
1214
1227
  console.error("Erro ao decodificar mensagem do WebSocket:", err);
1215
- callback(data);
1216
1228
  }
1217
1229
  });
1218
1230
  } else {
@@ -1268,7 +1280,7 @@ var AriClient = class {
1268
1280
  * @param app - The application name to connect to.
1269
1281
  * @returns {Promise<void>} Resolves when the WebSocket connects successfully.
1270
1282
  */
1271
- async connectWebSocket(app) {
1283
+ async connectWebSocket(app, subscribedEvents) {
1272
1284
  if (!app) {
1273
1285
  throw new Error(
1274
1286
  "The 'app' parameter is required to connect to the WebSocket."
@@ -1279,8 +1291,9 @@ var AriClient = class {
1279
1291
  return;
1280
1292
  }
1281
1293
  this.isReconnecting = true;
1294
+ const eventsParam = subscribedEvents && subscribedEvents.length > 0 ? `&event=${subscribedEvents.join(",")}` : "&subscribeAll=true";
1282
1295
  const protocol = this.config.secure ? "wss" : "ws";
1283
- const wsUrl = `${protocol}://${encodeURIComponent(this.config.username)}:${encodeURIComponent(this.config.password)}@${this.config.host}:${this.config.port}/ari/events?app=${app}`;
1296
+ const wsUrl = `${protocol}://${encodeURIComponent(this.config.username)}:${encodeURIComponent(this.config.password)}@${this.config.host}:${this.config.port}/ari/events?app=${app}${eventsParam}`;
1284
1297
  const backoffOptions = {
1285
1298
  delayFirstAttempt: false,
1286
1299
  startingDelay: 1e3,
@@ -1344,16 +1357,22 @@ var AriClient = class {
1344
1357
  return this.wsClient ? this.wsClient.isConnected() : false;
1345
1358
  }
1346
1359
  /**
1347
- * Registers a callback for a specific WebSocket event.
1360
+ * Registers a callback function for WebSocket events.
1361
+ * This method allows you to listen for and respond to WebSocket messages
1362
+ * and process specific event types.
1348
1363
  *
1349
- * @param event - The WebSocket event to listen for.
1350
- * @param callback - The callback function to execute when the event occurs.
1364
+ * @param callback - The callback function to execute when a WebSocket message is received.
1365
+ * The function will receive the parsed event data as its parameter.
1366
+ * @throws {Error} Throws an error if the WebSocket is not connected when trying to register the event.
1367
+ * @returns {void}
1351
1368
  */
1352
- onWebSocketEvent(event, callback) {
1369
+ onWebSocketEvent(callback) {
1353
1370
  if (!this.wsClient) {
1354
1371
  throw new Error("WebSocket is not connected.");
1355
1372
  }
1356
- this.wsClient.on(event, callback);
1373
+ this.wsClient.on("message", (data) => {
1374
+ callback(data);
1375
+ });
1357
1376
  }
1358
1377
  /**
1359
1378
  * Closes the WebSocket connection.
@@ -1496,7 +1515,14 @@ var AriClient = class {
1496
1515
  return this.channels.snoopChannelWithId(channelId, snoopId, options);
1497
1516
  }
1498
1517
  /**
1499
- * Dials a created channel.
1518
+ * Initiates a dial operation on a previously created channel.
1519
+ * This function attempts to connect the specified channel to its configured destination.
1520
+ *
1521
+ * @param channelId - The unique identifier of the channel to dial.
1522
+ * @param caller - Optional. The caller ID to use for the outgoing call. If not provided, the default caller ID for the channel will be used.
1523
+ * @param timeout - Optional. The maximum time in seconds to wait for the dial operation to complete. If not specified, the system's default timeout will be used.
1524
+ * @returns A Promise that resolves when the dial operation has been initiated successfully. Note that this does not guarantee that the call was answered, only that dialing has begun.
1525
+ * @throws Will throw an error if the dial operation fails, e.g., if the channel doesn't exist or is in an invalid state.
1500
1526
  */
1501
1527
  async dialChannel(channelId, caller, timeout) {
1502
1528
  return this.channels.dial(channelId, caller, timeout);
@@ -1532,46 +1558,139 @@ var AriClient = class {
1532
1558
  return this.channels.ringChannel(channelId);
1533
1559
  }
1534
1560
  /**
1535
- * Stops ringing indication on a channel.
1561
+ * Stops the ringing indication on a specified channel in the Asterisk system.
1562
+ *
1563
+ * This function sends a request to the Asterisk server to cease the ringing
1564
+ * indication on a particular channel. This is typically used when you want to
1565
+ * stop the ringing sound on a channel without answering or hanging up the call.
1566
+ *
1567
+ * @param channelId - The unique identifier of the channel on which to stop the ringing.
1568
+ * This should be a string that uniquely identifies the channel in the Asterisk system.
1569
+ *
1570
+ * @returns A Promise that resolves when the ringing has been successfully stopped on the specified channel.
1571
+ * The promise resolves to void, indicating no specific return value.
1572
+ * If an error occurs during the operation, the promise will be rejected with an error object.
1536
1573
  */
1537
1574
  async stopRingChannel(channelId) {
1538
1575
  return this.channels.stopRingChannel(channelId);
1539
1576
  }
1540
1577
  /**
1541
- * Sends DTMF to a channel.
1578
+ * Sends DTMF (Dual-Tone Multi-Frequency) tones to a specified channel.
1579
+ *
1580
+ * This function allows sending DTMF tones to a channel, which can be used for various purposes
1581
+ * such as interacting with IVR systems or sending signals during a call.
1582
+ *
1583
+ * @param channelId - The unique identifier of the channel to send DTMF tones to.
1584
+ * @param dtmf - A string representing the DTMF tones to send (e.g., "123#").
1585
+ * @param options - Optional parameters to control the timing of DTMF tones.
1586
+ * @param options.before - The time (in milliseconds) to wait before sending DTMF.
1587
+ * @param options.between - The time (in milliseconds) to wait between each DTMF tone.
1588
+ * @param options.duration - The duration (in milliseconds) of each DTMF tone.
1589
+ * @param options.after - The time (in milliseconds) to wait after sending all DTMF tones.
1590
+ * @returns A Promise that resolves when the DTMF tones have been successfully sent.
1542
1591
  */
1543
1592
  async sendDTMF(channelId, dtmf, options) {
1544
1593
  return this.channels.sendDTMF(channelId, dtmf, options);
1545
1594
  }
1546
1595
  /**
1547
- * Mutes a channel.
1596
+ * Mutes a channel in the Asterisk system.
1597
+ *
1598
+ * This function initiates a mute operation on the specified channel, preventing
1599
+ * audio transmission in the specified direction(s).
1600
+ *
1601
+ * @param channelId - The unique identifier of the channel to be muted.
1602
+ * This should be a string that uniquely identifies the channel in the Asterisk system.
1603
+ * @param direction - The direction of audio to mute. Can be one of:
1604
+ * - "both": Mute both incoming and outgoing audio (default)
1605
+ * - "in": Mute only incoming audio
1606
+ * - "out": Mute only outgoing audio
1607
+ *
1608
+ * @returns A Promise that resolves when the mute operation has been successfully completed.
1609
+ * The promise resolves to void, indicating no specific return value.
1610
+ * If an error occurs during the operation, the promise will be rejected with an error object.
1548
1611
  */
1549
1612
  async muteChannel(channelId, direction = "both") {
1550
1613
  return this.channels.muteChannel(channelId, direction);
1551
1614
  }
1552
1615
  /**
1553
- * Unmutes a channel.
1616
+ * Unmutes a channel in the Asterisk system.
1617
+ *
1618
+ * This function removes the mute status from a specified channel, allowing audio
1619
+ * transmission to resume in the specified direction(s).
1620
+ *
1621
+ * @param channelId - The unique identifier of the channel to be unmuted.
1622
+ * This should be a string that uniquely identifies the channel in the Asterisk system.
1623
+ * @param direction - The direction of audio to unmute. Can be one of:
1624
+ * - "both": Unmute both incoming and outgoing audio (default)
1625
+ * - "in": Unmute only incoming audio
1626
+ * - "out": Unmute only outgoing audio
1627
+ *
1628
+ * @returns A Promise that resolves when the unmute operation has been successfully completed.
1629
+ * The promise resolves to void, indicating no specific return value.
1630
+ * If an error occurs during the operation, the promise will be rejected with an error object.
1554
1631
  */
1555
1632
  async unmuteChannel(channelId, direction = "both") {
1556
1633
  return this.channels.unmuteChannel(channelId, direction);
1557
1634
  }
1558
1635
  /**
1559
- * Puts a channel on hold.
1636
+ * Puts a specified channel on hold.
1637
+ *
1638
+ * This function initiates a hold operation on the specified channel in the Asterisk system.
1639
+ * When a channel is put on hold, typically the audio is muted or replaced with hold music,
1640
+ * depending on the system configuration.
1641
+ *
1642
+ * @param channelId - The unique identifier of the channel to be put on hold.
1643
+ * This should be a string that uniquely identifies the channel in the Asterisk system.
1644
+ *
1645
+ * @returns A Promise that resolves when the hold operation has been successfully initiated.
1646
+ * The promise resolves to void, indicating no specific return value.
1647
+ * If an error occurs during the operation, the promise will be rejected with an error object.
1560
1648
  */
1561
1649
  async holdChannel(channelId) {
1562
1650
  return this.channels.holdChannel(channelId);
1563
1651
  }
1564
1652
  /**
1565
- * Removes a channel from hold.
1653
+ * Removes a specified channel from hold.
1654
+ *
1655
+ * This function initiates an unhold operation on the specified channel in the Asterisk system.
1656
+ * When a channel is taken off hold, it typically resumes normal audio transmission,
1657
+ * allowing the parties to continue their conversation.
1658
+ *
1659
+ * @param channelId - The unique identifier of the channel to be taken off hold.
1660
+ * This should be a string that uniquely identifies the channel in the Asterisk system.
1661
+ *
1662
+ * @returns A Promise that resolves when the unhold operation has been successfully initiated.
1663
+ * The promise resolves to void, indicating no specific return value.
1664
+ * If an error occurs during the operation, the promise will be rejected with an error object.
1566
1665
  */
1567
1666
  async unholdChannel(channelId) {
1568
1667
  return this.channels.unholdChannel(channelId);
1569
1668
  }
1570
1669
  /**
1571
- * Creates a new channel using the provided originate request data.
1670
+ * Creates a new channel in the Asterisk system using the provided originate request data.
1671
+ * This function initiates a new communication channel based on the specified parameters.
1572
1672
  *
1573
- * @param data - The originate request data containing channel creation parameters.
1574
- * @returns A promise that resolves to the created Channel object.
1673
+ * @param data - An object containing the originate request data for channel creation.
1674
+ * This includes details such as the endpoint to call, the context to use,
1675
+ * and any variables to set on the new channel.
1676
+ * @param data.endpoint - The endpoint to call (e.g., "SIP/1234").
1677
+ * @param data.extension - The extension to dial after the channel is created.
1678
+ * @param data.context - The dialplan context to use for the new channel.
1679
+ * @param data.priority - The priority to start at in the dialplan.
1680
+ * @param data.app - The application to execute on the channel (alternative to extension/context/priority).
1681
+ * @param data.appArgs - The arguments to pass to the application, if 'app' is specified.
1682
+ * @param data.callerId - The caller ID to set on the new channel.
1683
+ * @param data.timeout - The timeout (in seconds) to wait for the channel to be answered.
1684
+ * @param data.variables - An object containing key-value pairs of channel variables to set.
1685
+ * @param data.channelId - An optional ID to assign to the new channel.
1686
+ * @param data.otherChannelId - The ID of another channel to bridge with after creation.
1687
+ *
1688
+ * @returns A Promise that resolves to the created Channel object.
1689
+ * The Channel object contains details about the newly created channel,
1690
+ * such as its unique identifier, state, and other relevant information.
1691
+ *
1692
+ * @throws Will throw an error if the channel creation fails for any reason,
1693
+ * such as invalid parameters or system issues.
1575
1694
  */
1576
1695
  async createChannel(data) {
1577
1696
  return this.channels.createChannel(data);
@@ -1667,68 +1786,85 @@ var AriClient = class {
1667
1786
  return this.playbacks.getDetails(playbackId);
1668
1787
  }
1669
1788
  /**
1670
- * Controls a specific playback.
1789
+ * Controls a specific playback in the Asterisk server.
1671
1790
  *
1672
- * @param playbackId - The unique identifier of the playback.
1673
- * @param controlRequest - The PlaybackControlRequest containing the control operation.
1674
- * @returns {Promise<void>} A promise resolving when the control operation is successfully executed.
1791
+ * @param playbackId - The unique identifier of the playback to control.
1792
+ * @param controlRequest - An object containing the control operation details.
1793
+ * @returns A Promise that resolves when the control operation is successfully executed.
1675
1794
  */
1676
1795
  async controlPlayback(playbackId, controlRequest) {
1677
1796
  return this.playbacks.control(playbackId, controlRequest);
1678
1797
  }
1679
1798
  /**
1680
- * Stops a specific playback.
1799
+ * Stops a specific playback in the Asterisk server.
1681
1800
  *
1682
- * @param playbackId - The unique identifier of the playback.
1683
- * @returns {Promise<void>} A promise resolving when the playback is successfully stopped.
1801
+ * @param playbackId - The unique identifier of the playback to stop.
1802
+ * @returns A Promise that resolves when the playback is successfully stopped.
1684
1803
  */
1685
1804
  async stopPlayback(playbackId) {
1686
1805
  return this.playbacks.stop(playbackId);
1687
1806
  }
1688
1807
  /**
1689
- * Lists all available sounds.
1808
+ * Retrieves a list of all available sounds in the Asterisk server.
1690
1809
  *
1691
1810
  * @param params - Optional parameters to filter the list of sounds.
1692
- * @returns {Promise<Sound[]>} A promise resolving to the list of sounds.
1811
+ * @returns A Promise that resolves to an array of Sound objects representing the available sounds.
1693
1812
  */
1694
1813
  async listSounds(params) {
1695
1814
  return this.sounds.list(params);
1696
1815
  }
1697
1816
  /**
1698
- * Retrieves details of a specific sound.
1817
+ * Retrieves detailed information about a specific sound in the Asterisk server.
1699
1818
  *
1700
- * @param soundId - The unique identifier of the sound.
1701
- * @returns {Promise<Sound>} A promise resolving to the sound details.
1819
+ * @param soundId - The unique identifier of the sound to retrieve details for.
1820
+ * @returns A Promise that resolves to a Sound object containing the details of the specified sound.
1702
1821
  */
1703
1822
  async getSoundDetails(soundId) {
1704
1823
  return this.sounds.getDetails(soundId);
1705
1824
  }
1706
1825
  /**
1707
- * Retrieves information about the Asterisk server.
1826
+ * Retrieves general information about the Asterisk server.
1827
+ *
1828
+ * @returns A Promise that resolves to an AsteriskInfo object containing server information.
1708
1829
  */
1709
1830
  async getAsteriskInfo() {
1710
1831
  return this.asterisk.getInfo();
1711
1832
  }
1712
1833
  /**
1713
- * Lists all loaded modules in the Asterisk server.
1834
+ * Retrieves a list of all loaded modules in the Asterisk server.
1835
+ *
1836
+ * @returns A Promise that resolves to an array of Module objects representing the loaded modules.
1714
1837
  */
1715
1838
  async listModules() {
1716
1839
  return this.asterisk.listModules();
1717
1840
  }
1718
1841
  /**
1719
- * Manages a specific module in the Asterisk server.
1842
+ * Manages a specific module in the Asterisk server by loading, unloading, or reloading it.
1843
+ *
1844
+ * @param moduleName - The name of the module to manage.
1845
+ * @param action - The action to perform on the module: "load", "unload", or "reload".
1846
+ * @returns A Promise that resolves when the module management action is completed successfully.
1720
1847
  */
1721
1848
  async manageModule(moduleName, action) {
1722
1849
  return this.asterisk.manageModule(moduleName, action);
1723
1850
  }
1724
1851
  /**
1725
- * Retrieves all configured logging channels.
1852
+ * Retrieves a list of all configured logging channels in the Asterisk server.
1853
+ *
1854
+ * @returns A Promise that resolves to an array of Logging objects representing the configured logging channels.
1726
1855
  */
1727
1856
  async listLoggingChannels() {
1728
1857
  return this.asterisk.listLoggingChannels();
1729
1858
  }
1730
1859
  /**
1731
1860
  * Adds or removes a log channel in the Asterisk server.
1861
+ *
1862
+ * @param logChannelName - The name of the log channel to manage.
1863
+ * @param action - The action to perform: "add" to create a new log channel or "remove" to delete an existing one.
1864
+ * @param configuration - Optional configuration object for adding a log channel. Ignored when removing a channel.
1865
+ * @param configuration.type - The type of the log channel.
1866
+ * @param configuration.configuration - Additional configuration details for the log channel.
1867
+ * @returns A Promise that resolves when the log channel management action is completed successfully.
1732
1868
  */
1733
1869
  async manageLogChannel(logChannelName, action, configuration) {
1734
1870
  return this.asterisk.manageLogChannel(
@@ -1738,13 +1874,30 @@ var AriClient = class {
1738
1874
  );
1739
1875
  }
1740
1876
  /**
1741
- * Retrieves the value of a global variable.
1877
+ * Retrieves the value of a global variable from the Asterisk server.
1878
+ *
1879
+ * @param variableName - The name of the global variable to retrieve.
1880
+ * @returns A Promise that resolves to a Variable object containing the name and value of the global variable.
1742
1881
  */
1743
1882
  async getGlobalVariable(variableName) {
1744
1883
  return this.asterisk.getGlobalVariable(variableName);
1745
1884
  }
1746
1885
  /**
1747
- * Sets a global variable.
1886
+ * Sets a global variable in the Asterisk server.
1887
+ *
1888
+ * This function allows you to set or update the value of a global variable
1889
+ * in the Asterisk server. Global variables are accessible throughout the
1890
+ * entire Asterisk system and can be used for various purposes such as
1891
+ * configuration settings or sharing data between different parts of the system.
1892
+ *
1893
+ * @param variableName - The name of the global variable to set or update.
1894
+ * This should be a string identifying the variable uniquely.
1895
+ * @param value - The value to assign to the global variable. This can be any
1896
+ * string value, including empty strings.
1897
+ * @returns A Promise that resolves when the global variable has been successfully
1898
+ * set. The promise resolves to void, indicating no specific return value.
1899
+ * If an error occurs during the operation, the promise will be rejected
1900
+ * with an error object.
1748
1901
  */
1749
1902
  async setGlobalVariable(variableName, value) {
1750
1903
  return this.asterisk.setGlobalVariable(variableName, value);