@ipcom/asterisk-ari 0.0.3 → 0.0.5

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.
@@ -97,37 +97,95 @@ var Channels = class {
97
97
  // src/ari-client/websocketClient.ts
98
98
  var import_ws = __toESM(require("ws"), 1);
99
99
  var WebSocketClient = class {
100
+ // Para evitar reconexões automáticas quando fechado manualmente
100
101
  constructor(url) {
101
102
  this.url = url;
102
103
  }
103
104
  ws = null;
104
- async connect() {
105
+ isClosedManually = false;
106
+ async connect(retryInterval = 1e3) {
105
107
  return new Promise((resolve, reject) => {
106
108
  this.ws = new import_ws.default(this.url);
107
- this.ws.on("open", resolve);
108
- this.ws.on("error", reject);
109
+ this.ws.on("open", () => {
110
+ console.log("WebSocket conectado.");
111
+ this.isClosedManually = false;
112
+ resolve();
113
+ });
114
+ this.ws.on("error", (err) => {
115
+ console.error("Erro na conex\xE3o WebSocket:", err);
116
+ if (!this.isClosedManually) {
117
+ setTimeout(() => this.reconnect(retryInterval), retryInterval);
118
+ }
119
+ reject(err);
120
+ });
121
+ this.ws.on("close", (code, reason) => {
122
+ console.warn(`WebSocket desconectado: ${code} - ${reason}`);
123
+ if (!this.isClosedManually) {
124
+ setTimeout(() => this.reconnect(retryInterval), retryInterval);
125
+ }
126
+ });
109
127
  });
110
128
  }
129
+ reconnect(retryInterval) {
130
+ console.log("Tentando reconectar ao WebSocket...");
131
+ this.connect(retryInterval).catch((err) => {
132
+ console.error("Erro ao tentar reconectar:", err);
133
+ });
134
+ }
135
+ isConnected() {
136
+ return this.ws?.readyState === import_ws.default.OPEN;
137
+ }
111
138
  on(event, callback) {
112
- if (!this.ws) throw new Error("WebSocket not initialized.");
139
+ if (!this.ws || this.ws.readyState !== import_ws.default.OPEN) {
140
+ throw new Error("WebSocket n\xE3o est\xE1 conectado.");
141
+ }
113
142
  this.ws.on(event, callback);
114
143
  }
144
+ send(data) {
145
+ if (!this.ws || this.ws.readyState !== import_ws.default.OPEN) {
146
+ throw new Error("WebSocket n\xE3o est\xE1 conectado.");
147
+ }
148
+ this.ws.send(data, (err) => {
149
+ if (err) {
150
+ console.error("Erro ao enviar dados pelo WebSocket:", err);
151
+ }
152
+ });
153
+ }
115
154
  close() {
116
- if (this.ws) this.ws.close();
155
+ if (this.ws) {
156
+ this.isClosedManually = true;
157
+ this.ws.close();
158
+ console.log("WebSocket fechado manualmente.");
159
+ }
117
160
  }
118
161
  };
119
162
 
120
163
  // src/ari-client/ariClient.ts
121
164
  var AriClient = class {
122
165
  // Adicionado
166
+ /**
167
+ * Initializes a new instance of the AriClient class.
168
+ *
169
+ * This constructor sets up the necessary configurations for connecting to an Asterisk ARI server,
170
+ * including WebSocket and HTTP connections. It also initializes the channels resource.
171
+ *
172
+ * @param config - The configuration object for the ARI client.
173
+ * @param config.host - The hostname or IP address of the Asterisk server.
174
+ * @param config.port - The port number on which the Asterisk ARI is listening.
175
+ * @param config.username - The username for authentication with the Asterisk ARI.
176
+ * @param config.password - The password for authentication with the Asterisk ARI.
177
+ * @param config.secure - Optional. If true, uses secure protocols (WSS/HTTPS). Defaults to false.
178
+ */
123
179
  constructor(config) {
124
180
  this.config = config;
125
- const protocol = config.secure === false ? "ws" : "wss";
181
+ const protocol = config.secure ? "wss" : "ws";
182
+ const httpProtocol = config.secure ? "https" : "http";
126
183
  const encodedUsername = encodeURIComponent(config.username);
127
184
  const encodedPassword = encodeURIComponent(config.password);
128
185
  const normalizedHost = config.host.replace(/^https?:\/\//, "");
129
186
  const wsUrl = `${protocol}://${encodedUsername}:${encodedPassword}@${normalizedHost}:${config.port}/ari/events`;
130
- const baseUrl = `${config.secure === false ? "http" : "https"}://${normalizedHost}:${config.port}/ari`;
187
+ const baseUrl = `${httpProtocol}://${normalizedHost}:${config.port}/ari`;
188
+ console.log({ wsUrl, baseUrl });
131
189
  this.wsClient = new WebSocketClient(wsUrl);
132
190
  this.baseClient = new BaseClient(baseUrl, config.username, config.password);
133
191
  this.channels = new Channels(this.baseClient);
@@ -135,18 +193,80 @@ var AriClient = class {
135
193
  wsClient;
136
194
  baseClient;
137
195
  channels;
196
+ /**
197
+ * Conecta ao WebSocket do ARI.
198
+ * @returns {Promise<void>} Retorna uma promise resolvida ao conectar com sucesso.
199
+ */
138
200
  async connectWebSocket() {
139
- await this.wsClient.connect();
201
+ try {
202
+ await this.wsClient.connect();
203
+ console.log("WebSocket conectado com sucesso!");
204
+ } catch (err) {
205
+ console.error("Erro ao conectar ao WebSocket:", err);
206
+ throw err;
207
+ }
208
+ }
209
+ /**
210
+ * Checks if the WebSocket connection is currently active.
211
+ *
212
+ * This method provides a way to determine the current state of the WebSocket connection
213
+ * to the Asterisk ARI server.
214
+ *
215
+ * @returns {boolean} Returns true if the WebSocket is connected, false otherwise.
216
+ */
217
+ isWebSocketConnected() {
218
+ return this.wsClient.isConnected();
140
219
  }
220
+ /**
221
+ * Registers a callback function for a specific WebSocket event.
222
+ *
223
+ * This method allows you to attach event listeners to WebSocket events
224
+ * from the Asterisk ARI server. When the specified event occurs, the
225
+ * provided callback function will be executed.
226
+ *
227
+ * @param event - The name of the WebSocket event to listen for.
228
+ * @param callback - The function to be called when the event occurs.
229
+ * It receives the event data as its parameter.
230
+ * @returns void
231
+ */
141
232
  onWebSocketEvent(event, callback) {
142
233
  this.wsClient.on(event, callback);
143
234
  }
235
+ /**
236
+ * Closes the WebSocket connection to the Asterisk ARI server.
237
+ *
238
+ * This method terminates the existing WebSocket connection, if one is active.
239
+ * It's useful for cleaning up resources when the connection is no longer needed.
240
+ */
144
241
  closeWebSocket() {
145
242
  this.wsClient.close();
146
243
  }
244
+ /**
245
+ * Retrieves a list of all active channels from the Asterisk ARI server.
246
+ *
247
+ * This method makes an asynchronous GET request to the '/channels' endpoint
248
+ * of the Asterisk ARI.
249
+ *
250
+ * @returns {Promise<any>} A promise that resolves with the list of active channels.
251
+ * The exact structure of the returned data depends on the
252
+ * Asterisk ARI specification.
253
+ */
147
254
  async listChannels() {
148
255
  return this.baseClient.get("/channels");
149
256
  }
257
+ /**
258
+ * Initiates a new channel on the Asterisk server.
259
+ *
260
+ * This method makes an asynchronous POST request to the '/channels' endpoint
261
+ * of the Asterisk ARI to create a new channel with the specified parameters.
262
+ *
263
+ * @param {any} data - An object containing the parameters for the new channel.
264
+ * The structure of this object should conform to the
265
+ * Asterisk ARI specification for channel origination.
266
+ * @returns {Promise<any>} A promise that resolves with the response from the
267
+ * Asterisk server, typically containing details of
268
+ * the newly created channel.
269
+ */
150
270
  async originateChannel(data) {
151
271
  return this.baseClient.post("/channels", data);
152
272
  }
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../src/index.ts", "../../src/ari-client/baseClient.ts", "../../src/ari-client/resources/channels.ts", "../../src/ari-client/websocketClient.ts", "../../src/ari-client/ariClient.ts"],
4
- "sourcesContent": ["// Exporta a classe principal AriClient\nexport { AriClient } from \"./ari-client/ariClient\";\n\n// Exporta as classes dos recursos, caso o usu\u00E1rio precise us\u00E1-las diretamente\nexport { Channels } from \"./ari-client/resources/channels\";\n\n// Exporta interfaces importantes para tipagem\nexport type { AriClientConfig } from \"./ari-client/ariClient\";\nexport type { Channel } from \"./ari-client/interfaces/channels.types\";\n", "import axios, { type AxiosInstance } from \"axios\";\n\nexport class BaseClient {\n private client: AxiosInstance;\n\n constructor(baseUrl: string, username: string, password: string) {\n this.client = axios.create({\n baseURL: baseUrl,\n auth: { username, password },\n });\n }\n\n async get<T>(path: string): Promise<T> {\n const response = await this.client.get<T>(path);\n return response.data;\n }\n\n async post<T>(path: string, data?: unknown): Promise<T> {\n const response = await this.client.post<T>(path, data);\n return response.data;\n }\n}\n", "import type { BaseClient } from \"../baseClient\";\nimport type { Channel, OriginateRequest } from \"../interfaces/channels.types\";\n\nexport class Channels {\n constructor(private client: BaseClient) {}\n\n // Lista todos os canais ativos\n async list(): Promise<Channel[]> {\n return this.client.get<Channel[]>(\"/channels\");\n }\n\n // Cria um novo canal\n async originate(data: OriginateRequest): Promise<Channel> {\n return this.client.post<Channel>(\"/channels\", data);\n }\n\n // Obt\u00E9m detalhes de um canal espec\u00EDfico\n async getDetails(channelId: string): Promise<Channel> {\n return this.client.get<Channel>(`/channels/${channelId}`);\n }\n\n // Desliga (hangup) um canal\n async hangup(channelId: string): Promise<void> {\n return this.client.post<void>(`/channels/${channelId}/hangup`);\n }\n\n // Continua no dialplan\n async continueDialplan(\n channelId: string,\n context?: string,\n extension?: string,\n priority?: number,\n label?: string,\n ): Promise<void> {\n return this.client.post<void>(`/channels/${channelId}/continue`, {\n context,\n extension,\n priority,\n label,\n });\n }\n\n // Move o canal para outra aplica\u00E7\u00E3o Stasis\n async moveToApplication(\n channelId: string,\n app: string,\n appArgs?: string,\n ): Promise<void> {\n return this.client.post<void>(`/channels/${channelId}/move`, {\n app,\n appArgs,\n });\n }\n}\n", "import WebSocket from \"ws\";\n\nexport class WebSocketClient {\n private ws: WebSocket | null = null;\n\n constructor(private url: string) {}\n\n async connect(): Promise<void> {\n return new Promise((resolve, reject) => {\n this.ws = new WebSocket(this.url);\n this.ws.on(\"open\", resolve);\n this.ws.on(\"error\", reject);\n });\n }\n\n on(event: string, callback: (data: any) => void): void {\n if (!this.ws) throw new Error(\"WebSocket not initialized.\");\n this.ws.on(event, callback);\n }\n\n close(): void {\n if (this.ws) this.ws.close();\n }\n}\n", "import { BaseClient } from \"./baseClient.js\";\nimport { Channels } from \"./resources/channels\";\nimport { WebSocketClient } from \"./websocketClient.js\";\n\nexport interface AriClientConfig {\n host: string;\n port: number;\n username: string;\n password: string;\n secure?: boolean;\n}\n\nexport class AriClient {\n private wsClient: WebSocketClient;\n private readonly baseClient: BaseClient;\n\n public channels: Channels; // Adicionado\n\n constructor(private config: AriClientConfig) {\n const protocol = config.secure === false ? \"ws\" : \"wss\";\n\n const encodedUsername = encodeURIComponent(config.username);\n const encodedPassword = encodeURIComponent(config.password);\n\n const normalizedHost = config.host.replace(/^https?:\\/\\//, \"\");\n const wsUrl = `${protocol}://${encodedUsername}:${encodedPassword}@${normalizedHost}:${config.port}/ari/events`;\n const baseUrl = `${config.secure === false ? \"http\" : \"https\"}://${normalizedHost}:${config.port}/ari`;\n\n this.wsClient = new WebSocketClient(wsUrl);\n this.baseClient = new BaseClient(baseUrl, config.username, config.password);\n\n // Inicializa\u00E7\u00E3o da propriedade no construtor\n this.channels = new Channels(this.baseClient);\n }\n\n async connectWebSocket(): Promise<void> {\n await this.wsClient.connect();\n }\n\n onWebSocketEvent(event: string, callback: (data: any) => void): void {\n this.wsClient.on(event, callback);\n }\n\n closeWebSocket(): void {\n this.wsClient.close();\n }\n\n async listChannels() {\n return this.baseClient.get(\"/channels\");\n }\n\n async originateChannel(data: any) {\n return this.baseClient.post(\"/channels\", data);\n }\n}\n"],
5
- "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,mBAA0C;AAEnC,IAAM,aAAN,MAAiB;AAAA,EACd;AAAA,EAER,YAAY,SAAiB,UAAkB,UAAkB;AAC/D,SAAK,SAAS,aAAAA,QAAM,OAAO;AAAA,MACzB,SAAS;AAAA,MACT,MAAM,EAAE,UAAU,SAAS;AAAA,IAC7B,CAAC;AAAA,EACH;AAAA,EAEA,MAAM,IAAO,MAA0B;AACrC,UAAM,WAAW,MAAM,KAAK,OAAO,IAAO,IAAI;AAC9C,WAAO,SAAS;AAAA,EAClB;AAAA,EAEA,MAAM,KAAQ,MAAc,MAA4B;AACtD,UAAM,WAAW,MAAM,KAAK,OAAO,KAAQ,MAAM,IAAI;AACrD,WAAO,SAAS;AAAA,EAClB;AACF;;;AClBO,IAAM,WAAN,MAAe;AAAA,EACpB,YAAoB,QAAoB;AAApB;AAAA,EAAqB;AAAA;AAAA,EAGzC,MAAM,OAA2B;AAC/B,WAAO,KAAK,OAAO,IAAe,WAAW;AAAA,EAC/C;AAAA;AAAA,EAGA,MAAM,UAAU,MAA0C;AACxD,WAAO,KAAK,OAAO,KA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kB,mBAAmB,OAAO,QAAQ;AAC1D,UAAM,kBAAkB,mBAAmB,OAAO,QAAQ;AAE1D,UAAM,iBAAiB,OAAO,KAAK,QAAQ,gBAAgB,EAAE;AAC7D,UAAM,QAAQ,GAAG,QAAQ,MAAM,eAAe,IAAI,eAAe,IAAI,cAAc,IAAI,OAAO,IAAI;AAClG,UAAM,UAAU,GAAG,OAAO,WAAW,QAAQ,SAAS,OAAO,MAAM,cAAc,IAAI,OAAO,IAAI;AAEhG,SAAK,WAAW,IAAI,gBAAgB,KAAK;AACzC,SAAK,aAAa,IAAI,WAAW,SAAS,OAAO,UAAU,OAAO,QAAQ;AAG1E,SAAK,WAAW,IAAI,SAAS,KAAK,UAAU;AAAA,EAC9C;AAAA,EApBQ;AAAA,EACS;AAAA,EAEV;AAAA,EAmBP,MAAM,mBAAkC;AACtC,UAAM,KAAK,SAAS,QAAQ;AAAA,EAC9B;AAAA,EAEA,iBAAiB,OAAe,UAAqC;AACnE,SAAK,SAAS,GAAG,OAAO,QAAQ;AAAA,EAClC;AAAA,EAEA,iBAAuB;AACrB,SAAK,SAAS,MAAM;AAAA,EACtB;AAAA,EAEA,MAAM,eAAe;AACnB,WAAO,KAAK,WAAW,IAAI,WAAW;AAAA,EACxC;AAAA,EAEA,MAAM,iBAAiB,MAAW;AAChC,WAAO,KAAK,WAAW,KAAK,aAAa,IAAI;AAAA,EAC/C;AACF;",
4
+ "sourcesContent": ["// Exporta a classe principal AriClient\nexport { AriClient } from \"./ari-client/ariClient\";\n\n// Exporta as classes dos recursos, caso o usu\u00E1rio precise us\u00E1-las diretamente\nexport { Channels } from \"./ari-client/resources/channels\";\n\n// Exporta interfaces importantes para tipagem\nexport type { AriClientConfig } from \"./ari-client/ariClient\";\nexport type { Channel } from \"./ari-client/interfaces/channels.types\";\n", "import axios, { type AxiosInstance } from \"axios\";\n\nexport class BaseClient {\n private client: AxiosInstance;\n\n constructor(baseUrl: string, username: string, password: string) {\n this.client = axios.create({\n baseURL: baseUrl,\n auth: { username, password },\n });\n }\n\n async get<T>(path: string): Promise<T> {\n const response = await this.client.get<T>(path);\n return response.data;\n }\n\n async post<T>(path: string, data?: unknown): Promise<T> {\n const response = await this.client.post<T>(path, data);\n return response.data;\n }\n}\n", "import type { BaseClient } from \"../baseClient\";\nimport type { Channel, OriginateRequest } from \"../interfaces/channels.types\";\n\nexport class Channels {\n constructor(private client: BaseClient) {}\n\n // Lista todos os canais ativos\n async list(): Promise<Channel[]> {\n return this.client.get<Channel[]>(\"/channels\");\n }\n\n // Cria um novo canal\n async originate(data: OriginateRequest): Promise<Channel> {\n return this.client.post<Channel>(\"/channels\", data);\n }\n\n // Obt\u00E9m detalhes de um canal espec\u00EDfico\n async getDetails(channelId: string): Promise<Channel> {\n return this.client.get<Channel>(`/channels/${channelId}`);\n }\n\n // Desliga (hangup) um canal\n async hangup(channelId: string): Promise<void> {\n return this.client.post<void>(`/channels/${channelId}/hangup`);\n }\n\n // Continua no dialplan\n async continueDialplan(\n channelId: string,\n context?: string,\n extension?: string,\n priority?: number,\n label?: string,\n ): Promise<void> {\n return this.client.post<void>(`/channels/${channelId}/continue`, {\n context,\n extension,\n priority,\n label,\n });\n }\n\n // Move o canal para outra aplica\u00E7\u00E3o Stasis\n async moveToApplication(\n channelId: string,\n app: string,\n appArgs?: string,\n ): Promise<void> {\n return this.client.post<void>(`/channels/${channelId}/move`, {\n app,\n appArgs,\n });\n }\n}\n", "import WebSocket from \"ws\";\n\nexport class WebSocketClient {\n private ws: WebSocket | null = null;\n private isClosedManually = false; // Para evitar reconex\u00F5es autom\u00E1ticas quando fechado manualmente\n\n constructor(private url: string) {}\n\n async connect(retryInterval = 1000): Promise<void> {\n return new Promise((resolve, reject) => {\n this.ws = new WebSocket(this.url);\n\n this.ws.on(\"open\", () => {\n console.log(\"WebSocket conectado.\");\n this.isClosedManually = false;\n resolve();\n });\n\n this.ws.on(\"error\", (err) => {\n console.error(\"Erro na conex\u00E3o WebSocket:\", err);\n if (!this.isClosedManually) {\n setTimeout(() => this.reconnect(retryInterval), retryInterval);\n }\n reject(err);\n });\n\n this.ws.on(\"close\", (code, reason) => {\n console.warn(`WebSocket desconectado: ${code} - ${reason}`);\n if (!this.isClosedManually) {\n setTimeout(() => this.reconnect(retryInterval), retryInterval);\n }\n });\n });\n }\n\n private reconnect(retryInterval: number): void {\n console.log(\"Tentando reconectar ao WebSocket...\");\n this.connect(retryInterval).catch((err) => {\n console.error(\"Erro ao tentar reconectar:\", err);\n });\n }\n\n isConnected(): boolean {\n return this.ws?.readyState === WebSocket.OPEN;\n }\n\n on(event: string, callback: (data: any) => void): void {\n if (!this.ws || this.ws.readyState !== WebSocket.OPEN) {\n throw new Error(\"WebSocket n\u00E3o est\u00E1 conectado.\");\n }\n\n this.ws.on(event, callback);\n }\n\n send(data: any): void {\n if (!this.ws || this.ws.readyState !== WebSocket.OPEN) {\n throw new Error(\"WebSocket n\u00E3o est\u00E1 conectado.\");\n }\n\n this.ws.send(data, (err) => {\n if (err) {\n console.error(\"Erro ao enviar dados pelo WebSocket:\", err);\n }\n });\n }\n\n close(): void {\n if (this.ws) {\n this.isClosedManually = true;\n this.ws.close();\n console.log(\"WebSocket fechado manualmente.\");\n }\n }\n}\n", "import { BaseClient } from \"./baseClient.js\";\nimport { Channels } from \"./resources/channels\";\nimport { WebSocketClient } from \"./websocketClient.js\";\n\nexport interface AriClientConfig {\n host: string;\n port: number;\n username: string;\n password: string;\n secure?: boolean;\n}\n\nexport class AriClient {\n private wsClient: WebSocketClient;\n private readonly baseClient: BaseClient;\n\n public channels: Channels; // Adicionado\n\n /**\n * Initializes a new instance of the AriClient class.\n * \n * This constructor sets up the necessary configurations for connecting to an Asterisk ARI server,\n * including WebSocket and HTTP connections. It also initializes the channels resource.\n * \n * @param config - The configuration object for the ARI client.\n * @param config.host - The hostname or IP address of the Asterisk server.\n * @param config.port - The port number on which the Asterisk ARI is listening.\n * @param config.username - The username for authentication with the Asterisk ARI.\n * @param config.password - The password for authentication with the Asterisk ARI.\n * @param config.secure - Optional. If true, uses secure protocols (WSS/HTTPS). Defaults to false.\n */\n constructor(private config: AriClientConfig) {\n // Determina o protocolo com base na seguran\u00E7a\n const protocol = config.secure ? \"wss\" : \"ws\";\n const httpProtocol = config.secure ? \"https\" : \"http\";\n \n // Codifica credenciais para uso seguro no URL\n const encodedUsername = encodeURIComponent(config.username);\n const encodedPassword = encodeURIComponent(config.password);\n \n // Remove o protocolo do host, caso esteja presente\n const normalizedHost = config.host.replace(/^https?:\\/\\//, \"\");\n \n // Constr\u00F3i os URLs\n const wsUrl = `${protocol}://${encodedUsername}:${encodedPassword}@${normalizedHost}:${config.port}/ari/events`;\n const baseUrl = `${httpProtocol}://${normalizedHost}:${config.port}/ari`;\n \n console.log({ wsUrl, baseUrl });\n \n // Inicializa os clientes WebSocket e HTTP\n this.wsClient = new WebSocketClient(wsUrl);\n this.baseClient = new BaseClient(baseUrl, config.username, config.password);\n \n // Inicializa os recursos do cliente\n this.channels = new Channels(this.baseClient);\n }\n\n /**\n * Conecta ao WebSocket do ARI.\n * @returns {Promise<void>} Retorna uma promise resolvida ao conectar com sucesso.\n */\n async connectWebSocket(): Promise<void> {\n try {\n await this.wsClient.connect();\n console.log(\"WebSocket conectado com sucesso!\");\n } catch (err) {\n console.error(\"Erro ao conectar ao WebSocket:\", err);\n throw err;\n }\n }\n\n /**\n * Checks if the WebSocket connection is currently active.\n *\n * This method provides a way to determine the current state of the WebSocket connection\n * to the Asterisk ARI server.\n *\n * @returns {boolean} Returns true if the WebSocket is connected, false otherwise.\n */\n isWebSocketConnected(): boolean {\n return this.wsClient.isConnected();\n }\n\n /**\n * Registers a callback function for a specific WebSocket event.\n *\n * This method allows you to attach event listeners to WebSocket events\n * from the Asterisk ARI server. When the specified event occurs, the\n * provided callback function will be executed.\n *\n * @param event - The name of the WebSocket event to listen for.\n * @param callback - The function to be called when the event occurs.\n * It receives the event data as its parameter.\n * @returns void\n */\n onWebSocketEvent(event: string, callback: (data: any) => void): void {\n this.wsClient.on(event, callback);\n }\n\n /**\n * Closes the WebSocket connection to the Asterisk ARI server.\n *\n * This method terminates the existing WebSocket connection, if one is active.\n * It's useful for cleaning up resources when the connection is no longer needed.\n */\n closeWebSocket(): void {\n this.wsClient.close();\n }\n\n /**\n * Retrieves a list of all active channels from the Asterisk ARI server.\n *\n * This method makes an asynchronous GET request to the '/channels' endpoint\n * of the Asterisk ARI.\n *\n * @returns {Promise<any>} A promise that resolves with the list of active channels.\n * The exact structure of the returned data depends on the\n * Asterisk ARI specification.\n */\n async listChannels() {\n return this.baseClient.get(\"/channels\");\n }\n\n /**\n * Initiates a new channel on the Asterisk server.\n *\n * This method makes an asynchronous POST request to the '/channels' endpoint\n * of the Asterisk ARI to create a new channel with the specified parameters.\n *\n * @param {any} data - An object containing the parameters for the new channel.\n * The structure of this object should conform to the\n * Asterisk ARI specification for channel origination.\n * @returns {Promise<any>} A promise that resolves with the response from the\n * Asterisk server, typically containing details of\n * the newly created channel.\n */\n async originateChannel(data: any) {\n return this.baseClient.post(\"/channels\", data);\n }\n}\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,mBAA0C;AAEnC,IAAM,aAAN,MAAiB;AAAA,EACd;AAAA,EAER,YAAY,SAAiB,UAAkB,UAAkB;AAC/D,SAAK,SAAS,aAAAA,QAAM,OAAO;AAAA,MACzB,SAAS;AAAA,MACT,MAAM,EAAE,UAAU,SAAS;AAAA,IAC7B,CAAC;AAAA,EACH;AAAA,EAEA,MAAM,IAAO,MAA0B;AACrC,UAAM,WAAW,MAAM,KAAK,OAAO,IAAO,IAAI;AAC9C,WAAO,SAAS;AAAA,EAClB;AAAA,EAEA,MAAM,KAAQ,MAAc,MAA4B;AACtD,UAAM,WAAW,MAAM,KAAK,OAAO,KAAQ,MAAM,IAAI;AACrD,WAAO,SAAS;AAAA,EAClB;AACF;;;AClBO,IAAM,WAAN,MAAe;AAAA,EACpB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;AAAA,EAQA,iBAAuB;AACrB,SAAK,SAAS,MAAM;AAAA,EACtB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAYA,MAAM,eAAe;AACnB,WAAO,KAAK,WAAW,IAAI,WAAW;AAAA,EACxC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAeA,MAAM,iBAAiB,MAAW;AAChC,WAAO,KAAK,WAAW,KAAK,aAAa,IAAI;AAAA,EAC/C;AACF;",
6
6
  "names": ["axios", "WebSocket"]
7
7
  }
package/dist/esm/index.js CHANGED
@@ -60,37 +60,95 @@ var Channels = class {
60
60
  // src/ari-client/websocketClient.ts
61
61
  import WebSocket from "ws";
62
62
  var WebSocketClient = class {
63
+ // Para evitar reconexões automáticas quando fechado manualmente
63
64
  constructor(url) {
64
65
  this.url = url;
65
66
  }
66
67
  ws = null;
67
- async connect() {
68
+ isClosedManually = false;
69
+ async connect(retryInterval = 1e3) {
68
70
  return new Promise((resolve, reject) => {
69
71
  this.ws = new WebSocket(this.url);
70
- this.ws.on("open", resolve);
71
- this.ws.on("error", reject);
72
+ this.ws.on("open", () => {
73
+ console.log("WebSocket conectado.");
74
+ this.isClosedManually = false;
75
+ resolve();
76
+ });
77
+ this.ws.on("error", (err) => {
78
+ console.error("Erro na conex\xE3o WebSocket:", err);
79
+ if (!this.isClosedManually) {
80
+ setTimeout(() => this.reconnect(retryInterval), retryInterval);
81
+ }
82
+ reject(err);
83
+ });
84
+ this.ws.on("close", (code, reason) => {
85
+ console.warn(`WebSocket desconectado: ${code} - ${reason}`);
86
+ if (!this.isClosedManually) {
87
+ setTimeout(() => this.reconnect(retryInterval), retryInterval);
88
+ }
89
+ });
72
90
  });
73
91
  }
92
+ reconnect(retryInterval) {
93
+ console.log("Tentando reconectar ao WebSocket...");
94
+ this.connect(retryInterval).catch((err) => {
95
+ console.error("Erro ao tentar reconectar:", err);
96
+ });
97
+ }
98
+ isConnected() {
99
+ return this.ws?.readyState === WebSocket.OPEN;
100
+ }
74
101
  on(event, callback) {
75
- if (!this.ws) throw new Error("WebSocket not initialized.");
102
+ if (!this.ws || this.ws.readyState !== WebSocket.OPEN) {
103
+ throw new Error("WebSocket n\xE3o est\xE1 conectado.");
104
+ }
76
105
  this.ws.on(event, callback);
77
106
  }
107
+ send(data) {
108
+ if (!this.ws || this.ws.readyState !== WebSocket.OPEN) {
109
+ throw new Error("WebSocket n\xE3o est\xE1 conectado.");
110
+ }
111
+ this.ws.send(data, (err) => {
112
+ if (err) {
113
+ console.error("Erro ao enviar dados pelo WebSocket:", err);
114
+ }
115
+ });
116
+ }
78
117
  close() {
79
- if (this.ws) this.ws.close();
118
+ if (this.ws) {
119
+ this.isClosedManually = true;
120
+ this.ws.close();
121
+ console.log("WebSocket fechado manualmente.");
122
+ }
80
123
  }
81
124
  };
82
125
 
83
126
  // src/ari-client/ariClient.ts
84
127
  var AriClient = class {
85
128
  // Adicionado
129
+ /**
130
+ * Initializes a new instance of the AriClient class.
131
+ *
132
+ * This constructor sets up the necessary configurations for connecting to an Asterisk ARI server,
133
+ * including WebSocket and HTTP connections. It also initializes the channels resource.
134
+ *
135
+ * @param config - The configuration object for the ARI client.
136
+ * @param config.host - The hostname or IP address of the Asterisk server.
137
+ * @param config.port - The port number on which the Asterisk ARI is listening.
138
+ * @param config.username - The username for authentication with the Asterisk ARI.
139
+ * @param config.password - The password for authentication with the Asterisk ARI.
140
+ * @param config.secure - Optional. If true, uses secure protocols (WSS/HTTPS). Defaults to false.
141
+ */
86
142
  constructor(config) {
87
143
  this.config = config;
88
- const protocol = config.secure === false ? "ws" : "wss";
144
+ const protocol = config.secure ? "wss" : "ws";
145
+ const httpProtocol = config.secure ? "https" : "http";
89
146
  const encodedUsername = encodeURIComponent(config.username);
90
147
  const encodedPassword = encodeURIComponent(config.password);
91
148
  const normalizedHost = config.host.replace(/^https?:\/\//, "");
92
149
  const wsUrl = `${protocol}://${encodedUsername}:${encodedPassword}@${normalizedHost}:${config.port}/ari/events`;
93
- const baseUrl = `${config.secure === false ? "http" : "https"}://${normalizedHost}:${config.port}/ari`;
150
+ const baseUrl = `${httpProtocol}://${normalizedHost}:${config.port}/ari`;
151
+ console.log({ wsUrl, baseUrl });
94
152
  this.wsClient = new WebSocketClient(wsUrl);
95
153
  this.baseClient = new BaseClient(baseUrl, config.username, config.password);
96
154
  this.channels = new Channels(this.baseClient);
@@ -98,18 +156,80 @@ var AriClient = class {
98
156
  wsClient;
99
157
  baseClient;
100
158
  channels;
159
+ /**
160
+ * Conecta ao WebSocket do ARI.
161
+ * @returns {Promise<void>} Retorna uma promise resolvida ao conectar com sucesso.
162
+ */
101
163
  async connectWebSocket() {
102
- await this.wsClient.connect();
103
- }
164
+ try {
165
+ await this.wsClient.connect();
166
+ console.log("WebSocket conectado com sucesso!");
167
+ } catch (err) {
168
+ console.error("Erro ao conectar ao WebSocket:", err);
169
+ throw err;
170
+ }
171
+ }
172
+ /**
173
+ * Checks if the WebSocket connection is currently active.
174
+ *
175
+ * This method provides a way to determine the current state of the WebSocket connection
176
+ * to the Asterisk ARI server.
177
+ *
178
+ * @returns {boolean} Returns true if the WebSocket is connected, false otherwise.
179
+ */
180
+ isWebSocketConnected() {
181
+ return this.wsClient.isConnected();
182
+ }
183
+ /**
184
+ * Registers a callback function for a specific WebSocket event.
185
+ *
186
+ * This method allows you to attach event listeners to WebSocket events
187
+ * from the Asterisk ARI server. When the specified event occurs, the
188
+ * provided callback function will be executed.
189
+ *
190
+ * @param event - The name of the WebSocket event to listen for.
191
+ * @param callback - The function to be called when the event occurs.
192
+ * It receives the event data as its parameter.
193
+ * @returns void
194
+ */
104
195
  onWebSocketEvent(event, callback) {
105
196
  this.wsClient.on(event, callback);
106
197
  }
198
+ /**
199
+ * Closes the WebSocket connection to the Asterisk ARI server.
200
+ *
201
+ * This method terminates the existing WebSocket connection, if one is active.
202
+ * It's useful for cleaning up resources when the connection is no longer needed.
203
+ */
107
204
  closeWebSocket() {
108
205
  this.wsClient.close();
109
206
  }
207
+ /**
208
+ * Retrieves a list of all active channels from the Asterisk ARI server.
209
+ *
210
+ * This method makes an asynchronous GET request to the '/channels' endpoint
211
+ * of the Asterisk ARI.
212
+ *
213
+ * @returns {Promise<any>} A promise that resolves with the list of active channels.
214
+ * The exact structure of the returned data depends on the
215
+ * Asterisk ARI specification.
216
+ */
110
217
  async listChannels() {
111
218
  return this.baseClient.get("/channels");
112
219
  }
220
+ /**
221
+ * Initiates a new channel on the Asterisk server.
222
+ *
223
+ * This method makes an asynchronous POST request to the '/channels' endpoint
224
+ * of the Asterisk ARI to create a new channel with the specified parameters.
225
+ *
226
+ * @param {any} data - An object containing the parameters for the new channel.
227
+ * The structure of this object should conform to the
228
+ * Asterisk ARI specification for channel origination.
229
+ * @returns {Promise<any>} A promise that resolves with the response from the
230
+ * Asterisk server, typically containing details of
231
+ * the newly created channel.
232
+ */
113
233
  async originateChannel(data) {
114
234
  return this.baseClient.post("/channels", data);
115
235
  }
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../src/ari-client/baseClient.ts", "../../src/ari-client/resources/channels.ts", "../../src/ari-client/websocketClient.ts", "../../src/ari-client/ariClient.ts"],
4
- "sourcesContent": ["import axios, { type AxiosInstance } from \"axios\";\n\nexport class BaseClient {\n private client: AxiosInstance;\n\n constructor(baseUrl: string, username: string, password: string) {\n this.client = axios.create({\n baseURL: baseUrl,\n auth: { username, password },\n });\n }\n\n async get<T>(path: string): Promise<T> {\n const response = await this.client.get<T>(path);\n return response.data;\n }\n\n async post<T>(path: string, data?: unknown): Promise<T> {\n const response = await this.client.post<T>(path, data);\n return response.data;\n }\n}\n", "import type { BaseClient } from \"../baseClient\";\nimport type { Channel, OriginateRequest } from \"../interfaces/channels.types\";\n\nexport class Channels {\n constructor(private client: BaseClient) {}\n\n // Lista todos os canais ativos\n async list(): Promise<Channel[]> {\n return this.client.get<Channel[]>(\"/channels\");\n }\n\n // Cria um novo canal\n async originate(data: OriginateRequest): Promise<Channel> {\n return this.client.post<Channel>(\"/channels\", data);\n }\n\n // Obt\u00E9m detalhes de um canal espec\u00EDfico\n async getDetails(channelId: string): Promise<Channel> {\n return this.client.get<Channel>(`/channels/${channelId}`);\n }\n\n // Desliga (hangup) um canal\n async hangup(channelId: string): Promise<void> {\n return this.client.post<void>(`/channels/${channelId}/hangup`);\n }\n\n // Continua no dialplan\n async continueDialplan(\n channelId: string,\n context?: string,\n extension?: string,\n priority?: number,\n label?: string,\n ): Promise<void> {\n return this.client.post<void>(`/channels/${channelId}/continue`, {\n context,\n extension,\n priority,\n label,\n });\n }\n\n // Move o canal para outra aplica\u00E7\u00E3o Stasis\n async moveToApplication(\n channelId: string,\n app: string,\n appArgs?: string,\n ): Promise<void> {\n return this.client.post<void>(`/channels/${channelId}/move`, {\n app,\n appArgs,\n });\n }\n}\n", "import WebSocket from \"ws\";\n\nexport class WebSocketClient {\n private ws: WebSocket | null = null;\n\n constructor(private url: string) {}\n\n async connect(): Promise<void> {\n return new Promise((resolve, reject) => {\n this.ws = new WebSocket(this.url);\n this.ws.on(\"open\", resolve);\n this.ws.on(\"error\", reject);\n });\n }\n\n on(event: string, callback: (data: any) => void): void {\n if (!this.ws) throw new Error(\"WebSocket not initialized.\");\n this.ws.on(event, callback);\n }\n\n close(): void {\n if (this.ws) this.ws.close();\n }\n}\n", "import { BaseClient } from \"./baseClient.js\";\nimport { Channels } from \"./resources/channels\";\nimport { WebSocketClient } from \"./websocketClient.js\";\n\nexport interface AriClientConfig {\n host: string;\n port: number;\n username: string;\n password: string;\n secure?: boolean;\n}\n\nexport class AriClient {\n private wsClient: WebSocketClient;\n private readonly baseClient: BaseClient;\n\n public channels: Channels; // Adicionado\n\n constructor(private config: AriClientConfig) {\n const protocol = config.secure === false ? \"ws\" : \"wss\";\n\n const encodedUsername = encodeURIComponent(config.username);\n const encodedPassword = encodeURIComponent(config.password);\n\n const normalizedHost = config.host.replace(/^https?:\\/\\//, \"\");\n const wsUrl = `${protocol}://${encodedUsername}:${encodedPassword}@${normalizedHost}:${config.port}/ari/events`;\n const baseUrl = `${config.secure === false ? \"http\" : \"https\"}://${normalizedHost}:${config.port}/ari`;\n\n this.wsClient = new WebSocketClient(wsUrl);\n this.baseClient = new BaseClient(baseUrl, config.username, config.password);\n\n // Inicializa\u00E7\u00E3o da propriedade no construtor\n this.channels = new Channels(this.baseClient);\n }\n\n async connectWebSocket(): Promise<void> {\n await this.wsClient.connect();\n }\n\n onWebSocketEvent(event: string, callback: (data: any) => void): void {\n this.wsClient.on(event, callback);\n }\n\n closeWebSocket(): void {\n this.wsClient.close();\n }\n\n async listChannels() {\n return this.baseClient.get(\"/channels\");\n }\n\n async originateChannel(data: any) {\n return this.baseClient.post(\"/channels\", data);\n }\n}\n"],
5
- "mappings": ";AAAA,OAAO,WAAmC;AAEnC,IAAM,aAAN,MAAiB;AAAA,EACd;AAAA,EAER,YAAY,SAAiB,UAAkB,UAAkB;AAC/D,SAAK,SAAS,MAAM,OAAO;AAAA,MACzB,SAAS;AAAA,MACT,MAAM,EAAE,UAAU,SAAS;AAAA,IAC7B,CAAC;AAAA,EACH;AAAA,EAEA,MAAM,IAAO,MAA0B;AACrC,UAAM,WAAW,MAAM,KAAK,OAAO,IAAO,IAAI;AAC9C,WAAO,SAAS;AAAA,EAClB;AAAA,EAEA,MAAM,KAAQ,MAAc,MAA4B;AACtD,UAAM,WAAW,MAAM,KAAK,OAAO,KAAQ,MAAM,IAAI;AACrD,WAAO,SAAS;AAAA,EAClB;AACF;;;AClBO,IAAM,WAAN,MAAe;AAAA,EACpB,YAAoB,QAAoB;AAApB;AAAA,EAAqB;AAAA;AAAA,EAGzC,MAAM,OAA2B;AAC/B,WAAO,KAAK,OAAO,IAAe,WAAW;AAAA,EAC/C;AAAA;AAAA,EAGA,MAAM,UAAU,MAA0C;AACxD,WAAO,KAAK,OAAO,KAAc,aAAa,IAAI;AAAA,EACpD;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,UAAM,kBAAkB,mBAAmB,OAAO,QAAQ;AAE1D,UAAM,iBAAiB,OAAO,KAAK,QAAQ,gBAAgB,EAAE;AAC7D,UAAM,QAAQ,GAAG,QAAQ,MAAM,eAAe,IAAI,eAAe,IAAI,cAAc,IAAI,OAAO,IAAI;AAClG,UAAM,UAAU,GAAG,OAAO,WAAW,QAAQ,SAAS,OAAO,MAAM,cAAc,IAAI,OAAO,IAAI;AAEhG,SAAK,WAAW,IAAI,gBAAgB,KAAK;AACzC,SAAK,aAAa,IAAI,WAAW,SAAS,OAAO,UAAU,OAAO,QAAQ;AAG1E,SAAK,WAAW,IAAI,SAAS,KAAK,UAAU;AAAA,EAC9C;AAAA,EApBQ;AAAA,EACS;AAAA,EAEV;AAAA,EAmBP,MAAM,mBAAkC;AACtC,UAAM,KAAK,SAAS,QAAQ;AAAA,EAC9B;AAAA,EAEA,iBAAiB,OAAe,UAAqC;AACnE,SAAK,SAAS,GAAG,OAAO,QAAQ;AAAA,EAClC;AAAA,EAEA,iBAAuB;AACrB,SAAK,SAAS,MAAM;AAAA,EACtB;AAAA,EAEA,MAAM,eAAe;AACnB,WAAO,KAAK,WAAW,IAAI,WAAW;AAAA,EACxC;AAAA,EAEA,MAAM,iBAAiB,MAAW;AAChC,WAAO,KAAK,WAAW,KAAK,aAAa,IAAI;AAAA,EAC/C;AACF;",
4
+ "sourcesContent": ["import axios, { type AxiosInstance } from \"axios\";\n\nexport class BaseClient {\n private client: AxiosInstance;\n\n constructor(baseUrl: string, username: string, password: string) {\n this.client = axios.create({\n baseURL: baseUrl,\n auth: { username, password },\n });\n }\n\n async get<T>(path: string): Promise<T> {\n const response = await this.client.get<T>(path);\n return response.data;\n }\n\n async post<T>(path: string, data?: unknown): Promise<T> {\n const response = await this.client.post<T>(path, data);\n return response.data;\n }\n}\n", "import type { BaseClient } from \"../baseClient\";\nimport type { Channel, OriginateRequest } from \"../interfaces/channels.types\";\n\nexport class Channels {\n constructor(private client: BaseClient) {}\n\n // Lista todos os canais ativos\n async list(): Promise<Channel[]> {\n return this.client.get<Channel[]>(\"/channels\");\n }\n\n // Cria um novo canal\n async originate(data: OriginateRequest): Promise<Channel> {\n return this.client.post<Channel>(\"/channels\", data);\n }\n\n // Obt\u00E9m detalhes de um canal espec\u00EDfico\n async getDetails(channelId: string): Promise<Channel> {\n return this.client.get<Channel>(`/channels/${channelId}`);\n }\n\n // Desliga (hangup) um canal\n async hangup(channelId: string): Promise<void> {\n return this.client.post<void>(`/channels/${channelId}/hangup`);\n }\n\n // Continua no dialplan\n async continueDialplan(\n channelId: string,\n context?: string,\n extension?: string,\n priority?: number,\n label?: string,\n ): Promise<void> {\n return this.client.post<void>(`/channels/${channelId}/continue`, {\n context,\n extension,\n priority,\n label,\n });\n }\n\n // Move o canal para outra aplica\u00E7\u00E3o Stasis\n async moveToApplication(\n channelId: string,\n app: string,\n appArgs?: string,\n ): Promise<void> {\n return this.client.post<void>(`/channels/${channelId}/move`, {\n app,\n appArgs,\n });\n }\n}\n", "import WebSocket from \"ws\";\n\nexport class WebSocketClient {\n private ws: WebSocket | null = null;\n private isClosedManually = false; // Para evitar reconex\u00F5es autom\u00E1ticas quando fechado manualmente\n\n constructor(private url: string) {}\n\n async connect(retryInterval = 1000): Promise<void> {\n return new Promise((resolve, reject) => {\n this.ws = new WebSocket(this.url);\n\n this.ws.on(\"open\", () => {\n console.log(\"WebSocket conectado.\");\n this.isClosedManually = false;\n resolve();\n });\n\n this.ws.on(\"error\", (err) => {\n console.error(\"Erro na conex\u00E3o WebSocket:\", err);\n if (!this.isClosedManually) {\n setTimeout(() => this.reconnect(retryInterval), retryInterval);\n }\n reject(err);\n });\n\n this.ws.on(\"close\", (code, reason) => {\n console.warn(`WebSocket desconectado: ${code} - ${reason}`);\n if (!this.isClosedManually) {\n setTimeout(() => this.reconnect(retryInterval), retryInterval);\n }\n });\n });\n }\n\n private reconnect(retryInterval: number): void {\n console.log(\"Tentando reconectar ao WebSocket...\");\n this.connect(retryInterval).catch((err) => {\n console.error(\"Erro ao tentar reconectar:\", err);\n });\n }\n\n isConnected(): boolean {\n return this.ws?.readyState === WebSocket.OPEN;\n }\n\n on(event: string, callback: (data: any) => void): void {\n if (!this.ws || this.ws.readyState !== WebSocket.OPEN) {\n throw new Error(\"WebSocket n\u00E3o est\u00E1 conectado.\");\n }\n\n this.ws.on(event, callback);\n }\n\n send(data: any): void {\n if (!this.ws || this.ws.readyState !== WebSocket.OPEN) {\n throw new Error(\"WebSocket n\u00E3o est\u00E1 conectado.\");\n }\n\n this.ws.send(data, (err) => {\n if (err) {\n console.error(\"Erro ao enviar dados pelo WebSocket:\", err);\n }\n });\n }\n\n close(): void {\n if (this.ws) {\n this.isClosedManually = true;\n this.ws.close();\n console.log(\"WebSocket fechado manualmente.\");\n }\n }\n}\n", "import { BaseClient } from \"./baseClient.js\";\nimport { Channels } from \"./resources/channels\";\nimport { WebSocketClient } from \"./websocketClient.js\";\n\nexport interface AriClientConfig {\n host: string;\n port: number;\n username: string;\n password: string;\n secure?: boolean;\n}\n\nexport class AriClient {\n private wsClient: WebSocketClient;\n private readonly baseClient: BaseClient;\n\n public channels: Channels; // Adicionado\n\n /**\n * Initializes a new instance of the AriClient class.\n * \n * This constructor sets up the necessary configurations for connecting to an Asterisk ARI server,\n * including WebSocket and HTTP connections. It also initializes the channels resource.\n * \n * @param config - The configuration object for the ARI client.\n * @param config.host - The hostname or IP address of the Asterisk server.\n * @param config.port - The port number on which the Asterisk ARI is listening.\n * @param config.username - The username for authentication with the Asterisk ARI.\n * @param config.password - The password for authentication with the Asterisk ARI.\n * @param config.secure - Optional. If true, uses secure protocols (WSS/HTTPS). Defaults to false.\n */\n constructor(private config: AriClientConfig) {\n // Determina o protocolo com base na seguran\u00E7a\n const protocol = config.secure ? \"wss\" : \"ws\";\n const httpProtocol = config.secure ? \"https\" : \"http\";\n \n // Codifica credenciais para uso seguro no URL\n const encodedUsername = encodeURIComponent(config.username);\n const encodedPassword = encodeURIComponent(config.password);\n \n // Remove o protocolo do host, caso esteja presente\n const normalizedHost = config.host.replace(/^https?:\\/\\//, \"\");\n \n // Constr\u00F3i os URLs\n const wsUrl = `${protocol}://${encodedUsername}:${encodedPassword}@${normalizedHost}:${config.port}/ari/events`;\n const baseUrl = `${httpProtocol}://${normalizedHost}:${config.port}/ari`;\n \n console.log({ wsUrl, baseUrl });\n \n // Inicializa os clientes WebSocket e HTTP\n this.wsClient = new WebSocketClient(wsUrl);\n this.baseClient = new BaseClient(baseUrl, config.username, config.password);\n \n // Inicializa os recursos do cliente\n this.channels = new Channels(this.baseClient);\n }\n\n /**\n * Conecta ao WebSocket do ARI.\n * @returns {Promise<void>} Retorna uma promise resolvida ao conectar com sucesso.\n */\n async connectWebSocket(): Promise<void> {\n try {\n await this.wsClient.connect();\n console.log(\"WebSocket conectado com sucesso!\");\n } catch (err) {\n console.error(\"Erro ao conectar ao WebSocket:\", err);\n throw err;\n }\n }\n\n /**\n * Checks if the WebSocket connection is currently active.\n *\n * This method provides a way to determine the current state of the WebSocket connection\n * to the Asterisk ARI server.\n *\n * @returns {boolean} Returns true if the WebSocket is connected, false otherwise.\n */\n isWebSocketConnected(): boolean {\n return this.wsClient.isConnected();\n }\n\n /**\n * Registers a callback function for a specific WebSocket event.\n *\n * This method allows you to attach event listeners to WebSocket events\n * from the Asterisk ARI server. When the specified event occurs, the\n * provided callback function will be executed.\n *\n * @param event - The name of the WebSocket event to listen for.\n * @param callback - The function to be called when the event occurs.\n * It receives the event data as its parameter.\n * @returns void\n */\n onWebSocketEvent(event: string, callback: (data: any) => void): void {\n this.wsClient.on(event, callback);\n }\n\n /**\n * Closes the WebSocket connection to the Asterisk ARI server.\n *\n * This method terminates the existing WebSocket connection, if one is active.\n * It's useful for cleaning up resources when the connection is no longer needed.\n */\n closeWebSocket(): void {\n this.wsClient.close();\n }\n\n /**\n * Retrieves a list of all active channels from the Asterisk ARI server.\n *\n * This method makes an asynchronous GET request to the '/channels' endpoint\n * of the Asterisk ARI.\n *\n * @returns {Promise<any>} A promise that resolves with the list of active channels.\n * The exact structure of the returned data depends on the\n * Asterisk ARI specification.\n */\n async listChannels() {\n return this.baseClient.get(\"/channels\");\n }\n\n /**\n * Initiates a new channel on the Asterisk server.\n *\n * This method makes an asynchronous POST request to the '/channels' endpoint\n * of the Asterisk ARI to create a new channel with the specified parameters.\n *\n * @param {any} data - An object containing the parameters for the new channel.\n * The structure of this object should conform to the\n * Asterisk ARI specification for channel origination.\n * @returns {Promise<any>} A promise that resolves with the response from the\n * Asterisk server, typically containing details of\n * the newly created channel.\n */\n async originateChannel(data: any) {\n return this.baseClient.post(\"/channels\", data);\n }\n}\n"],
5
+ "mappings": ";AAAA,OAAO,WAAmC;AAEnC,IAAM,aAAN,MAAiB;AAAA,EACd;AAAA,EAER,YAAY,SAAiB,UAAkB,UAAkB;AAC/D,SAAK,SAAS,MAAM,OAAO;AAAA,MACzB,SAAS;AAAA,MACT,MAAM,EAAE,UAAU,SAAS;AAAA,IAC7B,CAAC;AAAA,EACH;AAAA,EAEA,MAAM,IAAO,MAA0B;AACrC,UAAM,WAAW,MAAM,KAAK,OAAO,IAAO,IAAI;AAC9C,WAAO,SAAS;AAAA,EAClB;AAAA,EAEA,MAAM,KAAQ,MAAc,MAA4B;AACtD,UAAM,WAAW,MAAM,KAAK,OAAO,KAAQ,MAAM,IAAI;AACrD,WAAO,SAAS;AAAA,EAClB;AACF;;;AClBO,IAAM,WAAN,MAAe;AAAA,EACpB,YAAoB,QAAoB;AAApB;AAAA,EAAqB;AAAA;AAAA,EAGzC,MAAM,OAA2B;AAC/B,WAAO,KAAK,OAAO,IAAe,WAAW;AAAA,EAC/C;AAAA;AAAA,EAGA,MAAM,UAAU,MAA0C;AACxD,WAAO,KAAK,OAAO,KAAc,aAAa,IAAI;AAAA,EACpD;AAAA;AAAA,EAGA,MAAM,WAAW,WAAqC;AACpD,WAAO,KAAK,OAAO,IAAa,aAAa,SAAS,EAAE;AAAA,EAC1D;AAAA;AAAA,EAGA,MAAM,OAAO,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6
6
  "names": []
7
7
  }
@@ -11,11 +11,78 @@ export declare class AriClient {
11
11
  private wsClient;
12
12
  private readonly baseClient;
13
13
  channels: Channels;
14
+ /**
15
+ * Initializes a new instance of the AriClient class.
16
+ *
17
+ * This constructor sets up the necessary configurations for connecting to an Asterisk ARI server,
18
+ * including WebSocket and HTTP connections. It also initializes the channels resource.
19
+ *
20
+ * @param config - The configuration object for the ARI client.
21
+ * @param config.host - The hostname or IP address of the Asterisk server.
22
+ * @param config.port - The port number on which the Asterisk ARI is listening.
23
+ * @param config.username - The username for authentication with the Asterisk ARI.
24
+ * @param config.password - The password for authentication with the Asterisk ARI.
25
+ * @param config.secure - Optional. If true, uses secure protocols (WSS/HTTPS). Defaults to false.
26
+ */
14
27
  constructor(config: AriClientConfig);
28
+ /**
29
+ * Conecta ao WebSocket do ARI.
30
+ * @returns {Promise<void>} Retorna uma promise resolvida ao conectar com sucesso.
31
+ */
15
32
  connectWebSocket(): Promise<void>;
33
+ /**
34
+ * Checks if the WebSocket connection is currently active.
35
+ *
36
+ * This method provides a way to determine the current state of the WebSocket connection
37
+ * to the Asterisk ARI server.
38
+ *
39
+ * @returns {boolean} Returns true if the WebSocket is connected, false otherwise.
40
+ */
41
+ isWebSocketConnected(): boolean;
42
+ /**
43
+ * Registers a callback function for a specific WebSocket event.
44
+ *
45
+ * This method allows you to attach event listeners to WebSocket events
46
+ * from the Asterisk ARI server. When the specified event occurs, the
47
+ * provided callback function will be executed.
48
+ *
49
+ * @param event - The name of the WebSocket event to listen for.
50
+ * @param callback - The function to be called when the event occurs.
51
+ * It receives the event data as its parameter.
52
+ * @returns void
53
+ */
16
54
  onWebSocketEvent(event: string, callback: (data: any) => void): void;
55
+ /**
56
+ * Closes the WebSocket connection to the Asterisk ARI server.
57
+ *
58
+ * This method terminates the existing WebSocket connection, if one is active.
59
+ * It's useful for cleaning up resources when the connection is no longer needed.
60
+ */
17
61
  closeWebSocket(): void;
62
+ /**
63
+ * Retrieves a list of all active channels from the Asterisk ARI server.
64
+ *
65
+ * This method makes an asynchronous GET request to the '/channels' endpoint
66
+ * of the Asterisk ARI.
67
+ *
68
+ * @returns {Promise<any>} A promise that resolves with the list of active channels.
69
+ * The exact structure of the returned data depends on the
70
+ * Asterisk ARI specification.
71
+ */
18
72
  listChannels(): Promise<unknown>;
73
+ /**
74
+ * Initiates a new channel on the Asterisk server.
75
+ *
76
+ * This method makes an asynchronous POST request to the '/channels' endpoint
77
+ * of the Asterisk ARI to create a new channel with the specified parameters.
78
+ *
79
+ * @param {any} data - An object containing the parameters for the new channel.
80
+ * The structure of this object should conform to the
81
+ * Asterisk ARI specification for channel origination.
82
+ * @returns {Promise<any>} A promise that resolves with the response from the
83
+ * Asterisk server, typically containing details of
84
+ * the newly created channel.
85
+ */
19
86
  originateChannel(data: any): Promise<unknown>;
20
87
  }
21
88
  //# sourceMappingURL=ariClient.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"ariClient.d.ts","sourceRoot":"","sources":["../../../src/ari-client/ariClient.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAGhD,MAAM,WAAW,eAAe;IAC9B,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,CAAC,EAAE,OAAO,CAAC;CAClB;AAED,qBAAa,SAAS;IAMR,OAAO,CAAC,MAAM;IAL1B,OAAO,CAAC,QAAQ,CAAkB;IAClC,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAa;IAEjC,QAAQ,EAAE,QAAQ,CAAC;gBAEN,MAAM,EAAE,eAAe;IAiBrC,gBAAgB,IAAI,OAAO,CAAC,IAAI,CAAC;IAIvC,gBAAgB,CAAC,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC,IAAI,EAAE,GAAG,KAAK,IAAI,GAAG,IAAI;IAIpE,cAAc,IAAI,IAAI;IAIhB,YAAY;IAIZ,gBAAgB,CAAC,IAAI,EAAE,GAAG;CAGjC"}
1
+ {"version":3,"file":"ariClient.d.ts","sourceRoot":"","sources":["../../../src/ari-client/ariClient.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAGhD,MAAM,WAAW,eAAe;IAC9B,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,CAAC,EAAE,OAAO,CAAC;CAClB;AAED,qBAAa,SAAS;IAmBR,OAAO,CAAC,MAAM;IAlB1B,OAAO,CAAC,QAAQ,CAAkB;IAClC,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAa;IAEjC,QAAQ,EAAE,QAAQ,CAAC;IAE1B;;;;;;;;;;;;OAYG;gBACiB,MAAM,EAAE,eAAe;IA0B3C;;;OAGG;IACG,gBAAgB,IAAI,OAAO,CAAC,IAAI,CAAC;IAUvC;;;;;;;OAOG;IACH,oBAAoB,IAAI,OAAO;IAI/B;;;;;;;;;;;OAWG;IACH,gBAAgB,CAAC,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC,IAAI,EAAE,GAAG,KAAK,IAAI,GAAG,IAAI;IAIpE;;;;;OAKG;IACH,cAAc,IAAI,IAAI;IAItB;;;;;;;;;OASG;IACG,YAAY;IAIlB;;;;;;;;;;;;OAYG;IACG,gBAAgB,CAAC,IAAI,EAAE,GAAG;CAGjC"}
@@ -1,9 +1,13 @@
1
1
  export declare class WebSocketClient {
2
2
  private url;
3
3
  private ws;
4
+ private isClosedManually;
4
5
  constructor(url: string);
5
- connect(): Promise<void>;
6
+ connect(retryInterval?: number): Promise<void>;
7
+ private reconnect;
8
+ isConnected(): boolean;
6
9
  on(event: string, callback: (data: any) => void): void;
10
+ send(data: any): void;
7
11
  close(): void;
8
12
  }
9
13
  //# sourceMappingURL=websocketClient.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"websocketClient.d.ts","sourceRoot":"","sources":["../../../src/ari-client/websocketClient.ts"],"names":[],"mappings":"AAEA,qBAAa,eAAe;IAGd,OAAO,CAAC,GAAG;IAFvB,OAAO,CAAC,EAAE,CAA0B;gBAEhB,GAAG,EAAE,MAAM;IAEzB,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAQ9B,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC,IAAI,EAAE,GAAG,KAAK,IAAI,GAAG,IAAI;IAKtD,KAAK,IAAI,IAAI;CAGd"}
1
+ {"version":3,"file":"websocketClient.d.ts","sourceRoot":"","sources":["../../../src/ari-client/websocketClient.ts"],"names":[],"mappings":"AAEA,qBAAa,eAAe;IAId,OAAO,CAAC,GAAG;IAHvB,OAAO,CAAC,EAAE,CAA0B;IACpC,OAAO,CAAC,gBAAgB,CAAS;gBAEb,GAAG,EAAE,MAAM;IAEzB,OAAO,CAAC,aAAa,SAAO,GAAG,OAAO,CAAC,IAAI,CAAC;IA2BlD,OAAO,CAAC,SAAS;IAOjB,WAAW,IAAI,OAAO;IAItB,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC,IAAI,EAAE,GAAG,KAAK,IAAI,GAAG,IAAI;IAQtD,IAAI,CAAC,IAAI,EAAE,GAAG,GAAG,IAAI;IAYrB,KAAK,IAAI,IAAI;CAOd"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ipcom/asterisk-ari",
3
- "version": "0.0.3",
3
+ "version": "0.0.5",
4
4
  "type": "module",
5
5
  "description": "JavaScript client for Asterisk REST Interface.",
6
6
  "homepage": "https://github.com/fabiotheo/ipcom-asterisk-ari",