ofsc-utility 1.0.45 → 1.0.47
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/CHANGELOG.md +3 -0
- package/dist/utilities/index.js +50 -33
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -6,6 +6,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
|
|
|
6
6
|
|
|
7
7
|
## [Unreleased]
|
|
8
8
|
|
|
9
|
+
### Maintenance
|
|
10
|
+
- version 1.0.46
|
|
11
|
+
|
|
9
12
|
### Added
|
|
10
13
|
- release v1.0.44 with getResource function and updated keywords
|
|
11
14
|
- version 1.0.43 with built-in resilience and exponential backoff
|
package/dist/utilities/index.js
CHANGED
|
@@ -69,40 +69,57 @@ 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 === 400 ||
|
|
88
|
+
res.status === 429 ||
|
|
89
|
+
res.status === 502 ||
|
|
90
|
+
res.status === 503 ||
|
|
91
|
+
res.status === 504;
|
|
92
|
+
if ((isRetryableStatus || isNoRouteToHost) &&
|
|
93
|
+
remainingRetries > 0) {
|
|
94
|
+
const retryAfter = res.headers.get("Retry-After");
|
|
95
|
+
let retryDelay = delay;
|
|
96
|
+
if (retryAfter) {
|
|
97
|
+
const retryAfterSeconds = Number(retryAfter);
|
|
98
|
+
if (!Number.isNaN(retryAfterSeconds)) {
|
|
99
|
+
retryDelay = retryAfterSeconds * 1000;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
console.warn(`Retrying in ${retryDelay}ms... (${remainingRetries} retries left)`);
|
|
103
|
+
await new Promise(resolve => setTimeout(resolve, retryDelay));
|
|
104
|
+
remainingRetries--;
|
|
105
|
+
delay *= 2;
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
if (!res.ok) {
|
|
109
|
+
throw new Error(`Request failed: ${res.status} ${res.statusText}\n${responseText}`);
|
|
110
|
+
}
|
|
111
|
+
let data;
|
|
112
|
+
try {
|
|
113
|
+
data = JSON.parse(responseText);
|
|
114
|
+
}
|
|
115
|
+
catch {
|
|
116
|
+
throw new Error(`Invalid JSON response from ${url}\n${responseText}`);
|
|
117
|
+
}
|
|
118
|
+
return {
|
|
119
|
+
data,
|
|
120
|
+
token: currentToken
|
|
121
|
+
};
|
|
100
122
|
}
|
|
101
|
-
// Return parsed JSON + latest token
|
|
102
|
-
return {
|
|
103
|
-
data: await res.json(),
|
|
104
|
-
token
|
|
105
|
-
};
|
|
106
123
|
};
|
|
107
124
|
exports.fetchWithRetry = fetchWithRetry;
|
|
108
125
|
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.47",
|
|
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",
|