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.
@@ -69,40 +69,56 @@ const fetchWithRetry = async (url, clientId, clientSecret, instanceUrl, token, r
69
69
  });
70
70
  };
71
71
  console.log(`Fetching ${url}`);
72
- // Try with the current token
73
- let res = await doFetch(token);
74
- /* ---------- 401: refresh token ONCE per call ---------- */
75
- if (res.status === 401) {
76
- console.warn("Token expired — renewing token…");
77
- token = await (0, oauthTokenService_1.getOAuthToken)(clientId, clientSecret, instanceUrl);
78
- res = await doFetch(token);
79
- }
80
- const responseText = await res.text();
81
- const isNoRouteToHost = res.status === 400 &&
82
- responseText.includes("NoRouteToHostException");
83
- const isRetryableStatus = res.status === 429 ||
84
- res.status === 502 ||
85
- res.status === 503 ||
86
- res.status === 504;
87
- /* ---------- retry with backoff ---------- */
88
- if ((isRetryableStatus || isNoRouteToHost) && retries > 0) {
89
- const retryAfter = res.headers.get("Retry-After");
90
- console.log("Retrying...", retryAfter);
91
- const delay = retryAfter ? Number(retryAfter) * 1000 : baseDelay;
92
- console.warn(`Retrying in ${delay}ms... (${retries} left)`);
93
- await new Promise(r => setTimeout(r, delay));
94
- return (0, exports.fetchWithRetry)(url, clientId, clientSecret, instanceUrl, token, retries - 1, baseDelay * 2);
95
- }
96
- // If still not OK → fail
97
- if (!res.ok) {
98
- const body = await res.text();
99
- throw new Error(`Request failed: ${res.status} ${res.statusText}\n${body}`);
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.45",
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",