@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.
@@ -970,8 +970,12 @@ type MedialaneErrorCode = "TOKEN_NOT_FOUND" | "COLLECTION_NOT_FOUND" | "ORDER_NO
970
970
 
971
971
  declare class MedialaneApiError extends Error {
972
972
  readonly status: number;
973
+ /** Parsed `Retry-After` (ms) when the server sent one — used by withRetry on 429. */
974
+ readonly retryAfterMs?: number | undefined;
973
975
  readonly code: MedialaneErrorCode;
974
- constructor(status: number, message: string);
976
+ constructor(status: number, message: string,
977
+ /** Parsed `Retry-After` (ms) when the server sent one — used by withRetry on 429. */
978
+ retryAfterMs?: number | undefined);
975
979
  }
976
980
  declare class ApiClient {
977
981
  private readonly baseUrl;
@@ -970,8 +970,12 @@ type MedialaneErrorCode = "TOKEN_NOT_FOUND" | "COLLECTION_NOT_FOUND" | "ORDER_NO
970
970
 
971
971
  declare class MedialaneApiError extends Error {
972
972
  readonly status: number;
973
+ /** Parsed `Retry-After` (ms) when the server sent one — used by withRetry on 429. */
974
+ readonly retryAfterMs?: number | undefined;
973
975
  readonly code: MedialaneErrorCode;
974
- constructor(status: number, message: string);
976
+ constructor(status: number, message: string,
977
+ /** Parsed `Retry-After` (ms) when the server sent one — used by withRetry on 429. */
978
+ retryAfterMs?: number | undefined);
975
979
  }
976
980
  declare class ApiClient {
977
981
  private readonly baseUrl;
@@ -220,15 +220,16 @@ async function withRetry(fn, opts) {
220
220
  return await fn();
221
221
  } catch (err) {
222
222
  lastError = err;
223
- if (err instanceof MedialaneApiError && err.status < 500) {
223
+ if (err instanceof MedialaneApiError && err.status < 500 && err.status !== 429) {
224
224
  throw err;
225
225
  }
226
- const isRetryable = err instanceof MedialaneApiError && err.status >= 500 || err instanceof TypeError;
226
+ const isRetryable = err instanceof MedialaneApiError && (err.status >= 500 || err.status === 429) || err instanceof TypeError;
227
227
  if (!isRetryable || attempt === maxAttempts - 1) {
228
228
  throw err;
229
229
  }
230
- const jitter = Math.random() * baseDelayMs;
231
- const delay = Math.min(baseDelayMs * Math.pow(2, attempt) + jitter, maxDelayMs);
230
+ const retryAfterMs = err instanceof MedialaneApiError ? err.retryAfterMs : void 0;
231
+ const backoff = baseDelayMs * Math.pow(2, attempt) + Math.random() * baseDelayMs;
232
+ const delay = Math.min(retryAfterMs ?? backoff, maxDelayMs);
232
233
  await sleep(delay);
233
234
  }
234
235
  }
@@ -245,13 +246,22 @@ function deriveErrorCode(status) {
245
246
  return "UNKNOWN";
246
247
  }
247
248
  var MedialaneApiError = class extends Error {
248
- constructor(status, message) {
249
+ constructor(status, message, retryAfterMs) {
249
250
  super(message);
250
251
  this.status = status;
252
+ this.retryAfterMs = retryAfterMs;
251
253
  this.name = "MedialaneApiError";
252
254
  this.code = deriveErrorCode(status);
253
255
  }
254
256
  };
257
+ function parseRetryAfter(value) {
258
+ if (!value) return void 0;
259
+ const seconds = Number(value);
260
+ if (Number.isFinite(seconds)) return Math.max(0, seconds * 1e3);
261
+ const date = Date.parse(value);
262
+ if (!Number.isNaN(date)) return Math.max(0, date - Date.now());
263
+ return void 0;
264
+ }
255
265
  var ApiClient = class {
256
266
  constructor(baseUrl, apiKey, retryOptions, chain = "STARKNET") {
257
267
  this.baseUrl = baseUrl;
@@ -290,7 +300,7 @@ var ApiClient = class {
290
300
  if (body.error) message = body.error;
291
301
  } catch {
292
302
  }
293
- throw new MedialaneApiError(response.status, message);
303
+ throw new MedialaneApiError(response.status, message, parseRetryAfter(response.headers.get("retry-after")));
294
304
  }
295
305
  return response;
296
306
  }, this.retryOptions);