@wats/graph 0.3.1

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 (89) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +3 -0
  3. package/dist/client.d.ts +41 -0
  4. package/dist/client.js +392 -0
  5. package/dist/createFetchTransport.d.ts +6 -0
  6. package/dist/createFetchTransport.js +46 -0
  7. package/dist/createMockTransport.d.ts +20 -0
  8. package/dist/createMockTransport.js +120 -0
  9. package/dist/endpoint.d.ts +26 -0
  10. package/dist/endpoint.js +223 -0
  11. package/dist/endpoints/businessManagement.d.ts +157 -0
  12. package/dist/endpoints/businessManagement.js +277 -0
  13. package/dist/endpoints/calling.d.ts +65 -0
  14. package/dist/endpoints/calling.js +284 -0
  15. package/dist/endpoints/flows/callables.d.ts +90 -0
  16. package/dist/endpoints/flows/callables.js +97 -0
  17. package/dist/endpoints/flows/dataExchange.d.ts +4 -0
  18. package/dist/endpoints/flows/dataExchange.js +40 -0
  19. package/dist/endpoints/flows/flowJson.d.ts +18 -0
  20. package/dist/endpoints/flows/flowJson.js +214 -0
  21. package/dist/endpoints/flows/index.d.ts +6 -0
  22. package/dist/endpoints/flows/index.js +26 -0
  23. package/dist/endpoints/flows/shared.d.ts +17 -0
  24. package/dist/endpoints/flows/shared.js +113 -0
  25. package/dist/endpoints/flows/types.d.ts +105 -0
  26. package/dist/endpoints/flows/types.js +0 -0
  27. package/dist/endpoints/flows.d.ts +2 -0
  28. package/dist/endpoints/flows.js +23 -0
  29. package/dist/endpoints/media.d.ts +123 -0
  30. package/dist/endpoints/media.js +976 -0
  31. package/dist/endpoints/messages.d.ts +375 -0
  32. package/dist/endpoints/messages.js +675 -0
  33. package/dist/endpoints/templates/builders.d.ts +9 -0
  34. package/dist/endpoints/templates/builders.js +101 -0
  35. package/dist/endpoints/templates/callables.d.ts +54 -0
  36. package/dist/endpoints/templates/callables.js +98 -0
  37. package/dist/endpoints/templates/index.d.ts +5 -0
  38. package/dist/endpoints/templates/index.js +16 -0
  39. package/dist/endpoints/templates/shared.d.ts +21 -0
  40. package/dist/endpoints/templates/shared.js +203 -0
  41. package/dist/endpoints/templates/types.d.ts +135 -0
  42. package/dist/endpoints/templates/types.js +0 -0
  43. package/dist/endpoints/templates/validation.d.ts +2 -0
  44. package/dist/endpoints/templates/validation.js +113 -0
  45. package/dist/endpoints/templates.d.ts +2 -0
  46. package/dist/endpoints/templates.js +14 -0
  47. package/dist/endpoints/waba/index.d.ts +4 -0
  48. package/dist/endpoints/waba/index.js +1 -0
  49. package/dist/endpoints/waba/phoneNumbers.d.ts +25 -0
  50. package/dist/endpoints/waba/phoneNumbers.js +23 -0
  51. package/dist/endpoints/waba/types.d.ts +8 -0
  52. package/dist/endpoints/waba/types.js +0 -0
  53. package/dist/endpoints/wabaEndpoints.d.ts +6 -0
  54. package/dist/endpoints/wabaEndpoints.js +38 -0
  55. package/dist/errorRegistry.d.ts +45 -0
  56. package/dist/errorRegistry.js +68 -0
  57. package/dist/errorSubclasses.d.ts +277 -0
  58. package/dist/errorSubclasses.js +613 -0
  59. package/dist/errors.d.ts +92 -0
  60. package/dist/errors.js +250 -0
  61. package/dist/index.d.ts +20 -0
  62. package/dist/index.js +48 -0
  63. package/dist/internal/validation/arrays.d.ts +19 -0
  64. package/dist/internal/validation/arrays.js +102 -0
  65. package/dist/internal/validation/errors.d.ts +3 -0
  66. package/dist/internal/validation/errors.js +13 -0
  67. package/dist/internal/validation/headers.d.ts +15 -0
  68. package/dist/internal/validation/headers.js +77 -0
  69. package/dist/internal/validation/index.d.ts +8 -0
  70. package/dist/internal/validation/index.js +8 -0
  71. package/dist/internal/validation/json.d.ts +10 -0
  72. package/dist/internal/validation/json.js +79 -0
  73. package/dist/internal/validation/options.d.ts +5 -0
  74. package/dist/internal/validation/options.js +36 -0
  75. package/dist/internal/validation/paths.d.ts +8 -0
  76. package/dist/internal/validation/paths.js +37 -0
  77. package/dist/internal/validation/records.d.ts +20 -0
  78. package/dist/internal/validation/records.js +69 -0
  79. package/dist/internal/validation/strings.d.ts +12 -0
  80. package/dist/internal/validation/strings.js +46 -0
  81. package/dist/pagination.d.ts +35 -0
  82. package/dist/pagination.js +160 -0
  83. package/dist/subclients/phoneNumberClient.d.ts +60 -0
  84. package/dist/subclients/phoneNumberClient.js +194 -0
  85. package/dist/subclients/wabaClient.d.ts +66 -0
  86. package/dist/subclients/wabaClient.js +180 -0
  87. package/dist/transport.d.ts +32 -0
  88. package/dist/transport.js +5 -0
  89. package/package.json +70 -0
