@medialane/sdk 0.71.0 → 0.71.1

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/index.cjs CHANGED
@@ -231,15 +231,16 @@ async function withRetry(fn, opts) {
231
231
  return await fn();
232
232
  } catch (err) {
233
233
  lastError = err;
234
- if (err instanceof MedialaneApiError && err.status < 500) {
234
+ if (err instanceof MedialaneApiError && err.status < 500 && err.status !== 429) {
235
235
  throw err;
236
236
  }
237
- const isRetryable = err instanceof MedialaneApiError && err.status >= 500 || err instanceof TypeError;
237
+ const isRetryable = err instanceof MedialaneApiError && (err.status >= 500 || err.status === 429) || err instanceof TypeError;
238
238
  if (!isRetryable || attempt === maxAttempts - 1) {
239
239
  throw err;
240
240
  }
241
- const jitter = Math.random() * baseDelayMs;
242
- const delay = Math.min(baseDelayMs * Math.pow(2, attempt) + jitter, maxDelayMs);
241
+ const retryAfterMs = err instanceof MedialaneApiError ? err.retryAfterMs : void 0;
242
+ const backoff = baseDelayMs * Math.pow(2, attempt) + Math.random() * baseDelayMs;
243
+ const delay = Math.min(retryAfterMs ?? backoff, maxDelayMs);
243
244
  await sleep(delay);
244
245
  }
245
246
  }
@@ -256,13 +257,22 @@ function deriveErrorCode(status) {
256
257
  return "UNKNOWN";
257
258
  }
258
259
  var MedialaneApiError = class extends Error {
259
- constructor(status, message) {
260
+ constructor(status, message, retryAfterMs) {
260
261
  super(message);
261
262
  this.status = status;
263
+ this.retryAfterMs = retryAfterMs;
262
264
  this.name = "MedialaneApiError";
263
265
  this.code = deriveErrorCode(status);
264
266
  }
265
267
  };
268
+ function parseRetryAfter(value) {
269
+ if (!value) return void 0;
270
+ const seconds = Number(value);
271
+ if (Number.isFinite(seconds)) return Math.max(0, seconds * 1e3);
272
+ const date = Date.parse(value);
273
+ if (!Number.isNaN(date)) return Math.max(0, date - Date.now());
274
+ return void 0;
275
+ }
266
276
  var ApiClient = class {
267
277
  constructor(baseUrl, apiKey, retryOptions, chain = "STARKNET") {
268
278
  this.baseUrl = baseUrl;
@@ -301,7 +311,7 @@ var ApiClient = class {
301
311
  if (body.error) message = body.error;
302
312
  } catch {
303
313
  }
304
- throw new MedialaneApiError(response.status, message);
314
+ throw new MedialaneApiError(response.status, message, parseRetryAfter(response.headers.get("retry-after")));
305
315
  }
306
316
  return response;
307
317
  }, this.retryOptions);