@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.
- package/LICENSE +21 -0
- package/README.md +3 -0
- package/dist/client.d.ts +41 -0
- package/dist/client.js +392 -0
- package/dist/createFetchTransport.d.ts +6 -0
- package/dist/createFetchTransport.js +46 -0
- package/dist/createMockTransport.d.ts +20 -0
- package/dist/createMockTransport.js +120 -0
- package/dist/endpoint.d.ts +26 -0
- package/dist/endpoint.js +223 -0
- package/dist/endpoints/businessManagement.d.ts +157 -0
- package/dist/endpoints/businessManagement.js +277 -0
- package/dist/endpoints/calling.d.ts +65 -0
- package/dist/endpoints/calling.js +284 -0
- package/dist/endpoints/flows/callables.d.ts +90 -0
- package/dist/endpoints/flows/callables.js +97 -0
- package/dist/endpoints/flows/dataExchange.d.ts +4 -0
- package/dist/endpoints/flows/dataExchange.js +40 -0
- package/dist/endpoints/flows/flowJson.d.ts +18 -0
- package/dist/endpoints/flows/flowJson.js +214 -0
- package/dist/endpoints/flows/index.d.ts +6 -0
- package/dist/endpoints/flows/index.js +26 -0
- package/dist/endpoints/flows/shared.d.ts +17 -0
- package/dist/endpoints/flows/shared.js +113 -0
- package/dist/endpoints/flows/types.d.ts +105 -0
- package/dist/endpoints/flows/types.js +0 -0
- package/dist/endpoints/flows.d.ts +2 -0
- package/dist/endpoints/flows.js +23 -0
- package/dist/endpoints/media.d.ts +123 -0
- package/dist/endpoints/media.js +976 -0
- package/dist/endpoints/messages.d.ts +375 -0
- package/dist/endpoints/messages.js +675 -0
- package/dist/endpoints/templates/builders.d.ts +9 -0
- package/dist/endpoints/templates/builders.js +101 -0
- package/dist/endpoints/templates/callables.d.ts +54 -0
- package/dist/endpoints/templates/callables.js +98 -0
- package/dist/endpoints/templates/index.d.ts +5 -0
- package/dist/endpoints/templates/index.js +16 -0
- package/dist/endpoints/templates/shared.d.ts +21 -0
- package/dist/endpoints/templates/shared.js +203 -0
- package/dist/endpoints/templates/types.d.ts +135 -0
- package/dist/endpoints/templates/types.js +0 -0
- package/dist/endpoints/templates/validation.d.ts +2 -0
- package/dist/endpoints/templates/validation.js +113 -0
- package/dist/endpoints/templates.d.ts +2 -0
- package/dist/endpoints/templates.js +14 -0
- package/dist/endpoints/waba/index.d.ts +4 -0
- package/dist/endpoints/waba/index.js +1 -0
- package/dist/endpoints/waba/phoneNumbers.d.ts +25 -0
- package/dist/endpoints/waba/phoneNumbers.js +23 -0
- package/dist/endpoints/waba/types.d.ts +8 -0
- package/dist/endpoints/waba/types.js +0 -0
- package/dist/endpoints/wabaEndpoints.d.ts +6 -0
- package/dist/endpoints/wabaEndpoints.js +38 -0
- package/dist/errorRegistry.d.ts +45 -0
- package/dist/errorRegistry.js +68 -0
- package/dist/errorSubclasses.d.ts +277 -0
- package/dist/errorSubclasses.js +613 -0
- package/dist/errors.d.ts +92 -0
- package/dist/errors.js +250 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.js +48 -0
- package/dist/internal/validation/arrays.d.ts +19 -0
- package/dist/internal/validation/arrays.js +102 -0
- package/dist/internal/validation/errors.d.ts +3 -0
- package/dist/internal/validation/errors.js +13 -0
- package/dist/internal/validation/headers.d.ts +15 -0
- package/dist/internal/validation/headers.js +77 -0
- package/dist/internal/validation/index.d.ts +8 -0
- package/dist/internal/validation/index.js +8 -0
- package/dist/internal/validation/json.d.ts +10 -0
- package/dist/internal/validation/json.js +79 -0
- package/dist/internal/validation/options.d.ts +5 -0
- package/dist/internal/validation/options.js +36 -0
- package/dist/internal/validation/paths.d.ts +8 -0
- package/dist/internal/validation/paths.js +37 -0
- package/dist/internal/validation/records.d.ts +20 -0
- package/dist/internal/validation/records.js +69 -0
- package/dist/internal/validation/strings.d.ts +12 -0
- package/dist/internal/validation/strings.js +46 -0
- package/dist/pagination.d.ts +35 -0
- package/dist/pagination.js +160 -0
- package/dist/subclients/phoneNumberClient.d.ts +60 -0
- package/dist/subclients/phoneNumberClient.js +194 -0
- package/dist/subclients/wabaClient.d.ts +66 -0
- package/dist/subclients/wabaClient.js +180 -0
- package/dist/transport.d.ts +32 -0
- package/dist/transport.js +5 -0
- package/package.json +70 -0
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { graphValidationError, wrapGraphValidation } from "./errors";
|
|
2
|
+
import { assertPlainDataRecord } from "./records";
|
|
3
|
+
export function copyOptionalParamsObject(params, helperName) {
|
|
4
|
+
if (params === undefined)
|
|
5
|
+
return {};
|
|
6
|
+
const record = assertPlainDataRecord(params, {
|
|
7
|
+
helperName,
|
|
8
|
+
path: "params"
|
|
9
|
+
});
|
|
10
|
+
const out = {};
|
|
11
|
+
const descriptors = wrapGraphValidation(`Invalid ${helperName} params: descriptors could not be inspected.`, () => Object.getOwnPropertyDescriptors(record));
|
|
12
|
+
for (const [key, descriptor] of Object.entries(descriptors)) {
|
|
13
|
+
if (descriptor.value !== undefined)
|
|
14
|
+
out[key] = descriptor.value;
|
|
15
|
+
}
|
|
16
|
+
return out;
|
|
17
|
+
}
|
|
18
|
+
export function splitRequiredStringDataProp(params, propName, helperName) {
|
|
19
|
+
const record = assertPlainDataRecord(params, {
|
|
20
|
+
helperName,
|
|
21
|
+
path: "params"
|
|
22
|
+
});
|
|
23
|
+
const descriptors = wrapGraphValidation(`Invalid ${helperName} params: descriptors could not be inspected.`, () => Object.getOwnPropertyDescriptors(record));
|
|
24
|
+
const prop = descriptors[propName];
|
|
25
|
+
if (prop === undefined || typeof prop.value !== "string") {
|
|
26
|
+
throw graphValidationError(`Invalid ${helperName} params: ${propName} must be a string data property.`);
|
|
27
|
+
}
|
|
28
|
+
const rest = {};
|
|
29
|
+
for (const [key, descriptor] of Object.entries(descriptors)) {
|
|
30
|
+
if (key === propName)
|
|
31
|
+
continue;
|
|
32
|
+
if (descriptor.value !== undefined)
|
|
33
|
+
rest[key] = descriptor.value;
|
|
34
|
+
}
|
|
35
|
+
return { value: prop.value, rest };
|
|
36
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export interface SafePathOptions {
|
|
2
|
+
readonly helperName: string;
|
|
3
|
+
readonly fieldName: string;
|
|
4
|
+
readonly maxLength?: number;
|
|
5
|
+
readonly maxDecodeRounds?: number;
|
|
6
|
+
}
|
|
7
|
+
export declare function assertSafePathSegment(value: unknown, opts: SafePathOptions): string;
|
|
8
|
+
export declare function assertRepeatedlyDecodedSafePathId(value: unknown, opts: SafePathOptions): string;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { graphValidationError } from "./errors";
|
|
2
|
+
import { assertBoundedString, hasAsciiControlChar } from "./strings";
|
|
3
|
+
function assertSafePathSegmentString(value, opts) {
|
|
4
|
+
if (hasAsciiControlChar(value)) {
|
|
5
|
+
throw graphValidationError(`Invalid ${opts.helperName} input: ${opts.fieldName} must not contain control characters.`);
|
|
6
|
+
}
|
|
7
|
+
if (value === "." || value === ".." || value.includes("/") || value.includes("\\") || value.includes("?") || value.includes("#")) {
|
|
8
|
+
throw graphValidationError(`Invalid ${opts.helperName} input: ${opts.fieldName} contains an unsafe path segment.`);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
export function assertSafePathSegment(value, opts) {
|
|
12
|
+
const out = assertBoundedString(value, {
|
|
13
|
+
helperName: opts.helperName,
|
|
14
|
+
fieldName: opts.fieldName,
|
|
15
|
+
maxLength: opts.maxLength ?? 512
|
|
16
|
+
});
|
|
17
|
+
assertSafePathSegmentString(out, opts);
|
|
18
|
+
return out;
|
|
19
|
+
}
|
|
20
|
+
export function assertRepeatedlyDecodedSafePathId(value, opts) {
|
|
21
|
+
const out = assertSafePathSegment(value, opts);
|
|
22
|
+
let decoded = out;
|
|
23
|
+
const maxDecodeRounds = opts.maxDecodeRounds ?? 8;
|
|
24
|
+
for (let round = 0;round < maxDecodeRounds; round += 1) {
|
|
25
|
+
let next;
|
|
26
|
+
try {
|
|
27
|
+
next = decodeURIComponent(decoded);
|
|
28
|
+
} catch (error) {
|
|
29
|
+
throw graphValidationError(`Invalid ${opts.helperName} input: ${opts.fieldName} contains malformed percent encoding.`, error);
|
|
30
|
+
}
|
|
31
|
+
if (next === decoded)
|
|
32
|
+
return out;
|
|
33
|
+
decoded = next;
|
|
34
|
+
assertSafePathSegmentString(decoded, opts);
|
|
35
|
+
}
|
|
36
|
+
throw graphValidationError(`Invalid ${opts.helperName} input: ${opts.fieldName} contains excessive percent encoding.`);
|
|
37
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export interface PlainDataRecordOptions {
|
|
2
|
+
readonly helperName: string;
|
|
3
|
+
readonly path: string;
|
|
4
|
+
readonly objectNoun?: "object" | "plain object";
|
|
5
|
+
readonly rejectInheritedToJSON?: boolean;
|
|
6
|
+
readonly rejectOwnToJSON?: boolean;
|
|
7
|
+
readonly rejectFunctionsSymbolsBigInts?: boolean;
|
|
8
|
+
}
|
|
9
|
+
export interface OwnDataValueOptions {
|
|
10
|
+
readonly helperName: string;
|
|
11
|
+
readonly path: string;
|
|
12
|
+
readonly required?: boolean;
|
|
13
|
+
}
|
|
14
|
+
export declare function isUnsafePrototypeKey(key: string): boolean;
|
|
15
|
+
export declare function rejectUnsafePrototypeKey(key: string, opts: {
|
|
16
|
+
readonly helperName: string;
|
|
17
|
+
readonly path: string;
|
|
18
|
+
}): void;
|
|
19
|
+
export declare function assertPlainDataRecord(value: unknown, opts: PlainDataRecordOptions): Record<string, unknown>;
|
|
20
|
+
export declare function ownDataValue(record: Record<string, unknown>, key: string, opts: OwnDataValueOptions): unknown;
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { graphValidationError, wrapGraphValidation } from "./errors";
|
|
2
|
+
import { hasAsciiControlChar } from "./strings";
|
|
3
|
+
export function isUnsafePrototypeKey(key) {
|
|
4
|
+
return key === "__proto__" || key === "constructor" || key === "prototype";
|
|
5
|
+
}
|
|
6
|
+
export function rejectUnsafePrototypeKey(key, opts) {
|
|
7
|
+
if (isUnsafePrototypeKey(key)) {
|
|
8
|
+
throw graphValidationError(`Invalid ${opts.helperName} input: ${opts.path} contains an unsafe prototype key.`);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
export function assertPlainDataRecord(value, opts) {
|
|
12
|
+
const noun = opts.objectNoun ?? "plain object";
|
|
13
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
14
|
+
throw graphValidationError(`Invalid ${opts.helperName} input: ${opts.path} must be a ${noun}.`);
|
|
15
|
+
}
|
|
16
|
+
const proto = wrapGraphValidation(`Invalid ${opts.helperName} input: ${opts.path} could not be inspected.`, () => Object.getPrototypeOf(value));
|
|
17
|
+
if (proto !== Object.prototype && proto !== null) {
|
|
18
|
+
throw graphValidationError(`Invalid ${opts.helperName} input: ${opts.path} must be a ${noun}.`);
|
|
19
|
+
}
|
|
20
|
+
const descriptors = wrapGraphValidation(`Invalid ${opts.helperName} input: ${opts.path} descriptors could not be inspected.`, () => Object.getOwnPropertyDescriptors(value));
|
|
21
|
+
if (opts.rejectOwnToJSON !== false && Object.prototype.hasOwnProperty.call(descriptors, "toJSON")) {
|
|
22
|
+
throw graphValidationError(`Invalid ${opts.helperName} input: ${opts.path} must not define toJSON.`);
|
|
23
|
+
}
|
|
24
|
+
if (opts.rejectInheritedToJSON !== false) {
|
|
25
|
+
wrapGraphValidation(`Invalid ${opts.helperName} input: ${opts.path} could not be inspected.`, () => {
|
|
26
|
+
if ("toJSON" in value) {
|
|
27
|
+
throw graphValidationError(`Invalid ${opts.helperName} input: ${opts.path} must not inherit toJSON.`);
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
const symbolDescriptors = Object.getOwnPropertySymbols(descriptors);
|
|
32
|
+
for (const symbolKey of symbolDescriptors) {
|
|
33
|
+
const descriptor = Object.getOwnPropertyDescriptor(descriptors, symbolKey)?.value;
|
|
34
|
+
if (descriptor === undefined) {
|
|
35
|
+
throw graphValidationError(`Invalid ${opts.helperName} input: ${opts.path} symbol descriptor could not be inspected.`);
|
|
36
|
+
}
|
|
37
|
+
if (typeof descriptor.get === "function" || typeof descriptor.set === "function") {
|
|
38
|
+
throw graphValidationError(`Invalid ${opts.helperName} input: ${opts.path} must not use symbol-keyed accessors.`);
|
|
39
|
+
}
|
|
40
|
+
throw graphValidationError(`Invalid ${opts.helperName} input: ${opts.path} must not contain symbol keys.`);
|
|
41
|
+
}
|
|
42
|
+
for (const [key, descriptor] of Object.entries(descriptors)) {
|
|
43
|
+
rejectUnsafePrototypeKey(key, { helperName: opts.helperName, path: opts.path });
|
|
44
|
+
if (key.length === 0 || hasAsciiControlChar(key)) {
|
|
45
|
+
throw graphValidationError(`Invalid ${opts.helperName} input: ${opts.path} contains an invalid key.`);
|
|
46
|
+
}
|
|
47
|
+
if (typeof descriptor.get === "function" || typeof descriptor.set === "function") {
|
|
48
|
+
throw graphValidationError(`Invalid ${opts.helperName} input: ${opts.path}.${key} must not use accessors.`);
|
|
49
|
+
}
|
|
50
|
+
if (opts.rejectFunctionsSymbolsBigInts === true && (typeof descriptor.value === "function" || typeof descriptor.value === "symbol" || typeof descriptor.value === "bigint")) {
|
|
51
|
+
throw graphValidationError(`Invalid ${opts.helperName} input: ${opts.path}.${key} must be JSON-like data.`);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return value;
|
|
55
|
+
}
|
|
56
|
+
export function ownDataValue(record, key, opts) {
|
|
57
|
+
const descriptors = wrapGraphValidation(`Invalid ${opts.helperName} input: ${opts.path} descriptors could not be inspected.`, () => Object.getOwnPropertyDescriptors(record));
|
|
58
|
+
const descriptor = descriptors[key];
|
|
59
|
+
if (descriptor === undefined) {
|
|
60
|
+
if (opts.required === true) {
|
|
61
|
+
throw graphValidationError(`Invalid ${opts.helperName} input: ${opts.path} is required.`);
|
|
62
|
+
}
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
if (typeof descriptor.get === "function" || typeof descriptor.set === "function") {
|
|
66
|
+
throw graphValidationError(`Invalid ${opts.helperName} input: ${opts.path} must be a data property.`);
|
|
67
|
+
}
|
|
68
|
+
return descriptor.value;
|
|
69
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export interface StringValidationOptions {
|
|
2
|
+
readonly helperName: string;
|
|
3
|
+
readonly fieldName: string;
|
|
4
|
+
readonly maxLength?: number;
|
|
5
|
+
readonly allowWhitespace?: boolean;
|
|
6
|
+
readonly allowControlChars?: boolean;
|
|
7
|
+
}
|
|
8
|
+
export type QueryStringValidationOptions = Omit<StringValidationOptions, "allowWhitespace" | "allowControlChars">;
|
|
9
|
+
export declare function hasAsciiControlChar(value: string): boolean;
|
|
10
|
+
export declare function assertNonEmptyString(value: unknown, opts: StringValidationOptions): string;
|
|
11
|
+
export declare function assertBoundedString(value: unknown, opts: StringValidationOptions): string;
|
|
12
|
+
export declare function assertQueryString(value: unknown, opts: QueryStringValidationOptions): string;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { graphValidationError } from "./errors";
|
|
2
|
+
export function hasAsciiControlChar(value) {
|
|
3
|
+
for (let index = 0;index < value.length; index += 1) {
|
|
4
|
+
const code = value.charCodeAt(index);
|
|
5
|
+
if (code < 32 || code === 127)
|
|
6
|
+
return true;
|
|
7
|
+
}
|
|
8
|
+
return false;
|
|
9
|
+
}
|
|
10
|
+
export function assertNonEmptyString(value, opts) {
|
|
11
|
+
if (typeof value !== "string") {
|
|
12
|
+
throw graphValidationError(`Invalid ${opts.helperName} input: ${opts.fieldName} must be a string.`);
|
|
13
|
+
}
|
|
14
|
+
if (value.length === 0) {
|
|
15
|
+
throw graphValidationError(`Invalid ${opts.helperName} input: ${opts.fieldName} must be non-empty.`);
|
|
16
|
+
}
|
|
17
|
+
if (opts.allowWhitespace !== true && value.trim().length === 0) {
|
|
18
|
+
throw graphValidationError(`Invalid ${opts.helperName} input: ${opts.fieldName} must not be whitespace-only.`);
|
|
19
|
+
}
|
|
20
|
+
if (opts.allowControlChars !== true && hasAsciiControlChar(value)) {
|
|
21
|
+
throw graphValidationError(`Invalid ${opts.helperName} input: ${opts.fieldName} must not contain control characters.`);
|
|
22
|
+
}
|
|
23
|
+
return value;
|
|
24
|
+
}
|
|
25
|
+
export function assertBoundedString(value, opts) {
|
|
26
|
+
const out = assertNonEmptyString(value, opts);
|
|
27
|
+
if (opts.maxLength !== undefined && out.length > opts.maxLength) {
|
|
28
|
+
throw graphValidationError(`Invalid ${opts.helperName} input: ${opts.fieldName} exceeds ${opts.maxLength}-character limit.`);
|
|
29
|
+
}
|
|
30
|
+
return out;
|
|
31
|
+
}
|
|
32
|
+
export function assertQueryString(value, opts) {
|
|
33
|
+
if (typeof value !== "string") {
|
|
34
|
+
throw graphValidationError(`Invalid ${opts.helperName} input: ${opts.fieldName} must be a string.`);
|
|
35
|
+
}
|
|
36
|
+
if (value.length === 0 || value.trim().length === 0) {
|
|
37
|
+
throw graphValidationError(`Invalid ${opts.helperName} input: ${opts.fieldName} must be non-empty.`);
|
|
38
|
+
}
|
|
39
|
+
if (opts.maxLength !== undefined && value.length > opts.maxLength) {
|
|
40
|
+
throw graphValidationError(`Invalid ${opts.helperName} input: ${opts.fieldName} exceeds ${opts.maxLength}-character limit.`);
|
|
41
|
+
}
|
|
42
|
+
if (hasAsciiControlChar(value)) {
|
|
43
|
+
throw graphValidationError(`Invalid ${opts.helperName} input: ${opts.fieldName} must not contain control characters.`);
|
|
44
|
+
}
|
|
45
|
+
return value;
|
|
46
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { EndpointCallable } from "./endpoint";
|
|
2
|
+
import type { GraphClient } from "./client";
|
|
3
|
+
export interface PaginationOptions {
|
|
4
|
+
readonly maxPages?: number;
|
|
5
|
+
readonly pageSize?: number;
|
|
6
|
+
readonly signal?: AbortSignal;
|
|
7
|
+
}
|
|
8
|
+
export interface PaginatedResult<T> {
|
|
9
|
+
readonly items: readonly T[];
|
|
10
|
+
readonly pagesConsumed: number;
|
|
11
|
+
readonly pageLimitReached: boolean;
|
|
12
|
+
readonly aborted: boolean;
|
|
13
|
+
}
|
|
14
|
+
export interface PaginatedPage<T> {
|
|
15
|
+
readonly data?: readonly T[];
|
|
16
|
+
readonly paging?: {
|
|
17
|
+
readonly cursors?: {
|
|
18
|
+
readonly before?: string;
|
|
19
|
+
readonly after?: string;
|
|
20
|
+
};
|
|
21
|
+
readonly next?: string;
|
|
22
|
+
readonly previous?: string;
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
export type PaginationErrorCode = "invalid_endpoint" | "invalid_max_pages" | "invalid_page_size" | "invalid_signal" | "aborted" | "page_fetch_failed";
|
|
26
|
+
export declare class PaginationError extends Error {
|
|
27
|
+
readonly code: PaginationErrorCode;
|
|
28
|
+
readonly cause?: unknown;
|
|
29
|
+
constructor(code: PaginationErrorCode, message: string, options?: {
|
|
30
|
+
cause?: unknown;
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
export declare const DEFAULT_MAX_PAGES = 1000;
|
|
34
|
+
export declare function paginate<TParams extends Record<string, string>, TBody, TItem>(client: GraphClient, endpoint: EndpointCallable<TParams, TBody, PaginatedPage<TItem>>, params: TParams, opts?: PaginationOptions): AsyncGenerator<TItem, PaginatedResult<TItem>, void>;
|
|
35
|
+
export declare function paginateAll<TParams extends Record<string, string>, TBody, TItem>(client: GraphClient, endpoint: EndpointCallable<TParams, TBody, PaginatedPage<TItem>>, params: TParams, opts?: PaginationOptions): Promise<PaginatedResult<TItem>>;
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
export class PaginationError extends Error {
|
|
2
|
+
code;
|
|
3
|
+
cause;
|
|
4
|
+
constructor(code, message, options) {
|
|
5
|
+
super(message);
|
|
6
|
+
this.name = "PaginationError";
|
|
7
|
+
this.code = code;
|
|
8
|
+
if (options !== undefined && "cause" in options) {
|
|
9
|
+
Object.defineProperty(this, "cause", {
|
|
10
|
+
value: options.cause,
|
|
11
|
+
enumerable: false,
|
|
12
|
+
writable: false,
|
|
13
|
+
configurable: true
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
export const DEFAULT_MAX_PAGES = 1000;
|
|
19
|
+
const DEFAULT_PAGE_SIZE_QUERY_KEY = "limit";
|
|
20
|
+
function isSignalAborted(signal) {
|
|
21
|
+
return signal !== undefined && signal.aborted;
|
|
22
|
+
}
|
|
23
|
+
function isPositiveInteger(value) {
|
|
24
|
+
return typeof value === "number" && Number.isFinite(value) && Number.isInteger(value) && value > 0;
|
|
25
|
+
}
|
|
26
|
+
function isAbortSignalLike(value) {
|
|
27
|
+
if (value === null || typeof value !== "object") {
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
const candidate = value;
|
|
31
|
+
return typeof candidate.aborted === "boolean" && typeof candidate.addEventListener === "function" && typeof candidate.removeEventListener === "function";
|
|
32
|
+
}
|
|
33
|
+
function isEndpointCallable(value) {
|
|
34
|
+
if (typeof value !== "function") {
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
const asObj = value;
|
|
38
|
+
if (typeof asObj.definition !== "object" || asObj.definition === null) {
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
const def = asObj.definition;
|
|
42
|
+
return typeof def.method === "string" && typeof def.pathTemplate === "string" && typeof def.params === "object" && def.params !== null;
|
|
43
|
+
}
|
|
44
|
+
function extractAfterCursor(nextUrl) {
|
|
45
|
+
try {
|
|
46
|
+
const url = new URL(nextUrl);
|
|
47
|
+
const after = url.searchParams.get("after");
|
|
48
|
+
return after === null ? undefined : after;
|
|
49
|
+
} catch {
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
function validateOptions(opts) {
|
|
54
|
+
if (opts !== undefined && (typeof opts !== "object" || opts === null)) {
|
|
55
|
+
throw new PaginationError("invalid_endpoint", "paginate: opts must be an object when provided.");
|
|
56
|
+
}
|
|
57
|
+
const maxPagesRaw = opts?.maxPages;
|
|
58
|
+
let maxPages = DEFAULT_MAX_PAGES;
|
|
59
|
+
if (maxPagesRaw !== undefined) {
|
|
60
|
+
if (!isPositiveInteger(maxPagesRaw)) {
|
|
61
|
+
throw new PaginationError("invalid_max_pages", `paginate: maxPages must be a positive integer; received ${String(maxPagesRaw)}.`);
|
|
62
|
+
}
|
|
63
|
+
maxPages = maxPagesRaw;
|
|
64
|
+
}
|
|
65
|
+
const pageSizeRaw = opts?.pageSize;
|
|
66
|
+
let pageSize;
|
|
67
|
+
if (pageSizeRaw !== undefined) {
|
|
68
|
+
if (!isPositiveInteger(pageSizeRaw)) {
|
|
69
|
+
throw new PaginationError("invalid_page_size", `paginate: pageSize must be a positive integer; received ${String(pageSizeRaw)}.`);
|
|
70
|
+
}
|
|
71
|
+
pageSize = pageSizeRaw;
|
|
72
|
+
}
|
|
73
|
+
const signalRaw = opts?.signal;
|
|
74
|
+
let signal;
|
|
75
|
+
if (signalRaw !== undefined) {
|
|
76
|
+
if (!isAbortSignalLike(signalRaw)) {
|
|
77
|
+
throw new PaginationError("invalid_signal", "paginate: signal must be an AbortSignal (duck-typed).");
|
|
78
|
+
}
|
|
79
|
+
signal = signalRaw;
|
|
80
|
+
}
|
|
81
|
+
return { maxPages, pageSize, signal };
|
|
82
|
+
}
|
|
83
|
+
export async function* paginate(client, endpoint, params, opts) {
|
|
84
|
+
if (!isEndpointCallable(endpoint)) {
|
|
85
|
+
throw new PaginationError("invalid_endpoint", "paginate: endpoint must be an EndpointCallable (see defineEndpoint).");
|
|
86
|
+
}
|
|
87
|
+
const { maxPages, pageSize, signal } = validateOptions(opts);
|
|
88
|
+
const collected = [];
|
|
89
|
+
let pagesConsumed = 0;
|
|
90
|
+
let aborted = false;
|
|
91
|
+
let pageLimitReached = false;
|
|
92
|
+
if (isSignalAborted(signal)) {
|
|
93
|
+
return {
|
|
94
|
+
items: collected,
|
|
95
|
+
pagesConsumed,
|
|
96
|
+
pageLimitReached,
|
|
97
|
+
aborted: true
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
let currentParams = params;
|
|
101
|
+
if (pageSize !== undefined) {
|
|
102
|
+
const existing = params[DEFAULT_PAGE_SIZE_QUERY_KEY];
|
|
103
|
+
if (existing === undefined) {
|
|
104
|
+
currentParams = {
|
|
105
|
+
...params,
|
|
106
|
+
[DEFAULT_PAGE_SIZE_QUERY_KEY]: String(pageSize)
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
while (true) {
|
|
111
|
+
if (isSignalAborted(signal)) {
|
|
112
|
+
aborted = true;
|
|
113
|
+
break;
|
|
114
|
+
}
|
|
115
|
+
let page;
|
|
116
|
+
try {
|
|
117
|
+
const invokeOpts = signal !== undefined ? { signal } : undefined;
|
|
118
|
+
page = await endpoint(client, currentParams, undefined, invokeOpts);
|
|
119
|
+
} catch (err) {
|
|
120
|
+
throw new PaginationError("page_fetch_failed", `paginate: underlying endpoint threw while fetching page ${pagesConsumed + 1}.`, { cause: err });
|
|
121
|
+
}
|
|
122
|
+
pagesConsumed += 1;
|
|
123
|
+
const data = page.data ?? [];
|
|
124
|
+
for (const item of data) {
|
|
125
|
+
collected.push(item);
|
|
126
|
+
yield item;
|
|
127
|
+
}
|
|
128
|
+
if (pagesConsumed >= maxPages) {
|
|
129
|
+
pageLimitReached = true;
|
|
130
|
+
break;
|
|
131
|
+
}
|
|
132
|
+
const nextUrl = page.paging?.next;
|
|
133
|
+
if (typeof nextUrl !== "string" || nextUrl.length === 0) {
|
|
134
|
+
break;
|
|
135
|
+
}
|
|
136
|
+
const nextAfter = extractAfterCursor(nextUrl);
|
|
137
|
+
if (nextAfter === undefined) {
|
|
138
|
+
break;
|
|
139
|
+
}
|
|
140
|
+
currentParams = {
|
|
141
|
+
...currentParams,
|
|
142
|
+
after: nextAfter
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
return {
|
|
146
|
+
items: collected,
|
|
147
|
+
pagesConsumed,
|
|
148
|
+
pageLimitReached,
|
|
149
|
+
aborted
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
export async function paginateAll(client, endpoint, params, opts) {
|
|
153
|
+
const gen = paginate(client, endpoint, params, opts);
|
|
154
|
+
while (true) {
|
|
155
|
+
const step = await gen.next();
|
|
156
|
+
if (step.done) {
|
|
157
|
+
return step.value;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import type { GraphClient } from "../client";
|
|
2
|
+
import type { EndpointInvokeOptions } from "../endpoint";
|
|
3
|
+
import { type BusinessProfileResponse, type CommerceSettingsResponse, type GetBusinessProfileInput, type GetCommerceSettingsInput, type GetPhoneNumberInfoInput, type GetPhoneNumberSettingsInput, type PhoneNumberInfo, type PhoneNumberSettingsResponse } from "../endpoints/businessManagement";
|
|
4
|
+
import { type AcceptCallRequest, type CallLifecycleResponse, type InitiateCallRequest, type PreAcceptCallRequest, type RejectCallRequest, type TerminateCallRequest } from "../endpoints/calling";
|
|
5
|
+
import { type GraphMessagesMarkMessageAsReadInput, type GraphMessagesRemoveReactionInput, type GraphMessagesRequestLocationInput, type GraphMessagesSendAudioInput, type GraphMessagesSendBody, type GraphMessagesSendButtonsInput, type GraphMessagesSendCatalogInput, type GraphMessagesSendContactsInput, type GraphMessagesSendCtaUrlInput, type GraphMessagesSendDocumentInput, type GraphMessagesSendImageInput, type GraphMessagesSendListInput, type GraphMessagesSendLocationInput, type GraphMessagesSendProductInput, type GraphMessagesSendProductsInput, type GraphMessagesSendReactionInput, type GraphMessagesSendResponse, type GraphMessagesSendStickerInput, type GraphMessagesSendTemplateInput, type GraphMessagesSendTextInput, type GraphMessagesSendVideoInput, type GraphMessagesTypingIndicatorInput } from "../endpoints/messages";
|
|
6
|
+
export interface PhoneNumberClientConfig {
|
|
7
|
+
readonly graphClient: GraphClient;
|
|
8
|
+
readonly phoneNumberId: string;
|
|
9
|
+
}
|
|
10
|
+
export declare function validatePhoneNumberClientConfig(config: PhoneNumberClientConfig): void;
|
|
11
|
+
export declare class PhoneNumberClient {
|
|
12
|
+
#private;
|
|
13
|
+
constructor(config: PhoneNumberClientConfig);
|
|
14
|
+
get phoneNumberId(): string;
|
|
15
|
+
get graphClient(): GraphClient;
|
|
16
|
+
/** Graph `GET /{phoneNumberId}` — returns phone-number inventory/profile fields. */
|
|
17
|
+
getInfo(params?: Omit<GetPhoneNumberInfoInput, "phoneNumberId">, opts?: EndpointInvokeOptions): Promise<PhoneNumberInfo>;
|
|
18
|
+
/** Graph `GET /{phoneNumberId}/settings`; includeSipCredentials responses may be sensitive. */
|
|
19
|
+
getSettings(params?: Omit<GetPhoneNumberSettingsInput, "phoneNumberId">, opts?: EndpointInvokeOptions): Promise<PhoneNumberSettingsResponse>;
|
|
20
|
+
/** Graph `GET /{phoneNumberId}/whatsapp_business_profile`. */
|
|
21
|
+
getBusinessProfile(params?: Omit<GetBusinessProfileInput, "phoneNumberId">, opts?: EndpointInvokeOptions): Promise<BusinessProfileResponse>;
|
|
22
|
+
/** Graph `GET /{phoneNumberId}/whatsapp_commerce_settings`. */
|
|
23
|
+
getCommerceSettings(params?: Omit<GetCommerceSettingsInput, "phoneNumberId">, opts?: EndpointInvokeOptions): Promise<CommerceSettingsResponse>;
|
|
24
|
+
/**
|
|
25
|
+
* Graph `POST /{phoneNumberId}/messages` — delegates to the F-6
|
|
26
|
+
* endpoint-registry `sendMessage` callable with the bound
|
|
27
|
+
* phoneNumberId injected automatically.
|
|
28
|
+
*/
|
|
29
|
+
sendMessage(body: GraphMessagesSendBody, opts?: EndpointInvokeOptions): Promise<GraphMessagesSendResponse>;
|
|
30
|
+
/**
|
|
31
|
+
* Ergonomic text helper for starting or continuing a chat with any
|
|
32
|
+
* phone-number-like recipient. Does not consult contacts; it validates
|
|
33
|
+
* the public JS input shape and delegates to `POST /{phoneNumberId}/messages`.
|
|
34
|
+
*/
|
|
35
|
+
sendText(input: GraphMessagesSendTextInput, opts?: EndpointInvokeOptions): Promise<GraphMessagesSendResponse>;
|
|
36
|
+
sendImage(input: GraphMessagesSendImageInput, opts?: EndpointInvokeOptions): Promise<GraphMessagesSendResponse>;
|
|
37
|
+
sendVideo(input: GraphMessagesSendVideoInput, opts?: EndpointInvokeOptions): Promise<GraphMessagesSendResponse>;
|
|
38
|
+
sendAudio(input: GraphMessagesSendAudioInput, opts?: EndpointInvokeOptions): Promise<GraphMessagesSendResponse>;
|
|
39
|
+
sendDocument(input: GraphMessagesSendDocumentInput, opts?: EndpointInvokeOptions): Promise<GraphMessagesSendResponse>;
|
|
40
|
+
sendSticker(input: GraphMessagesSendStickerInput, opts?: EndpointInvokeOptions): Promise<GraphMessagesSendResponse>;
|
|
41
|
+
sendLocation(input: GraphMessagesSendLocationInput, opts?: EndpointInvokeOptions): Promise<GraphMessagesSendResponse>;
|
|
42
|
+
sendContacts(input: GraphMessagesSendContactsInput, opts?: EndpointInvokeOptions): Promise<GraphMessagesSendResponse>;
|
|
43
|
+
sendReaction(input: GraphMessagesSendReactionInput, opts?: EndpointInvokeOptions): Promise<GraphMessagesSendResponse>;
|
|
44
|
+
removeReaction(input: GraphMessagesRemoveReactionInput, opts?: EndpointInvokeOptions): Promise<GraphMessagesSendResponse>;
|
|
45
|
+
sendButtons(input: GraphMessagesSendButtonsInput, opts?: EndpointInvokeOptions): Promise<GraphMessagesSendResponse>;
|
|
46
|
+
sendList(input: GraphMessagesSendListInput, opts?: EndpointInvokeOptions): Promise<GraphMessagesSendResponse>;
|
|
47
|
+
sendCtaUrl(input: GraphMessagesSendCtaUrlInput, opts?: EndpointInvokeOptions): Promise<GraphMessagesSendResponse>;
|
|
48
|
+
sendProduct(input: GraphMessagesSendProductInput, opts?: EndpointInvokeOptions): Promise<GraphMessagesSendResponse>;
|
|
49
|
+
sendProducts(input: GraphMessagesSendProductsInput, opts?: EndpointInvokeOptions): Promise<GraphMessagesSendResponse>;
|
|
50
|
+
sendCatalog(input: GraphMessagesSendCatalogInput, opts?: EndpointInvokeOptions): Promise<GraphMessagesSendResponse>;
|
|
51
|
+
requestLocation(input: GraphMessagesRequestLocationInput, opts?: EndpointInvokeOptions): Promise<GraphMessagesSendResponse>;
|
|
52
|
+
markMessageAsRead(input: GraphMessagesMarkMessageAsReadInput, opts?: EndpointInvokeOptions): Promise<GraphMessagesSendResponse>;
|
|
53
|
+
indicateTyping(input: GraphMessagesTypingIndicatorInput, opts?: EndpointInvokeOptions): Promise<GraphMessagesSendResponse>;
|
|
54
|
+
sendTemplate(input: GraphMessagesSendTemplateInput, opts?: EndpointInvokeOptions): Promise<GraphMessagesSendResponse>;
|
|
55
|
+
initiateCall(input: InitiateCallRequest, opts?: EndpointInvokeOptions): Promise<CallLifecycleResponse>;
|
|
56
|
+
preAcceptCall(input: PreAcceptCallRequest, opts?: EndpointInvokeOptions): Promise<CallLifecycleResponse>;
|
|
57
|
+
acceptCall(input: AcceptCallRequest, opts?: EndpointInvokeOptions): Promise<CallLifecycleResponse>;
|
|
58
|
+
rejectCall(input: RejectCallRequest, opts?: EndpointInvokeOptions): Promise<CallLifecycleResponse>;
|
|
59
|
+
terminateCall(input: TerminateCallRequest, opts?: EndpointInvokeOptions): Promise<CallLifecycleResponse>;
|
|
60
|
+
}
|