@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.
package/dist/esm/index.js CHANGED
@@ -846,6 +846,23 @@ function toQueryParams2(options) {
846
846
  function isChannelEvent(event) {
847
847
  return event.channel?.id !== void 0;
848
848
  }
849
+ var channelEvents = [
850
+ "ChannelCreated",
851
+ "ChannelDestroyed",
852
+ "ChannelEnteredBridge",
853
+ "ChannelLeftBridge",
854
+ "ChannelStateChange",
855
+ "ChannelDtmfReceived",
856
+ "ChannelDialplan",
857
+ "ChannelCallerId",
858
+ "ChannelUserevent",
859
+ "ChannelHangupRequest",
860
+ "ChannelVarset",
861
+ "ChannelTalkingStarted",
862
+ "ChannelTalkingFinished",
863
+ "ChannelHold",
864
+ "ChannelUnhold"
865
+ ];
849
866
 
850
867
  // src/ari-client/resources/channels.ts
851
868
  var ChannelInstance = class extends EventEmitter {
@@ -854,8 +871,22 @@ var ChannelInstance = class extends EventEmitter {
854
871
  this.client = client;
855
872
  this.baseClient = baseClient;
856
873
  this.channelId = channelId;
874
+ channelEvents.forEach((eventType) => {
875
+ this.client.onChannelEvent(eventType, (event) => {
876
+ if (this.isChannelEvent(event) && event.channel?.id === this.channelId) {
877
+ this.emit(eventType, event);
878
+ }
879
+ });
880
+ });
857
881
  }
858
882
  channelData = null;
883
+ /**
884
+ * Verifica se o evento possui a propriedade `channel`.
885
+ * @param event Evento recebido.
886
+ */
887
+ isChannelEvent(event) {
888
+ return event && typeof event === "object" && "channel" in event;
889
+ }
859
890
  /**
860
891
  * Getter para o ID do canal.
861
892
  */
@@ -877,11 +908,15 @@ var ChannelInstance = class extends EventEmitter {
877
908
  * Obtém os detalhes do canal.
878
909
  */
879
910
  async getDetails() {
880
- if (!this.channelId && !this.channelData) {
881
- throw new Error("Nenhum canal est\xE1 associado a esta inst\xE2ncia.");
911
+ if (this.channelData) {
912
+ return this.channelData;
882
913
  }
883
- const id = this.channelId || this.channelData?.id;
884
- const details = await this.baseClient.get(`/channels/${id}`);
914
+ if (!this.channelId) {
915
+ throw new Error("Nenhum ID de canal associado a esta inst\xE2ncia.");
916
+ }
917
+ const details = await this.baseClient.get(
918
+ `/channels/${this.channelId}`
919
+ );
885
920
  this.channelData = details;
886
921
  return details;
887
922
  }
@@ -890,7 +925,11 @@ var ChannelInstance = class extends EventEmitter {
890
925
  */
891
926
  async hangup() {
892
927
  if (!this.channelData) {
893
- throw new Error("O canal ainda n\xE3o foi criado.");
928
+ console.log("Canal n\xE3o inicializado, buscando detalhes...");
929
+ this.channelData = await this.getDetails();
930
+ }
931
+ if (!this.channelData?.id) {
932
+ throw new Error("N\xE3o foi poss\xEDvel inicializar o canal. ID inv\xE1lido.");
894
933
  }
895
934
  await this.baseClient.delete(`/channels/${this.channelData.id}`);
896
935
  }
@@ -898,11 +937,18 @@ var ChannelInstance = class extends EventEmitter {
898
937
  * Reproduz mídia no canal.
899
938
  */
900
939
  async play(options, playback) {
940
+ if (!this.channelData) {
941
+ console.log("Canal n\xE3o inicializado, buscando detalhes...");
942
+ this.channelData = await this.getDetails();
943
+ }
901
944
  if (!playback) {
902
945
  playback = this.client.Playback();
903
946
  }
947
+ if (!this.channelData?.id) {
948
+ throw new Error("N\xE3o foi poss\xEDvel inicializar o canal. ID inv\xE1lido.");
949
+ }
904
950
  await this.baseClient.post(
905
- `/channels/${this.channelData?.id}/play/${playback.id}`,
951
+ `/channels/${this.channelData.id}/play/${playback.id}`,
906
952
  options
907
953
  );
908
954
  return playback;
@@ -1602,8 +1648,13 @@ var AriClient = class {
1602
1648
  asterisk;
1603
1649
  bridges;
1604
1650
  /**
1605
- * Registra um listener para eventos globais de WebSocket.
1651
+ * Registra um listener para eventos de WebSocket relacionados a canais.
1652
+ * @param eventType Tipo de evento.
1653
+ * @param callback Callback a ser executado quando o evento for recebido.
1606
1654
  */
1655
+ onChannelEvent(eventType, callback) {
1656
+ this.wsClient?.on(eventType, callback);
1657
+ }
1607
1658
  /**
1608
1659
  * Registra um listener para eventos globais de WebSocket.
1609
1660
  * O channel no evento será automaticamente transformado em uma instância de ChannelInstance.