@ipcom/asterisk-ari 0.0.19 → 0.0.21

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
@@ -626,8 +626,8 @@ var Applications = class {
626
626
  }
627
627
  /**
628
628
  * Lists all applications.
629
- *
630
- * @returns A promise that resolves to an array of Application objects representing all registered applications.
629
+ *
630
+ * @returns A promise that resolves to an array of Application objects.
631
631
  * @throws {Error} If the API response is not an array.
632
632
  */
633
633
  async list() {
@@ -639,19 +639,27 @@ var Applications = class {
639
639
  }
640
640
  /**
641
641
  * Retrieves details of a specific application.
642
- *
643
- * @param appName - The unique name of the application.
644
- * @returns A promise that resolves to an ApplicationDetails object containing the details of the specified application.
642
+ *
643
+ * @param appName - The name of the application to retrieve details for.
644
+ * @returns A promise that resolves to an ApplicationDetails object.
645
+ * @throws {Error} If there's an error fetching the application details.
645
646
  */
646
647
  async getDetails(appName) {
647
- return this.client.get(`/applications/${appName}`);
648
+ try {
649
+ return await this.client.get(
650
+ `/applications/${appName}`
651
+ );
652
+ } catch (error) {
653
+ console.error(`Erro ao obter detalhes do aplicativo ${appName}:`, error);
654
+ throw error;
655
+ }
648
656
  }
649
657
  /**
650
658
  * Sends a message to a specific application.
651
- *
652
- * @param appName - The unique name of the application.
653
- * @param body - The message body to send.
654
- * @returns A promise that resolves when the message is sent successfully.
659
+ *
660
+ * @param appName - The name of the application to send the message to.
661
+ * @param body - The message to be sent, containing an event and optional data.
662
+ * @returns A promise that resolves when the message is successfully sent.
655
663
  */
656
664
  async sendMessage(appName, body) {
657
665
  await this.client.post(`/applications/${appName}/messages`, body);
@@ -1188,13 +1196,17 @@ var WebSocketClient = class {
1188
1196
  throw new Error("WebSocket n\xE3o est\xE1 conectado.");
1189
1197
  }
1190
1198
  if (event === "message") {
1191
- this.ws.on(event, (data) => {
1199
+ this.ws.on(event, (rawData) => {
1192
1200
  try {
1193
- const decodedData = JSON.parse(data.toString());
1194
- callback(decodedData);
1201
+ const decodedData = JSON.parse(rawData.toString());
1202
+ if (decodedData?.type) {
1203
+ const matchedEvent = decodedData;
1204
+ callback(matchedEvent);
1205
+ } else {
1206
+ console.warn("Mensagem sem tipo:", decodedData);
1207
+ }
1195
1208
  } catch (err) {
1196
1209
  console.error("Erro ao decodificar mensagem do WebSocket:", err);
1197
- callback(data);
1198
1210
  }
1199
1211
  });
1200
1212
  } else {
@@ -1250,7 +1262,7 @@ var AriClient = class {
1250
1262
  * @param app - The application name to connect to.
1251
1263
  * @returns {Promise<void>} Resolves when the WebSocket connects successfully.
1252
1264
  */
1253
- async connectWebSocket(app) {
1265
+ async connectWebSocket(app, subscribedEvents) {
1254
1266
  if (!app) {
1255
1267
  throw new Error(
1256
1268
  "The 'app' parameter is required to connect to the WebSocket."
@@ -1261,8 +1273,9 @@ var AriClient = class {
1261
1273
  return;
1262
1274
  }
1263
1275
  this.isReconnecting = true;
1276
+ const eventsParam = subscribedEvents && subscribedEvents.length > 0 ? `&event=${subscribedEvents.join(",")}` : "&subscribeAll=true";
1264
1277
  const protocol = this.config.secure ? "wss" : "ws";
1265
- const wsUrl = `${protocol}://${encodeURIComponent(this.config.username)}:${encodeURIComponent(this.config.password)}@${this.config.host}:${this.config.port}/ari/events?app=${app}`;
1278
+ const wsUrl = `${protocol}://${encodeURIComponent(this.config.username)}:${encodeURIComponent(this.config.password)}@${this.config.host}:${this.config.port}/ari/events?app=${app}${eventsParam}`;
1266
1279
  const backoffOptions = {
1267
1280
  delayFirstAttempt: false,
1268
1281
  startingDelay: 1e3,
@@ -1326,16 +1339,22 @@ var AriClient = class {
1326
1339
  return this.wsClient ? this.wsClient.isConnected() : false;
1327
1340
  }
1328
1341
  /**
1329
- * Registers a callback for a specific WebSocket event.
1342
+ * Registers a callback function for WebSocket events.
1343
+ * This method allows you to listen for and respond to WebSocket messages
1344
+ * and process specific event types.
1330
1345
  *
1331
- * @param event - The WebSocket event to listen for.
1332
- * @param callback - The callback function to execute when the event occurs.
1346
+ * @param callback - The callback function to execute when a WebSocket message is received.
1347
+ * The function will receive the parsed event data as its parameter.
1348
+ * @throws {Error} Throws an error if the WebSocket is not connected when trying to register the event.
1349
+ * @returns {void}
1333
1350
  */
1334
- onWebSocketEvent(event, callback) {
1351
+ onWebSocketEvent(callback) {
1335
1352
  if (!this.wsClient) {
1336
1353
  throw new Error("WebSocket is not connected.");
1337
1354
  }
1338
- this.wsClient.on(event, callback);
1355
+ this.wsClient.on("message", (data) => {
1356
+ callback(data);
1357
+ });
1339
1358
  }
1340
1359
  /**
1341
1360
  * Closes the WebSocket connection.
@@ -1478,7 +1497,14 @@ var AriClient = class {
1478
1497
  return this.channels.snoopChannelWithId(channelId, snoopId, options);
1479
1498
  }
1480
1499
  /**
1481
- * Dials a created channel.
1500
+ * Initiates a dial operation on a previously created channel.
1501
+ * This function attempts to connect the specified channel to its configured destination.
1502
+ *
1503
+ * @param channelId - The unique identifier of the channel to dial.
1504
+ * @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.
1505
+ * @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.
1506
+ * @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.
1507
+ * @throws Will throw an error if the dial operation fails, e.g., if the channel doesn't exist or is in an invalid state.
1482
1508
  */
1483
1509
  async dialChannel(channelId, caller, timeout) {
1484
1510
  return this.channels.dial(channelId, caller, timeout);
@@ -1514,46 +1540,139 @@ var AriClient = class {
1514
1540
  return this.channels.ringChannel(channelId);
1515
1541
  }
1516
1542
  /**
1517
- * Stops ringing indication on a channel.
1543
+ * Stops the ringing indication on a specified channel in the Asterisk system.
1544
+ *
1545
+ * This function sends a request to the Asterisk server to cease the ringing
1546
+ * indication on a particular channel. This is typically used when you want to
1547
+ * stop the ringing sound on a channel without answering or hanging up the call.
1548
+ *
1549
+ * @param channelId - The unique identifier of the channel on which to stop the ringing.
1550
+ * This should be a string that uniquely identifies the channel in the Asterisk system.
1551
+ *
1552
+ * @returns A Promise that resolves when the ringing has been successfully stopped on the specified channel.
1553
+ * The promise resolves to void, indicating no specific return value.
1554
+ * If an error occurs during the operation, the promise will be rejected with an error object.
1518
1555
  */
1519
1556
  async stopRingChannel(channelId) {
1520
1557
  return this.channels.stopRingChannel(channelId);
1521
1558
  }
1522
1559
  /**
1523
- * Sends DTMF to a channel.
1560
+ * Sends DTMF (Dual-Tone Multi-Frequency) tones to a specified channel.
1561
+ *
1562
+ * This function allows sending DTMF tones to a channel, which can be used for various purposes
1563
+ * such as interacting with IVR systems or sending signals during a call.
1564
+ *
1565
+ * @param channelId - The unique identifier of the channel to send DTMF tones to.
1566
+ * @param dtmf - A string representing the DTMF tones to send (e.g., "123#").
1567
+ * @param options - Optional parameters to control the timing of DTMF tones.
1568
+ * @param options.before - The time (in milliseconds) to wait before sending DTMF.
1569
+ * @param options.between - The time (in milliseconds) to wait between each DTMF tone.
1570
+ * @param options.duration - The duration (in milliseconds) of each DTMF tone.
1571
+ * @param options.after - The time (in milliseconds) to wait after sending all DTMF tones.
1572
+ * @returns A Promise that resolves when the DTMF tones have been successfully sent.
1524
1573
  */
1525
1574
  async sendDTMF(channelId, dtmf, options) {
1526
1575
  return this.channels.sendDTMF(channelId, dtmf, options);
1527
1576
  }
1528
1577
  /**
1529
- * Mutes a channel.
1578
+ * Mutes a channel in the Asterisk system.
1579
+ *
1580
+ * This function initiates a mute operation on the specified channel, preventing
1581
+ * audio transmission in the specified direction(s).
1582
+ *
1583
+ * @param channelId - The unique identifier of the channel to be muted.
1584
+ * This should be a string that uniquely identifies the channel in the Asterisk system.
1585
+ * @param direction - The direction of audio to mute. Can be one of:
1586
+ * - "both": Mute both incoming and outgoing audio (default)
1587
+ * - "in": Mute only incoming audio
1588
+ * - "out": Mute only outgoing audio
1589
+ *
1590
+ * @returns A Promise that resolves when the mute operation has been successfully completed.
1591
+ * The promise resolves to void, indicating no specific return value.
1592
+ * If an error occurs during the operation, the promise will be rejected with an error object.
1530
1593
  */
1531
1594
  async muteChannel(channelId, direction = "both") {
1532
1595
  return this.channels.muteChannel(channelId, direction);
1533
1596
  }
1534
1597
  /**
1535
- * Unmutes a channel.
1598
+ * Unmutes a channel in the Asterisk system.
1599
+ *
1600
+ * This function removes the mute status from a specified channel, allowing audio
1601
+ * transmission to resume in the specified direction(s).
1602
+ *
1603
+ * @param channelId - The unique identifier of the channel to be unmuted.
1604
+ * This should be a string that uniquely identifies the channel in the Asterisk system.
1605
+ * @param direction - The direction of audio to unmute. Can be one of:
1606
+ * - "both": Unmute both incoming and outgoing audio (default)
1607
+ * - "in": Unmute only incoming audio
1608
+ * - "out": Unmute only outgoing audio
1609
+ *
1610
+ * @returns A Promise that resolves when the unmute operation has been successfully completed.
1611
+ * The promise resolves to void, indicating no specific return value.
1612
+ * If an error occurs during the operation, the promise will be rejected with an error object.
1536
1613
  */
1537
1614
  async unmuteChannel(channelId, direction = "both") {
1538
1615
  return this.channels.unmuteChannel(channelId, direction);
1539
1616
  }
1540
1617
  /**
1541
- * Puts a channel on hold.
1618
+ * Puts a specified channel on hold.
1619
+ *
1620
+ * This function initiates a hold operation on the specified channel in the Asterisk system.
1621
+ * When a channel is put on hold, typically the audio is muted or replaced with hold music,
1622
+ * depending on the system configuration.
1623
+ *
1624
+ * @param channelId - The unique identifier of the channel to be put on hold.
1625
+ * This should be a string that uniquely identifies the channel in the Asterisk system.
1626
+ *
1627
+ * @returns A Promise that resolves when the hold operation has been successfully initiated.
1628
+ * The promise resolves to void, indicating no specific return value.
1629
+ * If an error occurs during the operation, the promise will be rejected with an error object.
1542
1630
  */
1543
1631
  async holdChannel(channelId) {
1544
1632
  return this.channels.holdChannel(channelId);
1545
1633
  }
1546
1634
  /**
1547
- * Removes a channel from hold.
1635
+ * Removes a specified channel from hold.
1636
+ *
1637
+ * This function initiates an unhold operation on the specified channel in the Asterisk system.
1638
+ * When a channel is taken off hold, it typically resumes normal audio transmission,
1639
+ * allowing the parties to continue their conversation.
1640
+ *
1641
+ * @param channelId - The unique identifier of the channel to be taken off hold.
1642
+ * This should be a string that uniquely identifies the channel in the Asterisk system.
1643
+ *
1644
+ * @returns A Promise that resolves when the unhold operation has been successfully initiated.
1645
+ * The promise resolves to void, indicating no specific return value.
1646
+ * If an error occurs during the operation, the promise will be rejected with an error object.
1548
1647
  */
1549
1648
  async unholdChannel(channelId) {
1550
1649
  return this.channels.unholdChannel(channelId);
1551
1650
  }
1552
1651
  /**
1553
- * Creates a new channel using the provided originate request data.
1652
+ * Creates a new channel in the Asterisk system using the provided originate request data.
1653
+ * This function initiates a new communication channel based on the specified parameters.
1554
1654
  *
1555
- * @param data - The originate request data containing channel creation parameters.
1556
- * @returns A promise that resolves to the created Channel object.
1655
+ * @param data - An object containing the originate request data for channel creation.
1656
+ * This includes details such as the endpoint to call, the context to use,
1657
+ * and any variables to set on the new channel.
1658
+ * @param data.endpoint - The endpoint to call (e.g., "SIP/1234").
1659
+ * @param data.extension - The extension to dial after the channel is created.
1660
+ * @param data.context - The dialplan context to use for the new channel.
1661
+ * @param data.priority - The priority to start at in the dialplan.
1662
+ * @param data.app - The application to execute on the channel (alternative to extension/context/priority).
1663
+ * @param data.appArgs - The arguments to pass to the application, if 'app' is specified.
1664
+ * @param data.callerId - The caller ID to set on the new channel.
1665
+ * @param data.timeout - The timeout (in seconds) to wait for the channel to be answered.
1666
+ * @param data.variables - An object containing key-value pairs of channel variables to set.
1667
+ * @param data.channelId - An optional ID to assign to the new channel.
1668
+ * @param data.otherChannelId - The ID of another channel to bridge with after creation.
1669
+ *
1670
+ * @returns A Promise that resolves to the created Channel object.
1671
+ * The Channel object contains details about the newly created channel,
1672
+ * such as its unique identifier, state, and other relevant information.
1673
+ *
1674
+ * @throws Will throw an error if the channel creation fails for any reason,
1675
+ * such as invalid parameters or system issues.
1557
1676
  */
1558
1677
  async createChannel(data) {
1559
1678
  return this.channels.createChannel(data);
@@ -1649,68 +1768,85 @@ var AriClient = class {
1649
1768
  return this.playbacks.getDetails(playbackId);
1650
1769
  }
1651
1770
  /**
1652
- * Controls a specific playback.
1771
+ * Controls a specific playback in the Asterisk server.
1653
1772
  *
1654
- * @param playbackId - The unique identifier of the playback.
1655
- * @param controlRequest - The PlaybackControlRequest containing the control operation.
1656
- * @returns {Promise<void>} A promise resolving when the control operation is successfully executed.
1773
+ * @param playbackId - The unique identifier of the playback to control.
1774
+ * @param controlRequest - An object containing the control operation details.
1775
+ * @returns A Promise that resolves when the control operation is successfully executed.
1657
1776
  */
1658
1777
  async controlPlayback(playbackId, controlRequest) {
1659
1778
  return this.playbacks.control(playbackId, controlRequest);
1660
1779
  }
1661
1780
  /**
1662
- * Stops a specific playback.
1781
+ * Stops a specific playback in the Asterisk server.
1663
1782
  *
1664
- * @param playbackId - The unique identifier of the playback.
1665
- * @returns {Promise<void>} A promise resolving when the playback is successfully stopped.
1783
+ * @param playbackId - The unique identifier of the playback to stop.
1784
+ * @returns A Promise that resolves when the playback is successfully stopped.
1666
1785
  */
1667
1786
  async stopPlayback(playbackId) {
1668
1787
  return this.playbacks.stop(playbackId);
1669
1788
  }
1670
1789
  /**
1671
- * Lists all available sounds.
1790
+ * Retrieves a list of all available sounds in the Asterisk server.
1672
1791
  *
1673
1792
  * @param params - Optional parameters to filter the list of sounds.
1674
- * @returns {Promise<Sound[]>} A promise resolving to the list of sounds.
1793
+ * @returns A Promise that resolves to an array of Sound objects representing the available sounds.
1675
1794
  */
1676
1795
  async listSounds(params) {
1677
1796
  return this.sounds.list(params);
1678
1797
  }
1679
1798
  /**
1680
- * Retrieves details of a specific sound.
1799
+ * Retrieves detailed information about a specific sound in the Asterisk server.
1681
1800
  *
1682
- * @param soundId - The unique identifier of the sound.
1683
- * @returns {Promise<Sound>} A promise resolving to the sound details.
1801
+ * @param soundId - The unique identifier of the sound to retrieve details for.
1802
+ * @returns A Promise that resolves to a Sound object containing the details of the specified sound.
1684
1803
  */
1685
1804
  async getSoundDetails(soundId) {
1686
1805
  return this.sounds.getDetails(soundId);
1687
1806
  }
1688
1807
  /**
1689
- * Retrieves information about the Asterisk server.
1808
+ * Retrieves general information about the Asterisk server.
1809
+ *
1810
+ * @returns A Promise that resolves to an AsteriskInfo object containing server information.
1690
1811
  */
1691
1812
  async getAsteriskInfo() {
1692
1813
  return this.asterisk.getInfo();
1693
1814
  }
1694
1815
  /**
1695
- * Lists all loaded modules in the Asterisk server.
1816
+ * Retrieves a list of all loaded modules in the Asterisk server.
1817
+ *
1818
+ * @returns A Promise that resolves to an array of Module objects representing the loaded modules.
1696
1819
  */
1697
1820
  async listModules() {
1698
1821
  return this.asterisk.listModules();
1699
1822
  }
1700
1823
  /**
1701
- * Manages a specific module in the Asterisk server.
1824
+ * Manages a specific module in the Asterisk server by loading, unloading, or reloading it.
1825
+ *
1826
+ * @param moduleName - The name of the module to manage.
1827
+ * @param action - The action to perform on the module: "load", "unload", or "reload".
1828
+ * @returns A Promise that resolves when the module management action is completed successfully.
1702
1829
  */
1703
1830
  async manageModule(moduleName, action) {
1704
1831
  return this.asterisk.manageModule(moduleName, action);
1705
1832
  }
1706
1833
  /**
1707
- * Retrieves all configured logging channels.
1834
+ * Retrieves a list of all configured logging channels in the Asterisk server.
1835
+ *
1836
+ * @returns A Promise that resolves to an array of Logging objects representing the configured logging channels.
1708
1837
  */
1709
1838
  async listLoggingChannels() {
1710
1839
  return this.asterisk.listLoggingChannels();
1711
1840
  }
1712
1841
  /**
1713
1842
  * Adds or removes a log channel in the Asterisk server.
1843
+ *
1844
+ * @param logChannelName - The name of the log channel to manage.
1845
+ * @param action - The action to perform: "add" to create a new log channel or "remove" to delete an existing one.
1846
+ * @param configuration - Optional configuration object for adding a log channel. Ignored when removing a channel.
1847
+ * @param configuration.type - The type of the log channel.
1848
+ * @param configuration.configuration - Additional configuration details for the log channel.
1849
+ * @returns A Promise that resolves when the log channel management action is completed successfully.
1714
1850
  */
1715
1851
  async manageLogChannel(logChannelName, action, configuration) {
1716
1852
  return this.asterisk.manageLogChannel(
@@ -1720,13 +1856,30 @@ var AriClient = class {
1720
1856
  );
1721
1857
  }
1722
1858
  /**
1723
- * Retrieves the value of a global variable.
1859
+ * Retrieves the value of a global variable from the Asterisk server.
1860
+ *
1861
+ * @param variableName - The name of the global variable to retrieve.
1862
+ * @returns A Promise that resolves to a Variable object containing the name and value of the global variable.
1724
1863
  */
1725
1864
  async getGlobalVariable(variableName) {
1726
1865
  return this.asterisk.getGlobalVariable(variableName);
1727
1866
  }
1728
1867
  /**
1729
- * Sets a global variable.
1868
+ * Sets a global variable in the Asterisk server.
1869
+ *
1870
+ * This function allows you to set or update the value of a global variable
1871
+ * in the Asterisk server. Global variables are accessible throughout the
1872
+ * entire Asterisk system and can be used for various purposes such as
1873
+ * configuration settings or sharing data between different parts of the system.
1874
+ *
1875
+ * @param variableName - The name of the global variable to set or update.
1876
+ * This should be a string identifying the variable uniquely.
1877
+ * @param value - The value to assign to the global variable. This can be any
1878
+ * string value, including empty strings.
1879
+ * @returns A Promise that resolves when the global variable has been successfully
1880
+ * set. The promise resolves to void, indicating no specific return value.
1881
+ * If an error occurs during the operation, the promise will be rejected
1882
+ * with an error object.
1730
1883
  */
1731
1884
  async setGlobalVariable(variableName, value) {
1732
1885
  return this.asterisk.setGlobalVariable(variableName, value);
@@ -1735,6 +1888,7 @@ var AriClient = class {
1735
1888
  export {
1736
1889
  Applications,
1737
1890
  AriClient,
1891
+ Asterisk,
1738
1892
  Channels,
1739
1893
  Endpoints,
1740
1894
  Playbacks,