@wats/graph 0.3.2 → 0.3.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/dist/endpoints/businessManagement.d.ts +144 -0
- package/dist/endpoints/businessManagement.js +273 -1
- package/dist/endpoints/messages/builders-basic.d.ts +11 -0
- package/dist/endpoints/messages/builders-basic.js +272 -0
- package/dist/endpoints/messages/builders-interactive.d.ts +11 -0
- package/dist/endpoints/messages/builders-interactive.js +156 -0
- package/dist/endpoints/messages/builders-template.d.ts +3 -0
- package/dist/endpoints/messages/builders-template.js +121 -0
- package/dist/endpoints/messages/callables.d.ts +31 -0
- package/dist/endpoints/messages/callables.js +57 -0
- package/dist/endpoints/messages/index.d.ts +6 -0
- package/dist/endpoints/messages/index.js +56 -0
- package/dist/endpoints/messages/types.d.ts +363 -0
- package/dist/endpoints/messages/types.js +0 -0
- package/dist/endpoints/messages/validation.d.ts +43 -0
- package/dist/endpoints/messages/validation.js +277 -0
- package/dist/endpoints/messages.d.ts +1 -375
- package/dist/endpoints/messages.js +1 -675
- package/dist/endpoints/templates/builders.js +23 -0
- package/dist/endpoints/templates/callables.d.ts +1 -0
- package/dist/endpoints/templates/callables.js +4 -2
- package/dist/endpoints/templates/groups.d.ts +126 -0
- package/dist/endpoints/templates/groups.js +156 -0
- package/dist/endpoints/templates/index.d.ts +3 -1
- package/dist/endpoints/templates/index.js +8 -0
- package/dist/endpoints/templates/types.d.ts +14 -1
- package/dist/endpoints/templates.d.ts +2 -2
- package/dist/endpoints/templates.js +6 -0
- package/dist/endpoints/waba/phoneNumbers.d.ts +2 -0
- package/dist/endpoints/wabaEndpoints.d.ts +2 -2
- package/dist/endpoints/wabaEndpoints.js +6 -0
- package/dist/errorSubclasses.d.ts +28 -0
- package/dist/errorSubclasses.js +63 -0
- package/dist/subclients/phoneNumberClient.d.ts +18 -2
- package/dist/subclients/phoneNumberClient.js +72 -1
- package/dist/subclients/wabaClient.d.ts +15 -1
- package/dist/subclients/wabaClient.js +33 -0
- package/package.json +10 -1
|
@@ -1,675 +1 @@
|
|
|
1
|
-
export
|
|
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
|
-
export const GRAPH_MESSAGES_TEXT_BODY_MAX_LENGTH = 4096;
|
|
17
|
-
export const GRAPH_MESSAGES_RECIPIENT_MAX_DIGITS = 15;
|
|
18
|
-
export const GRAPH_MESSAGES_REPLY_TO_MESSAGE_ID_MAX_LENGTH = 256;
|
|
19
|
-
export const GRAPH_MESSAGES_MEDIA_ID_MAX_LENGTH = 2048;
|
|
20
|
-
export const GRAPH_MESSAGES_MEDIA_LINK_MAX_LENGTH = 2048;
|
|
21
|
-
export const GRAPH_MESSAGES_MEDIA_CAPTION_MAX_LENGTH = 1024;
|
|
22
|
-
export const GRAPH_MESSAGES_DOCUMENT_FILENAME_MAX_LENGTH = 256;
|
|
23
|
-
export const GRAPH_MESSAGES_GENERAL_TEXT_MAX_LENGTH = 1024;
|
|
24
|
-
export const GRAPH_MESSAGES_SHORT_LABEL_MAX_LENGTH = 60;
|
|
25
|
-
export const GRAPH_MESSAGES_BUTTON_TITLE_MAX_LENGTH = 20;
|
|
26
|
-
export const GRAPH_MESSAGES_BUTTON_ID_MAX_LENGTH = 256;
|
|
27
|
-
export const GRAPH_MESSAGES_SECTION_TITLE_MAX_LENGTH = 24;
|
|
28
|
-
export const GRAPH_MESSAGES_ROW_TITLE_MAX_LENGTH = 24;
|
|
29
|
-
export const GRAPH_MESSAGES_ROW_DESCRIPTION_MAX_LENGTH = 72;
|
|
30
|
-
export const GRAPH_MESSAGES_MAX_REPLY_BUTTONS = 3;
|
|
31
|
-
export const GRAPH_MESSAGES_MAX_LIST_SECTIONS = 10;
|
|
32
|
-
export const GRAPH_MESSAGES_MAX_LIST_ROWS = 10;
|
|
33
|
-
export const GRAPH_MESSAGES_MAX_CONTACTS = 257;
|
|
34
|
-
export const GRAPH_MESSAGES_MAX_PRODUCT_ITEMS = 30;
|
|
35
|
-
function isPlainOptionsObject(value) {
|
|
36
|
-
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
37
|
-
}
|
|
38
|
-
function hasControlChar(value) {
|
|
39
|
-
for (let i = 0;i < value.length; i += 1) {
|
|
40
|
-
const code = value.charCodeAt(i);
|
|
41
|
-
if (code < 32 || code === 127)
|
|
42
|
-
return true;
|
|
43
|
-
}
|
|
44
|
-
return false;
|
|
45
|
-
}
|
|
46
|
-
function assertValidRecipient(to, helperName = "sendText") {
|
|
47
|
-
if (typeof to !== "string") {
|
|
48
|
-
throw new GraphRequestValidationError(`Invalid ${helperName} input: to must be a phone-number string.`);
|
|
49
|
-
}
|
|
50
|
-
if (to.length === 0 || to.trim().length === 0) {
|
|
51
|
-
throw new GraphRequestValidationError(`Invalid ${helperName} input: to must be a non-empty phone-number string.`);
|
|
52
|
-
}
|
|
53
|
-
if (hasControlChar(to)) {
|
|
54
|
-
throw new GraphRequestValidationError(`Invalid ${helperName} input: to must not contain control characters (CR/LF/NUL/etc.).`);
|
|
55
|
-
}
|
|
56
|
-
if (to.includes("/") || to.includes("\\") || to.includes("?") || to.includes("#") || to.includes(":") || to.includes("@")) {
|
|
57
|
-
throw new GraphRequestValidationError(`Invalid ${helperName} input: to must be a phone-number string, not a path, URL, or address.`);
|
|
58
|
-
}
|
|
59
|
-
if (!/^\+?\d{1,15}$/.test(to)) {
|
|
60
|
-
throw new GraphRequestValidationError(`Invalid ${helperName} input: to must be E.164-ish digits with optional leading + and at most ${GRAPH_MESSAGES_RECIPIENT_MAX_DIGITS} digits.`);
|
|
61
|
-
}
|
|
62
|
-
return to;
|
|
63
|
-
}
|
|
64
|
-
function assertValidText(text) {
|
|
65
|
-
if (typeof text !== "string") {
|
|
66
|
-
throw new GraphRequestValidationError("Invalid sendText input: text must be a string.");
|
|
67
|
-
}
|
|
68
|
-
if (text.length === 0 || text.trim().length === 0) {
|
|
69
|
-
throw new GraphRequestValidationError("Invalid sendText input: text must be a non-empty string.");
|
|
70
|
-
}
|
|
71
|
-
if (text.length > GRAPH_MESSAGES_TEXT_BODY_MAX_LENGTH) {
|
|
72
|
-
throw new GraphRequestValidationError(`Invalid sendText input: text exceeds ${GRAPH_MESSAGES_TEXT_BODY_MAX_LENGTH}-character limit.`);
|
|
73
|
-
}
|
|
74
|
-
return text;
|
|
75
|
-
}
|
|
76
|
-
function assertValidReplyToMessageId(value) {
|
|
77
|
-
if (typeof value !== "string") {
|
|
78
|
-
throw new GraphRequestValidationError("Invalid sendText input: replyToMessageId must be a string when provided.");
|
|
79
|
-
}
|
|
80
|
-
if (value.length === 0 || value.trim().length === 0) {
|
|
81
|
-
throw new GraphRequestValidationError("Invalid sendText input: replyToMessageId must be non-empty when provided.");
|
|
82
|
-
}
|
|
83
|
-
if (hasControlChar(value)) {
|
|
84
|
-
throw new GraphRequestValidationError("Invalid sendText input: replyToMessageId must not contain control characters (CR/LF/NUL/etc.).");
|
|
85
|
-
}
|
|
86
|
-
if (value.length > GRAPH_MESSAGES_REPLY_TO_MESSAGE_ID_MAX_LENGTH) {
|
|
87
|
-
throw new GraphRequestValidationError(`Invalid sendText input: replyToMessageId exceeds ${GRAPH_MESSAGES_REPLY_TO_MESSAGE_ID_MAX_LENGTH}-character limit.`);
|
|
88
|
-
}
|
|
89
|
-
return value;
|
|
90
|
-
}
|
|
91
|
-
export function buildSendTextPayload(input) {
|
|
92
|
-
if (!isPlainOptionsObject(input)) {
|
|
93
|
-
throw new GraphRequestValidationError("Invalid sendText input: expected an options object.");
|
|
94
|
-
}
|
|
95
|
-
const to = assertValidRecipient(input.to);
|
|
96
|
-
const text = assertValidText(input.text);
|
|
97
|
-
if (input.previewUrl !== undefined && typeof input.previewUrl !== "boolean") {
|
|
98
|
-
throw new GraphRequestValidationError("Invalid sendText input: previewUrl must be a boolean when provided.");
|
|
99
|
-
}
|
|
100
|
-
const payload = {
|
|
101
|
-
messaging_product: "whatsapp",
|
|
102
|
-
to,
|
|
103
|
-
type: "text",
|
|
104
|
-
text: {
|
|
105
|
-
body: text
|
|
106
|
-
}
|
|
107
|
-
};
|
|
108
|
-
if (input.previewUrl !== undefined) {
|
|
109
|
-
payload.text.preview_url = input.previewUrl;
|
|
110
|
-
}
|
|
111
|
-
if (input.replyToMessageId !== undefined) {
|
|
112
|
-
payload.context = {
|
|
113
|
-
message_id: assertValidReplyToMessageId(input.replyToMessageId)
|
|
114
|
-
};
|
|
115
|
-
}
|
|
116
|
-
return payload;
|
|
117
|
-
}
|
|
118
|
-
function assertNonEmptyControlFreeString(value, fieldName, maxLength, helperName) {
|
|
119
|
-
if (typeof value !== "string") {
|
|
120
|
-
throw new GraphRequestValidationError(`Invalid ${helperName} input: ${fieldName} must be a string when provided.`);
|
|
121
|
-
}
|
|
122
|
-
if (value.length === 0 || value.trim().length === 0) {
|
|
123
|
-
throw new GraphRequestValidationError(`Invalid ${helperName} input: ${fieldName} must be non-empty when provided.`);
|
|
124
|
-
}
|
|
125
|
-
if (hasControlChar(value)) {
|
|
126
|
-
throw new GraphRequestValidationError(`Invalid ${helperName} input: ${fieldName} must not contain control characters (CR/LF/NUL/etc.).`);
|
|
127
|
-
}
|
|
128
|
-
if (value.length > maxLength) {
|
|
129
|
-
throw new GraphRequestValidationError(`Invalid ${helperName} input: ${fieldName} exceeds ${maxLength}-character limit.`);
|
|
130
|
-
}
|
|
131
|
-
return value;
|
|
132
|
-
}
|
|
133
|
-
function assertValidMediaLink(value, helperName) {
|
|
134
|
-
const link = assertNonEmptyControlFreeString(value, "link", GRAPH_MESSAGES_MEDIA_LINK_MAX_LENGTH, helperName);
|
|
135
|
-
if (/\s/.test(link)) {
|
|
136
|
-
throw new GraphRequestValidationError(`Invalid ${helperName} input: link must not contain whitespace.`);
|
|
137
|
-
}
|
|
138
|
-
let url;
|
|
139
|
-
try {
|
|
140
|
-
url = new URL(link);
|
|
141
|
-
} catch {
|
|
142
|
-
throw new GraphRequestValidationError(`Invalid ${helperName} input: link must be a valid http(s) URL.`);
|
|
143
|
-
}
|
|
144
|
-
if (url.protocol !== "http:" && url.protocol !== "https:") {
|
|
145
|
-
throw new GraphRequestValidationError(`Invalid ${helperName} input: link must use http or https.`);
|
|
146
|
-
}
|
|
147
|
-
return link;
|
|
148
|
-
}
|
|
149
|
-
function assertNoUnsupportedMediaFields(input, helperName, options) {
|
|
150
|
-
if (!options.caption && input.caption !== undefined) {
|
|
151
|
-
throw new GraphRequestValidationError(`Invalid ${helperName} input: caption is not supported for this media type.`);
|
|
152
|
-
}
|
|
153
|
-
if (!options.filename && input.filename !== undefined) {
|
|
154
|
-
throw new GraphRequestValidationError(`Invalid ${helperName} input: filename is only supported for document messages.`);
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
function buildSendMediaPayload(input, mediaType, helperName, options) {
|
|
158
|
-
if (!isPlainOptionsObject(input)) {
|
|
159
|
-
throw new GraphRequestValidationError(`Invalid ${helperName} input: expected an options object.`);
|
|
160
|
-
}
|
|
161
|
-
const record = input;
|
|
162
|
-
assertNoUnsupportedMediaFields(record, helperName, options);
|
|
163
|
-
const to = assertValidRecipient(record.to, helperName);
|
|
164
|
-
const hasMediaId = record.mediaId !== undefined;
|
|
165
|
-
const hasLink = record.link !== undefined;
|
|
166
|
-
if (hasMediaId === hasLink) {
|
|
167
|
-
throw new GraphRequestValidationError(`Invalid ${helperName} input: exactly one of mediaId or link is required.`);
|
|
168
|
-
}
|
|
169
|
-
const media = {};
|
|
170
|
-
if (hasMediaId) {
|
|
171
|
-
media.id = assertNonEmptyControlFreeString(record.mediaId, "mediaId", GRAPH_MESSAGES_MEDIA_ID_MAX_LENGTH, helperName);
|
|
172
|
-
} else {
|
|
173
|
-
media.link = assertValidMediaLink(record.link, helperName);
|
|
174
|
-
}
|
|
175
|
-
if (record.caption !== undefined) {
|
|
176
|
-
media.caption = assertNonEmptyControlFreeString(record.caption, "caption", GRAPH_MESSAGES_MEDIA_CAPTION_MAX_LENGTH, helperName);
|
|
177
|
-
}
|
|
178
|
-
if (record.filename !== undefined) {
|
|
179
|
-
media.filename = assertNonEmptyControlFreeString(record.filename, "filename", GRAPH_MESSAGES_DOCUMENT_FILENAME_MAX_LENGTH, helperName);
|
|
180
|
-
}
|
|
181
|
-
const payload = {
|
|
182
|
-
messaging_product: "whatsapp",
|
|
183
|
-
to,
|
|
184
|
-
type: mediaType,
|
|
185
|
-
[mediaType]: media
|
|
186
|
-
};
|
|
187
|
-
if (record.replyToMessageId !== undefined) {
|
|
188
|
-
payload.context = {
|
|
189
|
-
message_id: assertValidReplyToMessageId(record.replyToMessageId)
|
|
190
|
-
};
|
|
191
|
-
}
|
|
192
|
-
return payload;
|
|
193
|
-
}
|
|
194
|
-
export function buildSendImagePayload(input) {
|
|
195
|
-
return buildSendMediaPayload(input, "image", "sendImage", {
|
|
196
|
-
caption: true,
|
|
197
|
-
filename: false
|
|
198
|
-
});
|
|
199
|
-
}
|
|
200
|
-
export function buildSendVideoPayload(input) {
|
|
201
|
-
return buildSendMediaPayload(input, "video", "sendVideo", {
|
|
202
|
-
caption: true,
|
|
203
|
-
filename: false
|
|
204
|
-
});
|
|
205
|
-
}
|
|
206
|
-
export function buildSendAudioPayload(input) {
|
|
207
|
-
return buildSendMediaPayload(input, "audio", "sendAudio", {
|
|
208
|
-
caption: false,
|
|
209
|
-
filename: false
|
|
210
|
-
});
|
|
211
|
-
}
|
|
212
|
-
export function buildSendDocumentPayload(input) {
|
|
213
|
-
return buildSendMediaPayload(input, "document", "sendDocument", {
|
|
214
|
-
caption: true,
|
|
215
|
-
filename: true
|
|
216
|
-
});
|
|
217
|
-
}
|
|
218
|
-
export function buildSendStickerPayload(input) {
|
|
219
|
-
return buildSendMediaPayload(input, "sticker", "sendSticker", {
|
|
220
|
-
caption: false,
|
|
221
|
-
filename: false
|
|
222
|
-
});
|
|
223
|
-
}
|
|
224
|
-
function assertValidNumberInRange(value, fieldName, min, max, helperName) {
|
|
225
|
-
if (typeof value !== "number" || !Number.isFinite(value) || value < min || value > max) {
|
|
226
|
-
throw new GraphRequestValidationError(`Invalid ${helperName} input: ${fieldName} must be a finite number between ${min} and ${max}.`);
|
|
227
|
-
}
|
|
228
|
-
return value;
|
|
229
|
-
}
|
|
230
|
-
function maybeText(value, fieldName, maxLength, helperName) {
|
|
231
|
-
if (value === undefined)
|
|
232
|
-
return;
|
|
233
|
-
return assertNonEmptyControlFreeString(value, fieldName, maxLength, helperName);
|
|
234
|
-
}
|
|
235
|
-
function withReplyContext(payload, input) {
|
|
236
|
-
if (input.replyToMessageId === undefined)
|
|
237
|
-
return payload;
|
|
238
|
-
return {
|
|
239
|
-
...payload,
|
|
240
|
-
context: { message_id: assertValidReplyToMessageId(input.replyToMessageId) }
|
|
241
|
-
};
|
|
242
|
-
}
|
|
243
|
-
function asRecordInput(input, helperName) {
|
|
244
|
-
if (!isPlainOptionsObject(input)) {
|
|
245
|
-
throw new GraphRequestValidationError(`Invalid ${helperName} input: expected an options object.`);
|
|
246
|
-
}
|
|
247
|
-
return input;
|
|
248
|
-
}
|
|
249
|
-
function assertArray(value, fieldName, helperName) {
|
|
250
|
-
if (!Array.isArray(value)) {
|
|
251
|
-
throw new GraphRequestValidationError(`Invalid ${helperName} input: ${fieldName} must be an array.`);
|
|
252
|
-
}
|
|
253
|
-
return value;
|
|
254
|
-
}
|
|
255
|
-
function assertBoundedArray(value, fieldName, min, max, helperName) {
|
|
256
|
-
const arr = assertArray(value, fieldName, helperName);
|
|
257
|
-
if (arr.length < min || arr.length > max) {
|
|
258
|
-
throw new GraphRequestValidationError(`Invalid ${helperName} input: ${fieldName} length must be between ${min} and ${max}.`);
|
|
259
|
-
}
|
|
260
|
-
for (let i = 0;i < arr.length; i += 1) {
|
|
261
|
-
if (!(i in arr)) {
|
|
262
|
-
throw new GraphRequestValidationError(`Invalid ${helperName} input: ${fieldName} must not contain sparse array holes.`);
|
|
263
|
-
}
|
|
264
|
-
}
|
|
265
|
-
if (Object.getPrototypeOf(arr) !== Array.prototype) {
|
|
266
|
-
throw new GraphRequestValidationError(`Invalid ${helperName} input: ${fieldName} must use Array.prototype.`);
|
|
267
|
-
}
|
|
268
|
-
if (Object.prototype.hasOwnProperty.call(arr, "map") || Object.prototype.hasOwnProperty.call(arr, Symbol.iterator)) {
|
|
269
|
-
throw new GraphRequestValidationError(`Invalid ${helperName} input: ${fieldName} must not override Array.prototype methods.`);
|
|
270
|
-
}
|
|
271
|
-
const descriptors = Object.getOwnPropertyDescriptors(arr);
|
|
272
|
-
const copy = [];
|
|
273
|
-
for (let i = 0;i < arr.length; i += 1) {
|
|
274
|
-
if (!Object.prototype.hasOwnProperty.call(arr, i)) {
|
|
275
|
-
throw new GraphRequestValidationError(`Invalid ${helperName} input: ${fieldName} must not contain inherited elements.`);
|
|
276
|
-
}
|
|
277
|
-
const descriptor = descriptors[String(i)];
|
|
278
|
-
if (descriptor === undefined || typeof descriptor.get === "function" || typeof descriptor.set === "function") {
|
|
279
|
-
throw new GraphRequestValidationError(`Invalid ${helperName} input: ${fieldName} must not use accessors.`);
|
|
280
|
-
}
|
|
281
|
-
copy.push(descriptor.value);
|
|
282
|
-
}
|
|
283
|
-
return copy;
|
|
284
|
-
}
|
|
285
|
-
function mapValidatedArray(values, mapper) {
|
|
286
|
-
const out = [];
|
|
287
|
-
for (let i = 0;i < values.length; i += 1) {
|
|
288
|
-
out.push(mapper(values[i], i));
|
|
289
|
-
}
|
|
290
|
-
return out;
|
|
291
|
-
}
|
|
292
|
-
export function buildSendLocationPayload(input) {
|
|
293
|
-
const record = asRecordInput(input, "sendLocation");
|
|
294
|
-
const payload = {
|
|
295
|
-
messaging_product: "whatsapp",
|
|
296
|
-
to: assertValidRecipient(record.to, "sendLocation"),
|
|
297
|
-
type: "location",
|
|
298
|
-
location: {
|
|
299
|
-
latitude: assertValidNumberInRange(record.latitude, "latitude", -90, 90, "sendLocation"),
|
|
300
|
-
longitude: assertValidNumberInRange(record.longitude, "longitude", -180, 180, "sendLocation")
|
|
301
|
-
}
|
|
302
|
-
};
|
|
303
|
-
const name = maybeText(record.name, "name", GRAPH_MESSAGES_SHORT_LABEL_MAX_LENGTH, "sendLocation");
|
|
304
|
-
const address = maybeText(record.address, "address", GRAPH_MESSAGES_GENERAL_TEXT_MAX_LENGTH, "sendLocation");
|
|
305
|
-
if (name !== undefined)
|
|
306
|
-
payload.location.name = name;
|
|
307
|
-
if (address !== undefined)
|
|
308
|
-
payload.location.address = address;
|
|
309
|
-
return withReplyContext(payload, record);
|
|
310
|
-
}
|
|
311
|
-
function normalizeContactName(value, helperName) {
|
|
312
|
-
if (!isPlainOptionsObject(value))
|
|
313
|
-
throw new GraphRequestValidationError(`Invalid ${helperName} input: contact.name must be an object.`);
|
|
314
|
-
const name = value;
|
|
315
|
-
const formatted = maybeText(name.formattedName ?? name.formatted, "name.formattedName", GRAPH_MESSAGES_GENERAL_TEXT_MAX_LENGTH, helperName);
|
|
316
|
-
if (formatted === undefined)
|
|
317
|
-
throw new GraphRequestValidationError(`Invalid ${helperName} input: contact.name.formattedName is required.`);
|
|
318
|
-
const out = { formatted_name: formatted };
|
|
319
|
-
const pairs = [["firstName", "first_name"], ["lastName", "last_name"], ["middleName", "middle_name"], ["suffix", "suffix"], ["prefix", "prefix"]];
|
|
320
|
-
for (const [camel, snake] of pairs) {
|
|
321
|
-
const v = maybeText(name[camel] ?? name[snake], `name.${camel}`, GRAPH_MESSAGES_SHORT_LABEL_MAX_LENGTH, helperName);
|
|
322
|
-
if (v !== undefined)
|
|
323
|
-
out[snake] = v;
|
|
324
|
-
}
|
|
325
|
-
return out;
|
|
326
|
-
}
|
|
327
|
-
function normalizeTypedStringRecord(value, fieldName, requiredField, helperName) {
|
|
328
|
-
if (!isPlainOptionsObject(value))
|
|
329
|
-
throw new GraphRequestValidationError(`Invalid ${helperName} input: ${fieldName} entries must be objects.`);
|
|
330
|
-
const record = value;
|
|
331
|
-
const primary = maybeText(record[requiredField], `${fieldName}.${requiredField}`, GRAPH_MESSAGES_GENERAL_TEXT_MAX_LENGTH, helperName);
|
|
332
|
-
if (primary === undefined)
|
|
333
|
-
throw new GraphRequestValidationError(`Invalid ${helperName} input: ${fieldName}.${requiredField} is required.`);
|
|
334
|
-
const out = { [requiredField]: primary };
|
|
335
|
-
const type = maybeText(record.type, `${fieldName}.type`, GRAPH_MESSAGES_SHORT_LABEL_MAX_LENGTH, helperName);
|
|
336
|
-
if (type !== undefined)
|
|
337
|
-
out.type = type;
|
|
338
|
-
return out;
|
|
339
|
-
}
|
|
340
|
-
function normalizeContact(value, helperName) {
|
|
341
|
-
if (!isPlainOptionsObject(value))
|
|
342
|
-
throw new GraphRequestValidationError(`Invalid ${helperName} input: contacts entries must be objects.`);
|
|
343
|
-
const contact = value;
|
|
344
|
-
const out = { name: normalizeContactName(contact.name, helperName) };
|
|
345
|
-
if (contact.phones !== undefined) {
|
|
346
|
-
out.phones = mapValidatedArray(assertBoundedArray(contact.phones, "contact.phones", 1, 20, helperName), (entry) => {
|
|
347
|
-
if (!isPlainOptionsObject(entry))
|
|
348
|
-
throw new GraphRequestValidationError(`Invalid ${helperName} input: phone entries must be objects.`);
|
|
349
|
-
const phone = entry;
|
|
350
|
-
const rawPhone = maybeText(phone.phone, "phone.phone", GRAPH_MESSAGES_SHORT_LABEL_MAX_LENGTH, helperName);
|
|
351
|
-
const waId = maybeText(phone.waId ?? phone.wa_id, "phone.waId", GRAPH_MESSAGES_SHORT_LABEL_MAX_LENGTH, helperName);
|
|
352
|
-
if (rawPhone === undefined && waId === undefined)
|
|
353
|
-
throw new GraphRequestValidationError(`Invalid ${helperName} input: phone requires phone or waId.`);
|
|
354
|
-
const normalized = {};
|
|
355
|
-
if (rawPhone !== undefined)
|
|
356
|
-
normalized.phone = rawPhone;
|
|
357
|
-
if (waId !== undefined)
|
|
358
|
-
normalized.wa_id = waId;
|
|
359
|
-
const type = maybeText(phone.type, "phone.type", GRAPH_MESSAGES_SHORT_LABEL_MAX_LENGTH, helperName);
|
|
360
|
-
if (type !== undefined)
|
|
361
|
-
normalized.type = type;
|
|
362
|
-
return normalized;
|
|
363
|
-
});
|
|
364
|
-
}
|
|
365
|
-
if (contact.emails !== undefined)
|
|
366
|
-
out.emails = mapValidatedArray(assertBoundedArray(contact.emails, "contact.emails", 1, 20, helperName), (entry) => normalizeTypedStringRecord(entry, "email", "email", helperName));
|
|
367
|
-
if (contact.urls !== undefined) {
|
|
368
|
-
out.urls = mapValidatedArray(assertBoundedArray(contact.urls, "contact.urls", 1, 20, helperName), (entry) => {
|
|
369
|
-
const normalized = normalizeTypedStringRecord(entry, "url", "url", helperName);
|
|
370
|
-
normalized.url = assertValidMediaLink(normalized.url, helperName);
|
|
371
|
-
return normalized;
|
|
372
|
-
});
|
|
373
|
-
}
|
|
374
|
-
if (contact.addresses !== undefined) {
|
|
375
|
-
out.addresses = mapValidatedArray(assertBoundedArray(contact.addresses, "contact.addresses", 1, 20, helperName), (entry) => {
|
|
376
|
-
if (!isPlainOptionsObject(entry))
|
|
377
|
-
throw new GraphRequestValidationError(`Invalid ${helperName} input: address entries must be objects.`);
|
|
378
|
-
const address = entry;
|
|
379
|
-
const normalized = {};
|
|
380
|
-
for (const [camel, snake] of [["street", "street"], ["city", "city"], ["state", "state"], ["zip", "zip"], ["country", "country"], ["countryCode", "country_code"], ["type", "type"]]) {
|
|
381
|
-
const v = maybeText(address[camel] ?? address[snake], `address.${camel}`, GRAPH_MESSAGES_GENERAL_TEXT_MAX_LENGTH, helperName);
|
|
382
|
-
if (v !== undefined)
|
|
383
|
-
normalized[snake] = v;
|
|
384
|
-
}
|
|
385
|
-
if (Object.keys(normalized).length === 0)
|
|
386
|
-
throw new GraphRequestValidationError(`Invalid ${helperName} input: address entries must contain at least one field.`);
|
|
387
|
-
return normalized;
|
|
388
|
-
});
|
|
389
|
-
}
|
|
390
|
-
if (contact.org !== undefined) {
|
|
391
|
-
if (!isPlainOptionsObject(contact.org))
|
|
392
|
-
throw new GraphRequestValidationError(`Invalid ${helperName} input: org must be an object.`);
|
|
393
|
-
const org = contact.org;
|
|
394
|
-
const normalized = {};
|
|
395
|
-
for (const key of ["company", "department", "title"]) {
|
|
396
|
-
const v = maybeText(org[key], `org.${key}`, GRAPH_MESSAGES_GENERAL_TEXT_MAX_LENGTH, helperName);
|
|
397
|
-
if (v !== undefined)
|
|
398
|
-
normalized[key] = v;
|
|
399
|
-
}
|
|
400
|
-
if (Object.keys(normalized).length > 0)
|
|
401
|
-
out.org = normalized;
|
|
402
|
-
}
|
|
403
|
-
const birthday = maybeText(contact.birthday, "birthday", 32, helperName);
|
|
404
|
-
if (birthday !== undefined)
|
|
405
|
-
out.birthday = birthday;
|
|
406
|
-
return out;
|
|
407
|
-
}
|
|
408
|
-
export function buildSendContactsPayload(input) {
|
|
409
|
-
const record = asRecordInput(input, "sendContacts");
|
|
410
|
-
const contacts = mapValidatedArray(assertBoundedArray(record.contacts, "contacts", 1, GRAPH_MESSAGES_MAX_CONTACTS, "sendContacts"), (c) => normalizeContact(c, "sendContacts"));
|
|
411
|
-
return withReplyContext({ messaging_product: "whatsapp", to: assertValidRecipient(record.to, "sendContacts"), type: "contacts", contacts }, record);
|
|
412
|
-
}
|
|
413
|
-
export function buildSendReactionPayload(input) {
|
|
414
|
-
const record = asRecordInput(input, "sendReaction");
|
|
415
|
-
const emoji = assertNonEmptyControlFreeString(record.emoji, "emoji", 32, "sendReaction");
|
|
416
|
-
return {
|
|
417
|
-
messaging_product: "whatsapp",
|
|
418
|
-
to: assertValidRecipient(record.to, "sendReaction"),
|
|
419
|
-
type: "reaction",
|
|
420
|
-
reaction: {
|
|
421
|
-
message_id: assertNonEmptyControlFreeString(record.messageId, "messageId", GRAPH_MESSAGES_REPLY_TO_MESSAGE_ID_MAX_LENGTH, "sendReaction"),
|
|
422
|
-
emoji
|
|
423
|
-
}
|
|
424
|
-
};
|
|
425
|
-
}
|
|
426
|
-
export function buildRemoveReactionPayload(input) {
|
|
427
|
-
const record = asRecordInput(input, "removeReaction");
|
|
428
|
-
return {
|
|
429
|
-
messaging_product: "whatsapp",
|
|
430
|
-
to: assertValidRecipient(record.to, "removeReaction"),
|
|
431
|
-
type: "reaction",
|
|
432
|
-
reaction: {
|
|
433
|
-
message_id: assertNonEmptyControlFreeString(record.messageId, "messageId", GRAPH_MESSAGES_REPLY_TO_MESSAGE_ID_MAX_LENGTH, "removeReaction"),
|
|
434
|
-
emoji: ""
|
|
435
|
-
}
|
|
436
|
-
};
|
|
437
|
-
}
|
|
438
|
-
function interactiveBase(input, helperName, interactive) {
|
|
439
|
-
const record = asRecordInput(input, helperName);
|
|
440
|
-
return withReplyContext({ messaging_product: "whatsapp", to: assertValidRecipient(record.to, helperName), type: "interactive", interactive }, record);
|
|
441
|
-
}
|
|
442
|
-
function addHeaderFooter(target, record, helperName) {
|
|
443
|
-
const headerText = maybeText(record.headerText, "headerText", GRAPH_MESSAGES_SHORT_LABEL_MAX_LENGTH, helperName);
|
|
444
|
-
const footerText = maybeText(record.footerText, "footerText", GRAPH_MESSAGES_SHORT_LABEL_MAX_LENGTH, helperName);
|
|
445
|
-
if (headerText !== undefined)
|
|
446
|
-
target.header = { type: "text", text: headerText };
|
|
447
|
-
if (footerText !== undefined)
|
|
448
|
-
target.footer = { text: footerText };
|
|
449
|
-
}
|
|
450
|
-
export function buildSendButtonsPayload(input) {
|
|
451
|
-
const record = asRecordInput(input, "sendButtons");
|
|
452
|
-
const buttons = mapValidatedArray(assertBoundedArray(record.buttons, "buttons", 1, GRAPH_MESSAGES_MAX_REPLY_BUTTONS, "sendButtons"), (entry) => {
|
|
453
|
-
if (!isPlainOptionsObject(entry))
|
|
454
|
-
throw new GraphRequestValidationError("Invalid sendButtons input: button entries must be objects.");
|
|
455
|
-
const button = entry;
|
|
456
|
-
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") } };
|
|
457
|
-
});
|
|
458
|
-
const interactive = { type: "button", body: { text: assertNonEmptyControlFreeString(record.bodyText, "bodyText", GRAPH_MESSAGES_GENERAL_TEXT_MAX_LENGTH, "sendButtons") }, action: { buttons } };
|
|
459
|
-
addHeaderFooter(interactive, record, "sendButtons");
|
|
460
|
-
return interactiveBase(input, "sendButtons", interactive);
|
|
461
|
-
}
|
|
462
|
-
export function buildSendListPayload(input) {
|
|
463
|
-
const record = asRecordInput(input, "sendList");
|
|
464
|
-
let totalRows = 0;
|
|
465
|
-
const sections = mapValidatedArray(assertBoundedArray(record.sections, "sections", 1, GRAPH_MESSAGES_MAX_LIST_SECTIONS, "sendList"), (entry) => {
|
|
466
|
-
if (!isPlainOptionsObject(entry))
|
|
467
|
-
throw new GraphRequestValidationError("Invalid sendList input: section entries must be objects.");
|
|
468
|
-
const section = entry;
|
|
469
|
-
const rows = mapValidatedArray(assertBoundedArray(section.rows, "section.rows", 1, GRAPH_MESSAGES_MAX_LIST_ROWS, "sendList"), (rowEntry) => {
|
|
470
|
-
totalRows += 1;
|
|
471
|
-
if (!isPlainOptionsObject(rowEntry))
|
|
472
|
-
throw new GraphRequestValidationError("Invalid sendList input: row entries must be objects.");
|
|
473
|
-
const row = rowEntry;
|
|
474
|
-
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") };
|
|
475
|
-
const desc = maybeText(row.description, "row.description", GRAPH_MESSAGES_ROW_DESCRIPTION_MAX_LENGTH, "sendList");
|
|
476
|
-
if (desc !== undefined)
|
|
477
|
-
normalized.description = desc;
|
|
478
|
-
return normalized;
|
|
479
|
-
});
|
|
480
|
-
const normalized = { rows };
|
|
481
|
-
const title = maybeText(section.title, "section.title", GRAPH_MESSAGES_SECTION_TITLE_MAX_LENGTH, "sendList");
|
|
482
|
-
if (title !== undefined)
|
|
483
|
-
normalized.title = title;
|
|
484
|
-
return normalized;
|
|
485
|
-
});
|
|
486
|
-
if (totalRows > GRAPH_MESSAGES_MAX_LIST_ROWS)
|
|
487
|
-
throw new GraphRequestValidationError(`Invalid sendList input: total row count exceeds ${GRAPH_MESSAGES_MAX_LIST_ROWS}.`);
|
|
488
|
-
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 } };
|
|
489
|
-
addHeaderFooter(interactive, record, "sendList");
|
|
490
|
-
return interactiveBase(input, "sendList", interactive);
|
|
491
|
-
}
|
|
492
|
-
export function buildSendCtaUrlPayload(input) {
|
|
493
|
-
const record = asRecordInput(input, "sendCtaUrl");
|
|
494
|
-
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") } } };
|
|
495
|
-
addHeaderFooter(interactive, record, "sendCtaUrl");
|
|
496
|
-
return interactiveBase(input, "sendCtaUrl", interactive);
|
|
497
|
-
}
|
|
498
|
-
export function buildSendProductPayload(input) {
|
|
499
|
-
const record = asRecordInput(input, "sendProduct");
|
|
500
|
-
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") } };
|
|
501
|
-
const bodyText = maybeText(record.bodyText, "bodyText", GRAPH_MESSAGES_GENERAL_TEXT_MAX_LENGTH, "sendProduct");
|
|
502
|
-
const footerText = maybeText(record.footerText, "footerText", GRAPH_MESSAGES_SHORT_LABEL_MAX_LENGTH, "sendProduct");
|
|
503
|
-
if (bodyText !== undefined)
|
|
504
|
-
interactive.body = { text: bodyText };
|
|
505
|
-
if (footerText !== undefined)
|
|
506
|
-
interactive.footer = { text: footerText };
|
|
507
|
-
return interactiveBase(input, "sendProduct", interactive);
|
|
508
|
-
}
|
|
509
|
-
export function buildSendProductsPayload(input) {
|
|
510
|
-
const record = asRecordInput(input, "sendProducts");
|
|
511
|
-
let totalItems = 0;
|
|
512
|
-
const sections = mapValidatedArray(assertBoundedArray(record.sections, "sections", 1, 10, "sendProducts"), (entry) => {
|
|
513
|
-
if (!isPlainOptionsObject(entry))
|
|
514
|
-
throw new GraphRequestValidationError("Invalid sendProducts input: section entries must be objects.");
|
|
515
|
-
const section = entry;
|
|
516
|
-
const productItems = mapValidatedArray(assertBoundedArray(section.productItems, "section.productItems", 1, GRAPH_MESSAGES_MAX_PRODUCT_ITEMS, "sendProducts"), (itemEntry) => {
|
|
517
|
-
totalItems += 1;
|
|
518
|
-
if (!isPlainOptionsObject(itemEntry))
|
|
519
|
-
throw new GraphRequestValidationError("Invalid sendProducts input: product item entries must be objects.");
|
|
520
|
-
const item = itemEntry;
|
|
521
|
-
return { product_retailer_id: assertNonEmptyControlFreeString(item.productRetailerId, "productRetailerId", GRAPH_MESSAGES_MEDIA_ID_MAX_LENGTH, "sendProducts") };
|
|
522
|
-
});
|
|
523
|
-
return { title: assertNonEmptyControlFreeString(section.title, "section.title", GRAPH_MESSAGES_SECTION_TITLE_MAX_LENGTH, "sendProducts"), product_items: productItems };
|
|
524
|
-
});
|
|
525
|
-
if (totalItems > GRAPH_MESSAGES_MAX_PRODUCT_ITEMS)
|
|
526
|
-
throw new GraphRequestValidationError(`Invalid sendProducts input: total product item count exceeds ${GRAPH_MESSAGES_MAX_PRODUCT_ITEMS}.`);
|
|
527
|
-
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 } };
|
|
528
|
-
const footerText = maybeText(record.footerText, "footerText", GRAPH_MESSAGES_SHORT_LABEL_MAX_LENGTH, "sendProducts");
|
|
529
|
-
if (footerText !== undefined)
|
|
530
|
-
interactive.footer = { text: footerText };
|
|
531
|
-
return interactiveBase(input, "sendProducts", interactive);
|
|
532
|
-
}
|
|
533
|
-
export function buildSendCatalogPayload(input) {
|
|
534
|
-
const record = asRecordInput(input, "sendCatalog");
|
|
535
|
-
const action = { name: "catalog_message" };
|
|
536
|
-
const thumbnail = maybeText(record.thumbnailProductRetailerId, "thumbnailProductRetailerId", GRAPH_MESSAGES_MEDIA_ID_MAX_LENGTH, "sendCatalog");
|
|
537
|
-
if (thumbnail !== undefined)
|
|
538
|
-
action.parameters = { thumbnail_product_retailer_id: thumbnail };
|
|
539
|
-
const interactive = { type: "catalog_message", body: { text: assertNonEmptyControlFreeString(record.bodyText, "bodyText", GRAPH_MESSAGES_GENERAL_TEXT_MAX_LENGTH, "sendCatalog") }, action };
|
|
540
|
-
addHeaderFooter(interactive, record, "sendCatalog");
|
|
541
|
-
return interactiveBase(input, "sendCatalog", interactive);
|
|
542
|
-
}
|
|
543
|
-
export function buildRequestLocationPayload(input) {
|
|
544
|
-
const record = asRecordInput(input, "requestLocation");
|
|
545
|
-
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" } });
|
|
546
|
-
}
|
|
547
|
-
export function buildMarkMessageAsReadPayload(input) {
|
|
548
|
-
const record = asRecordInput(input, "markMessageAsRead");
|
|
549
|
-
return { messaging_product: "whatsapp", status: "read", message_id: assertNonEmptyControlFreeString(record.messageId, "messageId", GRAPH_MESSAGES_REPLY_TO_MESSAGE_ID_MAX_LENGTH, "markMessageAsRead") };
|
|
550
|
-
}
|
|
551
|
-
export function buildTypingIndicatorPayload(input) {
|
|
552
|
-
const payload = buildMarkMessageAsReadPayload(input);
|
|
553
|
-
return { ...payload, typing_indicator: { type: "text" } };
|
|
554
|
-
}
|
|
555
|
-
function sanitizeTemplateParameter(value, helperName, path, seen, depth = 0) {
|
|
556
|
-
if (depth > 6) {
|
|
557
|
-
throw new GraphRequestValidationError(`Invalid ${helperName} input: ${path} exceeds maximum nesting depth.`);
|
|
558
|
-
}
|
|
559
|
-
if (typeof value === "string") {
|
|
560
|
-
if (value.length === 0 || value.length > GRAPH_MESSAGES_MEDIA_ID_MAX_LENGTH || hasControlChar(value)) {
|
|
561
|
-
throw new GraphRequestValidationError(`Invalid ${helperName} input: ${path} contains an invalid string.`);
|
|
562
|
-
}
|
|
563
|
-
return value;
|
|
564
|
-
}
|
|
565
|
-
if (typeof value === "number") {
|
|
566
|
-
if (!Number.isFinite(value)) {
|
|
567
|
-
throw new GraphRequestValidationError(`Invalid ${helperName} input: ${path} contains a non-finite number.`);
|
|
568
|
-
}
|
|
569
|
-
return value;
|
|
570
|
-
}
|
|
571
|
-
if (value === null || typeof value === "boolean")
|
|
572
|
-
return value;
|
|
573
|
-
if (value === undefined)
|
|
574
|
-
return;
|
|
575
|
-
if (Array.isArray(value)) {
|
|
576
|
-
const own = Object.getOwnPropertyDescriptors(value);
|
|
577
|
-
if (Object.prototype.hasOwnProperty.call(own, "toJSON") || "toJSON" in value) {
|
|
578
|
-
throw new GraphRequestValidationError(`Invalid ${helperName} input: ${path} must not define toJSON.`);
|
|
579
|
-
}
|
|
580
|
-
for (const [key, descriptor] of Object.entries(own)) {
|
|
581
|
-
if (typeof descriptor.get === "function" || typeof descriptor.set === "function") {
|
|
582
|
-
throw new GraphRequestValidationError(`Invalid ${helperName} input: ${path}.${key} must not use accessors.`);
|
|
583
|
-
}
|
|
584
|
-
}
|
|
585
|
-
return mapValidatedArray(assertBoundedArray(value, path, 0, 100, helperName), (entry, index) => sanitizeTemplateParameter(entry, helperName, `${path}[${index}]`, seen, depth + 1));
|
|
586
|
-
}
|
|
587
|
-
if (!isPlainOptionsObject(value)) {
|
|
588
|
-
throw new GraphRequestValidationError(`Invalid ${helperName} input: ${path} must be JSON-serializable.`);
|
|
589
|
-
}
|
|
590
|
-
const record = value;
|
|
591
|
-
const proto = Object.getPrototypeOf(record);
|
|
592
|
-
if (proto !== Object.prototype && proto !== null) {
|
|
593
|
-
throw new GraphRequestValidationError(`Invalid ${helperName} input: ${path} must be a plain object.`);
|
|
594
|
-
}
|
|
595
|
-
const descriptors = Object.getOwnPropertyDescriptors(record);
|
|
596
|
-
if (Object.prototype.hasOwnProperty.call(descriptors, "toJSON") || "toJSON" in record) {
|
|
597
|
-
throw new GraphRequestValidationError(`Invalid ${helperName} input: ${path} must not define toJSON.`);
|
|
598
|
-
}
|
|
599
|
-
for (const [key, descriptor] of Object.entries(descriptors)) {
|
|
600
|
-
if (typeof descriptor.get === "function" || typeof descriptor.set === "function") {
|
|
601
|
-
throw new GraphRequestValidationError(`Invalid ${helperName} input: ${path}.${key} must not use accessors.`);
|
|
602
|
-
}
|
|
603
|
-
if (typeof descriptor.value === "function" || typeof descriptor.value === "symbol") {
|
|
604
|
-
throw new GraphRequestValidationError(`Invalid ${helperName} input: ${path}.${key} must be JSON-serializable.`);
|
|
605
|
-
}
|
|
606
|
-
}
|
|
607
|
-
if (seen.has(record)) {
|
|
608
|
-
throw new GraphRequestValidationError(`Invalid ${helperName} input: ${path} must not contain cycles.`);
|
|
609
|
-
}
|
|
610
|
-
seen.add(record);
|
|
611
|
-
const out = {};
|
|
612
|
-
for (const [key, nested] of Object.entries(record)) {
|
|
613
|
-
if (key.length === 0 || key.length > GRAPH_MESSAGES_SHORT_LABEL_MAX_LENGTH || hasControlChar(key)) {
|
|
614
|
-
throw new GraphRequestValidationError(`Invalid ${helperName} input: ${path} contains an invalid key.`);
|
|
615
|
-
}
|
|
616
|
-
const sanitized = sanitizeTemplateParameter(nested, helperName, `${path}.${key}`, seen, depth + 1);
|
|
617
|
-
if (sanitized !== undefined)
|
|
618
|
-
out[key] = sanitized;
|
|
619
|
-
}
|
|
620
|
-
seen.delete(record);
|
|
621
|
-
return out;
|
|
622
|
-
}
|
|
623
|
-
function normalizeTemplateComponent(value, helperName) {
|
|
624
|
-
if (!isPlainOptionsObject(value))
|
|
625
|
-
throw new GraphRequestValidationError(`Invalid ${helperName} input: component entries must be objects.`);
|
|
626
|
-
const component = value;
|
|
627
|
-
const out = { type: assertNonEmptyControlFreeString(component.type, "component.type", GRAPH_MESSAGES_SHORT_LABEL_MAX_LENGTH, helperName) };
|
|
628
|
-
if (component.subType !== undefined)
|
|
629
|
-
out.sub_type = assertNonEmptyControlFreeString(component.subType, "component.subType", GRAPH_MESSAGES_SHORT_LABEL_MAX_LENGTH, helperName);
|
|
630
|
-
if (component.index !== undefined)
|
|
631
|
-
out.index = assertNonEmptyControlFreeString(component.index, "component.index", 8, helperName);
|
|
632
|
-
if (component.parameters !== undefined) {
|
|
633
|
-
const parameters = assertBoundedArray(component.parameters, "component.parameters", 0, 100, helperName);
|
|
634
|
-
out.parameters = mapValidatedArray(parameters, (parameter, index) => {
|
|
635
|
-
if (!isPlainOptionsObject(parameter))
|
|
636
|
-
throw new GraphRequestValidationError(`Invalid ${helperName} input: component parameters must be objects.`);
|
|
637
|
-
return sanitizeTemplateParameter(parameter, helperName, `component.parameters[${index}]`, new WeakSet);
|
|
638
|
-
});
|
|
639
|
-
}
|
|
640
|
-
return out;
|
|
641
|
-
}
|
|
642
|
-
export function buildSendTemplatePayload(input) {
|
|
643
|
-
const record = asRecordInput(input, "sendTemplate");
|
|
644
|
-
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") } };
|
|
645
|
-
if (record.components !== undefined)
|
|
646
|
-
template.components = mapValidatedArray(assertBoundedArray(record.components, "components", 0, 100, "sendTemplate"), (c) => normalizeTemplateComponent(c, "sendTemplate"));
|
|
647
|
-
return withReplyContext({ messaging_product: "whatsapp", to: assertValidRecipient(record.to, "sendTemplate"), type: "template", template }, record);
|
|
648
|
-
}
|
|
649
|
-
export function buildSendMessagePayload(input) {
|
|
650
|
-
const payload = {
|
|
651
|
-
messaging_product: "whatsapp",
|
|
652
|
-
to: input.to,
|
|
653
|
-
type: "text",
|
|
654
|
-
text: {
|
|
655
|
-
body: input.text
|
|
656
|
-
}
|
|
657
|
-
};
|
|
658
|
-
if (typeof input.previewUrl === "boolean") {
|
|
659
|
-
payload.text.preview_url = input.previewUrl;
|
|
660
|
-
}
|
|
661
|
-
return payload;
|
|
662
|
-
}
|
|
663
|
-
function normalizePhoneNumberId(phoneNumberId) {
|
|
664
|
-
const normalized = phoneNumberId.trim();
|
|
665
|
-
if (!/^\d+$/.test(normalized)) {
|
|
666
|
-
throw new GraphRequestValidationError("Invalid phoneNumberId. Expected a numeric Graph phone number ID path segment.");
|
|
667
|
-
}
|
|
668
|
-
return normalized;
|
|
669
|
-
}
|
|
670
|
-
export const sendMessage = defineEndpoint({
|
|
671
|
-
method: "POST",
|
|
672
|
-
pathTemplate: "/{phoneNumberId}/messages",
|
|
673
|
-
params: { phoneNumberId: { in: "path", required: true } },
|
|
674
|
-
bodyContentType: "application/json"
|
|
675
|
-
});
|
|
1
|
+
export * from "./messages/index.js";
|