whatsapp-cloud 0.1.1 → 0.1.3

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/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # whatzapp
2
2
 
3
+ ## 0.1.3
4
+
5
+ ### Patch Changes
6
+
7
+ - 6b76203: remove pkg
8
+
9
+ ## 0.1.2
10
+
11
+ ### Patch Changes
12
+
13
+ - b71b674: change input types
14
+
3
15
  ## 0.1.1
4
16
 
5
17
  ### Patch Changes
package/dist/index.cjs CHANGED
@@ -34,12 +34,18 @@ __export(index_exports, {
34
34
  incomingMessageSchema: () => incomingMessageSchema,
35
35
  incomingTextMessageSchema: () => incomingTextMessageSchema,
36
36
  messageResponseSchema: () => messageResponseSchema,
37
+ outgoingImageMessageSchema: () => outgoingImageMessageSchema,
38
+ outgoingLocationMessageSchema: () => outgoingLocationMessageSchema,
39
+ outgoingMessageSchema: () => outgoingMessageSchema,
40
+ outgoingReactionMessageSchema: () => outgoingReactionMessageSchema,
41
+ outgoingTextMessageSchema: () => outgoingTextMessageSchema,
37
42
  phoneNumberListResponseSchema: () => phoneNumberListResponseSchema,
38
43
  phoneNumberResponseSchema: () => phoneNumberResponseSchema,
39
- sendImageRequestSchema: () => sendImageRequestSchema,
40
- sendLocationRequestSchema: () => sendLocationRequestSchema,
41
- sendReactionRequestSchema: () => sendReactionRequestSchema,
42
- sendTextRequestSchema: () => sendTextRequestSchema,
44
+ sendImageInputSchema: () => sendImageInputSchema,
45
+ sendLocationInputSchema: () => sendLocationInputSchema,
46
+ sendReactionInputSchema: () => sendReactionInputSchema,
47
+ sendTextInputSchema: () => sendTextInputSchema,
48
+ statusSchema: () => statusSchema,
43
49
  templateBodyComponentSchema: () => templateBodyComponentSchema,
44
50
  templateButtonSchema: () => templateButtonSchema,
45
51
  templateButtonsComponentSchema: () => templateButtonsComponentSchema,
@@ -238,43 +244,60 @@ var HttpClient = class {
238
244
  }
239
245
  };
240
246
 
241
- // src/schemas/messages/request.ts
247
+ // src/schemas/messages/outgoing.ts
242
248
  var import_zod2 = require("zod");
243
- var baseMessageRequestSchema = import_zod2.z.object({
249
+ var baseOutgoingMessageSchema = import_zod2.z.object({
244
250
  to: import_zod2.z.string().regex(/^\+[1-9]\d{1,14}$/, "Invalid phone number format")
245
251
  });
246
- var imageSchema = import_zod2.z.object({
252
+ var textContentSchema = import_zod2.z.object({
253
+ body: import_zod2.z.string().min(1).max(4096),
254
+ preview_url: import_zod2.z.boolean().optional()
255
+ });
256
+ var imageContentSchema = import_zod2.z.object({
247
257
  id: import_zod2.z.string().optional(),
248
258
  link: import_zod2.z.string().url().optional(),
249
259
  caption: import_zod2.z.string().max(1024).optional()
250
260
  }).refine((data) => data.link || data.id, "Either link or id must be provided");
251
- var sendImageRequestSchema = baseMessageRequestSchema.extend({
252
- image: imageSchema
253
- });
254
- var textSchema = import_zod2.z.object({
255
- body: import_zod2.z.string().min(1).max(4096),
256
- preview_url: import_zod2.z.boolean().optional()
257
- });
258
- var sendTextRequestSchema = baseMessageRequestSchema.extend({
259
- text: textSchema
260
- });
261
- var locationSchema = import_zod2.z.object({
261
+ var locationContentSchema = import_zod2.z.object({
262
262
  longitude: import_zod2.z.number().min(-180).max(180),
263
263
  latitude: import_zod2.z.number().min(-90).max(90),
264
264
  name: import_zod2.z.string().optional(),
265
265
  address: import_zod2.z.string().optional()
266
266
  });
267
- var sendLocationRequestSchema = baseMessageRequestSchema.extend({
268
- location: locationSchema
269
- });
270
- var reactionSchema = import_zod2.z.object({
267
+ var reactionContentSchema = import_zod2.z.object({
271
268
  message_id: import_zod2.z.string().min(1),
272
269
  emoji: import_zod2.z.string().min(1).max(1)
273
- // Single emoji character
274
270
  });
275
- var sendReactionRequestSchema = baseMessageRequestSchema.extend({
276
- reaction: reactionSchema
271
+ var sendTextInputSchema = baseOutgoingMessageSchema.extend({
272
+ text: textContentSchema
273
+ });
274
+ var sendImageInputSchema = baseOutgoingMessageSchema.extend({
275
+ image: imageContentSchema
276
+ });
277
+ var sendLocationInputSchema = baseOutgoingMessageSchema.extend({
278
+ location: locationContentSchema
279
+ });
280
+ var sendReactionInputSchema = baseOutgoingMessageSchema.extend({
281
+ reaction: reactionContentSchema
282
+ });
283
+ var outgoingTextMessageSchema = sendTextInputSchema.extend({
284
+ type: import_zod2.z.literal("text")
285
+ });
286
+ var outgoingImageMessageSchema = sendImageInputSchema.extend({
287
+ type: import_zod2.z.literal("image")
288
+ });
289
+ var outgoingLocationMessageSchema = sendLocationInputSchema.extend({
290
+ type: import_zod2.z.literal("location")
277
291
  });
292
+ var outgoingReactionMessageSchema = sendReactionInputSchema.extend({
293
+ type: import_zod2.z.literal("reaction")
294
+ });
295
+ var outgoingMessageSchema = import_zod2.z.discriminatedUnion("type", [
296
+ outgoingTextMessageSchema,
297
+ outgoingImageMessageSchema,
298
+ outgoingLocationMessageSchema,
299
+ outgoingReactionMessageSchema
300
+ ]);
278
301
 
279
302
  // src/services/messages/utils/build-message-payload.ts
280
303
  function buildMessagePayload(to, type, content) {
@@ -345,8 +368,8 @@ function transformZodError(error) {
345
368
  }
346
369
 
347
370
  // src/services/messages/methods/send-text.ts
348
- async function sendText(messagesClient, request) {
349
- const result = sendTextRequestSchema.safeParse(request);
371
+ async function sendText(messagesClient, input) {
372
+ const result = sendTextInputSchema.safeParse(input);
350
373
  if (!result.success) {
351
374
  throw transformZodError(result.error);
352
375
  }
@@ -358,8 +381,8 @@ async function sendText(messagesClient, request) {
358
381
  }
359
382
 
360
383
  // src/services/messages/methods/send-image.ts
361
- async function sendImage(messagesClient, request) {
362
- const result = sendImageRequestSchema.safeParse(request);
384
+ async function sendImage(messagesClient, input) {
385
+ const result = sendImageInputSchema.safeParse(input);
363
386
  if (!result.success) {
364
387
  throw transformZodError(result.error);
365
388
  }
@@ -371,8 +394,8 @@ async function sendImage(messagesClient, request) {
371
394
  }
372
395
 
373
396
  // src/services/messages/methods/send-location.ts
374
- async function sendLocation(messagesClient, request) {
375
- const result = sendLocationRequestSchema.safeParse(request);
397
+ async function sendLocation(messagesClient, input) {
398
+ const result = sendLocationInputSchema.safeParse(input);
376
399
  if (!result.success) {
377
400
  throw transformZodError(result.error);
378
401
  }
@@ -384,8 +407,8 @@ async function sendLocation(messagesClient, request) {
384
407
  }
385
408
 
386
409
  // src/services/messages/methods/send-reaction.ts
387
- async function sendReaction(messagesClient, request) {
388
- const result = sendReactionRequestSchema.safeParse(request);
410
+ async function sendReaction(messagesClient, input) {
411
+ const result = sendReactionInputSchema.safeParse(input);
389
412
  if (!result.success) {
390
413
  throw transformZodError(result.error);
391
414
  }
@@ -443,42 +466,42 @@ var MessagesService = class {
443
466
  /**
444
467
  * Send a text message
445
468
  *
446
- * @param request - Text message request (to, text)
469
+ * @param input - Text message input (to, text)
447
470
  * @param phoneNumberId - Optional phone number ID (overrides client config)
448
471
  */
449
- async sendText(request, phoneNumberId) {
472
+ async sendText(input, phoneNumberId) {
450
473
  const client = this.getClient(phoneNumberId);
451
- return sendText(client, request);
474
+ return sendText(client, input);
452
475
  }
453
476
  /**
454
477
  * Send an image message
455
478
  *
456
- * @param request - Image message request (to, image)
479
+ * @param input - Image message input (to, image)
457
480
  * @param phoneNumberId - Optional phone number ID (overrides client config)
458
481
  */
459
- async sendImage(request, phoneNumberId) {
482
+ async sendImage(input, phoneNumberId) {
460
483
  const client = this.getClient(phoneNumberId);
461
- return sendImage(client, request);
484
+ return sendImage(client, input);
462
485
  }
463
486
  /**
464
487
  * Send a location message
465
488
  *
466
- * @param request - Location message request (to, location)
489
+ * @param input - Location message input (to, location)
467
490
  * @param phoneNumberId - Optional phone number ID (overrides client config)
468
491
  */
469
- async sendLocation(request, phoneNumberId) {
492
+ async sendLocation(input, phoneNumberId) {
470
493
  const client = this.getClient(phoneNumberId);
471
- return sendLocation(client, request);
494
+ return sendLocation(client, input);
472
495
  }
473
496
  /**
474
497
  * Send a reaction message
475
498
  *
476
- * @param request - Reaction message request (to, reaction)
499
+ * @param input - Reaction message input (to, reaction)
477
500
  * @param phoneNumberId - Optional phone number ID (overrides client config)
478
501
  */
479
- async sendReaction(request, phoneNumberId) {
502
+ async sendReaction(input, phoneNumberId) {
480
503
  const client = this.getClient(phoneNumberId);
481
- return sendReaction(client, request);
504
+ return sendReaction(client, input);
482
505
  }
483
506
  };
484
507
 
@@ -1191,7 +1214,7 @@ function verifyWebhook(query, verifyToken) {
1191
1214
  // src/schemas/webhooks/payload.ts
1192
1215
  var import_zod8 = require("zod");
1193
1216
 
1194
- // src/schemas/webhooks/incoming-message.ts
1217
+ // src/schemas/messages/incoming.ts
1195
1218
  var import_zod7 = require("zod");
1196
1219
  var baseIncomingMessageSchema = import_zod7.z.object({
1197
1220
  from: import_zod7.z.string(),
@@ -1249,14 +1272,78 @@ var webhookMetadataSchema = import_zod8.z.object({
1249
1272
  display_phone_number: import_zod8.z.string(),
1250
1273
  phone_number_id: import_zod8.z.string()
1251
1274
  });
1275
+ var conversationOriginSchema = import_zod8.z.object({
1276
+ type: import_zod8.z.enum([
1277
+ "authentication",
1278
+ "authentication_international",
1279
+ "marketing",
1280
+ "marketing_lite",
1281
+ "referral_conversion",
1282
+ "service",
1283
+ "utility"
1284
+ ])
1285
+ });
1286
+ var conversationSchema = import_zod8.z.object({
1287
+ id: import_zod8.z.string(),
1288
+ expiration_timestamp: import_zod8.z.string().optional(),
1289
+ // Only for sent status
1290
+ origin: conversationOriginSchema
1291
+ });
1292
+ var pricingSchema = import_zod8.z.object({
1293
+ billable: import_zod8.z.boolean(),
1294
+ // Deprecated but still present
1295
+ pricing_model: import_zod8.z.enum(["CBP", "PMP"]),
1296
+ type: import_zod8.z.enum(["regular", "free_customer_service", "free_entry_point"]),
1297
+ category: import_zod8.z.enum([
1298
+ "authentication",
1299
+ "authentication-international",
1300
+ "marketing",
1301
+ "marketing_lite",
1302
+ "referral_conversion",
1303
+ "service",
1304
+ "utility"
1305
+ ])
1306
+ });
1307
+ var statusErrorSchema = import_zod8.z.object({
1308
+ code: import_zod8.z.number(),
1309
+ title: import_zod8.z.string(),
1310
+ message: import_zod8.z.string(),
1311
+ error_data: import_zod8.z.object({
1312
+ details: import_zod8.z.string()
1313
+ }),
1314
+ href: import_zod8.z.string()
1315
+ });
1316
+ var statusSchema = import_zod8.z.object({
1317
+ id: import_zod8.z.string(),
1318
+ // WhatsApp message ID
1319
+ status: import_zod8.z.enum(["sent", "delivered", "read", "failed", "played"]),
1320
+ timestamp: import_zod8.z.string(),
1321
+ // Unix timestamp
1322
+ recipient_id: import_zod8.z.string(),
1323
+ // User phone number or group ID
1324
+ recipient_type: import_zod8.z.literal("group").optional(),
1325
+ // Only included if message sent to a group
1326
+ recipient_participant_id: import_zod8.z.string().optional(),
1327
+ // Only included if message sent to a group
1328
+ recipient_identity_key_hash: import_zod8.z.string().optional(),
1329
+ // Only included if identity change check enabled
1330
+ biz_opaque_callback_data: import_zod8.z.string().optional(),
1331
+ // Only included if message sent with biz_opaque_callback_data
1332
+ conversation: conversationSchema.optional(),
1333
+ // Conditional inclusion (see conversationSchema docs)
1334
+ pricing: pricingSchema.optional(),
1335
+ // Conditional inclusion (see pricingSchema docs)
1336
+ errors: import_zod8.z.array(statusErrorSchema).optional()
1337
+ // Only included if failure to send or deliver message
1338
+ });
1252
1339
  var webhookValueSchema = import_zod8.z.object({
1253
1340
  messaging_product: import_zod8.z.literal("whatsapp"),
1254
1341
  metadata: webhookMetadataSchema,
1255
1342
  contacts: import_zod8.z.array(contactSchema).optional(),
1256
1343
  messages: import_zod8.z.array(incomingMessageSchema).optional(),
1257
1344
  // Incoming messages
1258
- statuses: import_zod8.z.array(import_zod8.z.any()).optional()
1259
- // Status updates (for later)
1345
+ statuses: import_zod8.z.array(statusSchema).optional()
1346
+ // Status updates
1260
1347
  });
1261
1348
  var webhookChangeSchema = import_zod8.z.object({
1262
1349
  value: webhookValueSchema,
@@ -1371,6 +1458,9 @@ var WebhooksService = class {
1371
1458
  * Handlers are processed asynchronously. If you need to await handler completion,
1372
1459
  * use the low-level `extractMessages()` method instead.
1373
1460
  *
1461
+ * The `beforeHandler` return type is automatically inferred and provides
1462
+ * full type safety in message handlers.
1463
+ *
1374
1464
  * @param payload - Webhook payload from Meta (will be validated)
1375
1465
  * @param handlers - Object with handler functions for each message type
1376
1466
  * @param options - Optional error handling configuration
@@ -1388,7 +1478,7 @@ var WebhooksService = class {
1388
1478
  const contacts = change.value.contacts || [];
1389
1479
  for (const message of change.value.messages) {
1390
1480
  const contact = contacts.find((c) => c.wa_id === message.from);
1391
- const context = {
1481
+ const webhook = {
1392
1482
  metadata,
1393
1483
  ...contact && {
1394
1484
  contact: {
@@ -1398,20 +1488,39 @@ var WebhooksService = class {
1398
1488
  }
1399
1489
  };
1400
1490
  Promise.resolve().then(async () => {
1491
+ let before = void 0;
1492
+ if (handlers.beforeHandler) {
1493
+ try {
1494
+ before = await handlers.beforeHandler(
1495
+ message,
1496
+ webhook
1497
+ );
1498
+ } catch (error) {
1499
+ if (options?.onError) {
1500
+ options.onError(error, message);
1501
+ } else {
1502
+ console.error(
1503
+ `Error in beforeHandler for message ${message.id}:`,
1504
+ error
1505
+ );
1506
+ }
1507
+ before = void 0;
1508
+ }
1509
+ }
1401
1510
  switch (message.type) {
1402
1511
  case "text":
1403
1512
  if (handlers.text) {
1404
- await handlers.text(message, context);
1513
+ await handlers.text(message, webhook, before);
1405
1514
  }
1406
1515
  break;
1407
1516
  case "audio":
1408
1517
  if (handlers.audio) {
1409
- await handlers.audio(message, context);
1518
+ await handlers.audio(message, webhook, before);
1410
1519
  }
1411
1520
  break;
1412
1521
  case "image":
1413
1522
  if (handlers.image) {
1414
- await handlers.image(message, context);
1523
+ await handlers.image(message, webhook, before);
1415
1524
  }
1416
1525
  break;
1417
1526
  default:
@@ -1487,7 +1596,9 @@ var messageResponseSchema = import_zod10.z.object({
1487
1596
  ),
1488
1597
  messages: import_zod10.z.array(
1489
1598
  import_zod10.z.object({
1490
- id: import_zod10.z.string()
1599
+ id: import_zod10.z.string(),
1600
+ group_id: import_zod10.z.string().optional(),
1601
+ message_status: import_zod10.z.string().optional()
1491
1602
  })
1492
1603
  )
1493
1604
  });
@@ -1597,12 +1708,18 @@ var debugTokenResponseSchema = import_zod14.z.object({
1597
1708
  incomingMessageSchema,
1598
1709
  incomingTextMessageSchema,
1599
1710
  messageResponseSchema,
1711
+ outgoingImageMessageSchema,
1712
+ outgoingLocationMessageSchema,
1713
+ outgoingMessageSchema,
1714
+ outgoingReactionMessageSchema,
1715
+ outgoingTextMessageSchema,
1600
1716
  phoneNumberListResponseSchema,
1601
1717
  phoneNumberResponseSchema,
1602
- sendImageRequestSchema,
1603
- sendLocationRequestSchema,
1604
- sendReactionRequestSchema,
1605
- sendTextRequestSchema,
1718
+ sendImageInputSchema,
1719
+ sendLocationInputSchema,
1720
+ sendReactionInputSchema,
1721
+ sendTextInputSchema,
1722
+ statusSchema,
1606
1723
  templateBodyComponentSchema,
1607
1724
  templateButtonSchema,
1608
1725
  templateButtonsComponentSchema,