@wats/graph 0.3.2 → 0.3.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 (41) hide show
  1. package/README.md +32 -1
  2. package/dist/endpoints/businessManagement.d.ts +144 -0
  3. package/dist/endpoints/businessManagement.js +273 -1
  4. package/dist/endpoints/calling.d.ts +3 -0
  5. package/dist/endpoints/flows.d.ts +3 -0
  6. package/dist/endpoints/messages/builders-basic.d.ts +11 -0
  7. package/dist/endpoints/messages/builders-basic.js +272 -0
  8. package/dist/endpoints/messages/builders-interactive.d.ts +11 -0
  9. package/dist/endpoints/messages/builders-interactive.js +156 -0
  10. package/dist/endpoints/messages/builders-template.d.ts +3 -0
  11. package/dist/endpoints/messages/builders-template.js +121 -0
  12. package/dist/endpoints/messages/callables.d.ts +31 -0
  13. package/dist/endpoints/messages/callables.js +57 -0
  14. package/dist/endpoints/messages/index.d.ts +6 -0
  15. package/dist/endpoints/messages/index.js +56 -0
  16. package/dist/endpoints/messages/types.d.ts +363 -0
  17. package/dist/endpoints/messages/types.js +0 -0
  18. package/dist/endpoints/messages/validation.d.ts +43 -0
  19. package/dist/endpoints/messages/validation.js +277 -0
  20. package/dist/endpoints/messages.d.ts +1 -375
  21. package/dist/endpoints/messages.js +1 -675
  22. package/dist/endpoints/templates/builders.js +23 -0
  23. package/dist/endpoints/templates/callables.d.ts +1 -0
  24. package/dist/endpoints/templates/callables.js +4 -2
  25. package/dist/endpoints/templates/groups.d.ts +126 -0
  26. package/dist/endpoints/templates/groups.js +156 -0
  27. package/dist/endpoints/templates/index.d.ts +3 -1
  28. package/dist/endpoints/templates/index.js +8 -0
  29. package/dist/endpoints/templates/types.d.ts +14 -1
  30. package/dist/endpoints/templates.d.ts +2 -2
  31. package/dist/endpoints/templates.js +6 -0
  32. package/dist/endpoints/waba/phoneNumbers.d.ts +2 -0
  33. package/dist/endpoints/wabaEndpoints.d.ts +5 -2
  34. package/dist/endpoints/wabaEndpoints.js +6 -0
  35. package/dist/errorSubclasses.d.ts +28 -0
  36. package/dist/errorSubclasses.js +63 -0
  37. package/dist/subclients/phoneNumberClient.d.ts +18 -2
  38. package/dist/subclients/phoneNumberClient.js +72 -1
  39. package/dist/subclients/wabaClient.d.ts +15 -1
  40. package/dist/subclients/wabaClient.js +33 -0
  41. package/package.json +10 -1
