@gymspace/evolution 1.0.0

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 (101) hide show
  1. package/README.md +1273 -0
  2. package/dist/index.d.mts +1790 -0
  3. package/dist/index.d.ts +1790 -0
  4. package/dist/index.js +1091 -0
  5. package/dist/index.js.map +1 -0
  6. package/dist/index.mjs +1077 -0
  7. package/dist/index.mjs.map +1 -0
  8. package/package.json +63 -0
  9. package/src/api/errors.ts +220 -0
  10. package/src/api/routes.ts +76 -0
  11. package/src/api/service.ts +141 -0
  12. package/src/index.ts +89 -0
  13. package/src/modules/chats/index.ts +253 -0
  14. package/src/modules/chats/schemas/archive.ts +15 -0
  15. package/src/modules/chats/schemas/check.ts +20 -0
  16. package/src/modules/chats/schemas/delete-message.ts +16 -0
  17. package/src/modules/chats/schemas/fetch-profile-picture.ts +13 -0
  18. package/src/modules/chats/schemas/find-all.ts +49 -0
  19. package/src/modules/chats/schemas/find-contacts.ts +17 -0
  20. package/src/modules/chats/schemas/find-messages.ts +55 -0
  21. package/src/modules/chats/schemas/find-status-message.ts +11 -0
  22. package/src/modules/chats/schemas/get-base64-from-media-message.ts +20 -0
  23. package/src/modules/chats/schemas/index.ts +33 -0
  24. package/src/modules/chats/schemas/mark-as-read.ts +20 -0
  25. package/src/modules/chats/schemas/mark-as-unread.ts +17 -0
  26. package/src/modules/chats/schemas/presence.ts +41 -0
  27. package/src/modules/chats/schemas/update-message.ts +16 -0
  28. package/src/modules/groups/index.ts +246 -0
  29. package/src/modules/groups/schemas/accept-invite-code.ts +14 -0
  30. package/src/modules/groups/schemas/common.ts +89 -0
  31. package/src/modules/groups/schemas/create.ts +15 -0
  32. package/src/modules/groups/schemas/fetch-invite-code.ts +13 -0
  33. package/src/modules/groups/schemas/find-all.ts +26 -0
  34. package/src/modules/groups/schemas/find-by-invite-code.ts +39 -0
  35. package/src/modules/groups/schemas/find-by-jid.ts +14 -0
  36. package/src/modules/groups/schemas/find-members.ts +16 -0
  37. package/src/modules/groups/schemas/index.ts +24 -0
  38. package/src/modules/groups/schemas/leave.ts +14 -0
  39. package/src/modules/groups/schemas/revoke-invite-code.ts +14 -0
  40. package/src/modules/groups/schemas/send-group-invite.ts +15 -0
  41. package/src/modules/groups/schemas/toggle-ephemeral.ts +15 -0
  42. package/src/modules/groups/schemas/update-description.ts +15 -0
  43. package/src/modules/groups/schemas/update-members.ts +18 -0
  44. package/src/modules/groups/schemas/update-picture.ts +15 -0
  45. package/src/modules/groups/schemas/update-setting.ts +16 -0
  46. package/src/modules/groups/schemas/update-subject.ts +15 -0
  47. package/src/modules/index.ts +7 -0
  48. package/src/modules/instance/index.ts +126 -0
  49. package/src/modules/instance/schemas/connect.ts +14 -0
  50. package/src/modules/instance/schemas/connection-state.ts +16 -0
  51. package/src/modules/instance/schemas/create.ts +99 -0
  52. package/src/modules/instance/schemas/delete.ts +21 -0
  53. package/src/modules/instance/schemas/fetch-all.ts +57 -0
  54. package/src/modules/instance/schemas/index.ts +8 -0
  55. package/src/modules/instance/schemas/logout.ts +22 -0
  56. package/src/modules/instance/schemas/restart.ts +13 -0
  57. package/src/modules/instance/schemas/set-presence.ts +20 -0
  58. package/src/modules/messages/index.ts +246 -0
  59. package/src/modules/messages/schemas/audio.ts +12 -0
  60. package/src/modules/messages/schemas/base.ts +11 -0
  61. package/src/modules/messages/schemas/contact.ts +70 -0
  62. package/src/modules/messages/schemas/document.ts +12 -0
  63. package/src/modules/messages/schemas/image.ts +12 -0
  64. package/src/modules/messages/schemas/index.ts +31 -0
  65. package/src/modules/messages/schemas/list.ts +36 -0
  66. package/src/modules/messages/schemas/location.ts +76 -0
  67. package/src/modules/messages/schemas/media.ts +51 -0
  68. package/src/modules/messages/schemas/poll.ts +87 -0
  69. package/src/modules/messages/schemas/reaction.ts +21 -0
  70. package/src/modules/messages/schemas/status.ts +18 -0
  71. package/src/modules/messages/schemas/sticker.ts +77 -0
  72. package/src/modules/messages/schemas/template.ts +39 -0
  73. package/src/modules/messages/schemas/text.ts +88 -0
  74. package/src/modules/messages/schemas/video.ts +12 -0
  75. package/src/modules/messages/schemas/voice.ts +94 -0
  76. package/src/modules/profile/index.ts +110 -0
  77. package/src/modules/profile/schemas/fetch-business-profile.ts +29 -0
  78. package/src/modules/profile/schemas/fetch-privacy-settings.ts +11 -0
  79. package/src/modules/profile/schemas/fetch-profile.ts +23 -0
  80. package/src/modules/profile/schemas/index.ts +8 -0
  81. package/src/modules/profile/schemas/remove-picture.ts +4 -0
  82. package/src/modules/profile/schemas/update-name.ts +11 -0
  83. package/src/modules/profile/schemas/update-picture.ts +11 -0
  84. package/src/modules/profile/schemas/update-privacy-settings.ts +18 -0
  85. package/src/modules/profile/schemas/update-status.ts +11 -0
  86. package/src/modules/settings/index.ts +28 -0
  87. package/src/modules/settings/schemas/find.ts +11 -0
  88. package/src/modules/settings/schemas/index.ts +2 -0
  89. package/src/modules/settings/schemas/set.ts +18 -0
  90. package/src/modules/webhook/index.ts +28 -0
  91. package/src/modules/webhook/schemas/find.ts +7 -0
  92. package/src/modules/webhook/schemas/index.ts +2 -0
  93. package/src/modules/webhook/schemas/set.ts +16 -0
  94. package/src/schemas/client.ts +23 -0
  95. package/src/schemas/common.ts +44 -0
  96. package/src/types/api.ts +17 -0
  97. package/src/types/events.ts +53 -0
  98. package/src/types/messages.ts +5 -0
  99. package/src/types/tags.ts +14 -0
  100. package/src/types/webhooks.ts +255 -0
  101. package/src/utils/phone-numer-from-jid.ts +9 -0
package/dist/index.mjs ADDED
@@ -0,0 +1,1077 @@
1
+ import { isValidPhoneNumber, parsePhoneNumber } from 'libphonenumber-js';
2
+
3
+ // src/types/webhooks.ts
4
+ var ConnectionState = /* @__PURE__ */ ((ConnectionState2) => {
5
+ ConnectionState2["OPEN"] = "open";
6
+ ConnectionState2["CONNECTING"] = "connecting";
7
+ ConnectionState2["CLOSED"] = "close";
8
+ ConnectionState2["REFUSED"] = "refused";
9
+ return ConnectionState2;
10
+ })(ConnectionState || {});
11
+ var MessageType = /* @__PURE__ */ ((MessageType2) => {
12
+ MessageType2["CONVERSATION"] = "conversation";
13
+ MessageType2["TEXT"] = "textMessage";
14
+ MessageType2["EPHEMERAL"] = "ephemeralMessage";
15
+ MessageType2["AUDIO"] = "audioMessage";
16
+ MessageType2["IMAGE"] = "imageMessage";
17
+ MessageType2["VIDEO"] = "videoMessage";
18
+ MessageType2["DOCUMENT"] = "documentMessage";
19
+ MessageType2["STICKER"] = "stickerMessage";
20
+ MessageType2["CONTACT"] = "contactMessage";
21
+ MessageType2["LOCATION"] = "locationMessage";
22
+ MessageType2["REACTION"] = "reactionMessage";
23
+ MessageType2["BUTTONS"] = "buttonsMessage";
24
+ return MessageType2;
25
+ })(MessageType || {});
26
+
27
+ // src/api/errors.ts
28
+ var EvolutionApiError = class _EvolutionApiError extends Error {
29
+ constructor(message, cause, statusCode) {
30
+ const extractedMessage = extractErrorMessage(cause);
31
+ const finalMessage = extractedMessage || message || "Unknown error occurred";
32
+ super(finalMessage);
33
+ this.name = _EvolutionApiError.name;
34
+ this.message = finalMessage;
35
+ this.statusCode = statusCode;
36
+ this.details = cause;
37
+ if (Error.captureStackTrace) {
38
+ Error.captureStackTrace(this, _EvolutionApiError);
39
+ }
40
+ }
41
+ /**
42
+ * Returns a user-friendly string representation of the error
43
+ */
44
+ toString() {
45
+ let result = `${this.name}: ${this.message}`;
46
+ if (this.statusCode) {
47
+ result += ` (${this.statusCode})`;
48
+ }
49
+ if (this.details && typeof this.details === "object") {
50
+ const details = this.details;
51
+ const relevantDetails = [];
52
+ if (details.url) {
53
+ relevantDetails.push(`URL: ${details.url}`);
54
+ }
55
+ if (details.method) {
56
+ relevantDetails.push(`Method: ${details.method}`);
57
+ }
58
+ if (details.response && typeof details.response === "object") {
59
+ const response = details.response;
60
+ if (response.error && response.error !== this.message) {
61
+ relevantDetails.push(`Server Error: ${response.error}`);
62
+ }
63
+ if (response.message && response.message !== this.message) {
64
+ relevantDetails.push(`Server Message: ${response.message}`);
65
+ }
66
+ }
67
+ if (relevantDetails.length > 0) {
68
+ result += `
69
+ ${relevantDetails.join("\n ")}`;
70
+ }
71
+ }
72
+ return result;
73
+ }
74
+ /**
75
+ * Returns a JSON representation suitable for logging
76
+ */
77
+ toJSON() {
78
+ return {
79
+ name: this.name,
80
+ message: this.message,
81
+ statusCode: this.statusCode,
82
+ details: this.details,
83
+ stack: this.stack
84
+ };
85
+ }
86
+ };
87
+ function extractErrorMessage(response) {
88
+ if (!response) {
89
+ return null;
90
+ }
91
+ if (typeof response === "string") {
92
+ return response;
93
+ }
94
+ 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();
120
+ }
121
+ if (typeof path === "object" && path !== null) {
122
+ const nestedMessage = extractErrorMessage(path);
123
+ if (nestedMessage) {
124
+ return nestedMessage;
125
+ }
126
+ }
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}`;
132
+ }
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]";
142
+ }
143
+ }
144
+ }
145
+ return null;
146
+ }
147
+
148
+ // src/api/service.ts
149
+ var ApiService = class {
150
+ constructor(options) {
151
+ this.options = options;
152
+ }
153
+ setInstance(instance) {
154
+ this.options.instance = instance;
155
+ }
156
+ async request(path, options = {}) {
157
+ const { isInstanceUrl = true } = options;
158
+ let instance = this.options.instance;
159
+ if (options.instance) {
160
+ instance = options.instance;
161
+ delete options.instance;
162
+ }
163
+ if (isInstanceUrl && !instance) {
164
+ throw new EvolutionApiError("Instance not set", {
165
+ message: "Please set the instance before making a request or pass instance in the method options."
166
+ });
167
+ }
168
+ const { init, params } = this.makeInit(options);
169
+ const urlPath = isInstanceUrl ? `/${path}/${instance}/` : `/${path}/`;
170
+ const url = new URL(urlPath, this.options.serverUrl);
171
+ if (params.toString()) {
172
+ url.search = params.toString();
173
+ }
174
+ let response;
175
+ let data;
176
+ try {
177
+ response = await fetch(url, init);
178
+ data = await response.json();
179
+ } catch (error) {
180
+ throw new EvolutionApiError("Network or parsing error", error);
181
+ }
182
+ if (!response.ok) {
183
+ const errorMessage = extractErrorMessage(data) || `Request failed with status ${response.status}: ${response.statusText}`;
184
+ throw new EvolutionApiError(
185
+ errorMessage,
186
+ {
187
+ message: errorMessage,
188
+ response: JSON.stringify(data),
189
+ url: url.toString(),
190
+ params: params.toString(),
191
+ body: JSON.stringify(options.body)
192
+ },
193
+ response.status
194
+ );
195
+ }
196
+ return data;
197
+ }
198
+ makeInit(options) {
199
+ const init = {
200
+ method: options.method || "GET",
201
+ headers: {
202
+ "Content-Type": "application/json",
203
+ apikey: this.options.token,
204
+ // Evolution API uses apikey header
205
+ ...this.options.headers,
206
+ ...options.headers
207
+ }
208
+ };
209
+ if (options.body) {
210
+ init.body = JSON.stringify(options.body);
211
+ }
212
+ const params = new URLSearchParams();
213
+ if (options.params) {
214
+ Object.entries(options.params).forEach(([key, value]) => {
215
+ if (value !== void 0 && value !== null) {
216
+ params.append(key, String(value));
217
+ }
218
+ });
219
+ }
220
+ return { init, params };
221
+ }
222
+ async get(path, options = {}) {
223
+ return this.request(path, { ...options, method: "GET" });
224
+ }
225
+ async post(path, options = {}) {
226
+ return this.request(path, { ...options, method: "POST" });
227
+ }
228
+ async put(path, options = {}) {
229
+ return this.request(path, { ...options, method: "PUT" });
230
+ }
231
+ async patch(path, options = {}) {
232
+ return this.request(path, { ...options, method: "PATCH" });
233
+ }
234
+ async delete(path, options = {}) {
235
+ return this.request(path, { ...options, method: "DELETE" });
236
+ }
237
+ };
238
+
239
+ // src/api/routes.ts
240
+ var Routes = {
241
+ Message: {
242
+ SendText: "message/sendText",
243
+ SendMedia: "message/sendMedia",
244
+ SendVoice: "message/sendWhatsAppAudio",
245
+ SendSticker: "message/sendSticker",
246
+ SendLocation: "message/sendLocation",
247
+ SendContact: "message/sendContact",
248
+ SendReaction: "message/sendReaction",
249
+ SendTemplate: "message/sendTemplate",
250
+ SendStatus: "message/sendStatus",
251
+ SendList: "message/sendList"
252
+ },
253
+ Chats: {
254
+ Check: "chat/whatsappNumbers",
255
+ FindAll: "chat/findChats",
256
+ SendPresence: "chat/sendPresence",
257
+ MarkAsRead: "chat/markMessageAsRead",
258
+ MarkAsUnread: "chat/markChatUnread",
259
+ Archive: "chat/archive",
260
+ DeleteMessage: "chat/deleteMessageForEveryone",
261
+ FetchProfilePicture: "chat/fetchProfilePictureUrl",
262
+ FindContacts: "chat/findContacts",
263
+ FindMessages: "chat/findMessages",
264
+ FindStatusMessage: "chat/findStatusMessage",
265
+ UpdateMessage: "chat/updateMessage",
266
+ GetBase64FromMediaMessage: "chat/getBase64FromMediaMessage"
267
+ },
268
+ Groups: {
269
+ FindAll: "group/fetchAllGroups",
270
+ FindByJid: "group/findGroupInfos",
271
+ FindByInviteCode: "group/inviteInfo",
272
+ Create: "group/create",
273
+ UpdatePicture: "group/updateGroupPicture",
274
+ UpdateSubject: "group/updateGroupSubject",
275
+ UpdateDescription: "group/updateGroupDescription",
276
+ FetchInviteCode: "group/fetchInviteCode",
277
+ AcceptInviteCode: "group/acceptInviteCode",
278
+ RevokeInviteCode: "group/revokeInviteCode",
279
+ SendGroupInvite: "group/sendGroupInvite",
280
+ FindMembers: "group/findGroupMembers",
281
+ UpdateMembers: "group/updateGroupMembers",
282
+ UpdateSetting: "group/updateGroupSetting",
283
+ ToggleEphemeral: "group/toggleEphemeral",
284
+ Leave: "group/leaveGroup"
285
+ },
286
+ Profile: {
287
+ FetchBusinessProfile: "chat/fetchBusinessProfile",
288
+ FetchProfile: "chat/fetchProfile",
289
+ UpdateName: "chat/updateProfileName",
290
+ UpdateStatus: "chat/updateProfileStatus",
291
+ UpdatePicture: "chat/updateProfilePicture",
292
+ RemovePicture: "chat/removeProfilePicture",
293
+ FetchPrivacySettings: "chat/fetchPrivacySettings",
294
+ UpdatePrivacySettings: "chat/updatePrivacySettings"
295
+ },
296
+ Webhook: {
297
+ Set: "webhook/set",
298
+ Find: "webhook/find"
299
+ },
300
+ Settings: {
301
+ Set: "settings/set",
302
+ Find: "settings/find"
303
+ },
304
+ Instance: {
305
+ Create: "instance/create",
306
+ FetchAll: "instance/fetchInstances",
307
+ Connect: "instance/connect",
308
+ Restart: "instance/restart",
309
+ ConnectionState: "instance/connectionState",
310
+ Logout: "instance/logout",
311
+ Delete: "instance/delete",
312
+ SetPresence: "instance/setPresence"
313
+ }
314
+ };
315
+ var validatePhoneNumber = (value) => isValidPhoneNumber(value);
316
+ var validateJid = (value) => value.endsWith("@s.whatsapp.net");
317
+ var validateGroupJid = (value) => value.endsWith("@g.us");
318
+
319
+ // src/modules/chats/index.ts
320
+ var ChatsModule = class {
321
+ constructor(api) {
322
+ this.api = api;
323
+ }
324
+ /**
325
+ * Checks if phone numbers are registered on WhatsApp
326
+ * @param numbers - Array of phone numbers to check
327
+ * @param methodOptions - Method-specific options (instance override)
328
+ */
329
+ async check(numbers, methodOptions) {
330
+ const body = {
331
+ numbers: Array.isArray(numbers) ? numbers : [numbers]
332
+ };
333
+ const response = await this.api.post(Routes.Chats.Check, {
334
+ body,
335
+ ...methodOptions
336
+ });
337
+ return response;
338
+ }
339
+ /**
340
+ * Gets all chats
341
+ * @param methodOptions - Method-specific options (instance override)
342
+ */
343
+ async findAll(methodOptions) {
344
+ const response = await this.api.post(Routes.Chats.FindAll, {
345
+ body: {},
346
+ ...methodOptions
347
+ });
348
+ return response;
349
+ }
350
+ /**
351
+ * Updates presence status
352
+ * @param params - Presence parameters
353
+ * @param methodOptions - Method-specific options (instance override)
354
+ */
355
+ async updatePresence(options, methodOptions) {
356
+ if (!options.number) {
357
+ throw new Error("Number is required");
358
+ }
359
+ if (!validateJid(options.number) && !validateGroupJid(options.number)) {
360
+ options.number = `${options.number}@s.whatsapp.net`;
361
+ }
362
+ await this.api.post(Routes.Chats.SendPresence, {
363
+ body: options,
364
+ ...methodOptions
365
+ });
366
+ }
367
+ /**
368
+ * Marks messages as read
369
+ * @param options - Mark as read options
370
+ * @param methodOptions - Method-specific options (instance override)
371
+ */
372
+ async markAsRead(options, methodOptions) {
373
+ const response = await this.api.post(Routes.Chats.MarkAsRead, {
374
+ body: options,
375
+ ...methodOptions
376
+ });
377
+ return response;
378
+ }
379
+ /**
380
+ * Marks messages as unread
381
+ * @param options - Mark as unread options
382
+ * @param methodOptions - Method-specific options (instance override)
383
+ */
384
+ async markAsUnread(options, methodOptions) {
385
+ const response = await this.api.post(Routes.Chats.MarkAsUnread, {
386
+ body: options,
387
+ ...methodOptions
388
+ });
389
+ return response;
390
+ }
391
+ /**
392
+ * Archives a chat
393
+ * @param options - Archive options
394
+ * @param methodOptions - Method-specific options (instance override)
395
+ */
396
+ async archive(options, methodOptions) {
397
+ const response = await this.api.post(Routes.Chats.Archive, {
398
+ body: options,
399
+ ...methodOptions
400
+ });
401
+ return response;
402
+ }
403
+ /**
404
+ * Deletes a message
405
+ * @param options - Delete message options
406
+ * @param methodOptions - Method-specific options (instance override)
407
+ */
408
+ async deleteMessage(options, methodOptions) {
409
+ const response = await this.api.delete(Routes.Chats.DeleteMessage, {
410
+ body: options,
411
+ ...methodOptions
412
+ });
413
+ return response;
414
+ }
415
+ /**
416
+ * Fetches profile picture
417
+ * @param options - Fetch profile picture options
418
+ * @param methodOptions - Method-specific options (instance override)
419
+ */
420
+ async fetchProfilePicture(options, methodOptions) {
421
+ const response = await this.api.post(Routes.Chats.FetchProfilePicture, {
422
+ body: options,
423
+ ...methodOptions
424
+ });
425
+ return response;
426
+ }
427
+ /**
428
+ * Finds contacts
429
+ * @param options - Find contacts options
430
+ * @param methodOptions - Method-specific options (instance override)
431
+ */
432
+ async findContacts(options, methodOptions) {
433
+ const response = await this.api.post(Routes.Chats.FindContacts, {
434
+ body: options,
435
+ ...methodOptions
436
+ });
437
+ return response;
438
+ }
439
+ /**
440
+ * Finds messages
441
+ * @param options - Find messages options
442
+ * @param methodOptions - Method-specific options (instance override)
443
+ */
444
+ async findMessages(options, methodOptions) {
445
+ const response = await this.api.post(Routes.Chats.FindMessages, {
446
+ body: options,
447
+ ...methodOptions
448
+ });
449
+ return response;
450
+ }
451
+ /**
452
+ * Finds status messages
453
+ * @param options - Find status message options
454
+ * @param methodOptions - Method-specific options (instance override)
455
+ */
456
+ async findStatusMessage(options, methodOptions) {
457
+ const response = await this.api.post(Routes.Chats.FindStatusMessage, {
458
+ body: options,
459
+ ...methodOptions
460
+ });
461
+ return response;
462
+ }
463
+ /**
464
+ * Updates a message
465
+ * @param options - Update message options
466
+ * @param methodOptions - Method-specific options (instance override)
467
+ */
468
+ async updateMessage(options, methodOptions) {
469
+ const response = await this.api.put(Routes.Chats.UpdateMessage, {
470
+ body: options,
471
+ ...methodOptions
472
+ });
473
+ return response;
474
+ }
475
+ /**
476
+ * Gets base64 representation of media from a message
477
+ * @param options - Get base64 from media message options
478
+ * @param methodOptions - Method-specific options (instance override)
479
+ */
480
+ async getBase64FromMediaMessage(options, methodOptions) {
481
+ const response = await this.api.post(
482
+ Routes.Chats.GetBase64FromMediaMessage,
483
+ {
484
+ body: options,
485
+ ...methodOptions
486
+ }
487
+ );
488
+ return response;
489
+ }
490
+ };
491
+
492
+ // src/modules/groups/index.ts
493
+ var GroupsModule = class {
494
+ constructor(api) {
495
+ this.api = api;
496
+ }
497
+ async findAll(getParticipants = false, methodOptions) {
498
+ const response = await this.api.get(Routes.Groups.FindAll, {
499
+ params: { getParticipants },
500
+ ...methodOptions
501
+ });
502
+ if (getParticipants) {
503
+ return response;
504
+ }
505
+ return response;
506
+ }
507
+ /**
508
+ * Gets a group by invite code
509
+ * @param inviteCode - The group invite code (not the URL)
510
+ * @param methodOptions - Method-specific options (instance override)
511
+ */
512
+ async findByInviteCode(inviteCode, methodOptions) {
513
+ const response = await this.api.get(Routes.Groups.FindByInviteCode, {
514
+ params: { inviteCode },
515
+ ...methodOptions
516
+ });
517
+ return response;
518
+ }
519
+ /**
520
+ * Gets a group by JID
521
+ * @param groupJid - The group JID terminated with \@g.us
522
+ * @param methodOptions - Method-specific options (instance override)
523
+ */
524
+ async findByJid(groupJid, methodOptions) {
525
+ const response = await this.api.get(Routes.Groups.FindByJid, {
526
+ params: { groupJid },
527
+ ...methodOptions
528
+ });
529
+ return response;
530
+ }
531
+ async create(options, methodOptions) {
532
+ const response = await this.api.post(Routes.Groups.Create, {
533
+ body: options,
534
+ ...methodOptions
535
+ });
536
+ return response;
537
+ }
538
+ async updatePicture(options, methodOptions) {
539
+ const response = await this.api.post(Routes.Groups.UpdatePicture, {
540
+ body: options,
541
+ ...methodOptions
542
+ });
543
+ return response;
544
+ }
545
+ async updateSubject(options, methodOptions) {
546
+ const response = await this.api.post(Routes.Groups.UpdateSubject, {
547
+ body: options,
548
+ ...methodOptions
549
+ });
550
+ return response;
551
+ }
552
+ async updateDescription(options, methodOptions) {
553
+ const response = await this.api.post(Routes.Groups.UpdateDescription, {
554
+ body: options,
555
+ ...methodOptions
556
+ });
557
+ return response;
558
+ }
559
+ async fetchInviteCode(options, methodOptions) {
560
+ const response = await this.api.get(Routes.Groups.FetchInviteCode, {
561
+ params: options,
562
+ ...methodOptions
563
+ });
564
+ return response;
565
+ }
566
+ async acceptInviteCode(options, methodOptions) {
567
+ const response = await this.api.post(Routes.Groups.AcceptInviteCode, {
568
+ body: options,
569
+ ...methodOptions
570
+ });
571
+ return response;
572
+ }
573
+ async revokeInviteCode(options, methodOptions) {
574
+ const response = await this.api.post(Routes.Groups.RevokeInviteCode, {
575
+ body: options,
576
+ ...methodOptions
577
+ });
578
+ return response;
579
+ }
580
+ async sendGroupInvite(options, methodOptions) {
581
+ const response = await this.api.post(Routes.Groups.SendGroupInvite, {
582
+ body: options,
583
+ ...methodOptions
584
+ });
585
+ return response;
586
+ }
587
+ async findMembers(options, methodOptions) {
588
+ const response = await this.api.get(Routes.Groups.FindMembers, {
589
+ params: options,
590
+ ...methodOptions
591
+ });
592
+ return response;
593
+ }
594
+ async updateMembers(options, methodOptions) {
595
+ const response = await this.api.post(Routes.Groups.UpdateMembers, {
596
+ body: options,
597
+ ...methodOptions
598
+ });
599
+ return response;
600
+ }
601
+ async updateSetting(options, methodOptions) {
602
+ const response = await this.api.post(Routes.Groups.UpdateSetting, {
603
+ body: options,
604
+ ...methodOptions
605
+ });
606
+ return response;
607
+ }
608
+ async toggleEphemeral(options, methodOptions) {
609
+ const response = await this.api.post(Routes.Groups.ToggleEphemeral, {
610
+ body: options,
611
+ ...methodOptions
612
+ });
613
+ return response;
614
+ }
615
+ async leave(options, methodOptions) {
616
+ const response = await this.api.post(Routes.Groups.Leave, {
617
+ body: options,
618
+ ...methodOptions
619
+ });
620
+ return response;
621
+ }
622
+ };
623
+
624
+ // src/modules/instance/index.ts
625
+ var InstanceModule = class {
626
+ constructor(api) {
627
+ this.api = api;
628
+ }
629
+ async create(options) {
630
+ const response = await this.api.post(Routes.Instance.Create, {
631
+ body: options,
632
+ isInstanceUrl: false
633
+ });
634
+ return response;
635
+ }
636
+ async connect(options, methodOptions) {
637
+ const { instanceName } = options;
638
+ const instance = methodOptions?.instance ?? instanceName;
639
+ if (!instance) {
640
+ throw new Error("Instance name is required");
641
+ }
642
+ const response = await this.api.get(Routes.Instance.Connect, {
643
+ instance,
644
+ isInstanceUrl: true
645
+ });
646
+ return response;
647
+ }
648
+ async connectionState(options, methodOptions) {
649
+ const { instanceName } = options;
650
+ const instance = methodOptions?.instance ?? instanceName;
651
+ if (!instance) {
652
+ throw new Error("Instance name is required");
653
+ }
654
+ const response = await this.api.get(Routes.Instance.ConnectionState, {
655
+ instance,
656
+ isInstanceUrl: true
657
+ });
658
+ return response;
659
+ }
660
+ async logout(options, methodOptions) {
661
+ const { instanceName } = options;
662
+ const instance = methodOptions?.instance ?? instanceName;
663
+ if (!instance) {
664
+ throw new Error("Instance name is required");
665
+ }
666
+ const response = await this.api.delete(Routes.Instance.Logout, {
667
+ instance,
668
+ isInstanceUrl: true
669
+ });
670
+ return response;
671
+ }
672
+ async delete(options) {
673
+ const response = await this.api.delete(Routes.Instance.Delete, {
674
+ instance: options.instanceName,
675
+ isInstanceUrl: true
676
+ });
677
+ return response;
678
+ }
679
+ async restart(options, methodOptions) {
680
+ const { instanceName } = options;
681
+ const instance = methodOptions?.instance ?? instanceName;
682
+ if (!instance) {
683
+ throw new Error("Instance name is required");
684
+ }
685
+ const response = await this.api.post(Routes.Instance.Restart, {
686
+ instance,
687
+ isInstanceUrl: true
688
+ });
689
+ return response;
690
+ }
691
+ async fetchAll(options) {
692
+ const response = await this.api.get(Routes.Instance.FetchAll, {
693
+ params: options,
694
+ isInstanceUrl: false
695
+ });
696
+ return response;
697
+ }
698
+ async setPresence(options, methodOptions) {
699
+ const { instanceName, ...rest } = options;
700
+ const instance = methodOptions?.instance ?? instanceName;
701
+ if (!instance) {
702
+ throw new Error("Instance name is required");
703
+ }
704
+ const response = await this.api.post(Routes.Instance.SetPresence, {
705
+ body: rest,
706
+ instance,
707
+ isInstanceUrl: true
708
+ });
709
+ return response;
710
+ }
711
+ };
712
+
713
+ // src/modules/messages/index.ts
714
+ var MessagesModule = class {
715
+ constructor(api) {
716
+ this.api = api;
717
+ }
718
+ /**
719
+ * Sends a text message
720
+ * @param options - Text message options
721
+ * @param methodOptions - Method-specific options (instance override)
722
+ */
723
+ async sendText(options, methodOptions) {
724
+ const response = await this.api.post(Routes.Message.SendText, {
725
+ body: options,
726
+ ...methodOptions
727
+ });
728
+ return response;
729
+ }
730
+ /**
731
+ * Sends an image
732
+ * @param options - Image message options
733
+ * @param methodOptions - Method-specific options (instance override)
734
+ */
735
+ async sendImage(options, methodOptions) {
736
+ options.mediatype = "image";
737
+ const response = await this.api.post(Routes.Message.SendMedia, {
738
+ body: options,
739
+ ...methodOptions
740
+ });
741
+ return response;
742
+ }
743
+ /**
744
+ * Sends a video
745
+ * @param options - Video message options
746
+ * @param methodOptions - Method-specific options (instance override)
747
+ */
748
+ async sendVideo(options, methodOptions) {
749
+ options.mediatype = "video";
750
+ const response = await this.api.post(Routes.Message.SendMedia, {
751
+ body: options,
752
+ ...methodOptions
753
+ });
754
+ return response;
755
+ }
756
+ /**
757
+ * Sends a document
758
+ * @param options - Document message options
759
+ * @param methodOptions - Method-specific options (instance override)
760
+ */
761
+ async sendDocument(options, methodOptions) {
762
+ options.mediatype = "document";
763
+ const response = await this.api.post(Routes.Message.SendMedia, {
764
+ body: options,
765
+ ...methodOptions
766
+ });
767
+ return response;
768
+ }
769
+ /**
770
+ * Sends an audio
771
+ * @param options - Audio message options
772
+ * @param methodOptions - Method-specific options (instance override)
773
+ */
774
+ async sendAudio(options, methodOptions) {
775
+ options.mediatype = "audio";
776
+ const response = await this.api.post(Routes.Message.SendMedia, {
777
+ body: options,
778
+ ...methodOptions
779
+ });
780
+ return response;
781
+ }
782
+ /**
783
+ * Sends a voice message
784
+ * @param options - Voice message options
785
+ * @param methodOptions - Method-specific options (instance override)
786
+ */
787
+ async sendVoice(options, methodOptions) {
788
+ const response = await this.api.post(Routes.Message.SendVoice, {
789
+ body: options,
790
+ ...methodOptions
791
+ });
792
+ return response;
793
+ }
794
+ /**
795
+ * Sends a sticker
796
+ * @param options - Sticker message options
797
+ * @param methodOptions - Method-specific options (instance override)
798
+ */
799
+ async sendSticker(options, methodOptions) {
800
+ const response = await this.api.post(Routes.Message.SendSticker, {
801
+ body: options,
802
+ ...methodOptions
803
+ });
804
+ return response;
805
+ }
806
+ /**
807
+ * Sends a location
808
+ * @param options - Location message options
809
+ * @param methodOptions - Method-specific options (instance override)
810
+ */
811
+ async sendLocation(options, methodOptions) {
812
+ const response = await this.api.post(Routes.Message.SendLocation, {
813
+ body: options,
814
+ ...methodOptions
815
+ });
816
+ return response;
817
+ }
818
+ /**
819
+ * Sends a contact
820
+ * @param options - Contact message options
821
+ * @param methodOptions - Method-specific options (instance override)
822
+ */
823
+ async sendContact(options, methodOptions) {
824
+ const response = await this.api.post(Routes.Message.SendContact, {
825
+ body: options,
826
+ ...methodOptions
827
+ });
828
+ return response;
829
+ }
830
+ /**
831
+ * Sends a reaction
832
+ * @param options - Reaction message options
833
+ * @param methodOptions - Method-specific options (instance override)
834
+ */
835
+ async sendReaction(options, methodOptions) {
836
+ const response = await this.api.post(Routes.Message.SendReaction, {
837
+ body: options,
838
+ ...methodOptions
839
+ });
840
+ return response;
841
+ }
842
+ /**
843
+ * Sends a template
844
+ * @param options - Template message options
845
+ * @param methodOptions - Method-specific options (instance override)
846
+ */
847
+ async sendTemplate(options, methodOptions) {
848
+ const response = await this.api.post(Routes.Message.SendTemplate, {
849
+ body: options,
850
+ ...methodOptions
851
+ });
852
+ return response;
853
+ }
854
+ /**
855
+ * Sends a status
856
+ * @param options - Status message options
857
+ * @param methodOptions - Method-specific options (instance override)
858
+ */
859
+ async sendStatus(options, methodOptions) {
860
+ const response = await this.api.post(Routes.Message.SendStatus, {
861
+ body: options,
862
+ ...methodOptions
863
+ });
864
+ return response;
865
+ }
866
+ /**
867
+ * Sends a list
868
+ * @param options - List message options
869
+ * @param methodOptions - Method-specific options (instance override)
870
+ */
871
+ async sendList(options, methodOptions) {
872
+ const response = await this.api.post(Routes.Message.SendList, {
873
+ body: options,
874
+ ...methodOptions
875
+ });
876
+ return response;
877
+ }
878
+ };
879
+
880
+ // src/modules/profile/index.ts
881
+ var ProfileModule = class {
882
+ constructor(api) {
883
+ this.api = api;
884
+ }
885
+ async fetchBusinessProfile(options, methodOptions) {
886
+ const response = await this.api.post(Routes.Profile.FetchBusinessProfile, {
887
+ body: options,
888
+ ...methodOptions
889
+ });
890
+ return response;
891
+ }
892
+ async fetchProfile(options, methodOptions) {
893
+ const response = await this.api.post(Routes.Profile.FetchProfile, {
894
+ body: options,
895
+ ...methodOptions
896
+ });
897
+ return response;
898
+ }
899
+ async updateName(options, methodOptions) {
900
+ const response = await this.api.post(Routes.Profile.UpdateName, {
901
+ body: options,
902
+ ...methodOptions
903
+ });
904
+ return response;
905
+ }
906
+ async updateStatus(options, methodOptions) {
907
+ const response = await this.api.post(Routes.Profile.UpdateStatus, {
908
+ body: options,
909
+ ...methodOptions
910
+ });
911
+ return response;
912
+ }
913
+ async updatePicture(options, methodOptions) {
914
+ const response = await this.api.post(Routes.Profile.UpdatePicture, {
915
+ body: options,
916
+ ...methodOptions
917
+ });
918
+ return response;
919
+ }
920
+ async removePicture(methodOptions) {
921
+ const response = await this.api.delete(
922
+ Routes.Profile.RemovePicture,
923
+ methodOptions
924
+ );
925
+ return response;
926
+ }
927
+ async fetchPrivacySettings(methodOptions) {
928
+ const response = await this.api.get(
929
+ Routes.Profile.FetchPrivacySettings,
930
+ methodOptions
931
+ );
932
+ return response;
933
+ }
934
+ async updatePrivacySettings(options, methodOptions) {
935
+ const response = await this.api.put(Routes.Profile.UpdatePrivacySettings, {
936
+ body: options,
937
+ ...methodOptions
938
+ });
939
+ return response;
940
+ }
941
+ };
942
+
943
+ // src/modules/settings/index.ts
944
+ var SettingsModule = class {
945
+ constructor(api) {
946
+ this.api = api;
947
+ }
948
+ async set(options, methodOptions) {
949
+ const response = await this.api.post(Routes.Settings.Set, {
950
+ body: options,
951
+ ...methodOptions
952
+ });
953
+ return response;
954
+ }
955
+ async find(methodOptions) {
956
+ const response = await this.api.get(Routes.Settings.Find, methodOptions);
957
+ return response;
958
+ }
959
+ };
960
+
961
+ // src/modules/webhook/index.ts
962
+ var WebhookModule = class {
963
+ constructor(api) {
964
+ this.api = api;
965
+ }
966
+ async set(options, methodOptions) {
967
+ const response = await this.api.post(Routes.Webhook.Set, {
968
+ body: options,
969
+ ...methodOptions
970
+ });
971
+ return response;
972
+ }
973
+ async find(methodOptions) {
974
+ const response = await this.api.get(Routes.Webhook.Find, methodOptions);
975
+ return response;
976
+ }
977
+ };
978
+ function phoneNumberFromJid(jid) {
979
+ return parsePhoneNumber(`+${jid.split("@")[0]}`).number;
980
+ }
981
+
982
+ // src/types/messages.ts
983
+ var MessageUpdateStatus = /* @__PURE__ */ ((MessageUpdateStatus2) => {
984
+ MessageUpdateStatus2["DELIVERY_ACK"] = "DELIVERY_ACK";
985
+ MessageUpdateStatus2["READ"] = "READ";
986
+ MessageUpdateStatus2["SERVER_ACK"] = "SERVER_ACK";
987
+ return MessageUpdateStatus2;
988
+ })(MessageUpdateStatus || {});
989
+
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
+ // src/types/events.ts
997
+ var WebhookEvent = /* @__PURE__ */ ((WebhookEvent2) => {
998
+ WebhookEvent2["APPLICATION_STARTUP"] = "application.startup";
999
+ WebhookEvent2["QRCODE_UPDATED"] = "qrcode.updated";
1000
+ WebhookEvent2["CONNECTION_UPDATE"] = "connection.update";
1001
+ WebhookEvent2["MESSAGES_SET"] = "messages.set";
1002
+ WebhookEvent2["MESSAGES_UPSERT"] = "messages.upsert";
1003
+ WebhookEvent2["MESSAGES_UPDATE"] = "messages.update";
1004
+ WebhookEvent2["MESSAGES_DELETE"] = "messages.delete";
1005
+ WebhookEvent2["SEND_MESSAGE"] = "send.message";
1006
+ WebhookEvent2["CONTACTS_SET"] = "contacts.set";
1007
+ WebhookEvent2["CONTACTS_UPSERT"] = "contacts.upsert";
1008
+ WebhookEvent2["CONTACTS_UPDATE"] = "contacts.update";
1009
+ WebhookEvent2["PRESENCE_UPDATE"] = "presence.update";
1010
+ WebhookEvent2["CHATS_SET"] = "chats.set";
1011
+ WebhookEvent2["CHATS_UPDATE"] = "chats.update";
1012
+ WebhookEvent2["CHATS_UPSERT"] = "chats.upsert";
1013
+ WebhookEvent2["CHATS_DELETE"] = "chats.delete";
1014
+ WebhookEvent2["GROUPS_UPSERT"] = "groups.upsert";
1015
+ WebhookEvent2["GROUPS_UPDATE"] = "groups.update";
1016
+ WebhookEvent2["GROUP_PARTICIPANTS_UPDATE"] = "group.participants.update";
1017
+ WebhookEvent2["NEW_TOKEN"] = "new.jwt";
1018
+ return WebhookEvent2;
1019
+ })(WebhookEvent || {});
1020
+ var WebhookEventSetup = /* @__PURE__ */ ((WebhookEventSetup2) => {
1021
+ WebhookEventSetup2["APPLICATION_STARTUP"] = "APPLICATION_STARTUP";
1022
+ WebhookEventSetup2["QRCODE_UPDATED"] = "QRCODE_UPDATED";
1023
+ WebhookEventSetup2["CONNECTION_UPDATE"] = "CONNECTION_UPDATE";
1024
+ WebhookEventSetup2["MESSAGES_SET"] = "MESSAGES_SET";
1025
+ WebhookEventSetup2["MESSAGES_UPSERT"] = "MESSAGES_UPSERT";
1026
+ WebhookEventSetup2["MESSAGES_UPDATE"] = "MESSAGES_UPDATE";
1027
+ WebhookEventSetup2["MESSAGES_DELETE"] = "MESSAGES_DELETE";
1028
+ WebhookEventSetup2["SEND_MESSAGE"] = "SEND_MESSAGE";
1029
+ WebhookEventSetup2["CONTACTS_SET"] = "CONTACTS_SET";
1030
+ WebhookEventSetup2["CONTACTS_UPSERT"] = "CONTACTS_UPSERT";
1031
+ WebhookEventSetup2["CONTACTS_UPDATE"] = "CONTACTS_UPDATE";
1032
+ WebhookEventSetup2["PRESENCE_UPDATE"] = "PRESENCE_UPDATE";
1033
+ WebhookEventSetup2["CHATS_SET"] = "CHATS_SET";
1034
+ WebhookEventSetup2["CHATS_UPDATE"] = "CHATS_UPDATE";
1035
+ WebhookEventSetup2["CHATS_UPSERT"] = "CHATS_UPSERT";
1036
+ WebhookEventSetup2["CHATS_DELETE"] = "CHATS_DELETE";
1037
+ WebhookEventSetup2["GROUPS_UPSERT"] = "GROUPS_UPSERT";
1038
+ WebhookEventSetup2["GROUPS_UPDATE"] = "GROUPS_UPDATE";
1039
+ WebhookEventSetup2["GROUP_PARTICIPANTS_UPDATE"] = "GROUP_PARTICIPANTS_UPDATE";
1040
+ WebhookEventSetup2["NEW_TOKEN"] = "NEW_TOKEN";
1041
+ WebhookEventSetup2["MESSAGES_EDITED"] = "MESSAGES_EDITED";
1042
+ WebhookEventSetup2["LABELS_EDIT"] = "LABELS_EDIT";
1043
+ WebhookEventSetup2["LABELS_ASSOCIATION"] = "LABELS_ASSOCIATION";
1044
+ WebhookEventSetup2["CALL"] = "CALL";
1045
+ WebhookEventSetup2["TYPEBOT_START"] = "TYPEBOT_START";
1046
+ WebhookEventSetup2["TYPEBOT_CHANGE_STATUS"] = "TYPEBOT_CHANGE_STATUS";
1047
+ WebhookEventSetup2["ERRORS"] = "ERRORS";
1048
+ WebhookEventSetup2["ERRORS_WEBHOOK"] = "ERRORS_WEBHOOK";
1049
+ return WebhookEventSetup2;
1050
+ })(WebhookEventSetup || {});
1051
+
1052
+ // src/index.ts
1053
+ var EvolutionClient = class {
1054
+ /**
1055
+ * Evolution Client - API client for interacting with the Evolution API
1056
+ * @param options - Client options
1057
+ */
1058
+ constructor(options) {
1059
+ this.options = options;
1060
+ this.api = new ApiService(options);
1061
+ this.chats = new ChatsModule(this.api);
1062
+ this.groups = new GroupsModule(this.api);
1063
+ this.messages = new MessagesModule(this.api);
1064
+ this.instances = new InstanceModule(this.api);
1065
+ this.profile = new ProfileModule(this.api);
1066
+ this.webhook = new WebhookModule(this.api);
1067
+ this.settings = new SettingsModule(this.api);
1068
+ }
1069
+ setInstance(instance) {
1070
+ this.options.instance = instance;
1071
+ this.api.setInstance(instance);
1072
+ }
1073
+ };
1074
+
1075
+ export { ChatId, ConnectionState, EvolutionApiError, EvolutionClient, GroupJid, Jid, MessageId, MessageType, MessageUpdateStatus, WebhookEvent, WebhookEventSetup, phoneNumberFromJid, validatePhoneNumber };
1076
+ //# sourceMappingURL=index.mjs.map
1077
+ //# sourceMappingURL=index.mjs.map