@wats/graph 0.3.24 → 0.3.26
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/client.js +4 -2
- package/dist/endpoints/media.js +15 -1
- package/dist/errorSubclasses.js +16 -0
- package/dist/errors.d.ts +10 -0
- package/dist/errors.js +24 -0
- package/package.json +1 -1
package/dist/client.js
CHANGED
|
@@ -87,14 +87,16 @@ export class GraphClient {
|
|
|
87
87
|
throw createGraphApiError({
|
|
88
88
|
status: response.status,
|
|
89
89
|
payload: parsedBody.error,
|
|
90
|
-
fallbackMessage: getFallbackErrorMessage(response.status)
|
|
90
|
+
fallbackMessage: getFallbackErrorMessage(response.status),
|
|
91
|
+
headers: response.headers
|
|
91
92
|
});
|
|
92
93
|
}
|
|
93
94
|
const fallbackPayload = this.extractFallbackGraphPayload(parsedBody);
|
|
94
95
|
const params = {
|
|
95
96
|
status: response.status,
|
|
96
97
|
fallbackMessage: getFallbackErrorMessage(response.status),
|
|
97
|
-
classify: false
|
|
98
|
+
classify: false,
|
|
99
|
+
headers: response.headers
|
|
98
100
|
};
|
|
99
101
|
if (fallbackPayload !== undefined) {
|
|
100
102
|
params.payload = fallbackPayload;
|
package/dist/endpoints/media.js
CHANGED
|
@@ -509,6 +509,20 @@ function validateBase64Digest(value, label, code = "invalid_base64", expectedLen
|
|
|
509
509
|
throw new MediaCryptoError(code, `Invalid ${label}: malformed base64.`, cause);
|
|
510
510
|
}
|
|
511
511
|
}
|
|
512
|
+
function validateSha256Digest(value, label) {
|
|
513
|
+
if (typeof value !== "string" || value.trim().length === 0) {
|
|
514
|
+
throw new MediaValidationError("invalid_options", `Invalid ${label}: expected a non-empty hex or base64 SHA-256 digest string.`);
|
|
515
|
+
}
|
|
516
|
+
const trimmed = value.trim();
|
|
517
|
+
if (/^[0-9a-fA-F]{64}$/u.test(trimmed)) {
|
|
518
|
+
const out = new Uint8Array(32);
|
|
519
|
+
for (let i = 0;i < 32; i += 1) {
|
|
520
|
+
out[i] = Number.parseInt(trimmed.slice(i * 2, i * 2 + 2), 16);
|
|
521
|
+
}
|
|
522
|
+
return out;
|
|
523
|
+
}
|
|
524
|
+
return validateBase64Digest(trimmed, label, "invalid_options", 32);
|
|
525
|
+
}
|
|
512
526
|
function validateDownloadBytesOptions(options) {
|
|
513
527
|
if (!isRecord(options)) {
|
|
514
528
|
throw new MediaValidationError("invalid_options", "Invalid media download bytes options: expected an object.");
|
|
@@ -516,7 +530,7 @@ function validateDownloadBytesOptions(options) {
|
|
|
516
530
|
const url = validateHttpUrl(options.url);
|
|
517
531
|
const maxBytes = validateDownloadMaxBytes(options);
|
|
518
532
|
const signal = validateSignal(options.signal);
|
|
519
|
-
const expectedSha256 = options.expectedSha256 === undefined ? undefined :
|
|
533
|
+
const expectedSha256 = options.expectedSha256 === undefined ? undefined : validateSha256Digest(options.expectedSha256, "expectedSha256");
|
|
520
534
|
return {
|
|
521
535
|
url,
|
|
522
536
|
maxBytes,
|
package/dist/errorSubclasses.js
CHANGED
|
@@ -23,8 +23,24 @@ function coreParams(ctx, fallback) {
|
|
|
23
23
|
if (ctx.payload !== undefined) {
|
|
24
24
|
out.payload = ctx.payload;
|
|
25
25
|
}
|
|
26
|
+
const retryAfter = retryAfterFromContext(ctx);
|
|
27
|
+
if (retryAfter !== undefined) {
|
|
28
|
+
out.retryAfter = retryAfter;
|
|
29
|
+
}
|
|
26
30
|
return out;
|
|
27
31
|
}
|
|
32
|
+
function retryAfterFromContext(ctx) {
|
|
33
|
+
let raw;
|
|
34
|
+
try {
|
|
35
|
+
raw = ctx.headers.get("retry-after");
|
|
36
|
+
} catch {
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
if (typeof raw !== "string")
|
|
40
|
+
return;
|
|
41
|
+
const trimmed = raw.trim();
|
|
42
|
+
return trimmed.length === 0 ? undefined : trimmed;
|
|
43
|
+
}
|
|
28
44
|
|
|
29
45
|
export class AuthException extends GraphAuthError {
|
|
30
46
|
static errorCode = 0;
|
package/dist/errors.d.ts
CHANGED
|
@@ -17,6 +17,13 @@ export declare class GraphApiError extends Error {
|
|
|
17
17
|
readonly errorSubcode?: number;
|
|
18
18
|
readonly fbtraceId?: string;
|
|
19
19
|
readonly payload?: GraphApiErrorPayload;
|
|
20
|
+
/**
|
|
21
|
+
* Raw `Retry-After` response header value when Meta supplied one on a
|
|
22
|
+
* throttled request, preserved verbatim so an HTTP-facing layer can echo
|
|
23
|
+
* it back to its own caller. Only populated when the originating Graph
|
|
24
|
+
* response carried the header; otherwise undefined. WATS-130 R1.2.
|
|
25
|
+
*/
|
|
26
|
+
readonly retryAfter?: string;
|
|
20
27
|
/**
|
|
21
28
|
* Broad HTTP-status-driven classification:
|
|
22
29
|
* 4xx → "ClientError"
|
|
@@ -33,6 +40,7 @@ export declare class GraphApiError extends Error {
|
|
|
33
40
|
message: string;
|
|
34
41
|
status: number;
|
|
35
42
|
payload?: GraphApiErrorPayload;
|
|
43
|
+
retryAfter?: string;
|
|
36
44
|
});
|
|
37
45
|
}
|
|
38
46
|
export declare class GraphAuthError extends GraphApiError {
|
|
@@ -40,6 +48,7 @@ export declare class GraphAuthError extends GraphApiError {
|
|
|
40
48
|
message: string;
|
|
41
49
|
status: number;
|
|
42
50
|
payload?: GraphApiErrorPayload;
|
|
51
|
+
retryAfter?: string;
|
|
43
52
|
});
|
|
44
53
|
}
|
|
45
54
|
export declare class GraphRateLimitError extends GraphApiError {
|
|
@@ -47,6 +56,7 @@ export declare class GraphRateLimitError extends GraphApiError {
|
|
|
47
56
|
message: string;
|
|
48
57
|
status: number;
|
|
49
58
|
payload?: GraphApiErrorPayload;
|
|
59
|
+
retryAfter?: string;
|
|
50
60
|
});
|
|
51
61
|
}
|
|
52
62
|
export declare class GraphNetworkError extends Error {
|
package/dist/errors.js
CHANGED
|
@@ -5,12 +5,16 @@ export class GraphApiError extends Error {
|
|
|
5
5
|
errorSubcode;
|
|
6
6
|
fbtraceId;
|
|
7
7
|
payload;
|
|
8
|
+
retryAfter;
|
|
8
9
|
classification;
|
|
9
10
|
constructor(params) {
|
|
10
11
|
super(params.message);
|
|
11
12
|
this.name = "GraphApiError";
|
|
12
13
|
this.status = params.status;
|
|
13
14
|
this.classification = classifyStatus(params.status);
|
|
15
|
+
if (params.retryAfter !== undefined) {
|
|
16
|
+
this.retryAfter = params.retryAfter;
|
|
17
|
+
}
|
|
14
18
|
if (params.payload !== undefined) {
|
|
15
19
|
this.payload = params.payload;
|
|
16
20
|
if (params.payload.type !== undefined) {
|
|
@@ -37,6 +41,22 @@ function classifyStatus(status) {
|
|
|
37
41
|
}
|
|
38
42
|
return "Unknown";
|
|
39
43
|
}
|
|
44
|
+
function readRetryAfterHeader(headers) {
|
|
45
|
+
if (headers === undefined) {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
let raw;
|
|
49
|
+
try {
|
|
50
|
+
raw = headers.get("retry-after");
|
|
51
|
+
} catch {
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
if (typeof raw !== "string") {
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
const trimmed = raw.trim();
|
|
58
|
+
return trimmed.length === 0 ? undefined : trimmed;
|
|
59
|
+
}
|
|
40
60
|
|
|
41
61
|
export class GraphAuthError extends GraphApiError {
|
|
42
62
|
constructor(params) {
|
|
@@ -205,6 +225,7 @@ export function scrubErrorCause(err) {
|
|
|
205
225
|
export function createGraphApiError(params) {
|
|
206
226
|
const message = params.payload?.message ?? params.fallbackMessage ?? "Graph API request failed";
|
|
207
227
|
const shouldClassify = params.classify ?? true;
|
|
228
|
+
const retryAfter = readRetryAfterHeader(params.headers);
|
|
208
229
|
const ctorParams = {
|
|
209
230
|
message,
|
|
210
231
|
status: params.status
|
|
@@ -212,6 +233,9 @@ export function createGraphApiError(params) {
|
|
|
212
233
|
if (params.payload !== undefined) {
|
|
213
234
|
ctorParams.payload = params.payload;
|
|
214
235
|
}
|
|
236
|
+
if (retryAfter !== undefined) {
|
|
237
|
+
ctorParams.retryAfter = retryAfter;
|
|
238
|
+
}
|
|
215
239
|
if (!shouldClassify) {
|
|
216
240
|
return new GraphApiError(ctorParams);
|
|
217
241
|
}
|