@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
package/dist/errors.js ADDED
@@ -0,0 +1,250 @@
1
+ export class GraphApiError extends Error {
2
+ status;
3
+ type;
4
+ code;
5
+ errorSubcode;
6
+ fbtraceId;
7
+ payload;
8
+ classification;
9
+ constructor(params) {
10
+ super(params.message);
11
+ this.name = "GraphApiError";
12
+ this.status = params.status;
13
+ this.classification = classifyStatus(params.status);
14
+ if (params.payload !== undefined) {
15
+ this.payload = params.payload;
16
+ if (params.payload.type !== undefined) {
17
+ this.type = params.payload.type;
18
+ }
19
+ if (params.payload.code !== undefined) {
20
+ this.code = params.payload.code;
21
+ }
22
+ if (params.payload.error_subcode !== undefined) {
23
+ this.errorSubcode = params.payload.error_subcode;
24
+ }
25
+ if (params.payload.fbtrace_id !== undefined) {
26
+ this.fbtraceId = params.payload.fbtrace_id;
27
+ }
28
+ }
29
+ }
30
+ }
31
+ function classifyStatus(status) {
32
+ if (status >= 400 && status < 500) {
33
+ return "ClientError";
34
+ }
35
+ if (status >= 500 && status < 600) {
36
+ return "ServerError";
37
+ }
38
+ return "Unknown";
39
+ }
40
+
41
+ export class GraphAuthError extends GraphApiError {
42
+ constructor(params) {
43
+ super(params);
44
+ this.name = "GraphAuthError";
45
+ }
46
+ }
47
+
48
+ export class GraphRateLimitError extends GraphApiError {
49
+ constructor(params) {
50
+ super(params);
51
+ this.name = "GraphRateLimitError";
52
+ }
53
+ }
54
+
55
+ export class GraphNetworkError extends Error {
56
+ cause;
57
+ constructor(message, cause) {
58
+ super(message);
59
+ this.name = "GraphNetworkError";
60
+ this.cause = cause;
61
+ }
62
+ }
63
+
64
+ export class GraphRequestValidationError extends GraphApiError {
65
+ cause;
66
+ constructor(message, cause) {
67
+ super({
68
+ message,
69
+ status: 400,
70
+ payload: {
71
+ message,
72
+ type: "ValidationError"
73
+ }
74
+ });
75
+ this.name = "GraphRequestValidationError";
76
+ this.cause = cause;
77
+ }
78
+ }
79
+
80
+ export class GraphSerializationError extends GraphApiError {
81
+ cause;
82
+ constructor(message, cause) {
83
+ super({
84
+ message,
85
+ status: 400,
86
+ payload: {
87
+ message,
88
+ type: "SerializationError"
89
+ }
90
+ });
91
+ this.name = "GraphSerializationError";
92
+ this.cause = cause;
93
+ }
94
+ }
95
+ function isRecord(value) {
96
+ return typeof value === "object" && value !== null;
97
+ }
98
+ export function isGraphApiErrorPayload(value) {
99
+ if (!isRecord(value) || Array.isArray(value)) {
100
+ return false;
101
+ }
102
+ const message = value.message;
103
+ const type = value.type;
104
+ const code = value.code;
105
+ const errorSubcode = value.error_subcode;
106
+ const fbtraceId = value.fbtrace_id;
107
+ if (typeof message !== "string" || message.length === 0) {
108
+ return false;
109
+ }
110
+ if (type !== undefined && typeof type !== "string") {
111
+ return false;
112
+ }
113
+ if (code !== undefined && typeof code !== "number") {
114
+ return false;
115
+ }
116
+ if (errorSubcode !== undefined && typeof errorSubcode !== "number") {
117
+ return false;
118
+ }
119
+ if (fbtraceId !== undefined && typeof fbtraceId !== "string") {
120
+ return false;
121
+ }
122
+ return true;
123
+ }
124
+ export function isGraphErrorEnvelope(value) {
125
+ if (!isRecord(value)) {
126
+ return false;
127
+ }
128
+ return isGraphApiErrorPayload(value.error);
129
+ }
130
+ export const RATE_LIMIT_CODES = new Set([
131
+ 4,
132
+ 80007,
133
+ 130429,
134
+ 131048,
135
+ 131056
136
+ ]);
137
+ function isAuthClassification(status, payload) {
138
+ if (status === 401) {
139
+ return true;
140
+ }
141
+ if (status === 403) {
142
+ return true;
143
+ }
144
+ if (payload !== undefined && typeof payload.type === "string" && payload.type === "OAuthException" && status >= 400 && status < 500) {
145
+ return true;
146
+ }
147
+ if (payload !== undefined && typeof payload.code === "number" && payload.code === 190 && status >= 400 && status < 500) {
148
+ return true;
149
+ }
150
+ return false;
151
+ }
152
+ function isRateLimitClassification(status, payload) {
153
+ if (status === 429) {
154
+ return true;
155
+ }
156
+ if (status >= 400 && status < 500 && payload !== undefined && typeof payload.code === "number" && RATE_LIMIT_CODES.has(payload.code)) {
157
+ return true;
158
+ }
159
+ return false;
160
+ }
161
+ const BEARER_TOKEN_REGEXP = /Beare...+/g;
162
+ function redactString(value) {
163
+ return value.replace(BEARER_TOKEN_REGEXP, "Bearer ***");
164
+ }
165
+ function isErrorLike(value) {
166
+ return typeof value === "object" && value !== null && typeof value.message === "string";
167
+ }
168
+ function cloneErrorWithRedaction(err, seen) {
169
+ if (seen.has(err)) {
170
+ return err;
171
+ }
172
+ seen.add(err);
173
+ const proto = Object.getPrototypeOf(err);
174
+ const clone = Object.create(proto ?? Error.prototype);
175
+ clone.name = err.name;
176
+ clone.message = redactString(err.message);
177
+ if (typeof err.stack === "string") {
178
+ clone.stack = redactString(err.stack);
179
+ }
180
+ const maybeCause = err.cause;
181
+ if (maybeCause !== undefined) {
182
+ clone.cause = scrubErrorCauseInner(maybeCause, seen);
183
+ }
184
+ for (const key of Object.keys(err)) {
185
+ if (key === "message" || key === "stack" || key === "name" || key === "cause") {
186
+ continue;
187
+ }
188
+ const value = err[key];
189
+ clone[key] = typeof value === "string" ? redactString(value) : value;
190
+ }
191
+ return clone;
192
+ }
193
+ function scrubErrorCauseInner(value, seen) {
194
+ if (typeof value === "string") {
195
+ return redactString(value);
196
+ }
197
+ if (isErrorLike(value)) {
198
+ return cloneErrorWithRedaction(value, seen);
199
+ }
200
+ return value;
201
+ }
202
+ export function scrubErrorCause(err) {
203
+ return scrubErrorCauseInner(err, new WeakSet);
204
+ }
205
+ export function createGraphApiError(params) {
206
+ const message = params.payload?.message ?? params.fallbackMessage ?? "Graph API request failed";
207
+ const shouldClassify = params.classify ?? true;
208
+ const ctorParams = {
209
+ message,
210
+ status: params.status
211
+ };
212
+ if (params.payload !== undefined) {
213
+ ctorParams.payload = params.payload;
214
+ }
215
+ if (!shouldClassify) {
216
+ return new GraphApiError(ctorParams);
217
+ }
218
+ const code = typeof params.payload?.code === "number" ? params.payload.code : undefined;
219
+ const subcode = typeof params.payload?.error_subcode === "number" ? params.payload.error_subcode : undefined;
220
+ if (code !== undefined) {
221
+ const entry = resolveRegisteredErrorSafely(code, subcode);
222
+ if (entry !== undefined) {
223
+ const ctx = {
224
+ payload: params.payload,
225
+ status: params.status,
226
+ headers: params.headers ?? new Headers,
227
+ requestUrl: params.requestUrl ?? ""
228
+ };
229
+ const instance = entry.factory(ctx);
230
+ if (instance instanceof GraphAuthError && !isAuthClassification(params.status, params.payload)) {
231
+ return new GraphApiError(ctorParams);
232
+ }
233
+ if (instance instanceof GraphRateLimitError && !isRateLimitClassification(params.status, params.payload)) {
234
+ return new GraphApiError(ctorParams);
235
+ }
236
+ return instance;
237
+ }
238
+ }
239
+ if (isAuthClassification(params.status, params.payload)) {
240
+ return new GraphAuthError(ctorParams);
241
+ }
242
+ if (isRateLimitClassification(params.status, params.payload)) {
243
+ return new GraphRateLimitError(ctorParams);
244
+ }
245
+ return new GraphApiError(ctorParams);
246
+ }
247
+ import { resolveRegisteredError as _resolveRegisteredError } from "./errorRegistry";
248
+ function resolveRegisteredErrorSafely(code, subcode) {
249
+ return _resolveRegisteredError(code, subcode);
250
+ }
@@ -0,0 +1,20 @@
1
+ export * from "./client";
2
+ export * from "./errors";
3
+ export * from "./errorRegistry";
4
+ export * from "./errorSubclasses";
5
+ export * from "./endpoints/messages";
6
+ export * from "./endpoints/calling";
7
+ export * from "./endpoints/businessManagement";
8
+ export * from "./endpoints/wabaEndpoints";
9
+ export * from "./endpoint";
10
+ export { PhoneNumberClient, validatePhoneNumberClientConfig } from "./subclients/phoneNumberClient";
11
+ export type { PhoneNumberClientConfig } from "./subclients/phoneNumberClient";
12
+ export { WABAClient, validateWABAClientConfig } from "./subclients/wabaClient";
13
+ export type { WABAClientConfig } from "./subclients/wabaClient";
14
+ export * from "./transport";
15
+ export { createFetchTransport } from "./createFetchTransport";
16
+ export type { CreateFetchTransportOptions } from "./createFetchTransport";
17
+ export { paginate, paginateAll, PaginationError } from "./pagination";
18
+ export type { PaginationOptions, PaginatedResult, PaginatedPage, PaginationErrorCode } from "./pagination";
19
+ export { uploadMedia, downloadMedia, deleteMedia, downloadMediaBytes, decryptEncryptedMedia, createUploadSession, uploadFileToSession, getUploadSession, MediaNotImplementedError, MediaValidationError, MediaCryptoError, MediaIntegrityError, DEFAULT_MAX_MEDIA_UPLOAD_BYTES, MAX_MEDIA_UPLOAD_BYTES, DEFAULT_MAX_MEDIA_DOWNLOAD_BYTES, MAX_MEDIA_DOWNLOAD_BYTES, DEFAULT_MAX_UPLOAD_SESSION_BYTES, MAX_UPLOAD_SESSION_BYTES, MEDIA_LINEAR_ISSUE_UPLOAD, MEDIA_LINEAR_ISSUE_DOWNLOAD, MEDIA_LINEAR_ISSUE_DELETE, MEDIA_LINEAR_ISSUE_DECRYPT } from "./endpoints/media";
20
+ export type { MediaOperation, MediaNotImplementedCode, MediaValidationErrorCode, MediaCryptoErrorCode, MediaIntegrityErrorCode, MediaUploadBody, MediaUploadOptions, MediaUploadResponse, MediaDownloadOptions, MediaDownloadResponse, MediaDeleteOptions, MediaDeleteResponse, MediaDownloadBytesOptions, MediaDownloadBytesResponse, EncryptedMediaBundle, CreateUploadSessionParams, UploadSessionOptions, CreateUploadSessionResponse, UploadFileToSessionParams, UploadFileToSessionResponse, GetUploadSessionParams, GetUploadSessionResponse } from "./endpoints/media";
package/dist/index.js ADDED
@@ -0,0 +1,48 @@
1
+ export * from "./client";
2
+ export * from "./errors";
3
+ export * from "./errorRegistry";
4
+ export * from "./errorSubclasses";
5
+ export * from "./endpoints/messages";
6
+ export * from "./endpoints/calling";
7
+ export * from "./endpoints/businessManagement";
8
+ export * from "./endpoints/wabaEndpoints";
9
+ export * from "./endpoint";
10
+ export {
11
+ PhoneNumberClient,
12
+ validatePhoneNumberClientConfig
13
+ } from "./subclients/phoneNumberClient";
14
+ export {
15
+ WABAClient,
16
+ validateWABAClientConfig
17
+ } from "./subclients/wabaClient";
18
+ export * from "./transport";
19
+ export { createFetchTransport } from "./createFetchTransport";
20
+ export {
21
+ paginate,
22
+ paginateAll,
23
+ PaginationError
24
+ } from "./pagination";
25
+ export {
26
+ uploadMedia,
27
+ downloadMedia,
28
+ deleteMedia,
29
+ downloadMediaBytes,
30
+ decryptEncryptedMedia,
31
+ createUploadSession,
32
+ uploadFileToSession,
33
+ getUploadSession,
34
+ MediaNotImplementedError,
35
+ MediaValidationError,
36
+ MediaCryptoError,
37
+ MediaIntegrityError,
38
+ DEFAULT_MAX_MEDIA_UPLOAD_BYTES,
39
+ MAX_MEDIA_UPLOAD_BYTES,
40
+ DEFAULT_MAX_MEDIA_DOWNLOAD_BYTES,
41
+ MAX_MEDIA_DOWNLOAD_BYTES,
42
+ DEFAULT_MAX_UPLOAD_SESSION_BYTES,
43
+ MAX_UPLOAD_SESSION_BYTES,
44
+ MEDIA_LINEAR_ISSUE_UPLOAD,
45
+ MEDIA_LINEAR_ISSUE_DOWNLOAD,
46
+ MEDIA_LINEAR_ISSUE_DELETE,
47
+ MEDIA_LINEAR_ISSUE_DECRYPT
48
+ } from "./endpoints/media";
@@ -0,0 +1,19 @@
1
+ export interface DenseDataArrayOptions {
2
+ readonly helperName: string;
3
+ readonly path: string;
4
+ readonly maxLength?: number;
5
+ readonly rejectOwnToJSON?: boolean;
6
+ readonly minLength?: number;
7
+ readonly invalidTypeMessage?: string;
8
+ readonly invalidLengthMessage?: string;
9
+ readonly unsafePrototypeKeyMessage?: string;
10
+ readonly sparseArrayMessage?: string;
11
+ readonly unsupportedPropertyMessage?: string;
12
+ }
13
+ export interface JoinedStringQueryArrayOptions extends DenseDataArrayOptions {
14
+ readonly maxItemLength?: number;
15
+ readonly rejectCommas?: boolean;
16
+ readonly commaMessage?: string;
17
+ }
18
+ export declare function assertDenseDataArray(value: unknown, opts: DenseDataArrayOptions): unknown[];
19
+ export declare function assertJoinedStringQueryArray(value: unknown, opts: JoinedStringQueryArrayOptions): string;
@@ -0,0 +1,102 @@
1
+ import { graphValidationError, wrapGraphValidation } from "./errors";
2
+ import { assertQueryString } from "./strings";
3
+ function isUnsafePrototypeKey(key) {
4
+ return key === "__proto__" || key === "constructor" || key === "prototype";
5
+ }
6
+ export function assertDenseDataArray(value, opts) {
7
+ if (!Array.isArray(value)) {
8
+ throw graphValidationError(opts.invalidTypeMessage ?? `Invalid ${opts.helperName} input: ${opts.path} must be an array.`);
9
+ }
10
+ const reportedLength = wrapGraphValidation(`Invalid ${opts.helperName} input: ${opts.path} length could not be inspected.`, () => value.length);
11
+ const proto = wrapGraphValidation(`Invalid ${opts.helperName} input: ${opts.path} could not be inspected.`, () => Object.getPrototypeOf(value));
12
+ if (proto !== Array.prototype) {
13
+ throw graphValidationError(`Invalid ${opts.helperName} input: ${opts.path} must use Array.prototype.`);
14
+ }
15
+ const descriptors = wrapGraphValidation(`Invalid ${opts.helperName} input: ${opts.path} descriptors could not be inspected.`, () => Object.getOwnPropertyDescriptors(value));
16
+ const lengthDescriptor = wrapGraphValidation(`Invalid ${opts.helperName} input: ${opts.path} length descriptor could not be inspected.`, () => Object.getOwnPropertyDescriptor(value, "length"));
17
+ if (lengthDescriptor === undefined || typeof lengthDescriptor.get === "function" || typeof lengthDescriptor.set === "function" || typeof lengthDescriptor.value !== "number" || !Number.isSafeInteger(lengthDescriptor.value) || lengthDescriptor.value < 0) {
18
+ throw graphValidationError(`Invalid ${opts.helperName} input: ${opts.path} length could not be inspected.`);
19
+ }
20
+ const length = lengthDescriptor.value;
21
+ if (reportedLength !== length) {
22
+ throw graphValidationError(`Invalid ${opts.helperName} input: ${opts.path} has inconsistent array length.`);
23
+ }
24
+ const numericDescriptorKeys = Object.keys(descriptors).filter((key) => /^(0|[1-9]\d*)$/u.test(key));
25
+ const numericDescriptorKeySet = new Set(numericDescriptorKeys);
26
+ for (const key of numericDescriptorKeys) {
27
+ const index = Number(key);
28
+ if (Number.isSafeInteger(index) && index >= length) {
29
+ throw graphValidationError(`Invalid ${opts.helperName} input: ${opts.path} has indexes beyond its array length.`);
30
+ }
31
+ }
32
+ if (numericDescriptorKeys.length !== length) {
33
+ throw graphValidationError(opts.sparseArrayMessage ?? `Invalid ${opts.helperName} input: ${opts.path} has inconsistent array indexes.`);
34
+ }
35
+ if (length > 0 && !numericDescriptorKeySet.has(String(length - 1))) {
36
+ throw graphValidationError(opts.sparseArrayMessage ?? `Invalid ${opts.helperName} input: ${opts.path} has inconsistent array indexes.`);
37
+ }
38
+ if (opts.minLength !== undefined && length < opts.minLength || opts.maxLength !== undefined && length > opts.maxLength) {
39
+ throw graphValidationError(opts.invalidLengthMessage ?? `Invalid ${opts.helperName} input: ${opts.path} exceeds ${opts.maxLength}-item limit.`);
40
+ }
41
+ const symbolDescriptorKeys = Object.getOwnPropertySymbols(descriptors);
42
+ for (const symbolKey of symbolDescriptorKeys) {
43
+ const descriptor = Object.getOwnPropertyDescriptor(descriptors, symbolKey)?.value;
44
+ if (descriptor === undefined) {
45
+ throw graphValidationError(`Invalid ${opts.helperName} input: ${opts.path} symbol descriptor could not be inspected.`);
46
+ }
47
+ if (symbolKey === Symbol.iterator) {
48
+ throw graphValidationError(`Invalid ${opts.helperName} input: ${opts.path} must not override Array.prototype methods.`);
49
+ }
50
+ if (typeof descriptor.get === "function" || typeof descriptor.set === "function") {
51
+ throw graphValidationError(`Invalid ${opts.helperName} input: ${opts.path} must not use symbol-keyed accessors.`);
52
+ }
53
+ throw graphValidationError(`Invalid ${opts.helperName} input: ${opts.path} must not contain symbol keys.`);
54
+ }
55
+ for (const key of Object.keys(descriptors)) {
56
+ if (key === "length" || /^(0|[1-9]\d*)$/u.test(key))
57
+ continue;
58
+ if (isUnsafePrototypeKey(key)) {
59
+ throw graphValidationError(opts.unsafePrototypeKeyMessage ?? `Invalid ${opts.helperName} input: ${opts.path} contains an unsafe prototype key.`);
60
+ }
61
+ if (key !== "map" && key !== "toJSON") {
62
+ throw graphValidationError(opts.unsupportedPropertyMessage ?? `Invalid ${opts.helperName} input: ${opts.path} contains unsupported properties.`);
63
+ }
64
+ }
65
+ if (Object.prototype.hasOwnProperty.call(descriptors, "map")) {
66
+ throw graphValidationError(`Invalid ${opts.helperName} input: ${opts.path} must not override Array.prototype methods.`);
67
+ }
68
+ if (opts.rejectOwnToJSON !== false && Object.prototype.hasOwnProperty.call(descriptors, "toJSON")) {
69
+ throw graphValidationError(`Invalid ${opts.helperName} input: ${opts.path} must not define toJSON.`);
70
+ }
71
+ const out = [];
72
+ for (let index = 0;index < length; index += 1) {
73
+ const descriptor = descriptors[String(index)];
74
+ if (descriptor === undefined) {
75
+ throw graphValidationError(opts.sparseArrayMessage ?? `Invalid ${opts.helperName} input: ${opts.path} must not contain sparse array holes.`);
76
+ }
77
+ if (descriptor === undefined || typeof descriptor.get === "function" || typeof descriptor.set === "function") {
78
+ throw graphValidationError(`Invalid ${opts.helperName} input: ${opts.path} must not use accessors.`);
79
+ }
80
+ if (typeof descriptor.value === "function" || typeof descriptor.value === "symbol" || typeof descriptor.value === "bigint") {
81
+ throw graphValidationError(`Invalid ${opts.helperName} input: ${opts.path} must contain JSON-like data.`);
82
+ }
83
+ out.push(descriptor.value);
84
+ }
85
+ return out;
86
+ }
87
+ export function assertJoinedStringQueryArray(value, opts) {
88
+ const items = assertDenseDataArray(value, opts);
89
+ const out = [];
90
+ for (let index = 0;index < items.length; index += 1) {
91
+ const item = assertQueryString(items[index], {
92
+ helperName: opts.helperName,
93
+ fieldName: `${opts.path}[${index}]`,
94
+ maxLength: opts.maxItemLength
95
+ });
96
+ if (opts.rejectCommas === true && item.includes(",")) {
97
+ throw graphValidationError(opts.commaMessage ?? `Invalid ${opts.helperName} input: ${opts.path} array entries must not contain commas.`);
98
+ }
99
+ out.push(item);
100
+ }
101
+ return out.join(",");
102
+ }
@@ -0,0 +1,3 @@
1
+ import { GraphRequestValidationError } from "../../errors";
2
+ export declare function graphValidationError(message: string, cause?: unknown): GraphRequestValidationError;
3
+ export declare function wrapGraphValidation<T>(message: string, fn: () => T): T;
@@ -0,0 +1,13 @@
1
+ import { GraphRequestValidationError } from "../../errors";
2
+ export function graphValidationError(message, cause) {
3
+ return new GraphRequestValidationError(message, cause);
4
+ }
5
+ export function wrapGraphValidation(message, fn) {
6
+ try {
7
+ return fn();
8
+ } catch (error) {
9
+ if (error instanceof GraphRequestValidationError)
10
+ throw error;
11
+ throw graphValidationError(message, error);
12
+ }
13
+ }
@@ -0,0 +1,15 @@
1
+ export interface HeaderInitValidationOptions {
2
+ readonly helperName: string;
3
+ readonly path: string;
4
+ readonly invalidTypeMessage?: string;
5
+ readonly inspectMessage?: string;
6
+ readonly descriptorInspectMessage?: string;
7
+ readonly accessorMessage?: string;
8
+ readonly nonStringValueMessage?: string;
9
+ readonly unsafePrototypeKeyMessage?: string;
10
+ readonly invalidKeyMessage?: string;
11
+ readonly symbolAccessorMessage?: string;
12
+ readonly symbolKeyMessage?: string;
13
+ readonly ownToJSONMessage?: string;
14
+ }
15
+ export declare function sanitizeHeaderInit(headers: unknown, opts: HeaderInitValidationOptions): Headers | Record<string, string>;
@@ -0,0 +1,77 @@
1
+ import { graphValidationError, wrapGraphValidation } from "./errors";
2
+ import { hasAsciiControlChar } from "./strings";
3
+ import { isUnsafePrototypeKey } from "./records";
4
+ function invalidTypeMessage(opts) {
5
+ return opts.invalidTypeMessage ?? `Invalid ${opts.helperName} input: ${opts.path} must be a plain object.`;
6
+ }
7
+ function inspectMessage(opts) {
8
+ return opts.inspectMessage ?? `Invalid ${opts.helperName} input: ${opts.path} could not be inspected.`;
9
+ }
10
+ function descriptorInspectMessage(opts) {
11
+ return opts.descriptorInspectMessage ?? `Invalid ${opts.helperName} input: ${opts.path} descriptors could not be inspected.`;
12
+ }
13
+ function accessorMessage(opts) {
14
+ return opts.accessorMessage ?? `Invalid ${opts.helperName} input: ${opts.path} must not use accessors.`;
15
+ }
16
+ function nonStringValueMessage(opts) {
17
+ return opts.nonStringValueMessage ?? `Invalid ${opts.helperName} input: ${opts.path} values must be strings.`;
18
+ }
19
+ function unsafePrototypeKeyMessage(opts) {
20
+ return opts.unsafePrototypeKeyMessage ?? `Invalid ${opts.helperName} input: ${opts.path} contains an unsafe prototype key.`;
21
+ }
22
+ function invalidKeyMessage(opts) {
23
+ return opts.invalidKeyMessage ?? `Invalid ${opts.helperName} input: ${opts.path} contains an invalid key.`;
24
+ }
25
+ function symbolAccessorMessage(opts) {
26
+ return opts.symbolAccessorMessage ?? `Invalid ${opts.helperName} input: ${opts.path} must not use symbol-keyed accessors.`;
27
+ }
28
+ function symbolKeyMessage(opts) {
29
+ return opts.symbolKeyMessage ?? `Invalid ${opts.helperName} input: ${opts.path} must not contain symbol keys.`;
30
+ }
31
+ function ownToJSONMessage(opts) {
32
+ return opts.ownToJSONMessage ?? `Invalid ${opts.helperName} input: ${opts.path} must not define toJSON.`;
33
+ }
34
+ export function sanitizeHeaderInit(headers, opts) {
35
+ if (typeof headers !== "object" || headers === null || Array.isArray(headers)) {
36
+ throw graphValidationError(invalidTypeMessage(opts));
37
+ }
38
+ const proto = wrapGraphValidation(inspectMessage(opts), () => Object.getPrototypeOf(headers));
39
+ if (proto === Headers.prototype) {
40
+ wrapGraphValidation(inspectMessage(opts), () => Headers.prototype.has.call(headers, "x-wats-brand-check"));
41
+ return wrapGraphValidation(inspectMessage(opts), () => new Headers(headers));
42
+ }
43
+ if (proto !== Object.prototype && proto !== null) {
44
+ throw graphValidationError(invalidTypeMessage(opts));
45
+ }
46
+ const descriptors = wrapGraphValidation(descriptorInspectMessage(opts), () => Object.getOwnPropertyDescriptors(headers));
47
+ const out = {};
48
+ for (const key of Reflect.ownKeys(descriptors)) {
49
+ const descriptor = descriptors[key];
50
+ if (descriptor === undefined) {
51
+ throw graphValidationError(descriptorInspectMessage(opts));
52
+ }
53
+ if (typeof key === "symbol") {
54
+ if (typeof descriptor.get === "function" || typeof descriptor.set === "function") {
55
+ throw graphValidationError(symbolAccessorMessage(opts));
56
+ }
57
+ throw graphValidationError(symbolKeyMessage(opts));
58
+ }
59
+ if (key === "toJSON") {
60
+ throw graphValidationError(ownToJSONMessage(opts));
61
+ }
62
+ if (isUnsafePrototypeKey(key)) {
63
+ throw graphValidationError(unsafePrototypeKeyMessage(opts));
64
+ }
65
+ if (key.length === 0 || hasAsciiControlChar(key)) {
66
+ throw graphValidationError(invalidKeyMessage(opts));
67
+ }
68
+ if (typeof descriptor.get === "function" || typeof descriptor.set === "function") {
69
+ throw graphValidationError(accessorMessage(opts));
70
+ }
71
+ if (typeof descriptor.value !== "string") {
72
+ throw graphValidationError(nonStringValueMessage(opts));
73
+ }
74
+ out[key] = descriptor.value;
75
+ }
76
+ return out;
77
+ }
@@ -0,0 +1,8 @@
1
+ export * from "./arrays";
2
+ export * from "./errors";
3
+ export * from "./headers";
4
+ export * from "./json";
5
+ export * from "./options";
6
+ export * from "./paths";
7
+ export * from "./records";
8
+ export * from "./strings";
@@ -0,0 +1,8 @@
1
+ export * from "./arrays";
2
+ export * from "./errors";
3
+ export * from "./headers";
4
+ export * from "./json";
5
+ export * from "./options";
6
+ export * from "./paths";
7
+ export * from "./records";
8
+ export * from "./strings";
@@ -0,0 +1,10 @@
1
+ export interface SafeJsonCloneOptions {
2
+ readonly helperName: string;
3
+ readonly path: string;
4
+ readonly maxDepth: number;
5
+ readonly maxArrayLength: number;
6
+ readonly maxStringLength: number;
7
+ readonly maxKeys?: number;
8
+ readonly allowUndefined?: boolean;
9
+ }
10
+ export declare function safeJsonClone(value: unknown, opts: SafeJsonCloneOptions): unknown;
@@ -0,0 +1,79 @@
1
+ import { assertDenseDataArray } from "./arrays";
2
+ import { graphValidationError, wrapGraphValidation } from "./errors";
3
+ import { assertPlainDataRecord } from "./records";
4
+ import { hasAsciiControlChar } from "./strings";
5
+ function childPath(path, key) {
6
+ return /^[A-Za-z_$][A-Za-z0-9_$]*$/u.test(key) ? `${path}.${key}` : `${path}[${JSON.stringify(key)}]`;
7
+ }
8
+ function clone(value, opts, path, seen, depth) {
9
+ if (depth > opts.maxDepth) {
10
+ throw graphValidationError(`Invalid ${opts.helperName} input: ${path} exceeds maximum depth ${opts.maxDepth}.`);
11
+ }
12
+ if (value === null || typeof value === "boolean")
13
+ return value;
14
+ if (typeof value === "string") {
15
+ if (value.length > opts.maxStringLength || hasAsciiControlChar(value)) {
16
+ throw graphValidationError(`Invalid ${opts.helperName} input: ${path} contains an invalid string.`);
17
+ }
18
+ return value;
19
+ }
20
+ if (typeof value === "number") {
21
+ if (!Number.isFinite(value)) {
22
+ throw graphValidationError(`Invalid ${opts.helperName} input: ${path} contains a non-finite number.`);
23
+ }
24
+ return value;
25
+ }
26
+ if (value === undefined) {
27
+ if (opts.allowUndefined === true)
28
+ return;
29
+ throw graphValidationError(`Invalid ${opts.helperName} input: ${path} must be JSON-like data.`);
30
+ }
31
+ if (typeof value === "function" || typeof value === "symbol" || typeof value === "bigint") {
32
+ throw graphValidationError(`Invalid ${opts.helperName} input: ${path} must be JSON-like data.`);
33
+ }
34
+ if (typeof value !== "object") {
35
+ throw graphValidationError(`Invalid ${opts.helperName} input: ${path} must be JSON-like data.`);
36
+ }
37
+ if (seen.has(value)) {
38
+ throw graphValidationError(`Invalid ${opts.helperName} input: ${path} must not contain cycles.`);
39
+ }
40
+ seen.add(value);
41
+ try {
42
+ if (Array.isArray(value)) {
43
+ const arr = assertDenseDataArray(value, {
44
+ helperName: opts.helperName,
45
+ path,
46
+ maxLength: opts.maxArrayLength
47
+ });
48
+ const out = [];
49
+ for (let index = 0;index < arr.length; index += 1) {
50
+ const cloned = clone(arr[index], opts, `${path}[${index}]`, seen, depth + 1);
51
+ if (cloned !== undefined)
52
+ out.push(cloned);
53
+ }
54
+ return out;
55
+ }
56
+ const record = assertPlainDataRecord(value, {
57
+ helperName: opts.helperName,
58
+ path,
59
+ rejectFunctionsSymbolsBigInts: true
60
+ });
61
+ const descriptors = wrapGraphValidation(`Invalid ${opts.helperName} input: ${path} descriptors could not be inspected.`, () => Object.getOwnPropertyDescriptors(record));
62
+ const entries = Object.entries(descriptors);
63
+ if (opts.maxKeys !== undefined && entries.length > opts.maxKeys) {
64
+ throw graphValidationError(`Invalid ${opts.helperName} input: ${path} exceeds ${opts.maxKeys}-key limit.`);
65
+ }
66
+ const out = {};
67
+ for (const [key, descriptor] of entries) {
68
+ const cloned = clone(descriptor.value, opts, childPath(path, key), seen, depth + 1);
69
+ if (cloned !== undefined)
70
+ out[key] = cloned;
71
+ }
72
+ return out;
73
+ } finally {
74
+ seen.delete(value);
75
+ }
76
+ }
77
+ export function safeJsonClone(value, opts) {
78
+ return clone(value, opts, opts.path, new WeakSet, 0);
79
+ }
@@ -0,0 +1,5 @@
1
+ export declare function copyOptionalParamsObject(params: unknown, helperName: string): Record<string, unknown>;
2
+ export declare function splitRequiredStringDataProp(params: unknown, propName: string, helperName: string): {
3
+ readonly value: string;
4
+ readonly rest: Record<string, unknown>;
5
+ };