ofsc-utility 1.0.45 → 1.0.46
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/utilities/index.js +49 -33
- package/package.json +1 -1
package/dist/utilities/index.js
CHANGED
|
@@ -69,40 +69,56 @@ const fetchWithRetry = async (url, clientId, clientSecret, instanceUrl, token, r
|
|
|
69
69
|
});
|
|
70
70
|
};
|
|
71
71
|
console.log(`Fetching ${url}`);
|
|
72
|
-
|
|
73
|
-
let
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
res
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
res.status ===
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
72
|
+
let currentToken = token;
|
|
73
|
+
let tokenRefreshed = false;
|
|
74
|
+
let remainingRetries = retries;
|
|
75
|
+
let delay = baseDelay;
|
|
76
|
+
while (true) {
|
|
77
|
+
let res = await doFetch(currentToken);
|
|
78
|
+
if (res.status === 401 && !tokenRefreshed) {
|
|
79
|
+
console.warn("Token expired — renewing token…");
|
|
80
|
+
currentToken = await (0, oauthTokenService_1.getOAuthToken)(clientId, clientSecret, instanceUrl);
|
|
81
|
+
tokenRefreshed = true;
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
const responseText = await res.text();
|
|
85
|
+
const isNoRouteToHost = res.status === 400 &&
|
|
86
|
+
responseText.includes("NoRouteToHostException");
|
|
87
|
+
const isRetryableStatus = res.status === 429 ||
|
|
88
|
+
res.status === 502 ||
|
|
89
|
+
res.status === 503 ||
|
|
90
|
+
res.status === 504;
|
|
91
|
+
if ((isRetryableStatus || isNoRouteToHost) &&
|
|
92
|
+
remainingRetries > 0) {
|
|
93
|
+
const retryAfter = res.headers.get("Retry-After");
|
|
94
|
+
let retryDelay = delay;
|
|
95
|
+
if (retryAfter) {
|
|
96
|
+
const retryAfterSeconds = Number(retryAfter);
|
|
97
|
+
if (!Number.isNaN(retryAfterSeconds)) {
|
|
98
|
+
retryDelay = retryAfterSeconds * 1000;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
console.warn(`Retrying in ${retryDelay}ms... (${remainingRetries} retries left)`);
|
|
102
|
+
await new Promise(resolve => setTimeout(resolve, retryDelay));
|
|
103
|
+
remainingRetries--;
|
|
104
|
+
delay *= 2;
|
|
105
|
+
continue;
|
|
106
|
+
}
|
|
107
|
+
if (!res.ok) {
|
|
108
|
+
throw new Error(`Request failed: ${res.status} ${res.statusText}\n${responseText}`);
|
|
109
|
+
}
|
|
110
|
+
let data;
|
|
111
|
+
try {
|
|
112
|
+
data = JSON.parse(responseText);
|
|
113
|
+
}
|
|
114
|
+
catch {
|
|
115
|
+
throw new Error(`Invalid JSON response from ${url}\n${responseText}`);
|
|
116
|
+
}
|
|
117
|
+
return {
|
|
118
|
+
data,
|
|
119
|
+
token: currentToken
|
|
120
|
+
};
|
|
100
121
|
}
|
|
101
|
-
// Return parsed JSON + latest token
|
|
102
|
-
return {
|
|
103
|
-
data: await res.json(),
|
|
104
|
-
token
|
|
105
|
-
};
|
|
106
122
|
};
|
|
107
123
|
exports.fetchWithRetry = fetchWithRetry;
|
|
108
124
|
const fs_1 = __importDefault(require("fs"));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ofsc-utility",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.46",
|
|
4
4
|
"description": "TypeScript helpers for Oracle Field Service Cloud (OFSC): events, resources, inventories, metadata and CSV/Excel utilities.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|