@wats/graph 0.3.1 → 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/client.d.ts +2 -2
- package/dist/client.js +3 -3
- package/dist/createFetchTransport.d.ts +1 -1
- package/dist/createFetchTransport.js +1 -1
- package/dist/createMockTransport.d.ts +1 -1
- package/dist/endpoint.d.ts +1 -1
- package/dist/endpoint.js +1 -1
- package/dist/endpoints/businessManagement.d.ts +153 -9
- package/dist/endpoints/businessManagement.js +279 -7
- package/dist/endpoints/calling.d.ts +7 -7
- package/dist/endpoints/calling.js +2 -2
- package/dist/endpoints/flows/callables.d.ts +10 -10
- package/dist/endpoints/flows/callables.js +2 -2
- package/dist/endpoints/flows/dataExchange.d.ts +1 -1
- package/dist/endpoints/flows/dataExchange.js +2 -2
- package/dist/endpoints/flows/flowJson.d.ts +1 -1
- package/dist/endpoints/flows/flowJson.js +1 -1
- package/dist/endpoints/flows/index.d.ts +6 -6
- package/dist/endpoints/flows/index.js +4 -4
- package/dist/endpoints/flows/shared.d.ts +1 -1
- package/dist/endpoints/flows/shared.js +1 -1
- package/dist/endpoints/flows/types.d.ts +1 -1
- package/dist/endpoints/flows.d.ts +2 -2
- package/dist/endpoints/flows.js +1 -1
- package/dist/endpoints/media.d.ts +1 -1
- package/dist/endpoints/media.js +1 -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.d.ts +1 -1
- package/dist/endpoints/templates/builders.js +24 -1
- package/dist/endpoints/templates/callables.d.ts +7 -6
- package/dist/endpoints/templates/callables.js +7 -5
- package/dist/endpoints/templates/groups.d.ts +126 -0
- package/dist/endpoints/templates/groups.js +156 -0
- package/dist/endpoints/templates/index.d.ts +7 -5
- package/dist/endpoints/templates/index.js +11 -3
- package/dist/endpoints/templates/shared.d.ts +2 -2
- package/dist/endpoints/templates/shared.js +1 -1
- package/dist/endpoints/templates/types.d.ts +15 -2
- package/dist/endpoints/templates/validation.d.ts +1 -1
- package/dist/endpoints/templates/validation.js +2 -2
- package/dist/endpoints/templates.d.ts +2 -2
- package/dist/endpoints/templates.js +7 -1
- package/dist/endpoints/waba/index.d.ts +4 -4
- package/dist/endpoints/waba/index.js +1 -1
- package/dist/endpoints/waba/phoneNumbers.d.ts +6 -4
- package/dist/endpoints/waba/phoneNumbers.js +3 -3
- package/dist/endpoints/wabaEndpoints.d.ts +6 -6
- package/dist/endpoints/wabaEndpoints.js +9 -3
- package/dist/errorRegistry.d.ts +1 -1
- package/dist/errorSubclasses.d.ts +30 -2
- package/dist/errorSubclasses.js +65 -2
- package/dist/errors.js +1 -1
- package/dist/index.d.ts +20 -20
- package/dist/index.js +15 -15
- package/dist/internal/validation/arrays.js +2 -2
- package/dist/internal/validation/errors.d.ts +1 -1
- package/dist/internal/validation/errors.js +1 -1
- package/dist/internal/validation/headers.js +3 -3
- package/dist/internal/validation/index.d.ts +8 -8
- package/dist/internal/validation/index.js +8 -8
- package/dist/internal/validation/json.js +4 -4
- package/dist/internal/validation/options.js +2 -2
- package/dist/internal/validation/paths.js +2 -2
- package/dist/internal/validation/records.js +2 -2
- package/dist/internal/validation/strings.js +1 -1
- package/dist/pagination.d.ts +2 -2
- package/dist/subclients/phoneNumberClient.d.ts +21 -5
- package/dist/subclients/phoneNumberClient.js +78 -7
- package/dist/subclients/wabaClient.d.ts +19 -5
- package/dist/subclients/wabaClient.js +39 -6
- package/package.json +10 -1
|
@@ -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
|
+
});
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export * from "./types.js";
|
|
2
|
+
export { GRAPH_MESSAGES_TEXT_BODY_MAX_LENGTH, GRAPH_MESSAGES_RECIPIENT_MAX_DIGITS, GRAPH_MESSAGES_REPLY_TO_MESSAGE_ID_MAX_LENGTH, GRAPH_MESSAGES_MEDIA_ID_MAX_LENGTH, GRAPH_MESSAGES_MEDIA_LINK_MAX_LENGTH, GRAPH_MESSAGES_MEDIA_CAPTION_MAX_LENGTH, GRAPH_MESSAGES_DOCUMENT_FILENAME_MAX_LENGTH, GRAPH_MESSAGES_GENERAL_TEXT_MAX_LENGTH, GRAPH_MESSAGES_SHORT_LABEL_MAX_LENGTH, GRAPH_MESSAGES_BUTTON_TITLE_MAX_LENGTH, GRAPH_MESSAGES_BUTTON_ID_MAX_LENGTH, GRAPH_MESSAGES_SECTION_TITLE_MAX_LENGTH, GRAPH_MESSAGES_ROW_TITLE_MAX_LENGTH, GRAPH_MESSAGES_ROW_DESCRIPTION_MAX_LENGTH, GRAPH_MESSAGES_MAX_REPLY_BUTTONS, GRAPH_MESSAGES_MAX_LIST_SECTIONS, GRAPH_MESSAGES_MAX_LIST_ROWS, GRAPH_MESSAGES_MAX_CONTACTS, GRAPH_MESSAGES_MAX_PRODUCT_ITEMS } from "./validation.js";
|
|
3
|
+
export { buildSendTextPayload, buildSendImagePayload, buildSendVideoPayload, buildSendAudioPayload, buildSendDocumentPayload, buildSendStickerPayload, buildSendLocationPayload, buildSendContactsPayload, buildSendReactionPayload, buildRemoveReactionPayload } from "./builders-basic.js";
|
|
4
|
+
export { buildSendButtonsPayload, buildSendListPayload, buildSendCtaUrlPayload, buildSendCallPermissionRequestPayload, buildSendProductPayload, buildSendProductsPayload, buildSendCatalogPayload, buildRequestLocationPayload, buildMarkMessageAsReadPayload, buildTypingIndicatorPayload } from "./builders-interactive.js";
|
|
5
|
+
export { buildSendTemplatePayload, buildSendMarketingTemplatePayload } from "./builders-template.js";
|
|
6
|
+
export { buildSendMessagePayload, sendMarketingTemplate, sendMessage, GraphMessagesEndpoint } from "./callables.js";
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
export * from "./types.js";
|
|
2
|
+
export {
|
|
3
|
+
GRAPH_MESSAGES_TEXT_BODY_MAX_LENGTH,
|
|
4
|
+
GRAPH_MESSAGES_RECIPIENT_MAX_DIGITS,
|
|
5
|
+
GRAPH_MESSAGES_REPLY_TO_MESSAGE_ID_MAX_LENGTH,
|
|
6
|
+
GRAPH_MESSAGES_MEDIA_ID_MAX_LENGTH,
|
|
7
|
+
GRAPH_MESSAGES_MEDIA_LINK_MAX_LENGTH,
|
|
8
|
+
GRAPH_MESSAGES_MEDIA_CAPTION_MAX_LENGTH,
|
|
9
|
+
GRAPH_MESSAGES_DOCUMENT_FILENAME_MAX_LENGTH,
|
|
10
|
+
GRAPH_MESSAGES_GENERAL_TEXT_MAX_LENGTH,
|
|
11
|
+
GRAPH_MESSAGES_SHORT_LABEL_MAX_LENGTH,
|
|
12
|
+
GRAPH_MESSAGES_BUTTON_TITLE_MAX_LENGTH,
|
|
13
|
+
GRAPH_MESSAGES_BUTTON_ID_MAX_LENGTH,
|
|
14
|
+
GRAPH_MESSAGES_SECTION_TITLE_MAX_LENGTH,
|
|
15
|
+
GRAPH_MESSAGES_ROW_TITLE_MAX_LENGTH,
|
|
16
|
+
GRAPH_MESSAGES_ROW_DESCRIPTION_MAX_LENGTH,
|
|
17
|
+
GRAPH_MESSAGES_MAX_REPLY_BUTTONS,
|
|
18
|
+
GRAPH_MESSAGES_MAX_LIST_SECTIONS,
|
|
19
|
+
GRAPH_MESSAGES_MAX_LIST_ROWS,
|
|
20
|
+
GRAPH_MESSAGES_MAX_CONTACTS,
|
|
21
|
+
GRAPH_MESSAGES_MAX_PRODUCT_ITEMS
|
|
22
|
+
} from "./validation.js";
|
|
23
|
+
export {
|
|
24
|
+
buildSendTextPayload,
|
|
25
|
+
buildSendImagePayload,
|
|
26
|
+
buildSendVideoPayload,
|
|
27
|
+
buildSendAudioPayload,
|
|
28
|
+
buildSendDocumentPayload,
|
|
29
|
+
buildSendStickerPayload,
|
|
30
|
+
buildSendLocationPayload,
|
|
31
|
+
buildSendContactsPayload,
|
|
32
|
+
buildSendReactionPayload,
|
|
33
|
+
buildRemoveReactionPayload
|
|
34
|
+
} from "./builders-basic.js";
|
|
35
|
+
export {
|
|
36
|
+
buildSendButtonsPayload,
|
|
37
|
+
buildSendListPayload,
|
|
38
|
+
buildSendCtaUrlPayload,
|
|
39
|
+
buildSendCallPermissionRequestPayload,
|
|
40
|
+
buildSendProductPayload,
|
|
41
|
+
buildSendProductsPayload,
|
|
42
|
+
buildSendCatalogPayload,
|
|
43
|
+
buildRequestLocationPayload,
|
|
44
|
+
buildMarkMessageAsReadPayload,
|
|
45
|
+
buildTypingIndicatorPayload
|
|
46
|
+
} from "./builders-interactive.js";
|
|
47
|
+
export {
|
|
48
|
+
buildSendTemplatePayload,
|
|
49
|
+
buildSendMarketingTemplatePayload
|
|
50
|
+
} from "./builders-template.js";
|
|
51
|
+
export {
|
|
52
|
+
buildSendMessagePayload,
|
|
53
|
+
sendMarketingTemplate,
|
|
54
|
+
sendMessage,
|
|
55
|
+
GraphMessagesEndpoint
|
|
56
|
+
} from "./callables.js";
|