@@ -0,0 +1,9 @@
1
+ import type { CreateMessageTemplateBody, TemplateBodyComponentInput, TemplateButtonsComponentInput, TemplateComponent, TemplateFooterComponentInput, TemplateHeaderComponentInput, UpdateMessageTemplateBody } from "./types";
2
+ export declare function buildCreateMessageTemplateBody(input: CreateMessageTemplateBody): Record<string, unknown>;
3
+ export declare function buildUpdateMessageTemplateBody(input: UpdateMessageTemplateBody): Record<string, unknown>;
4
+ export declare function buildTemplateHeaderComponent(input: TemplateHeaderComponentInput | TemplateComponent): TemplateComponent;
5
+ export declare function buildTemplateBodyComponent(input: TemplateBodyComponentInput | TemplateComponent): TemplateComponent;
6
+ export declare function buildTemplateFooterComponent(input: TemplateFooterComponentInput | TemplateComponent): TemplateComponent;
7
+ export declare function buildTemplateButtonComponent(input: TemplateButtonsComponentInput | TemplateComponent): TemplateComponent & {
8
+ readonly buttons: readonly Record<string, unknown>[];
9
+ };
@@ -0,0 +1,101 @@
1
+ import {
2
+ TEMPLATE_MAX_BUTTONS,
3
+ TEMPLATE_SHORT_TEXT_MAX_LENGTH,
4
+ TEMPLATE_TEXT_MAX_LENGTH,
5
+ assertArray,
6
+ assertPlainRecord,
7
+ assertString,
8
+ mapCommonBodyFields,
9
+ maybeExample,
10
+ normalizeComponents,
11
+ validationError
12
+ } from "./shared";
13
+ export function buildCreateMessageTemplateBody(input) {
14
+ const record = assertPlainRecord(input, "createMessageTemplate");
15
+ const out = mapCommonBodyFields(record, "createMessageTemplate");
16
+ out.name = assertString(record.name, "name", "createMessageTemplate");
17
+ out.language = assertString(record.language, "language", "createMessageTemplate", 64);
18
+ out.category = assertString(record.category, "category", "createMessageTemplate", 64);
19
+ out.components = normalizeComponents(record.components, "createMessageTemplate", true);
20
+ return out;
21
+ }
22
+ export function buildUpdateMessageTemplateBody(input) {
23
+ const record = assertPlainRecord(input, "updateMessageTemplate");
24
+ const out = mapCommonBodyFields(record, "updateMessageTemplate");
25
+ if (record.category !== undefined)
26
+ out.category = assertString(record.category, "category", "updateMessageTemplate", 64);
27
+ return out;
28
+ }
29
+ export function buildTemplateHeaderComponent(input) {
30
+ const record = assertPlainRecord(input, "buildTemplateHeaderComponent");
31
+ const format = assertString(record.format, "format", "buildTemplateHeaderComponent", 32).toUpperCase();
32
+ const out = { type: "HEADER", format };
33
+ if (format === "TEXT")
34
+ out.text = assertString(record.text, "text", "buildTemplateHeaderComponent", TEMPLATE_TEXT_MAX_LENGTH);
35
+ else if (record.text !== undefined)
36
+ out.text = assertString(record.text, "text", "buildTemplateHeaderComponent", TEMPLATE_TEXT_MAX_LENGTH);
37
+ const example = maybeExample(record.example, "buildTemplateHeaderComponent");
38
+ if (example !== undefined)
39
+ out.example = example;
40
+ return out;
41
+ }
42
+ export function buildTemplateBodyComponent(input) {
43
+ const record = assertPlainRecord(input, "buildTemplateBodyComponent");
44
+ const out = { type: "BODY", text: assertString(record.text, "text", "buildTemplateBodyComponent", TEMPLATE_TEXT_MAX_LENGTH) };
45
+ const example = maybeExample(record.example, "buildTemplateBodyComponent");
46
+ if (example !== undefined)
47
+ out.example = example;
48
+ return out;
49
+ }
50
+ export function buildTemplateFooterComponent(input) {
51
+ const record = assertPlainRecord(input, "buildTemplateFooterComponent");
52
+ return { type: "FOOTER", text: assertString(record.text, "text", "buildTemplateFooterComponent", TEMPLATE_SHORT_TEXT_MAX_LENGTH) };
53
+ }
54
+ function normalizeButton(input, index) {
55
+ const record = assertPlainRecord(input, "buildTemplateButtonComponent", `buttons[${index}]`);
56
+ const type = assertString(record.type, "button.type", "buildTemplateButtonComponent", 32).toUpperCase();
57
+ const out = { type };
58
+ if (record.text !== undefined)
59
+ out.text = assertString(record.text, "button.text", "buildTemplateButtonComponent", TEMPLATE_SHORT_TEXT_MAX_LENGTH);
60
+ switch (type) {
61
+ case "QUICK_REPLY":
62
+ out.text = assertString(record.text, "button.text", "buildTemplateButtonComponent", TEMPLATE_SHORT_TEXT_MAX_LENGTH);
63
+ break;
64
+ case "URL":
65
+ out.text = assertString(record.text, "button.text", "buildTemplateButtonComponent", TEMPLATE_SHORT_TEXT_MAX_LENGTH);
66
+ out.url = assertString(record.url, "button.url", "buildTemplateButtonComponent", TEMPLATE_TEXT_MAX_LENGTH);
67
+ break;
68
+ case "PHONE_NUMBER":
69
+ out.text = assertString(record.text, "button.text", "buildTemplateButtonComponent", TEMPLATE_SHORT_TEXT_MAX_LENGTH);
70
+ out.phone_number = assertString(record.phoneNumber, "button.phoneNumber", "buildTemplateButtonComponent", TEMPLATE_SHORT_TEXT_MAX_LENGTH);
71
+ break;
72
+ case "COPY_CODE":
73
+ out.example = assertString(record.example, "button.example", "buildTemplateButtonComponent", TEMPLATE_SHORT_TEXT_MAX_LENGTH);
74
+ break;
75
+ case "CATALOG":
76
+ break;
77
+ case "FLOW":
78
+ out.text = assertString(record.text, "button.text", "buildTemplateButtonComponent", TEMPLATE_SHORT_TEXT_MAX_LENGTH);
79
+ if (record.flowId !== undefined)
80
+ out.flow_id = assertString(record.flowId, "button.flowId", "buildTemplateButtonComponent", TEMPLATE_SHORT_TEXT_MAX_LENGTH);
81
+ if (record.flowName !== undefined)
82
+ out.flow_name = assertString(record.flowName, "button.flowName", "buildTemplateButtonComponent", TEMPLATE_SHORT_TEXT_MAX_LENGTH);
83
+ if (record.flowAction !== undefined)
84
+ out.flow_action = assertString(record.flowAction, "button.flowAction", "buildTemplateButtonComponent", TEMPLATE_SHORT_TEXT_MAX_LENGTH);
85
+ if (record.navigateScreen !== undefined)
86
+ out.navigate_screen = assertString(record.navigateScreen, "button.navigateScreen", "buildTemplateButtonComponent", TEMPLATE_SHORT_TEXT_MAX_LENGTH);
87
+ break;
88
+ default:
89
+ throw validationError(`Invalid buildTemplateButtonComponent input: unsupported button type ${JSON.stringify(type)}.`);
90
+ }
91
+ return out;
92
+ }
93
+ export function buildTemplateButtonComponent(input) {
94
+ const record = assertPlainRecord(input, "buildTemplateButtonComponent");
95
+ const rawButtons = assertArray(record.buttons, "buttons", 1, TEMPLATE_MAX_BUTTONS, "buildTemplateButtonComponent");
96
+ const buttons = [];
97
+ for (let index = 0;index < rawButtons.length; index += 1) {
98
+ buttons.push(normalizeButton(rawButtons[index], index));
99
+ }
100
+ return { type: "BUTTONS", buttons };
101
+ }
@@ -0,0 +1,54 @@
1
+ import type { CreateMessageTemplateBody, DeleteMessageTemplateInput, GetMessageTemplateInput, ListMessageTemplatesInput, TemplateDetails, TemplateListResponse, TemplateMutationResponse, UpdateMessageTemplateBody } from "./types";
2
+ declare const listMessageTemplatesRaw: import("../..").EndpointCallable<{
3
+ wabaId: string;
4
+ fields?: string;
5
+ status?: string;
6
+ category?: string;
7
+ language?: string;
8
+ name?: string;
9
+ content?: string;
10
+ name_or_content?: string;
11
+ quality_score?: string;
12
+ limit?: string;
13
+ after?: string;
14
+ }, never, TemplateListResponse>;
15
+ export declare const listMessageTemplates: {
16
+ (client: Parameters<typeof listMessageTemplatesRaw>[0], params: ListMessageTemplatesInput, body?: never, opts?: Parameters<typeof listMessageTemplatesRaw>[3]): Promise<TemplateListResponse>;
17
+ readonly definition: typeof listMessageTemplatesRaw.definition;
18
+ };
19
+ declare const getMessageTemplateRaw: import("../..").EndpointCallable<{
20
+ templateId: string;
21
+ fields?: string;
22
+ }, never, TemplateDetails>;
23
+ export declare const getMessageTemplate: {
24
+ (client: Parameters<typeof getMessageTemplateRaw>[0], params: GetMessageTemplateInput, body?: never, opts?: Parameters<typeof getMessageTemplateRaw>[3]): Promise<TemplateDetails>;
25
+ readonly definition: typeof getMessageTemplateRaw.definition;
26
+ };
27
+ declare const createMessageTemplateRaw: import("../..").EndpointCallable<{
28
+ wabaId: string;
29
+ }, CreateMessageTemplateBody, TemplateMutationResponse>;
30
+ export declare const createMessageTemplate: {
31
+ (client: Parameters<typeof createMessageTemplateRaw>[0], params: {
32
+ readonly wabaId: string;
33
+ }, body: CreateMessageTemplateBody, opts?: Parameters<typeof createMessageTemplateRaw>[3]): Promise<TemplateMutationResponse>;
34
+ readonly definition: typeof createMessageTemplateRaw.definition;
35
+ };
36
+ declare const updateMessageTemplateRaw: import("../..").EndpointCallable<{
37
+ templateId: string;
38
+ }, UpdateMessageTemplateBody, TemplateMutationResponse>;
39
+ export declare const updateMessageTemplate: {
40
+ (client: Parameters<typeof updateMessageTemplateRaw>[0], params: {
41
+ readonly templateId: string;
42
+ }, body: UpdateMessageTemplateBody, opts?: Parameters<typeof updateMessageTemplateRaw>[3]): Promise<TemplateMutationResponse>;
43
+ readonly definition: typeof updateMessageTemplateRaw.definition;
44
+ };
45
+ declare const deleteMessageTemplateRaw: import("../..").EndpointCallable<{
46
+ wabaId: string;
47
+ name: string;
48
+ hsm_id?: string;
49
+ }, never, TemplateMutationResponse>;
50
+ export declare const deleteMessageTemplate: {
51
+ (client: Parameters<typeof deleteMessageTemplateRaw>[0], params: DeleteMessageTemplateInput, body?: never, opts?: Parameters<typeof deleteMessageTemplateRaw>[3]): Promise<TemplateMutationResponse>;
52
+ readonly definition: typeof deleteMessageTemplateRaw.definition;
53
+ };
54
+ export {};
@@ -0,0 +1,98 @@
1
+ import { defineEndpoint } from "../../endpoint";
2
+ import { buildCreateMessageTemplateBody, buildUpdateMessageTemplateBody } from "./builders";
3
+ import { assertPlainRecord, assertString } from "./shared";
4
+ function normalizeListParams(input) {
5
+ const record = assertPlainRecord(input, "listMessageTemplates");
6
+ const out = { wabaId: assertString(record.wabaId, "wabaId", "listMessageTemplates") };
7
+ for (const [publicKey, graphKey] of [
8
+ ["fields", "fields"],
9
+ ["status", "status"],
10
+ ["category", "category"],
11
+ ["language", "language"],
12
+ ["name", "name"],
13
+ ["content", "content"],
14
+ ["nameOrContent", "name_or_content"],
15
+ ["qualityScore", "quality_score"],
16
+ ["limit", "limit"],
17
+ ["after", "after"]
18
+ ]) {
19
+ if (record[publicKey] !== undefined)
20
+ out[graphKey] = assertString(record[publicKey], publicKey, "listMessageTemplates");
21
+ }
22
+ return out;
23
+ }
24
+ function normalizeGetParams(input) {
25
+ const record = assertPlainRecord(input, "getMessageTemplate");
26
+ const out = { templateId: assertString(record.templateId, "templateId", "getMessageTemplate") };
27
+ if (record.fields !== undefined)
28
+ out.fields = assertString(record.fields, "fields", "getMessageTemplate");
29
+ return out;
30
+ }
31
+ function normalizeDeleteParams(input) {
32
+ const record = assertPlainRecord(input, "deleteMessageTemplate");
33
+ const out = {
34
+ wabaId: assertString(record.wabaId, "wabaId", "deleteMessageTemplate"),
35
+ name: assertString(record.name, "name", "deleteMessageTemplate")
36
+ };
37
+ if (record.templateId !== undefined)
38
+ out.hsm_id = assertString(record.templateId, "templateId", "deleteMessageTemplate");
39
+ return out;
40
+ }
41
+ const listMessageTemplatesRaw = defineEndpoint({
42
+ method: "GET",
43
+ pathTemplate: "/{wabaId}/message_templates",
44
+ params: {
45
+ wabaId: { in: "path", required: true },
46
+ fields: { in: "query" },
47
+ status: { in: "query" },
48
+ category: { in: "query" },
49
+ language: { in: "query" },
50
+ name: { in: "query" },
51
+ content: { in: "query" },
52
+ name_or_content: { in: "query" },
53
+ quality_score: { in: "query" },
54
+ limit: { in: "query" },
55
+ after: { in: "query" }
56
+ }
57
+ });
58
+ export const listMessageTemplates = Object.assign(async function listMessageTemplates(client, params, body, opts) {
59
+ return listMessageTemplatesRaw(client, normalizeListParams(params), body, opts);
60
+ }, { definition: listMessageTemplatesRaw.definition });
61
+ const getMessageTemplateRaw = defineEndpoint({
62
+ method: "GET",
63
+ pathTemplate: "/{templateId}",
64
+ params: { templateId: { in: "path", required: true }, fields: { in: "query" } }
65
+ });
66
+ export const getMessageTemplate = Object.assign(async function getMessageTemplate(client, params, body, opts) {
67
+ return getMessageTemplateRaw(client, normalizeGetParams(params), body, opts);
68
+ }, { definition: getMessageTemplateRaw.definition });
69
+ const createMessageTemplateRaw = defineEndpoint({
70
+ method: "POST",
71
+ pathTemplate: "/{wabaId}/message_templates",
72
+ params: { wabaId: { in: "path", required: true } },
73
+ bodyContentType: "application/json",
74
+ buildBody: buildCreateMessageTemplateBody
75
+ });
76
+ export const createMessageTemplate = Object.assign(async function createMessageTemplate(client, params, body, opts) {
77
+ const record = assertPlainRecord(params, "createMessageTemplate", "params");
78
+ return createMessageTemplateRaw(client, { wabaId: assertString(record.wabaId, "wabaId", "createMessageTemplate") }, body, opts);
79
+ }, { definition: createMessageTemplateRaw.definition });
80
+ const updateMessageTemplateRaw = defineEndpoint({
81
+ method: "POST",
82
+ pathTemplate: "/{templateId}",
83
+ params: { templateId: { in: "path", required: true } },
84
+ bodyContentType: "application/json",
85
+ buildBody: buildUpdateMessageTemplateBody
86
+ });
87
+ export const updateMessageTemplate = Object.assign(async function updateMessageTemplate(client, params, body, opts) {
88
+ const record = assertPlainRecord(params, "updateMessageTemplate", "params");
89
+ return updateMessageTemplateRaw(client, { templateId: assertString(record.templateId, "templateId", "updateMessageTemplate") }, body, opts);
90
+ }, { definition: updateMessageTemplateRaw.definition });
91
+ const deleteMessageTemplateRaw = defineEndpoint({
92
+ method: "DELETE",
93
+ pathTemplate: "/{wabaId}/message_templates",
94
+ params: { wabaId: { in: "path", required: true }, name: { in: "query", required: true }, hsm_id: { in: "query" } }
95
+ });
96
+ export const deleteMessageTemplate = Object.assign(async function deleteMessageTemplate(client, params, body, opts) {
97
+ return deleteMessageTemplateRaw(client, normalizeDeleteParams(params), body, opts);
98
+ }, { definition: deleteMessageTemplateRaw.definition });
@@ -0,0 +1,5 @@
1
+ export { createMessageTemplate, deleteMessageTemplate, getMessageTemplate, listMessageTemplates, updateMessageTemplate } from "./callables";
2
+ export { buildCreateMessageTemplateBody, buildUpdateMessageTemplateBody, buildTemplateBodyComponent, buildTemplateButtonComponent, buildTemplateFooterComponent, buildTemplateHeaderComponent } from "./builders";
3
+ export { validateTemplateParameterCounts } from "./validation";
4
+ export type { GraphPaging } from "../wabaEndpoints";
5
+ export type { CreateMessageTemplateBody, DeleteMessageTemplateInput, GetMessageTemplateInput, ListMessageTemplatesInput, SendTemplateComponentForValidation, TemplateBodyComponentInput, TemplateButtonInput, TemplateButtonsComponentInput, TemplateButtonType, TemplateCategory, TemplateComponent, TemplateDefinitionForValidation, TemplateDetails, TemplateFooterComponentInput, TemplateHeaderComponentInput, TemplateHeaderFormat, TemplateLanguageCode, TemplateListResponse, TemplateMutationResponse, TemplateParameterFormat, TemplateQualityScore, TemplateStatus, UpdateMessageTemplateBody } from "./types";
@@ -0,0 +1,16 @@
1
+ export {
2
+ createMessageTemplate,
3
+ deleteMessageTemplate,
4
+ getMessageTemplate,
5
+ listMessageTemplates,
6
+ updateMessageTemplate
7
+ } from "./callables";
8
+ export {
9
+ buildCreateMessageTemplateBody,
10
+ buildUpdateMessageTemplateBody,
11
+ buildTemplateBodyComponent,
12
+ buildTemplateButtonComponent,
13
+ buildTemplateFooterComponent,
14
+ buildTemplateHeaderComponent
15
+ } from "./builders";
16
+ export { validateTemplateParameterCounts } from "./validation";
@@ -0,0 +1,21 @@
1
+ import { GraphRequestValidationError } from "../../errors";
2
+ import type { TemplateComponent } from "./types";
3
+ export declare const TEMPLATE_NAME_MAX_LENGTH = 512;
4
+ export declare const TEMPLATE_TEXT_MAX_LENGTH = 4096;
5
+ export declare const TEMPLATE_SHORT_TEXT_MAX_LENGTH = 1024;
6
+ export declare const TEMPLATE_MAX_COMPONENTS = 20;
7
+ export declare const TEMPLATE_MAX_BUTTONS = 10;
8
+ export declare const TEMPLATE_MAX_ARRAY = 100;
9
+ export declare const TEMPLATE_MAX_DEPTH = 8;
10
+ export declare function hasControlChar(value: string): boolean;
11
+ export declare function validationError(message: string): GraphRequestValidationError;
12
+ export declare function isPlainObject(value: unknown): value is Record<string, unknown>;
13
+ export declare function assertPlainRecord(value: unknown, helperName: string, path?: string): Record<string, unknown>;
14
+ export declare function assertString(value: unknown, fieldName: string, helperName: string, maxLength?: number): string;
15
+ export declare function maybeString(value: unknown, fieldName: string, helperName: string, maxLength?: number): string | undefined;
16
+ export declare function assertArray(value: unknown, fieldName: string, min: number, max: number, helperName: string): readonly unknown[];
17
+ export declare function safeJsonClone(value: unknown, helperName: string, path?: string, seen?: WeakSet<object>, depth?: number): unknown;
18
+ export declare function maybeExample(value: unknown, helperName: string): unknown;
19
+ export declare function normalizeComponent(value: unknown, helperName: string): TemplateComponent;
20
+ export declare function normalizeComponents(value: unknown, helperName: string, required: boolean): TemplateComponent[] | undefined;
21
+ export declare function mapCommonBodyFields(record: Record<string, unknown>, helperName: string): Record<string, unknown>;
@@ -0,0 +1,203 @@
1
+ import { GraphRequestValidationError } from "../../errors";
2
+ export const TEMPLATE_NAME_MAX_LENGTH = 512;
3
+ export const TEMPLATE_TEXT_MAX_LENGTH = 4096;
4
+ export const TEMPLATE_SHORT_TEXT_MAX_LENGTH = 1024;
5
+ export const TEMPLATE_MAX_COMPONENTS = 20;
6
+ export const TEMPLATE_MAX_BUTTONS = 10;
7
+ export const TEMPLATE_MAX_ARRAY = 100;
8
+ export const TEMPLATE_MAX_DEPTH = 8;
9
+ export function hasControlChar(value) {
10
+ for (let i = 0;i < value.length; i += 1) {
11
+ const code = value.charCodeAt(i);
12
+ if (code < 32 || code === 127)
13
+ return true;
14
+ }
15
+ return false;
16
+ }
17
+ export function validationError(message) {
18
+ return new GraphRequestValidationError(message);
19
+ }
20
+ export function isPlainObject(value) {
21
+ return typeof value === "object" && value !== null && !Array.isArray(value);
22
+ }
23
+ export function assertPlainRecord(value, helperName, path = "input") {
24
+ if (!isPlainObject(value)) {
25
+ throw validationError(`Invalid ${helperName} input: ${path} must be an object.`);
26
+ }
27
+ const proto = Object.getPrototypeOf(value);
28
+ if (proto !== Object.prototype && proto !== null) {
29
+ throw validationError(`Invalid ${helperName} input: ${path} must be a plain object.`);
30
+ }
31
+ const descriptors = Object.getOwnPropertyDescriptors(value);
32
+ if (Object.prototype.hasOwnProperty.call(descriptors, "toJSON") || "toJSON" in value) {
33
+ throw validationError(`Invalid ${helperName} input: ${path} must not define toJSON.`);
34
+ }
35
+ for (const [key, descriptor] of Object.entries(descriptors)) {
36
+ if (typeof descriptor.get === "function" || typeof descriptor.set === "function") {
37
+ throw validationError(`Invalid ${helperName} input: ${path}.${key} must not use accessors.`);
38
+ }
39
+ if (typeof descriptor.value === "function" || typeof descriptor.value === "symbol") {
40
+ throw validationError(`Invalid ${helperName} input: ${path}.${key} must be JSON-serializable.`);
41
+ }
42
+ }
43
+ return value;
44
+ }
45
+ export function assertString(value, fieldName, helperName, maxLength = TEMPLATE_NAME_MAX_LENGTH) {
46
+ if (typeof value !== "string") {
47
+ throw validationError(`Invalid ${helperName} input: ${fieldName} must be a string.`);
48
+ }
49
+ if (value.length === 0 || value.trim().length === 0) {
50
+ throw validationError(`Invalid ${helperName} input: ${fieldName} must be non-empty.`);
51
+ }
52
+ if (hasControlChar(value)) {
53
+ throw validationError(`Invalid ${helperName} input: ${fieldName} must not contain control characters (CR/LF/NUL/etc.).`);
54
+ }
55
+ if (value.length > maxLength) {
56
+ throw validationError(`Invalid ${helperName} input: ${fieldName} exceeds ${maxLength}-character limit.`);
57
+ }
58
+ return value;
59
+ }
60
+ export function maybeString(value, fieldName, helperName, maxLength = TEMPLATE_NAME_MAX_LENGTH) {
61
+ if (value === undefined)
62
+ return;
63
+ return assertString(value, fieldName, helperName, maxLength);
64
+ }
65
+ export function assertArray(value, fieldName, min, max, helperName) {
66
+ if (!Array.isArray(value)) {
67
+ throw validationError(`Invalid ${helperName} input: ${fieldName} must be an array.`);
68
+ }
69
+ if (value.length < min || value.length > max) {
70
+ throw validationError(`Invalid ${helperName} input: ${fieldName} length must be between ${min} and ${max}.`);
71
+ }
72
+ if (Object.getPrototypeOf(value) !== Array.prototype) {
73
+ throw validationError(`Invalid ${helperName} input: ${fieldName} must use Array.prototype.`);
74
+ }
75
+ if (Object.prototype.hasOwnProperty.call(value, Symbol.iterator) || Object.prototype.hasOwnProperty.call(value, "map")) {
76
+ throw validationError(`Invalid ${helperName} input: ${fieldName} must not override Array.prototype methods.`);
77
+ }
78
+ const descriptors = Object.getOwnPropertyDescriptors(value);
79
+ if (Object.prototype.hasOwnProperty.call(descriptors, "toJSON") || "toJSON" in value) {
80
+ throw validationError(`Invalid ${helperName} input: ${fieldName} must not define toJSON.`);
81
+ }
82
+ const copy = [];
83
+ for (let i = 0;i < value.length; i += 1) {
84
+ if (!Object.prototype.hasOwnProperty.call(value, i)) {
85
+ throw validationError(`Invalid ${helperName} input: ${fieldName} must not contain sparse array holes.`);
86
+ }
87
+ const descriptor = descriptors[String(i)];
88
+ if (descriptor === undefined || typeof descriptor.get === "function" || typeof descriptor.set === "function") {
89
+ throw validationError(`Invalid ${helperName} input: ${fieldName} must not use accessors.`);
90
+ }
91
+ copy.push(descriptor.value);
92
+ }
93
+ return copy;
94
+ }
95
+ export function safeJsonClone(value, helperName, path = "input", seen = new WeakSet, depth = 0) {
96
+ if (depth > TEMPLATE_MAX_DEPTH) {
97
+ throw validationError(`Invalid ${helperName} input: ${path} exceeds maximum nesting depth.`);
98
+ }
99
+ if (value === null || typeof value === "boolean")
100
+ return value;
101
+ if (typeof value === "string") {
102
+ if (hasControlChar(value))
103
+ throw validationError(`Invalid ${helperName} input: ${path} contains control characters.`);
104
+ if (value.length > TEMPLATE_TEXT_MAX_LENGTH)
105
+ throw validationError(`Invalid ${helperName} input: ${path} string exceeds limit.`);
106
+ return value;
107
+ }
108
+ if (typeof value === "number") {
109
+ if (!Number.isFinite(value))
110
+ throw validationError(`Invalid ${helperName} input: ${path} contains a non-finite number.`);
111
+ return value;
112
+ }
113
+ if (value === undefined)
114
+ return;
115
+ if (Array.isArray(value)) {
116
+ if (seen.has(value))
117
+ throw validationError(`Invalid ${helperName} input: ${path} must not contain cycles.`);
118
+ seen.add(value);
119
+ const arr = assertArray(value, path, 0, TEMPLATE_MAX_ARRAY, helperName);
120
+ const out = [];
121
+ for (let index = 0;index < arr.length; index += 1) {
122
+ out.push(safeJsonClone(arr[index], helperName, `${path}[${index}]`, seen, depth + 1));
123
+ }
124
+ seen.delete(value);
125
+ return out;
126
+ }
127
+ const record = assertPlainRecord(value, helperName, path);
128
+ if (seen.has(record))
129
+ throw validationError(`Invalid ${helperName} input: ${path} must not contain cycles.`);
130
+ seen.add(record);
131
+ const out = {};
132
+ for (const [key, nested] of Object.entries(record)) {
133
+ if (key.length === 0 || hasControlChar(key) || key.length > TEMPLATE_SHORT_TEXT_MAX_LENGTH) {
134
+ throw validationError(`Invalid ${helperName} input: ${path} contains an invalid key.`);
135
+ }
136
+ const cloned = safeJsonClone(nested, helperName, `${path}.${key}`, seen, depth + 1);
137
+ if (cloned !== undefined)
138
+ out[key] = cloned;
139
+ }
140
+ seen.delete(record);
141
+ return out;
142
+ }
143
+ export function maybeExample(value, helperName) {
144
+ if (value === undefined)
145
+ return;
146
+ const cloned = safeJsonClone(value, helperName, "example");
147
+ if (isPlainObject(cloned)) {
148
+ const rec = cloned;
149
+ const out = {};
150
+ for (const [key, nested] of Object.entries(rec)) {
151
+ if (key === "headerHandle")
152
+ out.header_handle = nested;
153
+ else if (key === "bodyText")
154
+ out.body_text = nested;
155
+ else
156
+ out[key] = nested;
157
+ }
158
+ return out;
159
+ }
160
+ return cloned;
161
+ }
162
+ export function normalizeComponent(value, helperName) {
163
+ const cloned = safeJsonClone(value, helperName, "component");
164
+ const record = assertPlainRecord(cloned, helperName, "component");
165
+ const type = assertString(record.type, "component.type", helperName, TEMPLATE_SHORT_TEXT_MAX_LENGTH).toUpperCase();
166
+ const out = { ...record, type };
167
+ return out;
168
+ }
169
+ export function normalizeComponents(value, helperName, required) {
170
+ if (value === undefined) {
171
+ if (required)
172
+ throw validationError(`Invalid ${helperName} input: components must be provided.`);
173
+ return;
174
+ }
175
+ const arr = assertArray(value, "components", required ? 1 : 0, TEMPLATE_MAX_COMPONENTS, helperName);
176
+ const out = [];
177
+ for (const entry of arr) {
178
+ out.push(normalizeComponent(entry, helperName));
179
+ }
180
+ return out;
181
+ }
182
+ export function mapCommonBodyFields(record, helperName) {
183
+ const out = {};
184
+ for (const [key, value] of Object.entries(record)) {
185
+ if (value === undefined)
186
+ continue;
187
+ if (key === "parameterFormat")
188
+ out.parameter_format = assertString(value, "parameterFormat", helperName, 32);
189
+ else if (key === "messageSendTtlSeconds") {
190
+ if (typeof value !== "number" || !Number.isInteger(value) || value < 0) {
191
+ throw validationError(`Invalid ${helperName} input: messageSendTtlSeconds must be a non-negative integer.`);
192
+ }
193
+ out.message_send_ttl_seconds = value;
194
+ } else if (key === "components") {
195
+ const components = normalizeComponents(value, helperName, helperName === "createMessageTemplate");
196
+ if (components !== undefined)
197
+ out.components = components;
198
+ } else {
199
+ out[key] = safeJsonClone(value, helperName, key);
200
+ }
201
+ }
202
+ return out;
203
+ }
@@ -0,0 +1,135 @@
1
+ import type { GraphPaging } from "../wabaEndpoints";
2
+ export type TemplateCategory = "MARKETING" | "UTILITY" | "AUTHENTICATION";
3
+ export type TemplateStatus = "APPROVED" | "IN_APPEAL" | "PENDING" | "REJECTED" | "PENDING_DELETION" | "DELETED" | "DISABLED" | "PAUSED" | "LIMIT_EXCEEDED";
4
+ export type TemplateLanguageCode = string;
5
+ export type TemplateParameterFormat = "POSITIONAL" | "NAMED";
6
+ export type TemplateQualityScore = "GREEN" | "YELLOW" | "RED" | "UNKNOWN" | string;
7
+ export type TemplateHeaderFormat = "TEXT" | "IMAGE" | "VIDEO" | "DOCUMENT" | "LOCATION";
8
+ export type TemplateButtonType = "QUICK_REPLY" | "URL" | "PHONE_NUMBER" | "COPY_CODE" | "CATALOG" | "FLOW";
9
+ export interface TemplateComponent {
10
+ readonly type: string;
11
+ readonly [key: string]: unknown;
12
+ }
13
+ export interface TemplateDetails {
14
+ readonly id?: string;
15
+ readonly name?: string;
16
+ readonly language?: string;
17
+ readonly status?: TemplateStatus | string;
18
+ readonly category?: TemplateCategory | string;
19
+ readonly components?: readonly TemplateComponent[];
20
+ readonly parameter_format?: TemplateParameterFormat;
21
+ readonly message_send_ttl_seconds?: number;
22
+ readonly quality_score?: TemplateQualityScore | Record<string, unknown>;
23
+ readonly [key: string]: unknown;
24
+ }
25
+ export interface TemplateListResponse {
26
+ readonly data?: readonly TemplateDetails[];
27
+ readonly paging?: GraphPaging;
28
+ }
29
+ export interface TemplateMutationResponse {
30
+ readonly id?: string;
31
+ readonly success?: boolean;
32
+ readonly [key: string]: unknown;
33
+ }
34
+ export interface ListMessageTemplatesInput {
35
+ readonly wabaId: string;
36
+ readonly fields?: string;
37
+ readonly status?: TemplateStatus | string;
38
+ readonly category?: TemplateCategory | string;
39
+ readonly language?: TemplateLanguageCode;
40
+ readonly name?: string;
41
+ readonly content?: string;
42
+ readonly nameOrContent?: string;
43
+ readonly qualityScore?: string;
44
+ readonly limit?: string;
45
+ readonly after?: string;
46
+ }
47
+ export interface GetMessageTemplateInput {
48
+ readonly templateId: string;
49
+ readonly fields?: string;
50
+ }
51
+ export interface CreateMessageTemplateBody {
52
+ readonly name: string;
53
+ readonly language: TemplateLanguageCode;
54
+ readonly category: TemplateCategory | string;
55
+ readonly components: readonly TemplateComponent[];
56
+ readonly parameterFormat?: TemplateParameterFormat;
57
+ readonly messageSendTtlSeconds?: number;
58
+ readonly [key: string]: unknown;
59
+ }
60
+ export interface UpdateMessageTemplateBody {
61
+ readonly category?: TemplateCategory | string;
62
+ readonly components?: readonly TemplateComponent[];
63
+ readonly parameterFormat?: TemplateParameterFormat;
64
+ readonly messageSendTtlSeconds?: number;
65
+ readonly [key: string]: unknown;
66
+ }
67
+ export interface DeleteMessageTemplateInput {
68
+ readonly wabaId: string;
69
+ readonly name: string;
70
+ /** Maps to Graph query parameter `hsm_id`. */
71
+ readonly templateId?: string;
72
+ }
73
+ export interface TemplateHeaderComponentInput {
74
+ readonly format: TemplateHeaderFormat;
75
+ readonly text?: string;
76
+ readonly example?: unknown;
77
+ readonly [key: string]: unknown;
78
+ }
79
+ export interface TemplateBodyComponentInput {
80
+ readonly text: string;
81
+ readonly example?: unknown;
82
+ readonly [key: string]: unknown;
83
+ }
84
+ export interface TemplateFooterComponentInput {
85
+ readonly text: string;
86
+ readonly [key: string]: unknown;
87
+ }
88
+ export type TemplateButtonInput = {
89
+ readonly type: "QUICK_REPLY";
90
+ readonly text: string;
91
+ readonly [key: string]: unknown;
92
+ } | {
93
+ readonly type: "URL";
94
+ readonly text: string;
95
+ readonly url: string;
96
+ readonly [key: string]: unknown;
97
+ } | {
98
+ readonly type: "PHONE_NUMBER";
99
+ readonly text: string;
100
+ readonly phoneNumber: string;
101
+ readonly [key: string]: unknown;
102
+ } | {
103
+ readonly type: "COPY_CODE";
104
+ readonly example: string;
105
+ readonly [key: string]: unknown;
106
+ } | {
107
+ readonly type: "CATALOG";
108
+ readonly text?: string;
109
+ readonly [key: string]: unknown;
110
+ } | {
111
+ readonly type: "FLOW";
112
+ readonly text: string;
113
+ readonly flowId?: string;
114
+ readonly flowName?: string;
115
+ readonly flowAction?: string;
116
+ readonly navigateScreen?: string;
117
+ readonly [key: string]: unknown;
118
+ };
119
+ export interface TemplateButtonsComponentInput {
120
+ readonly buttons: readonly TemplateButtonInput[];
121
+ readonly [key: string]: unknown;
122
+ }
123
+ export interface TemplateDefinitionForValidation {
124
+ readonly parameterFormat?: TemplateParameterFormat;
125
+ readonly parameter_format?: TemplateParameterFormat;
126
+ readonly components: readonly TemplateComponent[];
127
+ }
128
+ export type SendTemplateComponentForValidation = {
129
+ readonly type: string;
130
+ readonly subType?: string;
131
+ readonly sub_type?: string;
132
+ readonly index?: string;
133
+ readonly parameters?: readonly unknown[];
134
+ readonly [key: string]: unknown;
135
+ };
File without changes
@@ -0,0 +1,2 @@
1
+ import type { SendTemplateComponentForValidation, TemplateDefinitionForValidation } from "./types";
2
+ export declare function validateTemplateParameterCounts(definition: TemplateDefinitionForValidation, sendComponents: readonly SendTemplateComponentForValidation[]): void;