@ipcom/asterisk-ari 0.0.48 → 0.0.50

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.
@@ -868,6 +868,23 @@ function toQueryParams2(options) {
868
868
  function isChannelEvent(event) {
869
869
  return event.channel?.id !== void 0;
870
870
  }
871
+ var channelEvents = [
872
+ "ChannelCreated",
873
+ "ChannelDestroyed",
874
+ "ChannelEnteredBridge",
875
+ "ChannelLeftBridge",
876
+ "ChannelStateChange",
877
+ "ChannelDtmfReceived",
878
+ "ChannelDialplan",
879
+ "ChannelCallerId",
880
+ "ChannelUserevent",
881
+ "ChannelHangupRequest",
882
+ "ChannelVarset",
883
+ "ChannelTalkingStarted",
884
+ "ChannelTalkingFinished",
885
+ "ChannelHold",
886
+ "ChannelUnhold"
887
+ ];
871
888
 
872
889
  // src/ari-client/resources/channels.ts
873
890
  var ChannelInstance = class extends import_events.EventEmitter {
@@ -876,8 +893,22 @@ var ChannelInstance = class extends import_events.EventEmitter {
876
893
  this.client = client;
877
894
  this.baseClient = baseClient;
878
895
  this.channelId = channelId;
896
+ channelEvents.forEach((eventType) => {
897
+ this.client.onChannelEvent(eventType, (event) => {
898
+ if (this.isChannelEvent(event) && event.channel?.id === this.channelId) {
899
+ this.emit(eventType, event);
900
+ }
901
+ });
902
+ });
879
903
  }
880
904
  channelData = null;
905
+ /**
906
+ * Verifica se o evento possui a propriedade `channel`.
907
+ * @param event Evento recebido.
908
+ */
909
+ isChannelEvent(event) {
910
+ return event && typeof event === "object" && "channel" in event;
911
+ }
881
912
  /**
882
913
  * Getter para o ID do canal.
883
914
  */
@@ -899,11 +930,15 @@ var ChannelInstance = class extends import_events.EventEmitter {
899
930
  * Obtém os detalhes do canal.
900
931
  */
901
932
  async getDetails() {
902
- if (!this.channelId && !this.channelData) {
903
- throw new Error("Nenhum canal est\xE1 associado a esta inst\xE2ncia.");
933
+ if (this.channelData) {
934
+ return this.channelData;
904
935
  }
905
- const id = this.channelId || this.channelData?.id;
906
- const details = await this.baseClient.get(`/channels/${id}`);
936
+ if (!this.channelId) {
937
+ throw new Error("Nenhum ID de canal associado a esta inst\xE2ncia.");
938
+ }
939
+ const details = await this.baseClient.get(
940
+ `/channels/${this.channelId}`
941
+ );
907
942
  this.channelData = details;
908
943
  return details;
909
944
  }
@@ -912,7 +947,11 @@ var ChannelInstance = class extends import_events.EventEmitter {
912
947
  */
913
948
  async hangup() {
914
949
  if (!this.channelData) {
915
- throw new Error("O canal ainda n\xE3o foi criado.");
950
+ console.log("Canal n\xE3o inicializado, buscando detalhes...");
951
+ this.channelData = await this.getDetails();
952
+ }
953
+ if (!this.channelData?.id) {
954
+ throw new Error("N\xE3o foi poss\xEDvel inicializar o canal. ID inv\xE1lido.");
916
955
  }
917
956
  await this.baseClient.delete(`/channels/${this.channelData.id}`);
918
957
  }
@@ -920,11 +959,18 @@ var ChannelInstance = class extends import_events.EventEmitter {
920
959
  * Reproduz mídia no canal.
921
960
  */
922
961
  async play(options, playback) {
962
+ if (!this.channelData) {
963
+ console.log("Canal n\xE3o inicializado, buscando detalhes...");
964
+ this.channelData = await this.getDetails();
965
+ }
923
966
  if (!playback) {
924
967
  playback = this.client.Playback();
925
968
  }
969
+ if (!this.channelData?.id) {
970
+ throw new Error("N\xE3o foi poss\xEDvel inicializar o canal. ID inv\xE1lido.");
971
+ }
926
972
  await this.baseClient.post(
927
- `/channels/${this.channelData?.id}/play/${playback.id}`,
973
+ `/channels/${this.channelData.id}/play/${playback.id}`,
928
974
  options
929
975
  );
930
976
  return playback;
@@ -1624,8 +1670,13 @@ var AriClient = class {
1624
1670
  asterisk;
1625
1671
  bridges;
1626
1672
  /**
1627
- * Registra um listener para eventos globais de WebSocket.
1673
+ * Registra um listener para eventos de WebSocket relacionados a canais.
1674
+ * @param eventType Tipo de evento.
1675
+ * @param callback Callback a ser executado quando o evento for recebido.
1628
1676
  */
1677
+ onChannelEvent(eventType, callback) {
1678
+ this.wsClient?.on(eventType, callback);
1679
+ }
1629
1680
  /**
1630
1681
  * Registra um listener para eventos globais de WebSocket.
1631
1682
  * O channel no evento será automaticamente transformado em uma instância de ChannelInstance.