whatsapp-cloud 0.0.3 → 0.0.4

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 (52) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/README.md +11 -0
  3. package/agent_docs/DESIGN.md +707 -0
  4. package/agent_docs/MESSAGES_NAMESPACE_ANALYSIS.md +368 -0
  5. package/agent_docs/NAMING_DECISION.md +78 -0
  6. package/agent_docs/STRUCTURE.md +711 -0
  7. package/agent_docs/messages-namespace-design.md +357 -0
  8. package/package.json +5 -2
  9. package/src/client/HttpClient.ts +122 -0
  10. package/src/client/WhatsAppClient.ts +55 -0
  11. package/src/client/index.ts +2 -0
  12. package/src/errors.ts +58 -0
  13. package/src/index.ts +16 -1
  14. package/src/schemas/accounts/index.ts +1 -0
  15. package/src/schemas/accounts/phone-number.ts +20 -0
  16. package/src/schemas/business/account.ts +43 -0
  17. package/src/schemas/business/index.ts +2 -0
  18. package/src/schemas/client.ts +50 -0
  19. package/src/schemas/debug.ts +25 -0
  20. package/src/schemas/index.ts +5 -0
  21. package/src/schemas/messages/index.ts +2 -0
  22. package/src/schemas/messages/request.ts +82 -0
  23. package/src/schemas/messages/response.ts +19 -0
  24. package/src/services/accounts/AccountsClient.ts +42 -0
  25. package/src/services/accounts/AccountsService.ts +47 -0
  26. package/src/services/accounts/index.ts +2 -0
  27. package/src/services/accounts/methods/list-phone-numbers.ts +16 -0
  28. package/src/services/business/BusinessClient.ts +42 -0
  29. package/src/services/business/BusinessService.ts +47 -0
  30. package/src/services/business/index.ts +3 -0
  31. package/src/services/business/methods/list-accounts.ts +18 -0
  32. package/src/services/index.ts +2 -0
  33. package/src/services/messages/MessagesClient.ts +38 -0
  34. package/src/services/messages/MessagesService.ts +77 -0
  35. package/src/services/messages/index.ts +8 -0
  36. package/src/services/messages/methods/send-image.ts +33 -0
  37. package/src/services/messages/methods/send-location.ts +32 -0
  38. package/src/services/messages/methods/send-reaction.ts +33 -0
  39. package/src/services/messages/methods/send-text.ts +32 -0
  40. package/src/services/messages/utils/build-message-payload.ts +32 -0
  41. package/src/types/accounts/index.ts +1 -0
  42. package/src/types/accounts/phone-number.ts +9 -0
  43. package/src/types/business/account.ts +10 -0
  44. package/src/types/business/index.ts +2 -0
  45. package/src/types/client.ts +8 -0
  46. package/src/types/debug.ts +8 -0
  47. package/src/types/index.ts +5 -0
  48. package/src/types/messages/index.ts +2 -0
  49. package/src/types/messages/request.ts +27 -0
  50. package/src/types/messages/response.ts +7 -0
  51. package/src/utils/zod-error.ts +28 -0
  52. package/tsconfig.json +4 -1