@@ -0,0 +1,272 @@
1
+ import { GraphRequestValidationError } from "../../errors.js";
2
+ import {
3
+ GRAPH_MESSAGES_DOCUMENT_FILENAME_MAX_LENGTH,
4
+ GRAPH_MESSAGES_GENERAL_TEXT_MAX_LENGTH,
5
+ GRAPH_MESSAGES_MEDIA_CAPTION_MAX_LENGTH,
6
+ GRAPH_MESSAGES_MEDIA_ID_MAX_LENGTH,
7
+ GRAPH_MESSAGES_REPLY_TO_MESSAGE_ID_MAX_LENGTH,
8
+ GRAPH_MESSAGES_SHORT_LABEL_MAX_LENGTH,
9
+ GRAPH_MESSAGES_MAX_CONTACTS,
10
+ assertBoundedArray,
11
+ assertNonEmptyControlFreeString,
12
+ assertNoUnsupportedMediaFields,
13
+ assertValidMediaLink,
14
+ assertValidNumberInRange,
15
+ asRecordInput,
16
+ assertValidRecipient,
17
+ assertValidReplyToMessageId,
18
+ assertValidText,
19
+ isPlainOptionsObject,
20
+ mapValidatedArray,
21
+ maybeText,
22
+ withReplyContext
23
+ } from "./validation.js";
24
+ export function buildSendTextPayload(input) {
25
+ if (!isPlainOptionsObject(input)) {
26
+ throw new GraphRequestValidationError("Invalid sendText input: expected an options object.");
27
+ }
28
+ const to = assertValidRecipient(input.to);
29
+ const text = assertValidText(input.text);
30
+ if (input.previewUrl !== undefined && typeof input.previewUrl !== "boolean") {
31
+ throw new GraphRequestValidationError("Invalid sendText input: previewUrl must be a boolean when provided.");
32
+ }
33
+ const payload = {
34
+ messaging_product: "whatsapp",
35
+ to,
36
+ type: "text",
37
+ text: {
38
+ body: text
39
+ }
40
+ };
41
+ if (input.previewUrl !== undefined) {
42
+ payload.text.preview_url = input.previewUrl;
43
+ }
44
+ if (input.replyToMessageId !== undefined) {
45
+ payload.context = {
46
+ message_id: assertValidReplyToMessageId(input.replyToMessageId)
47
+ };
48
+ }
49
+ return payload;
50
+ }
51
+ function buildSendMediaPayload(input, mediaType, helperName, options) {
52
+ if (!isPlainOptionsObject(input)) {
53
+ throw new GraphRequestValidationError(`Invalid ${helperName} input: expected an options object.`);
54
+ }
55
+ const record = input;
56
+ assertNoUnsupportedMediaFields(record, helperName, options);
57
+ const to = assertValidRecipient(record.to, helperName);
58
+ const hasMediaId = record.mediaId !== undefined;
59
+ const hasLink = record.link !== undefined;
60
+ if (hasMediaId === hasLink) {
61
+ throw new GraphRequestValidationError(`Invalid ${helperName} input: exactly one of mediaId or link is required.`);
62
+ }
63
+ const media = {};
64
+ if (hasMediaId) {
65
+ media.id = assertNonEmptyControlFreeString(record.mediaId, "mediaId", GRAPH_MESSAGES_MEDIA_ID_MAX_LENGTH, helperName);
66
+ } else {
67
+ media.link = assertValidMediaLink(record.link, helperName);
68
+ }
69
+ if (record.caption !== undefined) {
70
+ media.caption = assertNonEmptyControlFreeString(record.caption, "caption", GRAPH_MESSAGES_MEDIA_CAPTION_MAX_LENGTH, helperName);
71
+ }
72
+ if (record.filename !== undefined) {
73
+ media.filename = assertNonEmptyControlFreeString(record.filename, "filename", GRAPH_MESSAGES_DOCUMENT_FILENAME_MAX_LENGTH, helperName);
74
+ }
75
+ const payload = {
76
+ messaging_product: "whatsapp",
77
+ to,
78
+ type: mediaType,
79
+ [mediaType]: media
80
+ };
81
+ if (record.replyToMessageId !== undefined) {
82
+ payload.context = {
83
+ message_id: assertValidReplyToMessageId(record.replyToMessageId)
84
+ };
85
+ }
86
+ return payload;
87
+ }
88
+ export function buildSendImagePayload(input) {
89
+ return buildSendMediaPayload(input, "image", "sendImage", {
90
+ caption: true,
91
+ filename: false
92
+ });
93
+ }
94
+ export function buildSendVideoPayload(input) {
95
+ return buildSendMediaPayload(input, "video", "sendVideo", {
96
+ caption: true,
97
+ filename: false
98
+ });
99
+ }
100
+ export function buildSendAudioPayload(input) {
101
+ const payload = buildSendMediaPayload(input, "audio", "sendAudio", {
102
+ caption: false,
103
+ filename: false
104
+ });
105
+ const record = input;
106
+ if (record.voice !== undefined) {
107
+ if (typeof record.voice !== "boolean") {
108
+ throw new GraphRequestValidationError("Invalid sendAudio input: voice must be a boolean when provided.");
109
+ }
110
+ if (record.voice)
111
+ payload.audio.voice = true;
112
+ }
113
+ return payload;
114
+ }
115
+ export function buildSendDocumentPayload(input) {
116
+ return buildSendMediaPayload(input, "document", "sendDocument", {
117
+ caption: true,
118
+ filename: true
119
+ });
120
+ }
121
+ export function buildSendStickerPayload(input) {
122
+ return buildSendMediaPayload(input, "sticker", "sendSticker", {
123
+ caption: false,
124
+ filename: false
125
+ });
126
+ }
127
+ export function buildSendLocationPayload(input) {
128
+ const record = asRecordInput(input, "sendLocation");
129
+ const payload = {
130
+ messaging_product: "whatsapp",
131
+ to: assertValidRecipient(record.to, "sendLocation"),
132
+ type: "location",
133
+ location: {
134
+ latitude: assertValidNumberInRange(record.latitude, "latitude", -90, 90, "sendLocation"),
135
+ longitude: assertValidNumberInRange(record.longitude, "longitude", -180, 180, "sendLocation")
136
+ }
137
+ };
138
+ const name = maybeText(record.name, "name", GRAPH_MESSAGES_SHORT_LABEL_MAX_LENGTH, "sendLocation");
139
+ const address = maybeText(record.address, "address", GRAPH_MESSAGES_GENERAL_TEXT_MAX_LENGTH, "sendLocation");
140
+ if (name !== undefined)
141
+ payload.location.name = name;
142
+ if (address !== undefined)
143
+ payload.location.address = address;
144
+ return withReplyContext(payload, record);
145
+ }
146
+ function normalizeContactName(value, helperName) {
147
+ if (!isPlainOptionsObject(value))
148
+ throw new GraphRequestValidationError(`Invalid ${helperName} input: contact.name must be an object.`);
149
+ const name = value;
150
+ const formatted = maybeText(name.formattedName ?? name.formatted, "name.formattedName", GRAPH_MESSAGES_GENERAL_TEXT_MAX_LENGTH, helperName);
151
+ if (formatted === undefined)
152
+ throw new GraphRequestValidationError(`Invalid ${helperName} input: contact.name.formattedName is required.`);
153
+ const out = { formatted_name: formatted };
154
+ const pairs = [["firstName", "first_name"], ["lastName", "last_name"], ["middleName", "middle_name"], ["suffix", "suffix"], ["prefix", "prefix"]];
155
+ for (const [camel, snake] of pairs) {
156
+ const v = maybeText(name[camel] ?? name[snake], `name.${camel}`, GRAPH_MESSAGES_SHORT_LABEL_MAX_LENGTH, helperName);
157
+ if (v !== undefined)
158
+ out[snake] = v;
159
+ }
160
+ return out;
161
+ }
162
+ function normalizeTypedStringRecord(value, fieldName, requiredField, helperName) {
163
+ if (!isPlainOptionsObject(value))
164
+ throw new GraphRequestValidationError(`Invalid ${helperName} input: ${fieldName} entries must be objects.`);
165
+ const record = value;
166
+ const primary = maybeText(record[requiredField], `${fieldName}.${requiredField}`, GRAPH_MESSAGES_GENERAL_TEXT_MAX_LENGTH, helperName);
167
+ if (primary === undefined)
168
+ throw new GraphRequestValidationError(`Invalid ${helperName} input: ${fieldName}.${requiredField} is required.`);
169
+ const out = { [requiredField]: primary };
170
+ const type = maybeText(record.type, `${fieldName}.type`, GRAPH_MESSAGES_SHORT_LABEL_MAX_LENGTH, helperName);
171
+ if (type !== undefined)
172
+ out.type = type;
173
+ return out;
174
+ }
175
+ function normalizeContact(value, helperName) {
176
+ if (!isPlainOptionsObject(value))
177
+ throw new GraphRequestValidationError(`Invalid ${helperName} input: contacts entries must be objects.`);
178
+ const contact = value;
179
+ const out = { name: normalizeContactName(contact.name, helperName) };
180
+ if (contact.phones !== undefined) {
181
+ out.phones = mapValidatedArray(assertBoundedArray(contact.phones, "contact.phones", 1, 20, helperName), (entry) => {
182
+ if (!isPlainOptionsObject(entry))
183
+ throw new GraphRequestValidationError(`Invalid ${helperName} input: phone entries must be objects.`);
184
+ const phone = entry;
185
+ const rawPhone = maybeText(phone.phone, "phone.phone", GRAPH_MESSAGES_SHORT_LABEL_MAX_LENGTH, helperName);
186
+ const waId = maybeText(phone.waId ?? phone.wa_id, "phone.waId", GRAPH_MESSAGES_SHORT_LABEL_MAX_LENGTH, helperName);
187
+ if (rawPhone === undefined && waId === undefined)
188
+ throw new GraphRequestValidationError(`Invalid ${helperName} input: phone requires phone or waId.`);
189
+ const normalized = {};
190
+ if (rawPhone !== undefined)
191
+ normalized.phone = rawPhone;
192
+ if (waId !== undefined)
193
+ normalized.wa_id = waId;
194
+ const type = maybeText(phone.type, "phone.type", GRAPH_MESSAGES_SHORT_LABEL_MAX_LENGTH, helperName);
195
+ if (type !== undefined)
196
+ normalized.type = type;
197
+ return normalized;
198
+ });
199
+ }
200
+ if (contact.emails !== undefined)
201
+ out.emails = mapValidatedArray(assertBoundedArray(contact.emails, "contact.emails", 1, 20, helperName), (entry) => normalizeTypedStringRecord(entry, "email", "email", helperName));
202
+ if (contact.urls !== undefined) {
203
+ out.urls = mapValidatedArray(assertBoundedArray(contact.urls, "contact.urls", 1, 20, helperName), (entry) => {
204
+ const normalized = normalizeTypedStringRecord(entry, "url", "url", helperName);
205
+ normalized.url = assertValidMediaLink(normalized.url, helperName);
206
+ return normalized;
207
+ });
208
+ }
209
+ if (contact.addresses !== undefined) {
210
+ out.addresses = mapValidatedArray(assertBoundedArray(contact.addresses, "contact.addresses", 1, 20, helperName), (entry) => {
211
+ if (!isPlainOptionsObject(entry))
212
+ throw new GraphRequestValidationError(`Invalid ${helperName} input: address entries must be objects.`);
213
+ const address = entry;
214
+ const normalized = {};
215
+ for (const [camel, snake] of [["street", "street"], ["city", "city"], ["state", "state"], ["zip", "zip"], ["country", "country"], ["countryCode", "country_code"], ["type", "type"]]) {
216
+ const v = maybeText(address[camel] ?? address[snake], `address.${camel}`, GRAPH_MESSAGES_GENERAL_TEXT_MAX_LENGTH, helperName);
217
+ if (v !== undefined)
218
+ normalized[snake] = v;
219
+ }
220
+ if (Object.keys(normalized).length === 0)
221
+ throw new GraphRequestValidationError(`Invalid ${helperName} input: address entries must contain at least one field.`);
222
+ return normalized;
223
+ });
224
+ }
225
+ if (contact.org !== undefined) {
226
+ if (!isPlainOptionsObject(contact.org))
227
+ throw new GraphRequestValidationError(`Invalid ${helperName} input: org must be an object.`);
228
+ const org = contact.org;
229
+ const normalized = {};
230
+ for (const key of ["company", "department", "title"]) {
231
+ const v = maybeText(org[key], `org.${key}`, GRAPH_MESSAGES_GENERAL_TEXT_MAX_LENGTH, helperName);
232
+ if (v !== undefined)
233
+ normalized[key] = v;
234
+ }
235
+ if (Object.keys(normalized).length > 0)
236
+ out.org = normalized;
237
+ }
238
+ const birthday = maybeText(contact.birthday, "birthday", 32, helperName);
239
+ if (birthday !== undefined)
240
+ out.birthday = birthday;
241
+ return out;
242
+ }
243
+ export function buildSendContactsPayload(input) {
244
+ const record = asRecordInput(input, "sendContacts");
245
+ const contacts = mapValidatedArray(assertBoundedArray(record.contacts, "contacts", 1, GRAPH_MESSAGES_MAX_CONTACTS, "sendContacts"), (c) => normalizeContact(c, "sendContacts"));
246
+ return withReplyContext({ messaging_product: "whatsapp", to: assertValidRecipient(record.to, "sendContacts"), type: "contacts", contacts }, record);
247
+ }
248
+ export function buildSendReactionPayload(input) {
249
+ const record = asRecordInput(input, "sendReaction");
250
+ const emoji = assertNonEmptyControlFreeString(record.emoji, "emoji", 32, "sendReaction");
251
+ return {
252
+ messaging_product: "whatsapp",
253
+ to: assertValidRecipient(record.to, "sendReaction"),
254
+ type: "reaction",
255
+ reaction: {
256
+ message_id: assertNonEmptyControlFreeString(record.messageId, "messageId", GRAPH_MESSAGES_REPLY_TO_MESSAGE_ID_MAX_LENGTH, "sendReaction"),
257
+ emoji
258
+ }
259
+ };
260
+ }
261
+ export function buildRemoveReactionPayload(input) {
262
+ const record = asRecordInput(input, "removeReaction");
263
+ return {
264
+ messaging_product: "whatsapp",
265
+ to: assertValidRecipient(record.to, "removeReaction"),
266
+ type: "reaction",
267
+ reaction: {
268
+ message_id: assertNonEmptyControlFreeString(record.messageId, "messageId", GRAPH_MESSAGES_REPLY_TO_MESSAGE_ID_MAX_LENGTH, "removeReaction"),
269
+ emoji: ""
270
+ }
271
+ };
272
+ }
@@ -0,0 +1,11 @@
1
+ import type { GraphMessagesInteractivePayload, GraphMessagesMarkMessageAsReadInput, GraphMessagesRequestLocationInput, GraphMessagesSendButtonsInput, GraphMessagesSendCallPermissionRequestInput, GraphMessagesSendCatalogInput, GraphMessagesSendCtaUrlInput, GraphMessagesSendListInput, GraphMessagesSendProductInput, GraphMessagesSendProductsInput, GraphMessagesStatusPayload, GraphMessagesTypingIndicatorInput } from "./types.js";
2
+ export declare function buildSendButtonsPayload(input: GraphMessagesSendButtonsInput): GraphMessagesInteractivePayload;
3
+ export declare function buildSendListPayload(input: GraphMessagesSendListInput): GraphMessagesInteractivePayload;
4
+ export declare function buildSendCtaUrlPayload(input: GraphMessagesSendCtaUrlInput): GraphMessagesInteractivePayload;
5
+ export declare function buildSendCallPermissionRequestPayload(input: GraphMessagesSendCallPermissionRequestInput): GraphMessagesInteractivePayload;
6
+ export declare function buildSendProductPayload(input: GraphMessagesSendProductInput): GraphMessagesInteractivePayload;
7
+ export declare function buildSendProductsPayload(input: GraphMessagesSendProductsInput): GraphMessagesInteractivePayload;
8
+ export declare function buildSendCatalogPayload(input: GraphMessagesSendCatalogInput): GraphMessagesInteractivePayload;
9
+ export declare function buildRequestLocationPayload(input: GraphMessagesRequestLocationInput): GraphMessagesInteractivePayload;
10
+ export declare function buildMarkMessageAsReadPayload(input: GraphMessagesMarkMessageAsReadInput): GraphMessagesStatusPayload;
11
+ export declare function buildTypingIndicatorPayload(input: GraphMessagesTypingIndicatorInput): GraphMessagesStatusPayload;
@@ -0,0 +1,156 @@
1
+ import { GraphRequestValidationError } from "../../errors.js";
2
+ import {
3
+ GRAPH_MESSAGES_BUTTON_ID_MAX_LENGTH,
4
+ GRAPH_MESSAGES_BUTTON_TITLE_MAX_LENGTH,
5
+ GRAPH_MESSAGES_GENERAL_TEXT_MAX_LENGTH,
6
+ GRAPH_MESSAGES_MAX_LIST_ROWS,
7
+ GRAPH_MESSAGES_MAX_PRODUCT_ITEMS,
8
+ GRAPH_MESSAGES_MAX_REPLY_BUTTONS,
9
+ GRAPH_MESSAGES_MAX_LIST_SECTIONS,
10
+ GRAPH_MESSAGES_MEDIA_ID_MAX_LENGTH,
11
+ GRAPH_MESSAGES_REPLY_TO_MESSAGE_ID_MAX_LENGTH,
12
+ GRAPH_MESSAGES_ROW_DESCRIPTION_MAX_LENGTH,
13
+ GRAPH_MESSAGES_ROW_TITLE_MAX_LENGTH,
14
+ GRAPH_MESSAGES_SECTION_TITLE_MAX_LENGTH,
15
+ GRAPH_MESSAGES_SHORT_LABEL_MAX_LENGTH,
16
+ assertBoundedArray,
17
+ assertNonEmptyControlFreeString,
18
+ assertOnlyKnownKeys,
19
+ assertValidMediaLink,
20
+ assertValidRecipient,
21
+ asRecordInput,
22
+ isPlainOptionsObject,
23
+ mapValidatedArray,
24
+ maybeText,
25
+ withReplyContext
26
+ } from "./validation.js";
27
+ function interactiveBase(input, helperName, interactive) {
28
+ const record = asRecordInput(input, helperName);
29
+ return withReplyContext({ messaging_product: "whatsapp", to: assertValidRecipient(record.to, helperName), type: "interactive", interactive }, record);
30
+ }
31
+ function addHeaderFooter(target, record, helperName) {
32
+ const headerText = maybeText(record.headerText, "headerText", GRAPH_MESSAGES_SHORT_LABEL_MAX_LENGTH, helperName);
33
+ const footerText = maybeText(record.footerText, "footerText", GRAPH_MESSAGES_SHORT_LABEL_MAX_LENGTH, helperName);
34
+ if (headerText !== undefined)
35
+ target.header = { type: "text", text: headerText };
36
+ if (footerText !== undefined)
37
+ target.footer = { text: footerText };
38
+ }
39
+ export function buildSendButtonsPayload(input) {
40
+ const record = asRecordInput(input, "sendButtons");
41
+ const buttons = mapValidatedArray(assertBoundedArray(record.buttons, "buttons", 1, GRAPH_MESSAGES_MAX_REPLY_BUTTONS, "sendButtons"), (entry) => {
42
+ if (!isPlainOptionsObject(entry))
43
+ throw new GraphRequestValidationError("Invalid sendButtons input: button entries must be objects.");
44
+ const button = entry;
45
+ return { type: "reply", reply: { id: assertNonEmptyControlFreeString(button.id, "button.id", GRAPH_MESSAGES_BUTTON_ID_MAX_LENGTH, "sendButtons"), title: assertNonEmptyControlFreeString(button.title, "button.title", GRAPH_MESSAGES_BUTTON_TITLE_MAX_LENGTH, "sendButtons") } };
46
+ });
47
+ const interactive = { type: "button", body: { text: assertNonEmptyControlFreeString(record.bodyText, "bodyText", GRAPH_MESSAGES_GENERAL_TEXT_MAX_LENGTH, "sendButtons") }, action: { buttons } };
48
+ addHeaderFooter(interactive, record, "sendButtons");
49
+ return interactiveBase(input, "sendButtons", interactive);
50
+ }
51
+ export function buildSendListPayload(input) {
52
+ const record = asRecordInput(input, "sendList");
53
+ let totalRows = 0;
54
+ const sections = mapValidatedArray(assertBoundedArray(record.sections, "sections", 1, GRAPH_MESSAGES_MAX_LIST_SECTIONS, "sendList"), (entry) => {
55
+ if (!isPlainOptionsObject(entry))
56
+ throw new GraphRequestValidationError("Invalid sendList input: section entries must be objects.");
57
+ const section = entry;
58
+ const rows = mapValidatedArray(assertBoundedArray(section.rows, "section.rows", 1, GRAPH_MESSAGES_MAX_LIST_ROWS, "sendList"), (rowEntry) => {
59
+ totalRows += 1;
60
+ if (!isPlainOptionsObject(rowEntry))
61
+ throw new GraphRequestValidationError("Invalid sendList input: row entries must be objects.");
62
+ const row = rowEntry;
63
+ const normalized = { id: assertNonEmptyControlFreeString(row.id, "row.id", GRAPH_MESSAGES_BUTTON_ID_MAX_LENGTH, "sendList"), title: assertNonEmptyControlFreeString(row.title, "row.title", GRAPH_MESSAGES_ROW_TITLE_MAX_LENGTH, "sendList") };
64
+ const desc = maybeText(row.description, "row.description", GRAPH_MESSAGES_ROW_DESCRIPTION_MAX_LENGTH, "sendList");
65
+ if (desc !== undefined)
66
+ normalized.description = desc;
67
+ return normalized;
68
+ });
69
+ const normalized = { rows };
70
+ const title = maybeText(section.title, "section.title", GRAPH_MESSAGES_SECTION_TITLE_MAX_LENGTH, "sendList");
71
+ if (title !== undefined)
72
+ normalized.title = title;
73
+ return normalized;
74
+ });
75
+ if (totalRows > GRAPH_MESSAGES_MAX_LIST_ROWS)
76
+ throw new GraphRequestValidationError(`Invalid sendList input: total row count exceeds ${GRAPH_MESSAGES_MAX_LIST_ROWS}.`);
77
+ const interactive = { type: "list", body: { text: assertNonEmptyControlFreeString(record.bodyText, "bodyText", GRAPH_MESSAGES_GENERAL_TEXT_MAX_LENGTH, "sendList") }, action: { button: assertNonEmptyControlFreeString(record.buttonText, "buttonText", GRAPH_MESSAGES_BUTTON_TITLE_MAX_LENGTH, "sendList"), sections } };
78
+ addHeaderFooter(interactive, record, "sendList");
79
+ return interactiveBase(input, "sendList", interactive);
80
+ }
81
+ export function buildSendCtaUrlPayload(input) {
82
+ const record = asRecordInput(input, "sendCtaUrl");
83
+ const interactive = { type: "cta_url", body: { text: assertNonEmptyControlFreeString(record.bodyText, "bodyText", GRAPH_MESSAGES_GENERAL_TEXT_MAX_LENGTH, "sendCtaUrl") }, action: { name: "cta_url", parameters: { display_text: assertNonEmptyControlFreeString(record.displayText, "displayText", GRAPH_MESSAGES_BUTTON_TITLE_MAX_LENGTH, "sendCtaUrl"), url: assertValidMediaLink(record.url, "sendCtaUrl") } } };
84
+ addHeaderFooter(interactive, record, "sendCtaUrl");
85
+ return interactiveBase(input, "sendCtaUrl", interactive);
86
+ }
87
+ export function buildSendCallPermissionRequestPayload(input) {
88
+ const record = asRecordInput(input, "sendCallPermissionRequest");
89
+ assertOnlyKnownKeys(record, ["to", "bodyText", "footerText", "replyToMessageId"], "sendCallPermissionRequest");
90
+ const interactive = {
91
+ type: "call_permission_request",
92
+ body: { text: assertNonEmptyControlFreeString(record.bodyText, "bodyText", GRAPH_MESSAGES_GENERAL_TEXT_MAX_LENGTH, "sendCallPermissionRequest") },
93
+ action: { name: "call_permission_request" }
94
+ };
95
+ const footerText = maybeText(record.footerText, "footerText", GRAPH_MESSAGES_SHORT_LABEL_MAX_LENGTH, "sendCallPermissionRequest");
96
+ if (footerText !== undefined)
97
+ interactive.footer = { text: footerText };
98
+ return interactiveBase(input, "sendCallPermissionRequest", interactive);
99
+ }
100
+ export function buildSendProductPayload(input) {
101
+ const record = asRecordInput(input, "sendProduct");
102
+ const interactive = { type: "product", action: { catalog_id: assertNonEmptyControlFreeString(record.catalogId, "catalogId", GRAPH_MESSAGES_MEDIA_ID_MAX_LENGTH, "sendProduct"), product_retailer_id: assertNonEmptyControlFreeString(record.productRetailerId, "productRetailerId", GRAPH_MESSAGES_MEDIA_ID_MAX_LENGTH, "sendProduct") } };
103
+ const bodyText = maybeText(record.bodyText, "bodyText", GRAPH_MESSAGES_GENERAL_TEXT_MAX_LENGTH, "sendProduct");
104
+ const footerText = maybeText(record.footerText, "footerText", GRAPH_MESSAGES_SHORT_LABEL_MAX_LENGTH, "sendProduct");
105
+ if (bodyText !== undefined)
106
+ interactive.body = { text: bodyText };
107
+ if (footerText !== undefined)
108
+ interactive.footer = { text: footerText };
109
+ return interactiveBase(input, "sendProduct", interactive);
110
+ }
111
+ export function buildSendProductsPayload(input) {
112
+ const record = asRecordInput(input, "sendProducts");
113
+ let totalItems = 0;
114
+ const sections = mapValidatedArray(assertBoundedArray(record.sections, "sections", 1, 10, "sendProducts"), (entry) => {
115
+ if (!isPlainOptionsObject(entry))
116
+ throw new GraphRequestValidationError("Invalid sendProducts input: section entries must be objects.");
117
+ const section = entry;
118
+ const productItems = mapValidatedArray(assertBoundedArray(section.productItems, "section.productItems", 1, GRAPH_MESSAGES_MAX_PRODUCT_ITEMS, "sendProducts"), (itemEntry) => {
119
+ totalItems += 1;
120
+ if (!isPlainOptionsObject(itemEntry))
121
+ throw new GraphRequestValidationError("Invalid sendProducts input: product item entries must be objects.");
122
+ const item = itemEntry;
123
+ return { product_retailer_id: assertNonEmptyControlFreeString(item.productRetailerId, "productRetailerId", GRAPH_MESSAGES_MEDIA_ID_MAX_LENGTH, "sendProducts") };
124
+ });
125
+ return { title: assertNonEmptyControlFreeString(section.title, "section.title", GRAPH_MESSAGES_SECTION_TITLE_MAX_LENGTH, "sendProducts"), product_items: productItems };
126
+ });
127
+ if (totalItems > GRAPH_MESSAGES_MAX_PRODUCT_ITEMS)
128
+ throw new GraphRequestValidationError(`Invalid sendProducts input: total product item count exceeds ${GRAPH_MESSAGES_MAX_PRODUCT_ITEMS}.`);
129
+ const interactive = { type: "product_list", header: { type: "text", text: assertNonEmptyControlFreeString(record.headerText, "headerText", GRAPH_MESSAGES_SHORT_LABEL_MAX_LENGTH, "sendProducts") }, body: { text: assertNonEmptyControlFreeString(record.bodyText, "bodyText", GRAPH_MESSAGES_GENERAL_TEXT_MAX_LENGTH, "sendProducts") }, action: { catalog_id: assertNonEmptyControlFreeString(record.catalogId, "catalogId", GRAPH_MESSAGES_MEDIA_ID_MAX_LENGTH, "sendProducts"), sections } };
130
+ const footerText = maybeText(record.footerText, "footerText", GRAPH_MESSAGES_SHORT_LABEL_MAX_LENGTH, "sendProducts");
131
+ if (footerText !== undefined)
132
+ interactive.footer = { text: footerText };
133
+ return interactiveBase(input, "sendProducts", interactive);
134
+ }
135
+ export function buildSendCatalogPayload(input) {
136
+ const record = asRecordInput(input, "sendCatalog");
137
+ const action = { name: "catalog_message" };
138
+ const thumbnail = maybeText(record.thumbnailProductRetailerId, "thumbnailProductRetailerId", GRAPH_MESSAGES_MEDIA_ID_MAX_LENGTH, "sendCatalog");
139
+ if (thumbnail !== undefined)
140
+ action.parameters = { thumbnail_product_retailer_id: thumbnail };
141
+ const interactive = { type: "catalog_message", body: { text: assertNonEmptyControlFreeString(record.bodyText, "bodyText", GRAPH_MESSAGES_GENERAL_TEXT_MAX_LENGTH, "sendCatalog") }, action };
142
+ addHeaderFooter(interactive, record, "sendCatalog");
143
+ return interactiveBase(input, "sendCatalog", interactive);
144
+ }
145
+ export function buildRequestLocationPayload(input) {
146
+ const record = asRecordInput(input, "requestLocation");
147
+ return interactiveBase(input, "requestLocation", { type: "location_request_message", body: { text: assertNonEmptyControlFreeString(record.bodyText, "bodyText", GRAPH_MESSAGES_GENERAL_TEXT_MAX_LENGTH, "requestLocation") }, action: { name: "send_location" } });
148
+ }
149
+ export function buildMarkMessageAsReadPayload(input) {
150
+ const record = asRecordInput(input, "markMessageAsRead");
151
+ return { messaging_product: "whatsapp", status: "read", message_id: assertNonEmptyControlFreeString(record.messageId, "messageId", GRAPH_MESSAGES_REPLY_TO_MESSAGE_ID_MAX_LENGTH, "markMessageAsRead") };
152
+ }
153
+ export function buildTypingIndicatorPayload(input) {
154
+ const payload = buildMarkMessageAsReadPayload(input);
155
+ return { ...payload, typing_indicator: { type: "text" } };
156
+ }
@@ -0,0 +1,3 @@
1
+ import type { GraphMessagesMarketingTemplatePayload, GraphMessagesSendMarketingTemplateInput, GraphMessagesSendTemplateInput, GraphMessagesTemplatePayload } from "./types.js";
2
+ export declare function buildSendTemplatePayload(input: GraphMessagesSendTemplateInput): GraphMessagesTemplatePayload;
3
+ export declare function buildSendMarketingTemplatePayload(input: GraphMessagesSendMarketingTemplateInput): GraphMessagesMarketingTemplatePayload;
@@ -0,0 +1,121 @@
1
+ import { GraphRequestValidationError } from "../../errors.js";
2
+ import {
3
+ GRAPH_MESSAGES_MEDIA_ID_MAX_LENGTH,
4
+ GRAPH_MESSAGES_SHORT_LABEL_MAX_LENGTH,
5
+ assertBoundedArray,
6
+ assertNonEmptyControlFreeString,
7
+ assertOnlyKnownKeys,
8
+ assertValidRecipient,
9
+ asRecordInput,
10
+ inspectTemplateValue,
11
+ isPlainOptionsObject,
12
+ withReplyContext,
13
+ mapValidatedArray,
14
+ sanitizeTemplateParameter
15
+ } from "./validation.js";
16
+ function normalizeTemplateComponent(value, helperName) {
17
+ const cloned = sanitizeTemplateParameter(value, helperName, "component", new WeakSet);
18
+ if (!isPlainOptionsObject(cloned))
19
+ throw new GraphRequestValidationError(`Invalid ${helperName} input: component entries must be objects.`);
20
+ const component = cloned;
21
+ const out = { type: assertNonEmptyControlFreeString(component.type, "component.type", GRAPH_MESSAGES_SHORT_LABEL_MAX_LENGTH, helperName) };
22
+ if (component.subType !== undefined)
23
+ out.sub_type = assertNonEmptyControlFreeString(component.subType, "component.subType", GRAPH_MESSAGES_SHORT_LABEL_MAX_LENGTH, helperName);
24
+ if (component.index !== undefined)
25
+ out.index = assertNonEmptyControlFreeString(component.index, "component.index", 8, helperName);
26
+ if (component.parameters !== undefined) {
27
+ const parameters = assertBoundedArray(component.parameters, "component.parameters", 0, 100, helperName);
28
+ out.parameters = mapValidatedArray(parameters, (parameter, index) => {
29
+ if (!isPlainOptionsObject(parameter))
30
+ throw new GraphRequestValidationError(`Invalid ${helperName} input: component parameters must be objects.`);
31
+ return sanitizeTemplateParameter(parameter, helperName, `component.parameters[${index}]`, new WeakSet);
32
+ });
33
+ }
34
+ return out;
35
+ }
36
+ export function buildSendTemplatePayload(input) {
37
+ const record = asRecordInput(input, "sendTemplate");
38
+ const template = { name: assertNonEmptyControlFreeString(record.name, "name", GRAPH_MESSAGES_MEDIA_ID_MAX_LENGTH, "sendTemplate"), language: { code: assertNonEmptyControlFreeString(record.languageCode, "languageCode", GRAPH_MESSAGES_SHORT_LABEL_MAX_LENGTH, "sendTemplate") } };
39
+ if (record.components !== undefined)
40
+ template.components = mapValidatedArray(assertBoundedArray(record.components, "components", 0, 100, "sendTemplate"), (c) => normalizeTemplateComponent(c, "sendTemplate"));
41
+ return withReplyContext({ messaging_product: "whatsapp", to: assertValidRecipient(record.to, "sendTemplate"), type: "template", template }, record);
42
+ }
43
+ function assertMarketingRecipient(value) {
44
+ if (value === undefined)
45
+ return;
46
+ return assertNonEmptyControlFreeString(value, "recipient", GRAPH_MESSAGES_MEDIA_ID_MAX_LENGTH, "sendMarketingTemplate");
47
+ }
48
+ function buildMarketingTemplateObject(record) {
49
+ const name = record.name;
50
+ const languageCode = record.languageCode;
51
+ const components = record.components;
52
+ const template = {
53
+ name: assertNonEmptyControlFreeString(name, "name", GRAPH_MESSAGES_MEDIA_ID_MAX_LENGTH, "sendMarketingTemplate"),
54
+ language: { code: assertNonEmptyControlFreeString(languageCode, "languageCode", GRAPH_MESSAGES_SHORT_LABEL_MAX_LENGTH, "sendMarketingTemplate") }
55
+ };
56
+ if (components !== undefined) {
57
+ template.components = mapValidatedArray(assertBoundedArray(components, "components", 0, 100, "sendMarketingTemplate"), (c) => normalizeTemplateComponent(c, "sendMarketingTemplate"));
58
+ }
59
+ return template;
60
+ }
61
+ function copySendMarketingTemplateInput(input) {
62
+ if (typeof input !== "object" || input === null) {
63
+ throw new GraphRequestValidationError("Invalid sendMarketingTemplate input: expected an options object.");
64
+ }
65
+ const record = input;
66
+ const isArrayInput = inspectTemplateValue("sendMarketingTemplate", "input", () => Array.isArray(record));
67
+ if (isArrayInput) {
68
+ throw new GraphRequestValidationError("Invalid sendMarketingTemplate input: expected an options object.");
69
+ }
70
+ const proto = inspectTemplateValue("sendMarketingTemplate", "input", () => Object.getPrototypeOf(record));
71
+ if (proto !== Object.prototype && proto !== null) {
72
+ throw new GraphRequestValidationError("Invalid sendMarketingTemplate input: expected a plain options object.");
73
+ }
74
+ const descriptors = inspectTemplateValue("sendMarketingTemplate", "input", () => Object.getOwnPropertyDescriptors(record));
75
+ if (Object.prototype.hasOwnProperty.call(descriptors, "toJSON") || inspectTemplateValue("sendMarketingTemplate", "input", () => ("toJSON" in record))) {
76
+ throw new GraphRequestValidationError("Invalid sendMarketingTemplate input: input must not define toJSON.");
77
+ }
78
+ const out = {};
79
+ for (const [key, descriptor] of Object.entries(descriptors)) {
80
+ if (typeof descriptor.get === "function" || typeof descriptor.set === "function") {
81
+ throw new GraphRequestValidationError(`Invalid sendMarketingTemplate input: ${key} must not use accessors.`);
82
+ }
83
+ if (descriptor.value !== undefined)
84
+ out[key] = descriptor.value;
85
+ }
86
+ assertOnlyKnownKeys(out, ["to", "recipient", "name", "languageCode", "components", "productPolicy", "messageActivitySharing"], "sendMarketingTemplate");
87
+ return out;
88
+ }
89
+ export function buildSendMarketingTemplatePayload(input) {
90
+ const record = copySendMarketingTemplateInput(input);
91
+ const to = record.to === undefined ? undefined : assertValidRecipient(record.to, "sendMarketingTemplate");
92
+ const recipient = assertMarketingRecipient(record.recipient);
93
+ if (to === undefined && recipient === undefined) {
94
+ throw new GraphRequestValidationError("Invalid sendMarketingTemplate input: at least one of to or recipient is required.");
95
+ }
96
+ const payload = {
97
+ messaging_product: "whatsapp",
98
+ recipient_type: "individual",
99
+ type: "template",
100
+ template: buildMarketingTemplateObject(record)
101
+ };
102
+ if (to !== undefined)
103
+ payload.to = to;
104
+ if (recipient !== undefined)
105
+ payload.recipient = recipient;
106
+ const productPolicy = record.productPolicy;
107
+ if (productPolicy !== undefined) {
108
+ if (productPolicy !== "CLOUD_API_FALLBACK" && productPolicy !== "STRICT") {
109
+ throw new GraphRequestValidationError("Invalid sendMarketingTemplate input: productPolicy must be CLOUD_API_FALLBACK or STRICT.");
110
+ }
111
+ payload.product_policy = productPolicy;
112
+ }
113
+ const messageActivitySharing = record.messageActivitySharing;
114
+ if (messageActivitySharing !== undefined) {
115
+ if (typeof messageActivitySharing !== "boolean") {
116
+ throw new GraphRequestValidationError("Invalid sendMarketingTemplate input: messageActivitySharing must be a boolean when provided.");
117
+ }
118
+ payload.message_activity_sharing = messageActivitySharing;
119
+ }
120
+ return payload;
121
+ }
@@ -0,0 +1,31 @@
1
+ import type { GraphClient, GraphRequestOptions } from "../../client.js";
2
+ import { type EndpointInvokeOptions } from "../../endpoint.js";
3
+ import type { GraphMessagesMarketingTemplateResponse, GraphMessagesSendBody, GraphMessagesSendMarketingTemplateInput, GraphMessagesSendMessageInput, GraphMessagesSendResponse, GraphMessagesTextPayload } from "./types.js";
4
+ interface GraphRequestExecutor {
5
+ request<TResponse>(options: GraphRequestOptions): Promise<TResponse>;
6
+ }
7
+ export declare function sendMarketingTemplate(client: GraphClient, params: {
8
+ phoneNumberId: string;
9
+ }, body: GraphMessagesSendMarketingTemplateInput, opts?: EndpointInvokeOptions): Promise<GraphMessagesMarketingTemplateResponse>;
10
+ export declare namespace sendMarketingTemplate {
11
+ var definition: import("../../endpoint.js").EndpointDefinition<{
12
+ phoneNumberId: string;
13
+ }, GraphMessagesSendMarketingTemplateInput, GraphMessagesMarketingTemplateResponse>;
14
+ }
15
+ export declare function buildSendMessagePayload(input: GraphMessagesSendMessageInput): GraphMessagesTextPayload;
16
+ /**
17
+ * `sendMessage` — Graph `POST /{phoneNumberId}/messages`.
18
+ *
19
+ * Built via `defineEndpoint`, so path-template parsing, param validation,
20
+ * control-char rejection, body passthrough, and F-5 error registry
21
+ * routing are handled uniformly.
22
+ */
23
+ export declare const sendMessage: import("../../endpoint.js").EndpointCallable<{
24
+ phoneNumberId: string;
25
+ }, GraphMessagesSendBody, GraphMessagesSendResponse>;
26
+ export declare class GraphMessagesEndpoint {
27
+ private readonly requestExecutor;
28
+ constructor(requestExecutor: GraphRequestExecutor);
29
+ sendMessage(input: GraphMessagesSendMessageInput): Promise<GraphMessagesSendResponse>;
30
+ }
31
+ export {};
@@ -0,0 +1,57 @@
1
+ export class GraphMessagesEndpoint {
2
+ requestExecutor;
3
+ constructor(requestExecutor) {
4
+ this.requestExecutor = requestExecutor;
5
+ }
6
+ async sendMessage(input) {
7
+ const phoneNumberId = normalizePhoneNumberId(input.phoneNumberId);
8
+ if (typeof this.requestExecutor.request !== "function") {
9
+ throw new GraphRequestValidationError("Invalid GraphMessagesEndpoint: requestExecutor must expose a request() method.");
10
+ }
11
+ return sendMessage(this.requestExecutor, { phoneNumberId }, buildSendMessagePayload({ ...input, phoneNumberId }));
12
+ }
13
+ }
14
+ import { defineEndpoint } from "../../endpoint.js";
15
+ import { GraphRequestValidationError } from "../../errors.js";
16
+ import { buildSendMarketingTemplatePayload } from "./builders-template.js";
17
+ const sendMarketingTemplateEndpoint = defineEndpoint({
18
+ method: "POST",
19
+ pathTemplate: "/{phoneNumberId}/marketing_messages",
20
+ params: { phoneNumberId: { in: "path", required: true } },
21
+ bodyContentType: "application/json",
22
+ buildBody: buildSendMarketingTemplatePayload
23
+ });
24
+ export async function sendMarketingTemplate(client, params, body, opts) {
25
+ if (body === undefined) {
26
+ throw new GraphRequestValidationError("Invalid sendMarketingTemplate input: body is required.");
27
+ }
28
+ return sendMarketingTemplateEndpoint(client, params, body, opts);
29
+ }
30
+ sendMarketingTemplate.definition = sendMarketingTemplateEndpoint.definition;
31
+ export function buildSendMessagePayload(input) {
32
+ const payload = {
33
+ messaging_product: "whatsapp",
34
+ to: input.to,
35
+ type: "text",
36
+ text: {
37
+ body: input.text
38
+ }
39
+ };
40
+ if (typeof input.previewUrl === "boolean") {
41
+ payload.text.preview_url = input.previewUrl;
42
+ }
43
+ return payload;
44
+ }
45
+ function normalizePhoneNumberId(phoneNumberId) {
46
+ const normalized = phoneNumberId.trim();
47
+ if (!/^\d+$/.test(normalized)) {
48
+ throw new GraphRequestValidationError("Invalid phoneNumberId. Expected a numeric Graph phone number ID path segment.");
49
+ }
50
+ return normalized;
51
+ }
52
+ export const sendMessage = defineEndpoint({
53
+ method: "POST",
54
+ pathTemplate: "/{phoneNumberId}/messages",
55
+ params: { phoneNumberId: { in: "path", required: true } },
56
+ bodyContentType: "application/json"
57
+ });