@gymspace/evolution 1.0.0 → 1.0.2

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.
Files changed (100) hide show
  1. package/dist/index.d.mts +26 -19
  2. package/dist/index.d.ts +26 -19
  3. package/dist/index.js +173 -62
  4. package/dist/index.js.map +1 -1
  5. package/dist/index.mjs +173 -62
  6. package/dist/index.mjs.map +1 -1
  7. package/package.json +1 -3
  8. package/src/api/errors.ts +0 -220
  9. package/src/api/routes.ts +0 -76
  10. package/src/api/service.ts +0 -141
  11. package/src/index.ts +0 -89
  12. package/src/modules/chats/index.ts +0 -253
  13. package/src/modules/chats/schemas/archive.ts +0 -15
  14. package/src/modules/chats/schemas/check.ts +0 -20
  15. package/src/modules/chats/schemas/delete-message.ts +0 -16
  16. package/src/modules/chats/schemas/fetch-profile-picture.ts +0 -13
  17. package/src/modules/chats/schemas/find-all.ts +0 -49
  18. package/src/modules/chats/schemas/find-contacts.ts +0 -17
  19. package/src/modules/chats/schemas/find-messages.ts +0 -55
  20. package/src/modules/chats/schemas/find-status-message.ts +0 -11
  21. package/src/modules/chats/schemas/get-base64-from-media-message.ts +0 -20
  22. package/src/modules/chats/schemas/index.ts +0 -33
  23. package/src/modules/chats/schemas/mark-as-read.ts +0 -20
  24. package/src/modules/chats/schemas/mark-as-unread.ts +0 -17
  25. package/src/modules/chats/schemas/presence.ts +0 -41
  26. package/src/modules/chats/schemas/update-message.ts +0 -16
  27. package/src/modules/groups/index.ts +0 -246
  28. package/src/modules/groups/schemas/accept-invite-code.ts +0 -14
  29. package/src/modules/groups/schemas/common.ts +0 -89
  30. package/src/modules/groups/schemas/create.ts +0 -15
  31. package/src/modules/groups/schemas/fetch-invite-code.ts +0 -13
  32. package/src/modules/groups/schemas/find-all.ts +0 -26
  33. package/src/modules/groups/schemas/find-by-invite-code.ts +0 -39
  34. package/src/modules/groups/schemas/find-by-jid.ts +0 -14
  35. package/src/modules/groups/schemas/find-members.ts +0 -16
  36. package/src/modules/groups/schemas/index.ts +0 -24
  37. package/src/modules/groups/schemas/leave.ts +0 -14
  38. package/src/modules/groups/schemas/revoke-invite-code.ts +0 -14
  39. package/src/modules/groups/schemas/send-group-invite.ts +0 -15
  40. package/src/modules/groups/schemas/toggle-ephemeral.ts +0 -15
  41. package/src/modules/groups/schemas/update-description.ts +0 -15
  42. package/src/modules/groups/schemas/update-members.ts +0 -18
  43. package/src/modules/groups/schemas/update-picture.ts +0 -15
  44. package/src/modules/groups/schemas/update-setting.ts +0 -16
  45. package/src/modules/groups/schemas/update-subject.ts +0 -15
  46. package/src/modules/index.ts +0 -7
  47. package/src/modules/instance/index.ts +0 -126
  48. package/src/modules/instance/schemas/connect.ts +0 -14
  49. package/src/modules/instance/schemas/connection-state.ts +0 -16
  50. package/src/modules/instance/schemas/create.ts +0 -99
  51. package/src/modules/instance/schemas/delete.ts +0 -21
  52. package/src/modules/instance/schemas/fetch-all.ts +0 -57
  53. package/src/modules/instance/schemas/index.ts +0 -8
  54. package/src/modules/instance/schemas/logout.ts +0 -22
  55. package/src/modules/instance/schemas/restart.ts +0 -13
  56. package/src/modules/instance/schemas/set-presence.ts +0 -20
  57. package/src/modules/messages/index.ts +0 -246
  58. package/src/modules/messages/schemas/audio.ts +0 -12
  59. package/src/modules/messages/schemas/base.ts +0 -11
  60. package/src/modules/messages/schemas/contact.ts +0 -70
  61. package/src/modules/messages/schemas/document.ts +0 -12
  62. package/src/modules/messages/schemas/image.ts +0 -12
  63. package/src/modules/messages/schemas/index.ts +0 -31
  64. package/src/modules/messages/schemas/list.ts +0 -36
  65. package/src/modules/messages/schemas/location.ts +0 -76
  66. package/src/modules/messages/schemas/media.ts +0 -51
  67. package/src/modules/messages/schemas/poll.ts +0 -87
  68. package/src/modules/messages/schemas/reaction.ts +0 -21
  69. package/src/modules/messages/schemas/status.ts +0 -18
  70. package/src/modules/messages/schemas/sticker.ts +0 -77
  71. package/src/modules/messages/schemas/template.ts +0 -39
  72. package/src/modules/messages/schemas/text.ts +0 -88
  73. package/src/modules/messages/schemas/video.ts +0 -12
  74. package/src/modules/messages/schemas/voice.ts +0 -94
  75. package/src/modules/profile/index.ts +0 -110
  76. package/src/modules/profile/schemas/fetch-business-profile.ts +0 -29
  77. package/src/modules/profile/schemas/fetch-privacy-settings.ts +0 -11
  78. package/src/modules/profile/schemas/fetch-profile.ts +0 -23
  79. package/src/modules/profile/schemas/index.ts +0 -8
  80. package/src/modules/profile/schemas/remove-picture.ts +0 -4
  81. package/src/modules/profile/schemas/update-name.ts +0 -11
  82. package/src/modules/profile/schemas/update-picture.ts +0 -11
  83. package/src/modules/profile/schemas/update-privacy-settings.ts +0 -18
  84. package/src/modules/profile/schemas/update-status.ts +0 -11
  85. package/src/modules/settings/index.ts +0 -28
  86. package/src/modules/settings/schemas/find.ts +0 -11
  87. package/src/modules/settings/schemas/index.ts +0 -2
  88. package/src/modules/settings/schemas/set.ts +0 -18
  89. package/src/modules/webhook/index.ts +0 -28
  90. package/src/modules/webhook/schemas/find.ts +0 -7
  91. package/src/modules/webhook/schemas/index.ts +0 -2
  92. package/src/modules/webhook/schemas/set.ts +0 -16
  93. package/src/schemas/client.ts +0 -23
  94. package/src/schemas/common.ts +0 -44
  95. package/src/types/api.ts +0 -17
  96. package/src/types/events.ts +0 -53
  97. package/src/types/messages.ts +0 -5
  98. package/src/types/tags.ts +0 -14
  99. package/src/types/webhooks.ts +0 -255
  100. package/src/utils/phone-numer-from-jid.ts +0 -9
package/dist/index.mjs CHANGED
@@ -1,5 +1,3 @@
1
- import { isValidPhoneNumber, parsePhoneNumber } from 'libphonenumber-js';
2
-
3
1
  // src/types/webhooks.ts
4
2
  var ConnectionState = /* @__PURE__ */ ((ConnectionState2) => {
5
3
  ConnectionState2["OPEN"] = "open";
@@ -64,7 +62,7 @@ var EvolutionApiError = class _EvolutionApiError extends Error {
64
62
  relevantDetails.push(`Server Message: ${response.message}`);
65
63
  }
66
64
  }
67
- if (relevantDetails.length > 0) {
65
+ if (relevantDetails?.length > 0) {
68
66
  result += `
69
67
  ${relevantDetails.join("\n ")}`;
70
68
  }
@@ -92,54 +90,59 @@ function extractErrorMessage(response) {
92
90
  return response;
93
91
  }
94
92
  if (typeof response === "object" && response !== null) {
95
- const errorObj = response;
96
- const messagePaths = [
97
- // Evolution API specific nested structure (most common)
98
- errorObj.response?.response?.message?.[0],
99
- errorObj.response?.response?.error,
100
- errorObj.response?.response?.description,
101
- // Evolution API first level nested
102
- Array.isArray(errorObj.response?.message) ? errorObj.response.message[0] : null,
103
- errorObj.response?.error,
104
- errorObj.response?.description,
105
- // Direct error message
106
- Array.isArray(errorObj.message) ? errorObj.message[0] : errorObj.message,
107
- errorObj.error,
108
- errorObj.description,
109
- errorObj.detail,
110
- // Other nested error messages
111
- errorObj.data?.error,
112
- errorObj.data?.message,
113
- // Array format messages (fallback)
114
- Array.isArray(errorObj.error) ? errorObj.error[0] : null,
115
- Array.isArray(errorObj.errors) ? errorObj.errors[0] : null
116
- ];
117
- for (const path of messagePaths) {
118
- if (typeof path === "string" && path.trim()) {
119
- return path.trim();
93
+ try {
94
+ const errorObj = response;
95
+ const messagePaths = [
96
+ // Evolution API specific nested structure (most common)
97
+ errorObj.response?.response?.message?.[0],
98
+ errorObj.response?.response?.error,
99
+ errorObj.response?.response?.description,
100
+ // Evolution API first level nested
101
+ Array.isArray(errorObj.response?.message) ? errorObj.response.message[0] : null,
102
+ errorObj.response?.error,
103
+ errorObj.response?.description,
104
+ // Direct error message
105
+ Array.isArray(errorObj.message) ? errorObj.message[0] : errorObj.message,
106
+ errorObj.error,
107
+ errorObj.description,
108
+ errorObj.detail,
109
+ // Other nested error messages
110
+ errorObj.data?.error,
111
+ errorObj.data?.message,
112
+ // Array format messages (fallback)
113
+ Array.isArray(errorObj.error) ? errorObj.error[0] : null,
114
+ Array.isArray(errorObj.errors) ? errorObj.errors[0] : null
115
+ ].filter(Boolean);
116
+ for (const path of messagePaths) {
117
+ if (path && typeof path === "string" && path.length > 0 && path.trim()) {
118
+ return path.trim();
119
+ }
120
+ if (path && typeof path === "object" && path !== null) {
121
+ const nestedMessage = extractErrorMessage(path);
122
+ if (nestedMessage) {
123
+ return nestedMessage;
124
+ }
125
+ }
120
126
  }
121
- if (typeof path === "object" && path !== null) {
122
- const nestedMessage = extractErrorMessage(path);
123
- if (nestedMessage) {
124
- return nestedMessage;
127
+ if (errorObj.validation && Array.isArray(errorObj.validation)) {
128
+ const validationErrors = errorObj.validation.map((v) => v?.message || v?.error || String(v)).filter(Boolean).join(", ");
129
+ if (validationErrors) {
130
+ return `Validation error: ${validationErrors}`;
125
131
  }
126
132
  }
127
- }
128
- if (errorObj.validation && Array.isArray(errorObj.validation)) {
129
- const validationErrors = errorObj.validation.map((v) => v.message || v.error || String(v)).filter(Boolean).join(", ");
130
- if (validationErrors) {
131
- return `Validation error: ${validationErrors}`;
133
+ if (errorObj.statusCode && errorObj.statusText) {
134
+ return `${errorObj.statusCode}: ${errorObj.statusText}`;
132
135
  }
133
- }
134
- if (errorObj.statusCode && errorObj.statusText) {
135
- return `${errorObj.statusCode}: ${errorObj.statusText}`;
136
- }
137
- if (Object.keys(errorObj).length > 0) {
138
- try {
139
- return JSON.stringify(errorObj);
140
- } catch {
141
- return "[Complex error object]";
136
+ const keys = Object.keys(errorObj);
137
+ if (keys && keys.length > 0) {
138
+ try {
139
+ return JSON.stringify(errorObj);
140
+ } catch {
141
+ return "[Complex error object]";
142
+ }
142
143
  }
144
+ } catch (error) {
145
+ return "[Error parsing error message]";
143
146
  }
144
147
  }
145
148
  return null;
@@ -154,6 +157,18 @@ var ApiService = class {
154
157
  this.options.instance = instance;
155
158
  }
156
159
  async request(path, options = {}) {
160
+ if (!path || typeof path !== "string" || path?.trim()?.length === 0) {
161
+ throw new EvolutionApiError("Invalid path parameter", {
162
+ message: "Path must be a non-empty string",
163
+ path
164
+ });
165
+ }
166
+ if (!this.options.serverUrl || typeof this.options.serverUrl !== "string") {
167
+ throw new EvolutionApiError("Invalid server URL configuration", {
168
+ message: "Server URL must be configured and be a valid string",
169
+ serverUrl: this.options.serverUrl
170
+ });
171
+ }
157
172
  const { isInstanceUrl = true } = options;
158
173
  let instance = this.options.instance;
159
174
  if (options.instance) {
@@ -162,7 +177,9 @@ var ApiService = class {
162
177
  }
163
178
  if (isInstanceUrl && !instance) {
164
179
  throw new EvolutionApiError("Instance not set", {
165
- message: "Please set the instance before making a request or pass instance in the method options."
180
+ message: "Please set the instance before making a request or pass instance in the method options.",
181
+ path,
182
+ isInstanceUrl
166
183
  });
167
184
  }
168
185
  const { init, params } = this.makeInit(options);
@@ -171,24 +188,65 @@ var ApiService = class {
171
188
  if (params.toString()) {
172
189
  url.search = params.toString();
173
190
  }
191
+ const curlCommand = this.generateCurlCommand(
192
+ init.method || "GET",
193
+ url.toString(),
194
+ init.headers,
195
+ init.body
196
+ );
197
+ if (this.options.debug) {
198
+ console.log("\n=== CURL COMMAND ===");
199
+ console.log(curlCommand);
200
+ console.log("===================\n");
201
+ }
174
202
  let response;
175
203
  let data;
204
+ let responseText = "";
176
205
  try {
177
206
  response = await fetch(url, init);
178
- data = await response.json();
207
+ responseText = await response.text();
208
+ data = responseText ? JSON.parse(responseText) : null;
179
209
  } catch (error) {
180
- throw new EvolutionApiError("Network or parsing error", error);
210
+ if (this.options.debug) {
211
+ console.log(error);
212
+ console.log("\n=== CURL COMMAND (ERROR) ===");
213
+ console.log(curlCommand);
214
+ console.log("============================\n");
215
+ }
216
+ if (error instanceof SyntaxError && error.message.includes("JSON")) {
217
+ throw new EvolutionApiError("Failed to parse response as JSON", {
218
+ originalError: error,
219
+ responseText: responseText || "Empty response",
220
+ url: url.toString(),
221
+ status: response?.status
222
+ });
223
+ }
224
+ throw new EvolutionApiError("Network or parsing error", {
225
+ originalError: error,
226
+ url: url.toString(),
227
+ responseText: responseText || "No response"
228
+ });
229
+ }
230
+ if (this.options.debug) {
231
+ console.log("\n=== API RESPONSE ===");
232
+ console.log(JSON.stringify(data, null, 2));
233
+ console.log("===================\n");
181
234
  }
182
235
  if (!response.ok) {
236
+ if (this.options.debug) {
237
+ console.log("\n=== CURL COMMAND (HTTP ERROR) ===");
238
+ console.log(curlCommand);
239
+ console.log("================================\n");
240
+ }
183
241
  const errorMessage = extractErrorMessage(data) || `Request failed with status ${response.status}: ${response.statusText}`;
184
242
  throw new EvolutionApiError(
185
243
  errorMessage,
186
244
  {
187
245
  message: errorMessage,
188
- response: JSON.stringify(data),
246
+ response: data || {},
189
247
  url: url.toString(),
190
248
  params: params.toString(),
191
- body: JSON.stringify(options.body)
249
+ body: options.body || {}
192
250
  },
193
251
  response.status
194
252
  );
@@ -219,6 +277,35 @@ var ApiService = class {
219
277
  }
220
278
  return { init, params };
221
279
  }
280
+ generateCurlCommand(method, url, headers, body) {
281
+ const curlParts = ["curl"];
282
+ if (method !== "GET") {
283
+ curlParts.push(`-X ${method}`);
284
+ }
285
+ let headerEntries;
286
+ if (headers instanceof Headers) {
287
+ const entries = [];
288
+ headers.forEach((value, key) => {
289
+ entries.push([key, value]);
290
+ });
291
+ headerEntries = entries;
292
+ } else {
293
+ headerEntries = Object.entries(headers);
294
+ }
295
+ headerEntries.forEach(([key, value]) => {
296
+ if (key.toLowerCase() !== "apikey") {
297
+ curlParts.push(`-H '${key}: ${value}'`);
298
+ } else {
299
+ curlParts.push(`-H '${key}: [REDACTED]'`);
300
+ }
301
+ });
302
+ if (body) {
303
+ const bodyStr = typeof body === "string" ? body : JSON.stringify(body);
304
+ curlParts.push(`-d '${bodyStr}'`);
305
+ }
306
+ curlParts.push(`'${url}'`);
307
+ return curlParts.join(" \\\n ");
308
+ }
222
309
  async get(path, options = {}) {
223
310
  return this.request(path, { ...options, method: "GET" });
224
311
  }
@@ -312,10 +399,43 @@ var Routes = {
312
399
  SetPresence: "instance/setPresence"
313
400
  }
314
401
  };
315
- var validatePhoneNumber = (value) => isValidPhoneNumber(value);
402
+
403
+ // src/schemas/common.ts
404
+ var validatePhoneNumber = (value) => {
405
+ const phoneRegex = /^\+\d{7,15}$/;
406
+ return phoneRegex.test(value);
407
+ };
316
408
  var validateJid = (value) => value.endsWith("@s.whatsapp.net");
317
409
  var validateGroupJid = (value) => value.endsWith("@g.us");
318
410
 
411
+ // src/types/tags.ts
412
+ var Jid = (jid) => jid;
413
+ var GroupJid = (jid) => jid;
414
+ var MessageId = (id) => id;
415
+ var ChatId = (id) => id;
416
+
417
+ // src/utils/phone-numer-from-jid.ts
418
+ function phoneNumberFromJid(jid) {
419
+ const numberPart = jid.split("@")[0];
420
+ if (jid.endsWith("@s.whatsapp.net")) {
421
+ return `+${numberPart}`;
422
+ }
423
+ return numberPart;
424
+ }
425
+
426
+ // src/modules/chats/schemas/find-all.ts
427
+ var FindAllChatsResponseTransform = (chats) => chats.map((chat) => ({
428
+ id: ChatId(chat.id),
429
+ jid: chat.remoteJid.endsWith("@g.us") ? GroupJid(chat.remoteJid) : Jid(chat.remoteJid),
430
+ phoneNumber: phoneNumberFromJid(chat.remoteJid),
431
+ name: chat.name || void 0,
432
+ labels: chat.labels || void 0,
433
+ createdAt: new Date(chat.createdAt),
434
+ updatedAt: new Date(chat.updatedAt),
435
+ pushName: chat.pushName || void 0,
436
+ pictureUrl: chat.profilePicUrl || void 0
437
+ }));
438
+
319
439
  // src/modules/chats/index.ts
320
440
  var ChatsModule = class {
321
441
  constructor(api) {
@@ -345,7 +465,7 @@ var ChatsModule = class {
345
465
  body: {},
346
466
  ...methodOptions
347
467
  });
348
- return response;
468
+ return FindAllChatsResponseTransform(response);
349
469
  }
350
470
  /**
351
471
  * Updates presence status
@@ -975,9 +1095,6 @@ var WebhookModule = class {
975
1095
  return response;
976
1096
  }
977
1097
  };
978
- function phoneNumberFromJid(jid) {
979
- return parsePhoneNumber(`+${jid.split("@")[0]}`).number;
980
- }
981
1098
 
982
1099
  // src/types/messages.ts
983
1100
  var MessageUpdateStatus = /* @__PURE__ */ ((MessageUpdateStatus2) => {
@@ -987,12 +1104,6 @@ var MessageUpdateStatus = /* @__PURE__ */ ((MessageUpdateStatus2) => {
987
1104
  return MessageUpdateStatus2;
988
1105
  })(MessageUpdateStatus || {});
989
1106
 
990
- // src/types/tags.ts
991
- var Jid = (jid) => jid;
992
- var GroupJid = (jid) => jid;
993
- var MessageId = (id) => id;
994
- var ChatId = (id) => id;
995
-
996
1107
  // src/types/events.ts
997
1108
  var WebhookEvent = /* @__PURE__ */ ((WebhookEvent2) => {
998
1109
  WebhookEvent2["APPLICATION_STARTUP"] = "application.startup";