@@ -0,0 +1,32 @@
1
+ import type { MessagesClient } from "../MessagesClient";
2
+ import { sendTextRequestSchema } from "../../../schemas/messages/request";
3
+ import type { SendTextRequest } from "../../../types/messages/request";
4
+ import type { MessageResponse } from "../../../types/messages/response";
5
+ import { buildMessagePayload } from "../utils/build-message-payload";
6
+ import { transformZodError } from "../../../utils/zod-error";
7
+
8
+ /**
9
+ * Send a text message
10
+ *
11
+ * @param messagesClient - Messages client with phone number ID baked in
12
+ * @param request - Text message request (to, text)
13
+ */
14
+ export async function sendText(
15
+ messagesClient: MessagesClient,
16
+ request: SendTextRequest
17
+ ): Promise<MessageResponse> {
18
+ // Validate request with schema - throws WhatsAppValidationError if invalid
19
+ const result = sendTextRequestSchema.safeParse(request);
20
+ if (!result.success) {
21
+ throw transformZodError(result.error);
22
+ }
23
+ const data = result.data;
24
+
25
+ // Build message payload using common structure
26
+ const payload = buildMessagePayload(data.to, "text", {
27
+ text: data.text,
28
+ });
29
+
30
+ // Make API request - messagesClient handles the phoneNumberId prefix automatically
31
+ return messagesClient.post<MessageResponse>("/messages", payload);
32
+ }
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Builds the base message payload structure that's common to all WhatsApp messages
3
+ *
4
+ * According to WhatsApp API docs, all messages follow this structure:
5
+ * {
6
+ * "messaging_product": "whatsapp",
7
+ * "recipient_type": "individual",
8
+ * "to": "<WHATSAPP_USER_PHONE_NUMBER>",
9
+ * "type": "<MESSAGE_TYPE>",
10
+ * "<MESSAGE_TYPE>": {<MESSAGE_CONTENTS>}
11
+ * }
12
+ *
13
+ * Note: undefined values in content are automatically omitted by JSON.stringify
14
+ *
15
+ * @param to - WhatsApp phone number (E.164 format)
16
+ * @param type - Message type (text, image, video, etc.)
17
+ * @param content - Message-specific content object
18
+ * @returns Complete message payload ready for API request
19
+ */
20
+ export function buildMessagePayload<T extends Record<string, unknown>>(
21
+ to: string,
22
+ type: string,
23
+ content: T
24
+ ) {
25
+ return {
26
+ messaging_product: "whatsapp" as const,
27
+ recipient_type: "individual" as const,
28
+ to,
29
+ type,
30
+ ...content,
31
+ };
32
+ }
@@ -0,0 +1 @@
1
+ export type * from "./phone-number";
@@ -0,0 +1,9 @@
1
+ import { z } from "zod";
2
+ import { phoneNumberListResponseSchema } from "../../schemas/accounts/phone-number";
3
+
4
+ /**
5
+ * Type for phone number list response
6
+ */
7
+ export type PhoneNumberListResponse = z.infer<
8
+ typeof phoneNumberListResponseSchema
9
+ >;
@@ -0,0 +1,10 @@
1
+ import { z } from "zod";
2
+ import { businessAccountsListResponseSchema } from "../../schemas/business/account";
3
+
4
+ /**
5
+ * Type for WhatsApp Business Accounts list response
6
+ */
7
+ export type BusinessAccountsListResponse = z.infer<
8
+ typeof businessAccountsListResponseSchema
9
+ >;
10
+
@@ -0,0 +1,2 @@
1
+ export type * from "./account";
2
+
@@ -0,0 +1,8 @@
1
+ import { z } from "zod";
2
+ import { clientConfigSchema } from "../schemas/client";
3
+
4
+ /**
5
+ * Client configuration type
6
+ */
7
+ export type ClientConfig = z.infer<typeof clientConfigSchema>;
8
+
@@ -0,0 +1,8 @@
1
+ import { z } from "zod";
2
+ import { debugTokenResponseSchema } from "../schemas/debug";
3
+
4
+ /**
5
+ * Type for debug token response
6
+ */
7
+ export type DebugTokenResponse = z.infer<typeof debugTokenResponseSchema>;
8
+
@@ -0,0 +1,5 @@
1
+ export type * from "./client";
2
+ export type * from "./messages/index";
3
+ export type * from "./accounts/index";
4
+ export type * from "./business/index";
5
+ export type * from "./debug";
@@ -0,0 +1,2 @@
1
+ export type * from "./request";
2
+ export type * from "./response";
@@ -0,0 +1,27 @@
1
+ import { z } from "zod";
2
+ import {
3
+ sendTextRequestSchema,
4
+ sendImageRequestSchema,
5
+ sendLocationRequestSchema,
6
+ sendReactionRequestSchema,
7
+ } from "../../schemas/messages/request";
8
+
9
+ /**
10
+ * Type for sending a text message
11
+ */
12
+ export type SendTextRequest = z.infer<typeof sendTextRequestSchema>;
13
+
14
+ /**
15
+ * Type for sending an image message
16
+ */
17
+ export type SendImageRequest = z.infer<typeof sendImageRequestSchema>;
18
+
19
+ /**
20
+ * Type for sending a location message
21
+ */
22
+ export type SendLocationRequest = z.infer<typeof sendLocationRequestSchema>;
23
+
24
+ /**
25
+ * Type for sending a reaction message
26
+ */
27
+ export type SendReactionRequest = z.infer<typeof sendReactionRequestSchema>;
@@ -0,0 +1,7 @@
1
+ import { z } from "zod";
2
+ import { messageResponseSchema } from "../../schemas/messages/response";
3
+
4
+ /**
5
+ * Type for message response
6
+ */
7
+ export type MessageResponse = z.infer<typeof messageResponseSchema>;
@@ -0,0 +1,28 @@
1
+ import { ZodError } from "zod";
2
+ import { WhatsAppValidationError } from "../errors";
3
+
4
+ /**
5
+ * Transform a ZodError into a WhatsAppValidationError
6
+ *
7
+ * Uses the first error's message (Zod already orders errors by importance)
8
+ * Can be used for any validation: config, requests, responses, etc.
9
+ */
10
+ export function transformZodError(error: ZodError): WhatsAppValidationError {
11
+ const issues = error.issues.map((err) => ({
12
+ path: err.path as readonly (string | number)[],
13
+ message: err.message,
14
+ }));
15
+
16
+ // Use the first error as the main message (Zod orders by importance)
17
+ const firstError = error.issues[0];
18
+ if (firstError) {
19
+ return new WhatsAppValidationError(
20
+ firstError.message,
21
+ typeof firstError.path[0] === "string" ? firstError.path[0] : undefined,
22
+ issues
23
+ );
24
+ }
25
+
26
+ // Fallback (shouldn't happen)
27
+ return new WhatsAppValidationError("Validation failed", undefined, issues);
28
+ }
package/tsconfig.json CHANGED
@@ -1,5 +1,7 @@
1
1
  {
2
2
  // Visit https://aka.ms/tsconfig to read more about this file
3
+ "include": ["src/**/*"],
4
+ "exclude": ["node_modules", "dist", "examples"],
3
5
  "compilerOptions": {
4
6
  // File Layout
5
7
  "rootDir": "./src",
@@ -7,7 +9,8 @@
7
9
 
8
10
  // Environment Settings
9
11
  // See also https://aka.ms/tsconfig/module
10
- "module": "nodenext",
12
+ "module": "esnext",
13
+ "moduleResolution": "bundler",
11
14
  "target": "esnext",
12
15
  "types": [],
13
16
  // For nodejs: