wuzapi 1.3.1 → 1.4.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.
package/dist/index.js CHANGED
@@ -17,8 +17,7 @@ class BaseClient {
17
17
  this.axios = axios.create({
18
18
  baseURL: config.apiUrl,
19
19
  headers: {
20
- "Content-Type": "application/json",
21
- Authorization: config.token
20
+ "Content-Type": "application/json"
22
21
  }
23
22
  });
24
23
  this.axios.interceptors.response.use(
@@ -39,11 +38,29 @@ class BaseClient {
39
38
  }
40
39
  );
41
40
  }
42
- async request(method, endpoint, data) {
41
+ /**
42
+ * Resolve the token from request options or instance config
43
+ * Throws an error if no token is available
44
+ */
45
+ resolveToken(options) {
46
+ const token = options?.token || this.config.token;
47
+ if (!token) {
48
+ throw new WuzapiError(
49
+ 401,
50
+ "No authentication token provided. Either set a token in the client config or provide one in the request options."
51
+ );
52
+ }
53
+ return token;
54
+ }
55
+ async request(method, endpoint, data, options) {
56
+ const token = this.resolveToken(options);
43
57
  const response = await this.axios.request({
44
58
  method,
45
59
  url: endpoint,
46
- data
60
+ data,
61
+ headers: {
62
+ Authorization: token
63
+ }
47
64
  });
48
65
  if (!response.data.success) {
49
66
  throw new WuzapiError(
@@ -54,296 +71,348 @@ class BaseClient {
54
71
  }
55
72
  return response.data.data;
56
73
  }
57
- async get(endpoint) {
58
- return this.request("GET", endpoint);
74
+ async get(endpoint, options) {
75
+ return this.request("GET", endpoint, void 0, options);
59
76
  }
60
- async post(endpoint, data) {
61
- return this.request("POST", endpoint, data);
77
+ async post(endpoint, data, options) {
78
+ return this.request("POST", endpoint, data, options);
62
79
  }
63
- async delete(endpoint) {
64
- return this.request("DELETE", endpoint);
80
+ async delete(endpoint, options) {
81
+ return this.request("DELETE", endpoint, void 0, options);
65
82
  }
66
83
  }
67
84
  class AdminModule extends BaseClient {
68
85
  /**
69
86
  * List all users
70
87
  */
71
- async listUsers() {
72
- return this.get("/admin/users");
88
+ async listUsers(options) {
89
+ return this.get("/admin/users", options);
73
90
  }
74
91
  /**
75
92
  * Add a new user
76
93
  */
77
- async addUser(user) {
78
- return this.post("/admin/users", user);
94
+ async addUser(user, options) {
95
+ return this.post("/admin/users", user, options);
79
96
  }
80
97
  /**
81
98
  * Delete a user by ID
82
99
  */
83
- async deleteUser(id) {
84
- return this.delete(`/admin/users/${id}`);
100
+ async deleteUser(id, options) {
101
+ return this.delete(`/admin/users/${id}`, options);
85
102
  }
86
103
  }
87
104
  class SessionModule extends BaseClient {
88
105
  /**
89
106
  * Connect to WhatsApp servers
90
107
  */
91
- async connect(options) {
92
- return this.post("/session/connect", options);
108
+ async connect(request, options) {
109
+ return this.post("/session/connect", request, options);
93
110
  }
94
111
  /**
95
112
  * Disconnect from WhatsApp servers
96
113
  */
97
- async disconnect() {
98
- return this.post("/session/disconnect");
114
+ async disconnect(options) {
115
+ return this.post(
116
+ "/session/disconnect",
117
+ void 0,
118
+ options
119
+ );
99
120
  }
100
121
  /**
101
122
  * Logout and finish the session
102
123
  */
103
- async logout() {
104
- return this.post("/session/logout");
124
+ async logout(options) {
125
+ return this.post("/session/logout", void 0, options);
105
126
  }
106
127
  /**
107
128
  * Get session status
108
129
  */
109
- async getStatus() {
110
- return this.get("/session/status");
130
+ async getStatus(options) {
131
+ return this.get("/session/status", options);
111
132
  }
112
133
  /**
113
134
  * Get QR code for scanning
114
135
  */
115
- async getQRCode() {
116
- return this.get("/session/qr");
136
+ async getQRCode(options) {
137
+ return this.get("/session/qr", options);
117
138
  }
118
139
  /**
119
140
  * Configure S3 storage
120
141
  */
121
- async configureS3(config) {
122
- return this.post("/session/s3/config", config);
142
+ async configureS3(config, options) {
143
+ return this.post("/session/s3/config", config, options);
123
144
  }
124
145
  /**
125
146
  * Get S3 configuration
126
147
  */
127
- async getS3Config() {
128
- return this.get("/session/s3/config");
148
+ async getS3Config(options) {
149
+ return this.get("/session/s3/config", options);
129
150
  }
130
151
  /**
131
152
  * Test S3 connection
132
153
  */
133
- async testS3() {
134
- return this.post("/session/s3/test");
154
+ async testS3(options) {
155
+ return this.post("/session/s3/test", void 0, options);
135
156
  }
136
157
  /**
137
158
  * Delete S3 configuration
138
159
  */
139
- async deleteS3Config() {
140
- return this.delete("/session/s3/config");
160
+ async deleteS3Config(options) {
161
+ return this.delete("/session/s3/config", options);
141
162
  }
142
163
  }
143
164
  class UserModule extends BaseClient {
144
165
  /**
145
166
  * Get user details for specified phone numbers
146
167
  */
147
- async getInfo(phones) {
168
+ async getInfo(phones, options) {
148
169
  const request = { Phone: phones };
149
- return this.post("/user/info", request);
170
+ return this.post("/user/info", request, options);
150
171
  }
151
172
  /**
152
173
  * Check if phone numbers are registered WhatsApp users
153
174
  */
154
- async check(phones) {
175
+ async check(phones, options) {
155
176
  const request = { Phone: phones };
156
- return this.post("/user/check", request);
177
+ return this.post("/user/check", request, options);
157
178
  }
158
179
  /**
159
180
  * Get user avatar/profile picture
160
181
  */
161
- async getAvatar(phone, preview = true) {
182
+ async getAvatar(phone, preview = true, options) {
162
183
  const request = { Phone: phone, Preview: preview };
163
- return this.post("/user/avatar", request);
184
+ return this.post("/user/avatar", request, options);
164
185
  }
165
186
  /**
166
187
  * Get all contacts
167
188
  */
168
- async getContacts() {
169
- return this.get("/user/contacts");
189
+ async getContacts(options) {
190
+ return this.get("/user/contacts", options);
170
191
  }
171
192
  }
172
193
  class ChatModule extends BaseClient {
173
194
  /**
174
195
  * Send a text message
175
196
  */
176
- async sendText(request) {
177
- return this.post("/chat/send/text", request);
197
+ async sendText(request, options) {
198
+ return this.post("/chat/send/text", request, options);
178
199
  }
179
200
  /**
180
201
  * Send a template message with buttons
181
202
  */
182
- async sendTemplate(request) {
183
- return this.post("/chat/send/template", request);
203
+ async sendTemplate(request, options) {
204
+ return this.post(
205
+ "/chat/send/template",
206
+ request,
207
+ options
208
+ );
184
209
  }
185
210
  /**
186
211
  * Send an audio message
187
212
  */
188
- async sendAudio(request) {
189
- return this.post("/chat/send/audio", request);
213
+ async sendAudio(request, options) {
214
+ return this.post("/chat/send/audio", request, options);
190
215
  }
191
216
  /**
192
217
  * Send an image message
193
218
  */
194
- async sendImage(request) {
195
- return this.post("/chat/send/image", request);
219
+ async sendImage(request, options) {
220
+ return this.post("/chat/send/image", request, options);
196
221
  }
197
222
  /**
198
223
  * Send a document message
199
224
  */
200
- async sendDocument(request) {
201
- return this.post("/chat/send/document", request);
225
+ async sendDocument(request, options) {
226
+ return this.post(
227
+ "/chat/send/document",
228
+ request,
229
+ options
230
+ );
202
231
  }
203
232
  /**
204
233
  * Send a video message
205
234
  */
206
- async sendVideo(request) {
207
- return this.post("/chat/send/video", request);
235
+ async sendVideo(request, options) {
236
+ return this.post("/chat/send/video", request, options);
208
237
  }
209
238
  /**
210
239
  * Send a sticker message
211
240
  */
212
- async sendSticker(request) {
213
- return this.post("/chat/send/sticker", request);
241
+ async sendSticker(request, options) {
242
+ return this.post(
243
+ "/chat/send/sticker",
244
+ request,
245
+ options
246
+ );
214
247
  }
215
248
  /**
216
249
  * Send a location message
217
250
  */
218
- async sendLocation(request) {
219
- return this.post("/chat/send/location", request);
251
+ async sendLocation(request, options) {
252
+ return this.post(
253
+ "/chat/send/location",
254
+ request,
255
+ options
256
+ );
220
257
  }
221
258
  /**
222
259
  * Send a contact message
223
260
  */
224
- async sendContact(request) {
225
- return this.post("/chat/send/contact", request);
261
+ async sendContact(request, options) {
262
+ return this.post(
263
+ "/chat/send/contact",
264
+ request,
265
+ options
266
+ );
226
267
  }
227
268
  /**
228
269
  * Send chat presence indication (typing indicator)
229
270
  */
230
- async sendPresence(request) {
231
- await this.post("/chat/presence", request);
271
+ async sendPresence(request, options) {
272
+ await this.post("/chat/presence", request, options);
232
273
  }
233
274
  /**
234
275
  * Mark messages as read
235
276
  */
236
- async markRead(request) {
237
- await this.post("/chat/markread", request);
277
+ async markRead(request, options) {
278
+ await this.post("/chat/markread", request, options);
238
279
  }
239
280
  /**
240
281
  * React to a message
241
282
  */
242
- async react(request) {
243
- return this.post("/chat/react", request);
283
+ async react(request, options) {
284
+ return this.post("/chat/react", request, options);
244
285
  }
245
286
  /**
246
287
  * Download an image from a message
247
288
  */
248
- async downloadImage(request) {
249
- return this.post("/chat/downloadimage", request);
289
+ async downloadImage(request, options) {
290
+ return this.post(
291
+ "/chat/downloadimage",
292
+ request,
293
+ options
294
+ );
250
295
  }
251
296
  /**
252
297
  * Download a video from a message
253
298
  */
254
- async downloadVideo(request) {
255
- return this.post("/chat/downloadvideo", request);
299
+ async downloadVideo(request, options) {
300
+ return this.post(
301
+ "/chat/downloadvideo",
302
+ request,
303
+ options
304
+ );
256
305
  }
257
306
  /**
258
307
  * Download an audio from a message
259
308
  */
260
- async downloadAudio(request) {
261
- return this.post("/chat/downloadaudio", request);
309
+ async downloadAudio(request, options) {
310
+ return this.post(
311
+ "/chat/downloadaudio",
312
+ request,
313
+ options
314
+ );
262
315
  }
263
316
  /**
264
317
  * Download a document from a message
265
318
  */
266
- async downloadDocument(request) {
267
- return this.post("/chat/downloaddocument", request);
319
+ async downloadDocument(request, options) {
320
+ return this.post(
321
+ "/chat/downloaddocument",
322
+ request,
323
+ options
324
+ );
268
325
  }
269
326
  }
270
327
  class GroupModule extends BaseClient {
271
328
  /**
272
329
  * List all subscribed groups
273
330
  */
274
- async list() {
275
- return this.get("/group/list");
331
+ async list(options) {
332
+ return this.get("/group/list", options);
276
333
  }
277
334
  /**
278
335
  * Get group invite link
279
336
  */
280
- async getInviteLink(groupJID) {
337
+ async getInviteLink(groupJID, options) {
281
338
  const request = { GroupJID: groupJID };
282
- return this.post("/group/invitelink", request);
339
+ return this.post(
340
+ "/group/invitelink",
341
+ request,
342
+ options
343
+ );
283
344
  }
284
345
  /**
285
346
  * Get group information
286
347
  */
287
- async getInfo(groupJID) {
348
+ async getInfo(groupJID, options) {
288
349
  const request = { GroupJID: groupJID };
289
- return this.post("/group/info", request);
350
+ return this.post("/group/info", request, options);
290
351
  }
291
352
  /**
292
353
  * Change group photo (JPEG only)
293
354
  */
294
- async setPhoto(groupJID, image) {
355
+ async setPhoto(groupJID, image, options) {
295
356
  const request = { GroupJID: groupJID, Image: image };
296
- return this.post("/group/photo", request);
357
+ return this.post("/group/photo", request, options);
297
358
  }
298
359
  /**
299
360
  * Change group name
300
361
  */
301
- async setName(groupJID, name) {
362
+ async setName(groupJID, name, options) {
302
363
  const request = { GroupJID: groupJID, Name: name };
303
- return this.post("/group/name", request);
364
+ return this.post("/group/name", request, options);
304
365
  }
305
366
  /**
306
367
  * Create a new group
307
368
  */
308
- async create(name, participants) {
369
+ async create(name, participants, options) {
309
370
  const request = { name, participants };
310
- return this.post("/group/create", request);
371
+ return this.post("/group/create", request, options);
311
372
  }
312
373
  /**
313
374
  * Set group locked status
314
375
  */
315
- async setLocked(groupJID, locked) {
376
+ async setLocked(groupJID, locked, options) {
316
377
  const request = { groupjid: groupJID, locked };
317
- return this.post("/group/locked", request);
378
+ return this.post("/group/locked", request, options);
318
379
  }
319
380
  /**
320
381
  * Set disappearing messages timer
321
382
  */
322
- async setEphemeral(groupJID, duration) {
383
+ async setEphemeral(groupJID, duration, options) {
323
384
  const request = { groupjid: groupJID, duration };
324
- return this.post("/group/ephemeral", request);
385
+ return this.post(
386
+ "/group/ephemeral",
387
+ request,
388
+ options
389
+ );
325
390
  }
326
391
  /**
327
392
  * Remove group photo
328
393
  */
329
- async removePhoto(groupJID) {
394
+ async removePhoto(groupJID, options) {
330
395
  const request = { groupjid: groupJID };
331
- return this.post("/group/photo/remove", request);
396
+ return this.post(
397
+ "/group/photo/remove",
398
+ request,
399
+ options
400
+ );
332
401
  }
333
402
  }
334
403
  class WebhookModule extends BaseClient {
335
404
  /**
336
405
  * Set webhook URL and events to subscribe to
337
406
  */
338
- async setWebhook(webhookURL) {
407
+ async setWebhook(webhookURL, options) {
339
408
  const request = { webhookURL };
340
- return this.post("/webhook", request);
409
+ return this.post("/webhook", request, options);
341
410
  }
342
411
  /**
343
412
  * Get current webhook configuration
344
413
  */
345
- async getWebhook() {
346
- return this.get("/webhook");
414
+ async getWebhook(options) {
415
+ return this.get("/webhook", options);
347
416
  }
348
417
  }
349
418
  class WuzapiClient {
@@ -369,9 +438,9 @@ class WuzapiClient {
369
438
  /**
370
439
  * Test connection to the API
371
440
  */
372
- async ping() {
441
+ async ping(options) {
373
442
  try {
374
- await this.session.getStatus();
443
+ await this.session.getStatus(options);
375
444
  return true;
376
445
  } catch {
377
446
  return false;