@ipcom/asterisk-ari 0.0.158 → 0.0.160

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.
@@ -1803,7 +1803,7 @@ var ChannelInstance = class {
1803
1803
  * Otherwise, all listeners for the given event type are removed.
1804
1804
  *
1805
1805
  * @param {T} event - The type of WebSocket event to remove listener(s) for
1806
- * @param {Function} [listener] - Optional specific listener to remove
1806
+ * @param {(data: WebSocketEvent) => void} [listener] - Optional specific listener to remove
1807
1807
  * @throws {Error} If no event type is provided
1808
1808
  */
1809
1809
  off(event, listener) {
@@ -1897,6 +1897,78 @@ var ChannelInstance = class {
1897
1897
  throw new Error(`Failed to originate channel: ${message}`);
1898
1898
  }
1899
1899
  }
1900
+ /**
1901
+ * Continues the execution of a dialplan for the current channel.
1902
+ *
1903
+ * @param {string} [context] - The dialplan context to continue execution in, if specified.
1904
+ * @param {string} [extension] - The dialplan extension to proceed with, if provided.
1905
+ * @param {number} [priority] - The priority within the dialplan extension to resume at, if specified.
1906
+ * @param {string} [label] - The label to start from within the dialplan, if given.
1907
+ * @return {Promise<void>} Resolves when the dialplan is successfully continued.
1908
+ */
1909
+ async continueDialplan(context, extension, priority, label) {
1910
+ try {
1911
+ if (!this.channelData) {
1912
+ this.channelData = await this.getDetails();
1913
+ }
1914
+ await this.baseClient.post(`/channels/${this.id}/continue`, {
1915
+ context,
1916
+ extension,
1917
+ priority,
1918
+ label
1919
+ });
1920
+ } catch (error) {
1921
+ const message = getErrorMessage2(error);
1922
+ console.error(`Error continuing dialplan for channel ${this.id}:`, message);
1923
+ throw new Error(`Failed to continue dialplan: ${message}`);
1924
+ }
1925
+ }
1926
+ /**
1927
+ * Initiates a snoop operation on this channel with the provided options.
1928
+ * Snooping allows you to listen in or interact with an existing call.
1929
+ *
1930
+ * @param {SnoopOptions} options - Configuration options for the snooping operation.
1931
+ * @return {Promise<Channel>} A promise that resolves to the snooped channel data.
1932
+ * @throws {Error} If the channel is not initialized or if snooping fails.
1933
+ */
1934
+ async snoop(options) {
1935
+ try {
1936
+ if (!this.channelData) {
1937
+ this.channelData = await this.getDetails();
1938
+ }
1939
+ const queryParams = toQueryParams2(options);
1940
+ return await this.baseClient.post(
1941
+ `/channels/${this.id}/snoop?${queryParams}`
1942
+ );
1943
+ } catch (error) {
1944
+ const message = getErrorMessage2(error);
1945
+ console.error(`Error snooping on channel ${this.id}:`, message);
1946
+ throw new Error(`Failed to snoop channel: ${message}`);
1947
+ }
1948
+ }
1949
+ /**
1950
+ * Initiates a snoop operation on this channel with a specific snoop ID.
1951
+ *
1952
+ * @param {string} snoopId - The unique identifier for the snoop operation.
1953
+ * @param {SnoopOptions} options - Configuration options for the snooping operation.
1954
+ * @return {Promise<Channel>} A promise that resolves to the snooped channel data.
1955
+ * @throws {Error} If the channel is not initialized or if snooping fails.
1956
+ */
1957
+ async snoopWithId(snoopId, options) {
1958
+ try {
1959
+ if (!this.channelData) {
1960
+ this.channelData = await this.getDetails();
1961
+ }
1962
+ const queryParams = toQueryParams2(options);
1963
+ return await this.baseClient.post(
1964
+ `/channels/${this.id}/snoop/${snoopId}?${queryParams}`
1965
+ );
1966
+ } catch (error) {
1967
+ const message = getErrorMessage2(error);
1968
+ console.error(`Error snooping with ID on channel ${this.id}:`, message);
1969
+ throw new Error(`Failed to snoop channel with ID: ${message}`);
1970
+ }
1971
+ }
1900
1972
  /**
1901
1973
  * Plays media on the channel
1902
1974
  */
@@ -2765,7 +2837,7 @@ var PlaybackInstance = class {
2765
2837
  * Registers an event listener for a specific WebSocket event type.
2766
2838
  *
2767
2839
  * @param {T} event - Event type to listen for
2768
- * @param {Function} listener - Callback function for the event
2840
+ * @param {(data: WebSocketEvent) => void} listener - Callback function for the event
2769
2841
  */
2770
2842
  on(event, listener) {
2771
2843
  if (!event) {
@@ -2793,7 +2865,7 @@ var PlaybackInstance = class {
2793
2865
  * Registers a one-time event listener for a specific WebSocket event type.
2794
2866
  *
2795
2867
  * @param {T} event - Event type to listen for
2796
- * @param {Function} listener - Callback function for the event
2868
+ * @param {(data: WebSocketEvent) => void} listener - Callback function for the event
2797
2869
  */
2798
2870
  once(event, listener) {
2799
2871
  if (!event) {
@@ -2823,7 +2895,7 @@ var PlaybackInstance = class {
2823
2895
  * Removes event listener(s) for a specific WebSocket event type.
2824
2896
  *
2825
2897
  * @param {T} event - Event type to remove listener(s) for
2826
- * @param {Function} [listener] - Optional specific listener to remove
2898
+ * @param {(data: WebSocketEvent) => void} [listener] - Optional specific listener to remove
2827
2899
  */
2828
2900
  off(event, listener) {
2829
2901
  if (!event) {
@@ -3004,11 +3076,11 @@ var Playbacks = class {
3004
3076
  * This method performs the following cleanup operations:
3005
3077
  * 1. Clears all pending timeouts in the event queue.
3006
3078
  * 2. Removes all playback instances.
3007
- *
3079
+ *
3008
3080
  * @remarks
3009
3081
  * This method should be called when the Playbacks instance is no longer needed
3010
3082
  * to ensure proper resource management and prevent memory leaks.
3011
- *
3083
+ *
3012
3084
  * @returns {void} This method doesn't return a value.
3013
3085
  */
3014
3086
  cleanup() {
@@ -3324,6 +3396,58 @@ var WebSocketClient = class extends import_events4.EventEmitter {
3324
3396
  this.isConnecting = false;
3325
3397
  }
3326
3398
  }
3399
+ /**
3400
+ * Reconecta o WebSocket com uma lista atualizada de aplicações.
3401
+ *
3402
+ * @param {string[]} newApps - Lista de aplicações para reconectar
3403
+ * @param {WebSocketEventType[]} [subscribedEvents] - Tipos de eventos para se inscrever (opcional)
3404
+ * @returns {Promise<void>} Promise resolvida quando reconectado com sucesso
3405
+ */
3406
+ async reconnectWithApps(newApps, subscribedEvents) {
3407
+ if (!newApps.length) {
3408
+ throw new Error("At least one application name is required");
3409
+ }
3410
+ const uniqueApps = Array.from(/* @__PURE__ */ new Set([...this.apps, ...newApps]));
3411
+ if (uniqueApps.length === this.apps.length && uniqueApps.every((app) => this.apps.includes(app))) {
3412
+ console.log(
3413
+ "No changes in applications list, maintaining current connection"
3414
+ );
3415
+ return;
3416
+ }
3417
+ console.log(
3418
+ `Reconnecting WebSocket with updated applications: ${uniqueApps.join(", ")}`
3419
+ );
3420
+ this.apps = uniqueApps;
3421
+ if (subscribedEvents) {
3422
+ this.subscribedEvents = subscribedEvents;
3423
+ }
3424
+ if (this.ws) {
3425
+ await new Promise((resolve) => {
3426
+ this.once("disconnected", () => resolve());
3427
+ this.close();
3428
+ });
3429
+ }
3430
+ await this.connect();
3431
+ console.log("WebSocket reconnected successfully with updated applications");
3432
+ }
3433
+ /**
3434
+ * Adiciona novas aplicações à conexão WebSocket existente.
3435
+ *
3436
+ * @param {string[]} newApps - Lista de novas aplicações para adicionar
3437
+ * @param {WebSocketEventType[]} [subscribedEvents] - Tipos de eventos para se inscrever (opcional)
3438
+ * @returns {Promise<void>} Promise resolvida quando as aplicações são adicionadas com sucesso
3439
+ */
3440
+ async addApps(newApps, subscribedEvents) {
3441
+ if (!newApps.length) {
3442
+ throw new Error("At least one application name is required");
3443
+ }
3444
+ const appsToAdd = newApps.filter((app) => !this.apps.includes(app));
3445
+ if (appsToAdd.length === 0) {
3446
+ console.log("All applications are already registered");
3447
+ return;
3448
+ }
3449
+ await this.reconnectWithApps(appsToAdd, subscribedEvents);
3450
+ }
3327
3451
  /**
3328
3452
  * Initializes a WebSocket connection with exponential backoff retry mechanism.
3329
3453
  *
@@ -3595,7 +3719,6 @@ var AriClient = class {
3595
3719
  baseClient;
3596
3720
  webSocketClient;
3597
3721
  eventListeners = /* @__PURE__ */ new Map();
3598
- // Armazena os listeners para limpeza
3599
3722
  channels;
3600
3723
  endpoints;
3601
3724
  applications;
@@ -3653,16 +3776,19 @@ var AriClient = class {
3653
3776
  * @param {string[]} apps - List of application names to subscribe to
3654
3777
  * @param {WebSocketEventType[]} [subscribedEvents] - Optional list of specific event types to subscribe to
3655
3778
  * @returns {Promise<void>} Resolves when connection is established
3656
- * @throws {Error} If connection fails or if WebSocket is already connected
3779
+ * @throws {Error} If connection fails
3657
3780
  */
3658
3781
  async connectWebSocket(apps, subscribedEvents) {
3659
3782
  try {
3660
3783
  if (!apps.length) {
3661
3784
  throw new Error("At least one application name is required.");
3662
3785
  }
3663
- if (this.webSocketClient) {
3664
- await this.closeWebSocket();
3665
- await new Promise((resolve) => setTimeout(resolve, 1e3));
3786
+ if (this.webSocketClient && this.webSocketClient.isConnected()) {
3787
+ console.log(
3788
+ "WebSocket already connected. Reconnecting with updated apps..."
3789
+ );
3790
+ await this.webSocketClient.reconnectWithApps(apps, subscribedEvents);
3791
+ return;
3666
3792
  }
3667
3793
  this.webSocketClient = new WebSocketClient(
3668
3794
  this.baseClient,
@@ -3673,10 +3799,25 @@ var AriClient = class {
3673
3799
  await this.webSocketClient.connect();
3674
3800
  } catch (error) {
3675
3801
  console.error("Failed to establish WebSocket connection:", error);
3676
- this.webSocketClient = void 0;
3677
3802
  throw error;
3678
3803
  }
3679
3804
  }
3805
+ /**
3806
+ * Adds applications to the existing WebSocket connection.
3807
+ *
3808
+ * @param {string[]} apps - Additional applications to subscribe to
3809
+ * @param {WebSocketEventType[]} [subscribedEvents] - Optional list of specific event types to subscribe to
3810
+ * @returns {Promise<void>} Resolves when applications are added successfully
3811
+ * @throws {Error} If no WebSocket connection exists or if the operation fails
3812
+ */
3813
+ async addApplicationsToWebSocket(apps, subscribedEvents) {
3814
+ if (!this.webSocketClient || !this.webSocketClient.isConnected()) {
3815
+ throw new Error(
3816
+ "No active WebSocket connection. Create one first with connectWebSocket()."
3817
+ );
3818
+ }
3819
+ await this.webSocketClient.addApps(apps, subscribedEvents);
3820
+ }
3680
3821
  /**
3681
3822
  * Destroys the ARI Client instance, cleaning up all resources and removing circular references.
3682
3823
  * This method should be called when the ARI Client is no longer needed to ensure proper cleanup.
@@ -3702,6 +3843,13 @@ var AriClient = class {
3702
3843
  throw error;
3703
3844
  }
3704
3845
  }
3846
+ /**
3847
+ * Registers an event listener for WebSocket events.
3848
+ *
3849
+ * @param {T} event - The event type to listen for
3850
+ * @param {Function} listener - Callback function for handling the event
3851
+ * @throws {Error} If WebSocket is not connected
3852
+ */
3705
3853
  /**
3706
3854
  * Registers an event listener for WebSocket events.
3707
3855
  *
@@ -3721,6 +3869,7 @@ var AriClient = class {
3721
3869
  this.webSocketClient.on(event, listener);
3722
3870
  existingListeners.push(listener);
3723
3871
  this.eventListeners.set(event, existingListeners);
3872
+ console.log(`Event listener successfully registered for ${event}`);
3724
3873
  }
3725
3874
  /**
3726
3875
  * Registers a one-time event listener for WebSocket events.
@@ -3745,7 +3894,10 @@ var AriClient = class {
3745
3894
  this.off(event, wrappedListener);
3746
3895
  };
3747
3896
  this.webSocketClient.once(event, wrappedListener);
3748
- this.eventListeners.set(event, [...existingListeners, wrappedListener]);
3897
+ this.eventListeners.set(event, [
3898
+ ...existingListeners,
3899
+ wrappedListener
3900
+ ]);
3749
3901
  console.log(`One-time event listener registered for ${event}`);
3750
3902
  }
3751
3903
  /**
@@ -3763,7 +3915,9 @@ var AriClient = class {
3763
3915
  const existingListeners = this.eventListeners.get(event) || [];
3764
3916
  this.eventListeners.set(
3765
3917
  event,
3766
- existingListeners.filter((l) => l !== listener)
3918
+ existingListeners.filter(
3919
+ (l) => l !== listener
3920
+ )
3767
3921
  );
3768
3922
  console.log(`Event listener removed for ${event}`);
3769
3923
  }