@wats/graph 0.3.26 → 0.3.28
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/README.md +1 -1
- package/dist/endpoints/businessManagement.d.ts +372 -1
- package/dist/endpoints/businessManagement.js +775 -5
- package/dist/endpoints/calling.d.ts +41 -1
- package/dist/endpoints/calling.js +100 -2
- package/dist/endpoints/flows/dataChannel.d.ts +139 -0
- package/dist/endpoints/flows/dataChannel.js +252 -0
- package/dist/endpoints/flows/flowDsl.d.ts +306 -0
- package/dist/endpoints/flows/flowDsl.js +610 -0
- package/dist/endpoints/flows/flowMediaUpload.d.ts +102 -0
- package/dist/endpoints/flows/flowMediaUpload.js +188 -0
- package/dist/endpoints/flows/index.d.ts +9 -1
- package/dist/endpoints/flows/index.js +70 -0
- package/dist/endpoints/flows/metricsMigration.d.ts +36 -0
- package/dist/endpoints/flows/metricsMigration.js +207 -0
- package/dist/endpoints/flows/types.d.ts +145 -0
- package/dist/endpoints/flows/types.js +14 -0
- package/dist/endpoints/flows.d.ts +2 -2
- package/dist/endpoints/flows.js +49 -1
- package/dist/endpoints/messages/builders-interactive.d.ts +9 -1
- package/dist/endpoints/messages/builders-interactive.js +49 -0
- package/dist/endpoints/messages/builders-template.d.ts +2 -1
- package/dist/endpoints/messages/builders-template.js +53 -2
- package/dist/endpoints/messages/deepLinks.d.ts +12 -0
- package/dist/endpoints/messages/deepLinks.js +38 -0
- package/dist/endpoints/messages/index.d.ts +4 -3
- package/dist/endpoints/messages/index.js +13 -2
- package/dist/endpoints/messages/types.d.ts +26 -3
- package/dist/endpoints/messages/validation.d.ts +7 -0
- package/dist/endpoints/messages/validation.js +12 -0
- package/dist/endpoints/oauth.d.ts +57 -0
- package/dist/endpoints/oauth.js +116 -0
- package/dist/endpoints/templates/advanced.d.ts +280 -0
- package/dist/endpoints/templates/advanced.js +519 -0
- package/dist/endpoints/templates/builders.js +21 -1
- package/dist/endpoints/templates/index.d.ts +2 -0
- package/dist/endpoints/templates/index.js +15 -0
- package/dist/endpoints/templates/shared.js +7 -1
- package/dist/endpoints/templates/types.d.ts +17 -2
- package/dist/endpoints/templates.d.ts +2 -1
- package/dist/endpoints/templates.js +14 -1
- package/dist/endpoints/wabaEndpoints.d.ts +4 -4
- package/dist/endpoints/wabaEndpoints.js +56 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.js +1 -0
- package/dist/nodeMedia.d.ts +57 -0
- package/dist/nodeMedia.js +178 -0
- package/dist/subclients/phoneNumberClient.d.ts +92 -2
- package/dist/subclients/phoneNumberClient.js +133 -1
- package/dist/subclients/wabaClient.d.ts +82 -2
- package/dist/subclients/wabaClient.js +102 -2
- package/package.json +10 -3
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import type { CryptoProvider } from "@wats/crypto";
|
|
2
|
+
/**
|
|
3
|
+
* Maximum accepted size of a downloaded CDN media file, in bytes (25 MiB).
|
|
4
|
+
* Meta's Flow media-upload cap is 25 MiB; this finite bound prevents a hostile
|
|
5
|
+
* or oversized `cdnFile` from driving unbounded allocation or hashing work.
|
|
6
|
+
*/
|
|
7
|
+
export declare const FLOW_MEDIA_MAX_BYTES = 26214400;
|
|
8
|
+
/**
|
|
9
|
+
* Thrown by `decryptFlowMedia` / `decryptFlowMediaFile` for ANY failure on the
|
|
10
|
+
* decrypt path — missing or malformed fields, invalid base64, wrong key/IV
|
|
11
|
+
* length, hash mismatch, HMAC mismatch, AES failure, bad PKCS#7 padding, or a
|
|
12
|
+
* plaintext-hash mismatch. Carries no key/IV/plaintext/tag material; the
|
|
13
|
+
* `cause` is preserved for server-side diagnostics but the public message is a
|
|
14
|
+
* FIXED string so nothing sensitive is leaked.
|
|
15
|
+
*/
|
|
16
|
+
export declare class FlowMediaDecryptionError extends Error {
|
|
17
|
+
readonly cause?: unknown;
|
|
18
|
+
constructor(cause?: unknown);
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Thrown when the injected CryptoProvider lacks a capability the media-upload
|
|
22
|
+
* runtime requires (`sha256`, `hmacSha256`, `timingSafeEqual`, or
|
|
23
|
+
* `aesCbcDecrypt`). This is a server-side misconfiguration, distinct from a
|
|
24
|
+
* malformed inbound payload.
|
|
25
|
+
*/
|
|
26
|
+
export declare class FlowMediaCryptoUnavailableError extends Error {
|
|
27
|
+
constructor(capability: string);
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* The `encryption_metadata` object Meta sends inside each per-file Flow media
|
|
31
|
+
* dict. All five fields are base64 strings; Meta uses these exact snake_case
|
|
32
|
+
* keys on the wire (kept snake_case to match, like `EncryptedFlowRequestWire`).
|
|
33
|
+
*
|
|
34
|
+
* Decoded byte lengths: `iv` = 16, `encryption_key` = 32, `hmac_key` = 32,
|
|
35
|
+
* `encrypted_hash` = 32 (base64(SHA256(full cdn_file))),
|
|
36
|
+
* `plaintext_hash` = 32 (base64(SHA256(unpadded plaintext))).
|
|
37
|
+
*/
|
|
38
|
+
export interface FlowMediaEncryptionMetadata {
|
|
39
|
+
readonly iv: string;
|
|
40
|
+
readonly encryption_key: string;
|
|
41
|
+
readonly hmac_key: string;
|
|
42
|
+
readonly encrypted_hash: string;
|
|
43
|
+
readonly plaintext_hash: string;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* One per-file dict from the Flow request's `data[key]` array (e.g.
|
|
47
|
+
* `data.photo_picker[index]`). `media_id`, `cdn_url`, and `file_name` are
|
|
48
|
+
* sibling string fields to `encryption_metadata`.
|
|
49
|
+
*/
|
|
50
|
+
export interface FlowMediaEncryptedFile {
|
|
51
|
+
readonly media_id: string;
|
|
52
|
+
readonly cdn_url: string;
|
|
53
|
+
readonly file_name: string;
|
|
54
|
+
readonly encryption_metadata: FlowMediaEncryptionMetadata;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* The decrypted result for a single media file: the raw plaintext bytes plus
|
|
58
|
+
* the identifying metadata the caller needs to persist the file. Mirrors
|
|
59
|
+
* pywa's `FlowRequestDecryptedMedia` frozen dataclass
|
|
60
|
+
* (`{ media_id, filename, data }`).
|
|
61
|
+
*/
|
|
62
|
+
export interface DecryptedFlowMedia {
|
|
63
|
+
readonly mediaId: string;
|
|
64
|
+
readonly filename: string;
|
|
65
|
+
readonly data: Uint8Array;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Decrypt a Flow media-upload CDN file (REFERENCE-151 §7).
|
|
69
|
+
*
|
|
70
|
+
* Steps:
|
|
71
|
+
* 1. Validate `cdnFile` is a non-empty Uint8Array ≤ FLOW_MEDIA_MAX_BYTES and
|
|
72
|
+
* long enough to hold one AES block + the 10-byte HMAC trailer
|
|
73
|
+
* (≥ 26 bytes).
|
|
74
|
+
* 2. Validate `metadata` is a plain object with all five non-empty string
|
|
75
|
+
* fields; base64-decode `iv`/`encryption_key`/`hmac_key` and verify their
|
|
76
|
+
* decoded lengths (16/32/32).
|
|
77
|
+
* 3. Split `ciphertext = cdnFile[:-10]`, `hmac10 = cdnFile[-10:]`.
|
|
78
|
+
* 4. VERIFY `base64(SHA256(cdnFile)) == metadata.encrypted_hash` (full file).
|
|
79
|
+
* 5. VERIFY `HMAC-SHA256(hmacKey, iv ‖ ciphertext)[:10] == hmac10`.
|
|
80
|
+
* 6. `plaintext = crypto.aesCbcDecrypt(encryptionKey, iv, ciphertext)`
|
|
81
|
+
* (PKCS#7 unpad is automatic in the provider).
|
|
82
|
+
* 7. VERIFY `base64(SHA256(plaintext)) == metadata.plaintext_hash`.
|
|
83
|
+
* 8. RETURN the raw plaintext Uint8Array.
|
|
84
|
+
*
|
|
85
|
+
* ALL three verifications use `crypto.timingSafeEqual` (constant-time) — a
|
|
86
|
+
* strict improvement over pywa's non-constant-time `!=`. On ANY failure throws
|
|
87
|
+
* {@link FlowMediaDecryptionError} (fixed message, no secret echo); a missing
|
|
88
|
+
* provider capability throws {@link FlowMediaCryptoUnavailableError}. Never
|
|
89
|
+
* throws a host TypeError/RangeError.
|
|
90
|
+
*/
|
|
91
|
+
export declare function decryptFlowMedia(crypto: CryptoProvider, cdnFile: Uint8Array, metadata: FlowMediaEncryptionMetadata): Promise<Uint8Array>;
|
|
92
|
+
/**
|
|
93
|
+
* Validate a per-file media dict (`media_id` / `cdn_url` / `file_name` /
|
|
94
|
+
* `encryption_metadata`) and delegate to {@link decryptFlowMedia}. Returns a
|
|
95
|
+
* {@link DecryptedFlowMedia} carrying the media id, filename, and raw
|
|
96
|
+
* plaintext bytes — mirroring pywa's `FlowRequestDecryptedMedia`.
|
|
97
|
+
*
|
|
98
|
+
* `cdn_url` is NOT fetched here — the caller downloads the CDN file and passes
|
|
99
|
+
* the raw bytes as `cdnFile`. This keeps the function pure and transport-
|
|
100
|
+
* agnostic (no live Meta calls, no network inside the crypto path).
|
|
101
|
+
*/
|
|
102
|
+
export declare function decryptFlowMediaFile(crypto: CryptoProvider, encryptedFile: FlowMediaEncryptedFile, cdnFile: Uint8Array): Promise<DecryptedFlowMedia>;
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
import { flowIsPlainObject } from "./shared.js";
|
|
2
|
+
export const FLOW_MEDIA_MAX_BYTES = 26214400;
|
|
3
|
+
const FLOW_MEDIA_MAX_BASE64_FIELD_LENGTH = 256;
|
|
4
|
+
const FLOW_MEDIA_HMAC_TAG_LENGTH = 10;
|
|
5
|
+
const FLOW_MEDIA_AES_BLOCK_SIZE = 16;
|
|
6
|
+
|
|
7
|
+
export class FlowMediaDecryptionError extends Error {
|
|
8
|
+
cause;
|
|
9
|
+
constructor(cause) {
|
|
10
|
+
super("Flow media could not be decrypted");
|
|
11
|
+
this.name = "FlowMediaDecryptionError";
|
|
12
|
+
if (cause !== undefined)
|
|
13
|
+
this.cause = cause;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export class FlowMediaCryptoUnavailableError extends Error {
|
|
18
|
+
constructor(capability) {
|
|
19
|
+
super(`CryptoProvider does not implement required capability: ${capability}`);
|
|
20
|
+
this.name = "FlowMediaCryptoUnavailableError";
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
const TEXT_ENCODER = new TextEncoder;
|
|
24
|
+
function decodeBase64Field(value) {
|
|
25
|
+
if (typeof value !== "string" || value.length === 0 || value.trim().length === 0) {
|
|
26
|
+
throw new FlowMediaDecryptionError;
|
|
27
|
+
}
|
|
28
|
+
if (value.length > FLOW_MEDIA_MAX_BASE64_FIELD_LENGTH) {
|
|
29
|
+
throw new FlowMediaDecryptionError;
|
|
30
|
+
}
|
|
31
|
+
let binary;
|
|
32
|
+
try {
|
|
33
|
+
binary = atob(value);
|
|
34
|
+
} catch (cause) {
|
|
35
|
+
throw new FlowMediaDecryptionError(cause);
|
|
36
|
+
}
|
|
37
|
+
if (binary.length === 0) {
|
|
38
|
+
throw new FlowMediaDecryptionError;
|
|
39
|
+
}
|
|
40
|
+
const out = new Uint8Array(binary.length);
|
|
41
|
+
for (let i = 0;i < binary.length; i += 1) {
|
|
42
|
+
out[i] = binary.charCodeAt(i);
|
|
43
|
+
}
|
|
44
|
+
return out;
|
|
45
|
+
}
|
|
46
|
+
function encodeBase64(bytes) {
|
|
47
|
+
let binary = "";
|
|
48
|
+
for (let i = 0;i < bytes.length; i += 1) {
|
|
49
|
+
binary += String.fromCharCode(bytes[i]);
|
|
50
|
+
}
|
|
51
|
+
return btoa(binary);
|
|
52
|
+
}
|
|
53
|
+
function concatBytes(a, b) {
|
|
54
|
+
const out = new Uint8Array(a.byteLength + b.byteLength);
|
|
55
|
+
out.set(a, 0);
|
|
56
|
+
out.set(b, a.byteLength);
|
|
57
|
+
return out;
|
|
58
|
+
}
|
|
59
|
+
function assertMetadataShape(metadata) {
|
|
60
|
+
if (!flowIsPlainObject(metadata)) {
|
|
61
|
+
throw new FlowMediaDecryptionError;
|
|
62
|
+
}
|
|
63
|
+
const record = metadata;
|
|
64
|
+
const requiredKeys = [
|
|
65
|
+
"iv",
|
|
66
|
+
"encryption_key",
|
|
67
|
+
"hmac_key",
|
|
68
|
+
"encrypted_hash",
|
|
69
|
+
"plaintext_hash"
|
|
70
|
+
];
|
|
71
|
+
for (const key of requiredKeys) {
|
|
72
|
+
const value = record[key];
|
|
73
|
+
if (typeof value !== "string" || value.length === 0) {
|
|
74
|
+
throw new FlowMediaDecryptionError;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
export async function decryptFlowMedia(crypto, cdnFile, metadata) {
|
|
79
|
+
if (typeof crypto.sha256 !== "function") {
|
|
80
|
+
throw new FlowMediaCryptoUnavailableError("sha256");
|
|
81
|
+
}
|
|
82
|
+
if (typeof crypto.hmacSha256 !== "function") {
|
|
83
|
+
throw new FlowMediaCryptoUnavailableError("hmacSha256");
|
|
84
|
+
}
|
|
85
|
+
if (typeof crypto.timingSafeEqual !== "function") {
|
|
86
|
+
throw new FlowMediaCryptoUnavailableError("timingSafeEqual");
|
|
87
|
+
}
|
|
88
|
+
if (typeof crypto.aesCbcDecrypt !== "function") {
|
|
89
|
+
throw new FlowMediaCryptoUnavailableError("aesCbcDecrypt");
|
|
90
|
+
}
|
|
91
|
+
if (!(cdnFile instanceof Uint8Array)) {
|
|
92
|
+
throw new FlowMediaDecryptionError;
|
|
93
|
+
}
|
|
94
|
+
if (cdnFile.byteLength === 0) {
|
|
95
|
+
throw new FlowMediaDecryptionError;
|
|
96
|
+
}
|
|
97
|
+
if (cdnFile.byteLength > FLOW_MEDIA_MAX_BYTES) {
|
|
98
|
+
throw new FlowMediaDecryptionError;
|
|
99
|
+
}
|
|
100
|
+
if (cdnFile.byteLength < FLOW_MEDIA_AES_BLOCK_SIZE + FLOW_MEDIA_HMAC_TAG_LENGTH) {
|
|
101
|
+
throw new FlowMediaDecryptionError;
|
|
102
|
+
}
|
|
103
|
+
assertMetadataShape(metadata);
|
|
104
|
+
let iv;
|
|
105
|
+
let encryptionKey;
|
|
106
|
+
let hmacKey;
|
|
107
|
+
try {
|
|
108
|
+
iv = decodeBase64Field(metadata.iv);
|
|
109
|
+
encryptionKey = decodeBase64Field(metadata.encryption_key);
|
|
110
|
+
hmacKey = decodeBase64Field(metadata.hmac_key);
|
|
111
|
+
} catch (cause) {
|
|
112
|
+
if (cause instanceof FlowMediaCryptoUnavailableError)
|
|
113
|
+
throw cause;
|
|
114
|
+
throw new FlowMediaDecryptionError(cause);
|
|
115
|
+
}
|
|
116
|
+
if (iv.byteLength !== FLOW_MEDIA_AES_BLOCK_SIZE) {
|
|
117
|
+
throw new FlowMediaDecryptionError;
|
|
118
|
+
}
|
|
119
|
+
if (encryptionKey.byteLength !== 32) {
|
|
120
|
+
throw new FlowMediaDecryptionError;
|
|
121
|
+
}
|
|
122
|
+
if (hmacKey.byteLength !== 32) {
|
|
123
|
+
throw new FlowMediaDecryptionError;
|
|
124
|
+
}
|
|
125
|
+
const ciphertext = cdnFile.subarray(0, cdnFile.byteLength - FLOW_MEDIA_HMAC_TAG_LENGTH);
|
|
126
|
+
const hmac10 = cdnFile.subarray(cdnFile.byteLength - FLOW_MEDIA_HMAC_TAG_LENGTH);
|
|
127
|
+
let encHash;
|
|
128
|
+
try {
|
|
129
|
+
encHash = await crypto.sha256(cdnFile);
|
|
130
|
+
} catch (cause) {
|
|
131
|
+
throw new FlowMediaDecryptionError(cause);
|
|
132
|
+
}
|
|
133
|
+
const encHashB64Bytes = TEXT_ENCODER.encode(encodeBase64(encHash));
|
|
134
|
+
const expectedEncHashB64Bytes = TEXT_ENCODER.encode(metadata.encrypted_hash);
|
|
135
|
+
if (!crypto.timingSafeEqual(encHashB64Bytes, expectedEncHashB64Bytes)) {
|
|
136
|
+
throw new FlowMediaDecryptionError;
|
|
137
|
+
}
|
|
138
|
+
const macInput = concatBytes(iv, ciphertext);
|
|
139
|
+
let macFull;
|
|
140
|
+
try {
|
|
141
|
+
macFull = await crypto.hmacSha256(hmacKey, macInput);
|
|
142
|
+
} catch (cause) {
|
|
143
|
+
throw new FlowMediaDecryptionError(cause);
|
|
144
|
+
}
|
|
145
|
+
const mac10 = macFull.subarray(0, FLOW_MEDIA_HMAC_TAG_LENGTH);
|
|
146
|
+
if (!crypto.timingSafeEqual(mac10, hmac10)) {
|
|
147
|
+
throw new FlowMediaDecryptionError;
|
|
148
|
+
}
|
|
149
|
+
let plaintext;
|
|
150
|
+
try {
|
|
151
|
+
plaintext = await crypto.aesCbcDecrypt(encryptionKey, iv, ciphertext);
|
|
152
|
+
} catch (cause) {
|
|
153
|
+
throw new FlowMediaDecryptionError(cause);
|
|
154
|
+
}
|
|
155
|
+
let ptHash;
|
|
156
|
+
try {
|
|
157
|
+
ptHash = await crypto.sha256(plaintext);
|
|
158
|
+
} catch (cause) {
|
|
159
|
+
throw new FlowMediaDecryptionError(cause);
|
|
160
|
+
}
|
|
161
|
+
const ptHashB64Bytes = TEXT_ENCODER.encode(encodeBase64(ptHash));
|
|
162
|
+
const expectedPtHashB64Bytes = TEXT_ENCODER.encode(metadata.plaintext_hash);
|
|
163
|
+
if (!crypto.timingSafeEqual(ptHashB64Bytes, expectedPtHashB64Bytes)) {
|
|
164
|
+
throw new FlowMediaDecryptionError;
|
|
165
|
+
}
|
|
166
|
+
return plaintext;
|
|
167
|
+
}
|
|
168
|
+
export async function decryptFlowMediaFile(crypto, encryptedFile, cdnFile) {
|
|
169
|
+
if (!flowIsPlainObject(encryptedFile)) {
|
|
170
|
+
throw new FlowMediaDecryptionError;
|
|
171
|
+
}
|
|
172
|
+
const record = encryptedFile;
|
|
173
|
+
const mediaId = record.media_id;
|
|
174
|
+
const fileName = record.file_name;
|
|
175
|
+
const metadata = record.encryption_metadata;
|
|
176
|
+
if (typeof mediaId !== "string" || mediaId.length === 0) {
|
|
177
|
+
throw new FlowMediaDecryptionError;
|
|
178
|
+
}
|
|
179
|
+
if (typeof fileName !== "string" || fileName.length === 0) {
|
|
180
|
+
throw new FlowMediaDecryptionError;
|
|
181
|
+
}
|
|
182
|
+
const cdnUrl = record.cdn_url;
|
|
183
|
+
if (typeof cdnUrl !== "string" || cdnUrl.length === 0) {
|
|
184
|
+
throw new FlowMediaDecryptionError;
|
|
185
|
+
}
|
|
186
|
+
const data = await decryptFlowMedia(crypto, cdnFile, metadata);
|
|
187
|
+
return { mediaId, filename: fileName, data };
|
|
188
|
+
}
|
|
@@ -1,6 +1,14 @@
|
|
|
1
1
|
export { createFlow, deleteFlow, deprecateFlow, getFlow, getFlowAssets, listFlows, publishFlow, updateFlowJson, updateFlowMetadata } from "./callables.js";
|
|
2
2
|
export { FLOW_JSON_MAX_ARRAY_LENGTH, FLOW_JSON_MAX_BYTES, FLOW_JSON_MAX_COMPONENTS, FLOW_JSON_MAX_DEPTH, FLOW_JSON_MAX_SCREENS, FLOW_JSON_MAX_STRING_LENGTH, FLOW_MAX_CATEGORIES } from "./shared.js";
|
|
3
3
|
export { buildFlowJson, validateFlowJson } from "./flowJson.js";
|
|
4
|
+
export { FLOW_DSL_MAX_CONTROL_DEPTH, calendarPicker, checkboxGroup, chipsSelector, completeAction, dataExchangeAction, dataSource, datePicker, documentPicker, dropdown, embeddedLink, flowJson, footer, form, ifComponent, image, imageCarousel, imageCarouselItem, navigateAction, navigationItem, navigationList, openUrlAction, optIn, photoPicker, radioButtonsGroup, richText, screen, singleColumnLayout, switchComponent, textArea, textBody, textCaption, textHeading, textInput, textSubheading, updateDataAction } from "./flowDsl.js";
|
|
5
|
+
export type { FlowJsonProps, FormProps, IfProps, ImageCarouselItemProps, NavigateActionProps, NavigationItemProps, ScreenProps, SwitchProps } from "./flowDsl.js";
|
|
4
6
|
export { buildFlowCloseResponse, buildFlowErrorResponse, buildFlowScreenResponse } from "./dataExchange.js";
|
|
7
|
+
export { FLOW_DATA_CHANNEL_MAX_BYTES, FLOW_ENDPOINT_STATUS, FlowCryptoUnavailableError, FlowRequestDecryptionError, FlowSignatureError, FlowTokenNoLongerValidError, buildFlowErrorAckResponse, buildFlowPingResponse, decryptRequest, encryptResponse, flowRequestHasError, handleFlowRequest } from "./dataChannel.js";
|
|
8
|
+
export type { DecryptedFlowRequest, FlowEndpointResult, FlowRequestHandler, HandleFlowRequestOptions } from "./dataChannel.js";
|
|
9
|
+
export { FLOW_MEDIA_MAX_BYTES, FlowMediaCryptoUnavailableError, FlowMediaDecryptionError, decryptFlowMedia, decryptFlowMediaFile } from "./flowMediaUpload.js";
|
|
10
|
+
export type { DecryptedFlowMedia, FlowMediaEncryptedFile, FlowMediaEncryptionMetadata } from "./flowMediaUpload.js";
|
|
11
|
+
export { buildFlowMetricField, getFlowMetrics, migrateFlows } from "./metricsMigration.js";
|
|
12
|
+
export { KNOWN_FLOW_METRIC_NAMES, KNOWN_FLOW_METRIC_GRANULARITIES, MIGRATE_FLOWS_MAX_NAMES, MIGRATE_FLOWS_NAME_MAX_LENGTH } from "./types.js";
|
|
5
13
|
export type { GraphPaging } from "../wabaEndpoints.js";
|
|
6
|
-
export type { CreateFlowBody, FlowAssetDetails, FlowAssetsResponse, FlowCategory, FlowCloseResponse, FlowCloseResponseInput, FlowDetails, FlowErrorResponse, FlowErrorResponseInput, FlowJson, FlowListResponse, FlowMutationResponse, FlowScreenResponse, FlowScreenResponseInput, FlowStatus, GetFlowAssetsInput, GetFlowInput, ListFlowsInput, UpdateFlowJsonBody, UpdateFlowMetadataBody } from "./types.js";
|
|
14
|
+
export type { CreateFlowBody, EncryptedFlowRequest, EncryptedFlowRequestInput, EncryptedFlowRequestWire, FlowAssetDetails, FlowAssetsResponse, FlowCategory, FlowCloseResponse, FlowCloseResponseInput, FlowDetails, FlowErrorResponse, FlowErrorResponseInput, FlowJson, FlowListResponse, FlowMetric, FlowMetricDataPoint, FlowMetricGranularity, FlowMetricName, FlowMutationResponse, FlowRequest, FlowRequestAction, FlowResponsePayload, FlowScreenResponse, FlowScreenResponseInput, FlowStatus, GetFlowAssetsInput, GetFlowInput, GetFlowMetricsInput, ListFlowsInput, MigratedFlow, MigratedFlowError, MigrateFlowsInput, MigrateFlowsResponse, UpdateFlowJsonBody, UpdateFlowMetadataBody } from "./types.js";
|
|
@@ -19,8 +19,78 @@ export {
|
|
|
19
19
|
FLOW_MAX_CATEGORIES
|
|
20
20
|
} from "./shared.js";
|
|
21
21
|
export { buildFlowJson, validateFlowJson } from "./flowJson.js";
|
|
22
|
+
export {
|
|
23
|
+
FLOW_DSL_MAX_CONTROL_DEPTH,
|
|
24
|
+
calendarPicker,
|
|
25
|
+
checkboxGroup,
|
|
26
|
+
chipsSelector,
|
|
27
|
+
completeAction,
|
|
28
|
+
dataExchangeAction,
|
|
29
|
+
dataSource,
|
|
30
|
+
datePicker,
|
|
31
|
+
documentPicker,
|
|
32
|
+
dropdown,
|
|
33
|
+
embeddedLink,
|
|
34
|
+
flowJson,
|
|
35
|
+
footer,
|
|
36
|
+
form,
|
|
37
|
+
ifComponent,
|
|
38
|
+
image,
|
|
39
|
+
imageCarousel,
|
|
40
|
+
imageCarouselItem,
|
|
41
|
+
navigateAction,
|
|
42
|
+
navigationItem,
|
|
43
|
+
navigationList,
|
|
44
|
+
openUrlAction,
|
|
45
|
+
optIn,
|
|
46
|
+
photoPicker,
|
|
47
|
+
radioButtonsGroup,
|
|
48
|
+
richText,
|
|
49
|
+
screen,
|
|
50
|
+
singleColumnLayout,
|
|
51
|
+
switchComponent,
|
|
52
|
+
textArea,
|
|
53
|
+
textBody,
|
|
54
|
+
textCaption,
|
|
55
|
+
textHeading,
|
|
56
|
+
textInput,
|
|
57
|
+
textSubheading,
|
|
58
|
+
updateDataAction
|
|
59
|
+
} from "./flowDsl.js";
|
|
22
60
|
export {
|
|
23
61
|
buildFlowCloseResponse,
|
|
24
62
|
buildFlowErrorResponse,
|
|
25
63
|
buildFlowScreenResponse
|
|
26
64
|
} from "./dataExchange.js";
|
|
65
|
+
export {
|
|
66
|
+
FLOW_DATA_CHANNEL_MAX_BYTES,
|
|
67
|
+
FLOW_ENDPOINT_STATUS,
|
|
68
|
+
FlowCryptoUnavailableError,
|
|
69
|
+
FlowRequestDecryptionError,
|
|
70
|
+
FlowSignatureError,
|
|
71
|
+
FlowTokenNoLongerValidError,
|
|
72
|
+
buildFlowErrorAckResponse,
|
|
73
|
+
buildFlowPingResponse,
|
|
74
|
+
decryptRequest,
|
|
75
|
+
encryptResponse,
|
|
76
|
+
flowRequestHasError,
|
|
77
|
+
handleFlowRequest
|
|
78
|
+
} from "./dataChannel.js";
|
|
79
|
+
export {
|
|
80
|
+
FLOW_MEDIA_MAX_BYTES,
|
|
81
|
+
FlowMediaCryptoUnavailableError,
|
|
82
|
+
FlowMediaDecryptionError,
|
|
83
|
+
decryptFlowMedia,
|
|
84
|
+
decryptFlowMediaFile
|
|
85
|
+
} from "./flowMediaUpload.js";
|
|
86
|
+
export {
|
|
87
|
+
buildFlowMetricField,
|
|
88
|
+
getFlowMetrics,
|
|
89
|
+
migrateFlows
|
|
90
|
+
} from "./metricsMigration.js";
|
|
91
|
+
export {
|
|
92
|
+
KNOWN_FLOW_METRIC_NAMES,
|
|
93
|
+
KNOWN_FLOW_METRIC_GRANULARITIES,
|
|
94
|
+
MIGRATE_FLOWS_MAX_NAMES,
|
|
95
|
+
MIGRATE_FLOWS_NAME_MAX_LENGTH
|
|
96
|
+
} from "./types.js";
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { EndpointInvokeOptions } from "../../endpoint.js";
|
|
2
|
+
import type { GraphClient } from "../../client.js";
|
|
3
|
+
import type { FlowMetric, FlowMetricResponse, GetFlowMetricsInput, MigrateFlowsInput, MigrateFlowsResponse } from "./types.js";
|
|
4
|
+
/**
|
|
5
|
+
* Build the exact Graph `fields` string for a Flow metrics request:
|
|
6
|
+
* `metric.name({NAME}).granularity({G}).since({S}).until({U})` with the
|
|
7
|
+
* optional `.since(...)`/`.until(...)` segments omitted when not provided.
|
|
8
|
+
*
|
|
9
|
+
* Rejects unknown metric/granularity, malformed date strings, control
|
|
10
|
+
* characters, and unsafe id values. Returns the assembled field string
|
|
11
|
+
* ready to be sent as the `fields` query parameter.
|
|
12
|
+
*/
|
|
13
|
+
export declare function buildFlowMetricField(input: GetFlowMetricsInput): string;
|
|
14
|
+
declare const getFlowMetricsRaw: import("../../endpoint.js").EndpointCallable<{
|
|
15
|
+
flowId: string;
|
|
16
|
+
fields: string;
|
|
17
|
+
}, never, FlowMetricResponse>;
|
|
18
|
+
export declare const getFlowMetrics: {
|
|
19
|
+
(client: GraphClient, params: GetFlowMetricsInput, body?: never, opts?: EndpointInvokeOptions): Promise<FlowMetric>;
|
|
20
|
+
readonly definition: typeof getFlowMetricsRaw.definition;
|
|
21
|
+
};
|
|
22
|
+
interface MigrateRawResponse {
|
|
23
|
+
readonly migrated_flows?: readonly unknown[];
|
|
24
|
+
readonly failed_flows?: readonly unknown[];
|
|
25
|
+
readonly [key: string]: unknown;
|
|
26
|
+
}
|
|
27
|
+
declare const migrateFlowsRaw: import("../../endpoint.js").EndpointCallable<{
|
|
28
|
+
destinationWabaId: string;
|
|
29
|
+
source_waba_id: string;
|
|
30
|
+
source_flow_names: string;
|
|
31
|
+
}, never, MigrateRawResponse>;
|
|
32
|
+
export declare const migrateFlows: {
|
|
33
|
+
(client: GraphClient, params: MigrateFlowsInput, body?: never, opts?: EndpointInvokeOptions): Promise<MigrateFlowsResponse>;
|
|
34
|
+
readonly definition: typeof migrateFlowsRaw.definition;
|
|
35
|
+
};
|
|
36
|
+
export {};
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
import { defineEndpoint } from "../../endpoint.js";
|
|
2
|
+
import {
|
|
3
|
+
KNOWN_FLOW_METRIC_GRANULARITIES,
|
|
4
|
+
KNOWN_FLOW_METRIC_NAMES,
|
|
5
|
+
MIGRATE_FLOWS_MAX_NAMES,
|
|
6
|
+
MIGRATE_FLOWS_NAME_MAX_LENGTH
|
|
7
|
+
} from "./types.js";
|
|
8
|
+
import {
|
|
9
|
+
flowArray,
|
|
10
|
+
flowAssertPlainRecord,
|
|
11
|
+
flowError,
|
|
12
|
+
flowHasControlChar,
|
|
13
|
+
flowString
|
|
14
|
+
} from "./shared.js";
|
|
15
|
+
const HELPER_METRICS = "getFlowMetrics";
|
|
16
|
+
const KNOWN_METRIC_NAME_SET = new Set(KNOWN_FLOW_METRIC_NAMES);
|
|
17
|
+
const KNOWN_GRANULARITY_SET = new Set(KNOWN_FLOW_METRIC_GRANULARITIES);
|
|
18
|
+
const DATE_REGEXP = /^\d{4}-\d{2}-\d{2}$/;
|
|
19
|
+
function assertKnownMetricName(value) {
|
|
20
|
+
if (!KNOWN_METRIC_NAME_SET.has(value)) {
|
|
21
|
+
throw flowError(`Invalid ${HELPER_METRICS} input: name must be one of ${KNOWN_FLOW_METRIC_NAMES.join(", ")} (got ${JSON.stringify(value)}).`);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
function assertKnownGranularity(value) {
|
|
25
|
+
if (!KNOWN_GRANULARITY_SET.has(value)) {
|
|
26
|
+
throw flowError(`Invalid ${HELPER_METRICS} input: granularity must be one of ${KNOWN_FLOW_METRIC_GRANULARITIES.join(", ")} (got ${JSON.stringify(value)}).`);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
function assertDateString(value, fieldName) {
|
|
30
|
+
if (!DATE_REGEXP.test(value)) {
|
|
31
|
+
throw flowError(`Invalid ${HELPER_METRICS} input: ${fieldName} must be a YYYY-MM-DD string (got ${JSON.stringify(value)}).`);
|
|
32
|
+
}
|
|
33
|
+
if (flowHasControlChar(value)) {
|
|
34
|
+
throw flowError(`Invalid ${HELPER_METRICS} input: ${fieldName} must not contain control characters.`);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
export function buildFlowMetricField(input) {
|
|
38
|
+
const record = flowAssertPlainRecord(input, HELPER_METRICS, "input");
|
|
39
|
+
const name = flowString(record.name, "name", HELPER_METRICS, 128);
|
|
40
|
+
assertKnownMetricName(name);
|
|
41
|
+
const granularity = flowString(record.granularity, "granularity", HELPER_METRICS, 32);
|
|
42
|
+
assertKnownGranularity(granularity);
|
|
43
|
+
const since = record.since === undefined ? undefined : flowString(record.since, "since", HELPER_METRICS, 16);
|
|
44
|
+
const until = record.until === undefined ? undefined : flowString(record.until, "until", HELPER_METRICS, 16);
|
|
45
|
+
if (since !== undefined)
|
|
46
|
+
assertDateString(since, "since");
|
|
47
|
+
if (until !== undefined)
|
|
48
|
+
assertDateString(until, "until");
|
|
49
|
+
let field = `metric.name(${name}).granularity(${granularity})`;
|
|
50
|
+
if (since !== undefined)
|
|
51
|
+
field += `.since(${since})`;
|
|
52
|
+
if (until !== undefined)
|
|
53
|
+
field += `.until(${until})`;
|
|
54
|
+
return field;
|
|
55
|
+
}
|
|
56
|
+
function normalizeFlowMetricsParams(input) {
|
|
57
|
+
const record = flowAssertPlainRecord(input, HELPER_METRICS, "params");
|
|
58
|
+
const flowId = flowString(record.flowId, "flowId", HELPER_METRICS);
|
|
59
|
+
const fields = buildFlowMetricField(input);
|
|
60
|
+
return { flowId, fields };
|
|
61
|
+
}
|
|
62
|
+
function parseFlowMetric(raw) {
|
|
63
|
+
const out = {};
|
|
64
|
+
if (raw !== null && typeof raw === "object") {
|
|
65
|
+
for (const [key, value] of Object.entries(raw)) {
|
|
66
|
+
if (key !== "metric")
|
|
67
|
+
out[key] = value;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
const metric = raw?.metric;
|
|
71
|
+
if (metric !== null && typeof metric === "object" && metric !== undefined) {
|
|
72
|
+
const metricRec = metric;
|
|
73
|
+
if (typeof metricRec.name === "string")
|
|
74
|
+
out.name = metricRec.name;
|
|
75
|
+
if (typeof metricRec.granularity === "string")
|
|
76
|
+
out.granularity = metricRec.granularity;
|
|
77
|
+
const dataPoints = metricRec.data_points;
|
|
78
|
+
if (Array.isArray(dataPoints)) {
|
|
79
|
+
out.dataPoints = dataPoints.map((entry) => normalizeDataPoint(entry));
|
|
80
|
+
}
|
|
81
|
+
for (const [key, value] of Object.entries(metricRec)) {
|
|
82
|
+
if (key === "name" || key === "granularity" || key === "data_points")
|
|
83
|
+
continue;
|
|
84
|
+
out[key] = value;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return out;
|
|
88
|
+
}
|
|
89
|
+
function normalizeDataPoint(entry) {
|
|
90
|
+
if (entry === null || typeof entry !== "object")
|
|
91
|
+
return entry;
|
|
92
|
+
const rec = entry;
|
|
93
|
+
const out = {};
|
|
94
|
+
if (typeof rec.timestamp === "string")
|
|
95
|
+
out.timestamp = rec.timestamp;
|
|
96
|
+
if (Array.isArray(rec.data)) {
|
|
97
|
+
out.data = rec.data.map((item) => item);
|
|
98
|
+
}
|
|
99
|
+
for (const [key, value] of Object.entries(rec)) {
|
|
100
|
+
if (key === "timestamp" || key === "data")
|
|
101
|
+
continue;
|
|
102
|
+
out[key] = value;
|
|
103
|
+
}
|
|
104
|
+
return out;
|
|
105
|
+
}
|
|
106
|
+
const getFlowMetricsRaw = defineEndpoint({
|
|
107
|
+
method: "GET",
|
|
108
|
+
pathTemplate: "/{flowId}",
|
|
109
|
+
params: {
|
|
110
|
+
flowId: { in: "path", required: true },
|
|
111
|
+
fields: { in: "query", required: true }
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
export const getFlowMetrics = Object.assign(async function getFlowMetrics(client, params, body, opts) {
|
|
115
|
+
const normalized = normalizeFlowMetricsParams(params);
|
|
116
|
+
const raw = await getFlowMetricsRaw(client, normalized, body, opts);
|
|
117
|
+
return parseFlowMetric(raw);
|
|
118
|
+
}, { definition: getFlowMetricsRaw.definition });
|
|
119
|
+
const HELPER_MIGRATE = "migrateFlows";
|
|
120
|
+
function normalizeMigrateFlowsParams(input) {
|
|
121
|
+
const record = flowAssertPlainRecord(input, HELPER_MIGRATE, "params");
|
|
122
|
+
const destinationWabaId = flowString(record.destinationWabaId, "destinationWabaId", HELPER_MIGRATE);
|
|
123
|
+
const sourceWabaId = flowString(record.sourceWabaId, "sourceWabaId", HELPER_MIGRATE);
|
|
124
|
+
const namesArr = flowArray(record.sourceFlowNames, "sourceFlowNames", 1, MIGRATE_FLOWS_MAX_NAMES, HELPER_MIGRATE);
|
|
125
|
+
const names = [];
|
|
126
|
+
for (let index = 0;index < namesArr.length; index += 1) {
|
|
127
|
+
const raw = namesArr[index];
|
|
128
|
+
const name = flowString(raw, `sourceFlowNames[${index}]`, HELPER_MIGRATE, MIGRATE_FLOWS_NAME_MAX_LENGTH);
|
|
129
|
+
if (name.includes(",")) {
|
|
130
|
+
throw flowError(`Invalid ${HELPER_MIGRATE} input: sourceFlowNames[${index}] must not contain a comma (comma is the wire separator).`);
|
|
131
|
+
}
|
|
132
|
+
names.push(name);
|
|
133
|
+
}
|
|
134
|
+
return {
|
|
135
|
+
destinationWabaId,
|
|
136
|
+
source_waba_id: sourceWabaId,
|
|
137
|
+
source_flow_names: names.join(",")
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
function parseMigrateFlowsResponse(raw) {
|
|
141
|
+
const out = {};
|
|
142
|
+
if (raw !== null && typeof raw === "object") {
|
|
143
|
+
for (const [key, value] of Object.entries(raw)) {
|
|
144
|
+
if (key !== "migrated_flows" && key !== "failed_flows")
|
|
145
|
+
out[key] = value;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
const migrated = raw?.migrated_flows;
|
|
149
|
+
if (Array.isArray(migrated)) {
|
|
150
|
+
out.migratedFlows = migrated.map((entry) => normalizeMigratedFlow(entry));
|
|
151
|
+
}
|
|
152
|
+
const failed = raw?.failed_flows;
|
|
153
|
+
if (Array.isArray(failed)) {
|
|
154
|
+
out.failedFlows = failed.map((entry) => normalizeMigratedFlowError(entry));
|
|
155
|
+
}
|
|
156
|
+
return out;
|
|
157
|
+
}
|
|
158
|
+
function normalizeMigratedFlow(entry) {
|
|
159
|
+
if (entry === null || typeof entry !== "object")
|
|
160
|
+
return entry;
|
|
161
|
+
const rec = entry;
|
|
162
|
+
const out = {};
|
|
163
|
+
if (typeof rec.source_name === "string")
|
|
164
|
+
out.sourceName = rec.source_name;
|
|
165
|
+
if (typeof rec.source_id === "string")
|
|
166
|
+
out.sourceId = rec.source_id;
|
|
167
|
+
if (typeof rec.migrated_id === "string")
|
|
168
|
+
out.migratedId = rec.migrated_id;
|
|
169
|
+
for (const [key, value] of Object.entries(rec)) {
|
|
170
|
+
if (key === "source_name" || key === "source_id" || key === "migrated_id")
|
|
171
|
+
continue;
|
|
172
|
+
out[key] = value;
|
|
173
|
+
}
|
|
174
|
+
return out;
|
|
175
|
+
}
|
|
176
|
+
function normalizeMigratedFlowError(entry) {
|
|
177
|
+
if (entry === null || typeof entry !== "object")
|
|
178
|
+
return entry;
|
|
179
|
+
const rec = entry;
|
|
180
|
+
const out = {};
|
|
181
|
+
if (typeof rec.source_name === "string")
|
|
182
|
+
out.sourceName = rec.source_name;
|
|
183
|
+
if (rec.error_code !== undefined)
|
|
184
|
+
out.errorCode = rec.error_code;
|
|
185
|
+
if (typeof rec.error_message === "string")
|
|
186
|
+
out.errorMessage = rec.error_message;
|
|
187
|
+
for (const [key, value] of Object.entries(rec)) {
|
|
188
|
+
if (key === "source_name" || key === "error_code" || key === "error_message")
|
|
189
|
+
continue;
|
|
190
|
+
out[key] = value;
|
|
191
|
+
}
|
|
192
|
+
return out;
|
|
193
|
+
}
|
|
194
|
+
const migrateFlowsRaw = defineEndpoint({
|
|
195
|
+
method: "POST",
|
|
196
|
+
pathTemplate: "/{destinationWabaId}/migrate_flows",
|
|
197
|
+
params: {
|
|
198
|
+
destinationWabaId: { in: "path", required: true },
|
|
199
|
+
source_waba_id: { in: "query", required: true },
|
|
200
|
+
source_flow_names: { in: "query", required: true }
|
|
201
|
+
}
|
|
202
|
+
});
|
|
203
|
+
export const migrateFlows = Object.assign(async function migrateFlows(client, params, body, opts) {
|
|
204
|
+
const normalized = normalizeMigrateFlowsParams(params);
|
|
205
|
+
const raw = await migrateFlowsRaw(client, normalized, body, opts);
|
|
206
|
+
return parseMigrateFlowsResponse(raw);
|
|
207
|
+
}, { definition: migrateFlowsRaw.definition });
|