@panomc/sdk 1.0.0-dev.58 → 1.0.0-dev.59
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/core/js/api.util.js +23 -1
- package/package.json +1 -1
package/core/js/api.util.js
CHANGED
|
@@ -9,6 +9,12 @@ import { show } from "$lib/components/ToastContainer.svelte";
|
|
|
9
9
|
export const NETWORK_ERROR = 'NETWORK_ERROR';
|
|
10
10
|
export const networkErrorBody = { result: 'error', error: NETWORK_ERROR };
|
|
11
11
|
|
|
12
|
+
const NETWORK_RETRY_DELAY_MS = 400;
|
|
13
|
+
|
|
14
|
+
function isIdempotent(method) {
|
|
15
|
+
return !method || ['GET', 'HEAD', 'OPTIONS'].includes(method.toUpperCase());
|
|
16
|
+
}
|
|
17
|
+
|
|
12
18
|
// Function to build query parameters from an object
|
|
13
19
|
export function buildQueryParams(params) {
|
|
14
20
|
const queryString = Object.keys(params)
|
|
@@ -222,9 +228,25 @@ const ApiUtil = {
|
|
|
222
228
|
}
|
|
223
229
|
|
|
224
230
|
// Perform fetch request
|
|
225
|
-
const
|
|
231
|
+
const doFetch = () =>
|
|
226
232
|
request && request.fetch ? request.fetch(path, options) : fetch(path, options);
|
|
227
233
|
|
|
234
|
+
// A request that never got a response (fetch rejected) is retried ONCE before the
|
|
235
|
+
// offline splash: browsers reuse a pooled HTTP/3 connection that the peer may have
|
|
236
|
+
// just idled out (Firefox: 30 s), and the request on it fails instantly although the
|
|
237
|
+
// server is fine. A new connection succeeds. Only idempotent methods, so a POST/PUT
|
|
238
|
+
// is never sent twice; HTTP error responses are not retried (fetch resolves them).
|
|
239
|
+
const fetchMethod =
|
|
240
|
+
browser && isIdempotent(data.method)
|
|
241
|
+
? doFetch().catch((err) => {
|
|
242
|
+
if (err?.name === 'AbortError') throw err;
|
|
243
|
+
|
|
244
|
+
return new Promise((resolve) => setTimeout(resolve, NETWORK_RETRY_DELAY_MS)).then(
|
|
245
|
+
doFetch,
|
|
246
|
+
);
|
|
247
|
+
})
|
|
248
|
+
: doFetch();
|
|
249
|
+
|
|
228
250
|
// Handle response
|
|
229
251
|
return fetchMethod
|
|
230
252
|
.then(bodyHandler)
|