uaw-mcp 1.0.2 → 1.0.3
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/api.js +46 -5
- package/package.json +1 -1
package/dist/api.js
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
1
|
import { config } from "./config.js";
|
|
2
|
+
const MAX_RETRIES = 3;
|
|
3
|
+
const BASE_DELAY_MS = 1000;
|
|
4
|
+
async function sleep(ms) {
|
|
5
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
6
|
+
}
|
|
2
7
|
async function parseResponse(res) {
|
|
3
8
|
const text = await res.text();
|
|
4
9
|
let parsed;
|
|
@@ -18,10 +23,48 @@ async function parseResponse(res) {
|
|
|
18
23
|
"message" in parsed
|
|
19
24
|
? String(parsed.message)
|
|
20
25
|
: `HTTP ${res.status} ${res.statusText}`;
|
|
21
|
-
|
|
26
|
+
const err = new Error(message);
|
|
27
|
+
err.status = res.status;
|
|
28
|
+
if (res.status === 429) {
|
|
29
|
+
err.retryAfter = parseInt(res.headers.get("Retry-After") ?? "60", 10);
|
|
30
|
+
}
|
|
31
|
+
throw err;
|
|
22
32
|
}
|
|
23
33
|
return parsed;
|
|
24
34
|
}
|
|
35
|
+
async function fetchWithRetry(url, init) {
|
|
36
|
+
let lastError;
|
|
37
|
+
for (let attempt = 0; attempt < MAX_RETRIES; attempt++) {
|
|
38
|
+
try {
|
|
39
|
+
const res = await fetch(url, init);
|
|
40
|
+
return await parseResponse(res);
|
|
41
|
+
}
|
|
42
|
+
catch (err) {
|
|
43
|
+
lastError = err;
|
|
44
|
+
const status = err.status;
|
|
45
|
+
// 429 — respect Retry-After header, then retry
|
|
46
|
+
if (status === 429) {
|
|
47
|
+
const retryAfter = err.retryAfter ?? 60;
|
|
48
|
+
// Cap wait at 30s for MCP responsiveness
|
|
49
|
+
const waitMs = Math.min(retryAfter * 1000, 30_000);
|
|
50
|
+
if (attempt < MAX_RETRIES - 1) {
|
|
51
|
+
await sleep(waitMs);
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
throw new Error(`Rate limit exceeded. The UAW API allows limited requests per window. ` +
|
|
55
|
+
`Please wait ${retryAfter} seconds before retrying.`);
|
|
56
|
+
}
|
|
57
|
+
// 5xx — exponential backoff
|
|
58
|
+
if (status !== undefined && status >= 500 && attempt < MAX_RETRIES - 1) {
|
|
59
|
+
await sleep(BASE_DELAY_MS * Math.pow(2, attempt));
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
// 4xx (not 429) — don't retry
|
|
63
|
+
throw err;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
throw lastError;
|
|
67
|
+
}
|
|
25
68
|
export async function apiGet(path, params) {
|
|
26
69
|
const url = new URL(`${config.apiBase}${path}`);
|
|
27
70
|
if (params) {
|
|
@@ -30,17 +73,15 @@ export async function apiGet(path, params) {
|
|
|
30
73
|
url.searchParams.set(k, v);
|
|
31
74
|
}
|
|
32
75
|
}
|
|
33
|
-
|
|
34
|
-
return parseResponse(res);
|
|
76
|
+
return fetchWithRetry(url.toString());
|
|
35
77
|
}
|
|
36
78
|
export async function apiPost(path, body, apiKey) {
|
|
37
79
|
const headers = { "Content-Type": "application/json" };
|
|
38
80
|
if (apiKey)
|
|
39
81
|
headers["Authorization"] = `Bearer ${apiKey}`;
|
|
40
|
-
|
|
82
|
+
return fetchWithRetry(`${config.apiBase}${path}`, {
|
|
41
83
|
method: "POST",
|
|
42
84
|
headers,
|
|
43
85
|
body: JSON.stringify(body),
|
|
44
86
|
});
|
|
45
|
-
return parseResponse(res);
|
|
46
87
|
}
|