@profplum700/etsy-v3-api-client 2.5.3 → 2.5.4

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
@@ -677,6 +677,11 @@ class FileTokenStorage {
677
677
  }
678
678
  }
679
679
 
680
+ const ETSY_RATE_LIMITS = {
681
+ MAX_REQUESTS_PER_DAY: 5000,
682
+ MAX_REQUESTS_PER_SECOND: 5,
683
+ MIN_REQUEST_INTERVAL: 200,
684
+ };
680
685
  class EtsyRateLimiter {
681
686
  constructor(config) {
682
687
  this.requestCount = 0;
@@ -685,9 +690,9 @@ class EtsyRateLimiter {
685
690
  this.isHeaderBasedLimiting = false;
686
691
  this.currentRetryCount = 0;
687
692
  this.config = {
688
- maxRequestsPerDay: 10000,
689
- maxRequestsPerSecond: 10,
690
- minRequestInterval: 100,
693
+ maxRequestsPerDay: ETSY_RATE_LIMITS.MAX_REQUESTS_PER_DAY,
694
+ maxRequestsPerSecond: ETSY_RATE_LIMITS.MAX_REQUESTS_PER_SECOND,
695
+ minRequestInterval: ETSY_RATE_LIMITS.MIN_REQUEST_INTERVAL,
691
696
  maxRetries: 3,
692
697
  baseDelayMs: 1000,
693
698
  maxDelayMs: 30000,
@@ -742,7 +747,7 @@ class EtsyRateLimiter {
742
747
  if (value === null)
743
748
  return undefined;
744
749
  const num = parseInt(value, 10);
745
- return isNaN(num) ? undefined : num;
750
+ return Number.isNaN(num) ? undefined : num;
746
751
  };
747
752
  return {
748
753
  limitPerSecond: parseNumber(getHeader('x-limit-per-second')),
@@ -1083,7 +1088,7 @@ class FieldValidator {
1083
1088
  const value = data[this.field];
1084
1089
  if (value === undefined || value === null)
1085
1090
  return null;
1086
- if (typeof value !== 'number' || isNaN(value)) {
1091
+ if (typeof value !== 'number' || Number.isNaN(value)) {
1087
1092
  return {
1088
1093
  field: this.field,
1089
1094
  message: options.message || `${this.field} must be a number`,
@@ -1292,9 +1297,14 @@ function combineValidators(...validators) {
1292
1297
 
1293
1298
  class DefaultLogger {
1294
1299
  debug(message, ...args) {
1295
- const isDevelopment = isNode$1
1296
- ? process.env.NODE_ENV === 'development'
1297
- : window.location?.hostname === 'localhost' || window.location?.hostname === '127.0.0.1';
1300
+ let isDevelopment;
1301
+ if (isNode$1) {
1302
+ isDevelopment = process.env.NODE_ENV === 'development';
1303
+ }
1304
+ else {
1305
+ const host = window.location?.hostname;
1306
+ isDevelopment = host === 'localhost' || host === '127.0.0.1';
1307
+ }
1298
1308
  if (isDevelopment) {
1299
1309
  console.log(`[DEBUG] ${message}`, ...args);
1300
1310
  }
@@ -1343,9 +1353,9 @@ class EtsyClient {
1343
1353
  this.sharedSecret = config.sharedSecret;
1344
1354
  if (config.rateLimiting?.enabled !== false) {
1345
1355
  this.rateLimiter = new EtsyRateLimiter({
1346
- maxRequestsPerDay: config.rateLimiting?.maxRequestsPerDay || 10000,
1347
- maxRequestsPerSecond: config.rateLimiting?.maxRequestsPerSecond || 10,
1348
- minRequestInterval: config.rateLimiting?.minRequestInterval ?? 100,
1356
+ maxRequestsPerDay: config.rateLimiting?.maxRequestsPerDay || ETSY_RATE_LIMITS.MAX_REQUESTS_PER_DAY,
1357
+ maxRequestsPerSecond: config.rateLimiting?.maxRequestsPerSecond || ETSY_RATE_LIMITS.MAX_REQUESTS_PER_SECOND,
1358
+ minRequestInterval: config.rateLimiting?.minRequestInterval ?? ETSY_RATE_LIMITS.MIN_REQUEST_INTERVAL,
1349
1359
  maxRetries: config.rateLimiting?.maxRetries,
1350
1360
  baseDelayMs: config.rateLimiting?.baseDelayMs,
1351
1361
  maxDelayMs: config.rateLimiting?.maxDelayMs,
@@ -1376,7 +1386,12 @@ class EtsyClient {
1376
1386
  if (useCache && this.cache && requestOptions.method === 'GET') {
1377
1387
  const cached = await this.cache.get(cacheKey);
1378
1388
  if (cached) {
1379
- return JSON.parse(cached);
1389
+ try {
1390
+ return JSON.parse(cached);
1391
+ }
1392
+ catch {
1393
+ await this.cache.delete(cacheKey);
1394
+ }
1380
1395
  }
1381
1396
  }
1382
1397
  await this.rateLimiter.waitForRateLimit();
@@ -2606,9 +2621,9 @@ class GlobalRequestQueue {
2606
2621
  this.requestCount = 0;
2607
2622
  this.dailyReset = new Date();
2608
2623
  this.lastRequestTime = 0;
2609
- this.maxRequestsPerDay = 10000;
2610
- this.maxRequestsPerSecond = 10;
2611
- this.minRequestInterval = 100;
2624
+ this.maxRequestsPerDay = ETSY_RATE_LIMITS.MAX_REQUESTS_PER_DAY;
2625
+ this.maxRequestsPerSecond = ETSY_RATE_LIMITS.MAX_REQUESTS_PER_SECOND;
2626
+ this.minRequestInterval = ETSY_RATE_LIMITS.MIN_REQUEST_INTERVAL;
2612
2627
  this.setNextDailyReset();
2613
2628
  }
2614
2629
  static getInstance() {
@@ -2693,11 +2708,12 @@ class GlobalRequestQueue {
2693
2708
  try {
2694
2709
  let result;
2695
2710
  if (item.timeout) {
2696
- const remainingTimeout = item.timeout - elapsed;
2711
+ const timeout = item.timeout;
2712
+ const remainingTimeout = timeout - elapsed;
2697
2713
  let timeoutId;
2698
2714
  const timeoutPromise = new Promise((_, reject) => {
2699
2715
  timeoutId = setTimeout(() => {
2700
- reject(new Error(`Request timeout after ${item.timeout}ms (exceeded during execution)`));
2716
+ reject(new Error(`Request timeout after ${timeout}ms (exceeded during execution)`));
2701
2717
  }, remainingTimeout);
2702
2718
  if (timeoutId && typeof timeoutId.unref === 'function') {
2703
2719
  timeoutId.unref();
@@ -3014,7 +3030,18 @@ class EtsyWebhookHandler {
3014
3030
  }
3015
3031
  }
3016
3032
  parseEvent(payload) {
3017
- const data = typeof payload === 'string' ? JSON.parse(payload) : payload;
3033
+ let data;
3034
+ if (typeof payload === 'string') {
3035
+ try {
3036
+ data = JSON.parse(payload);
3037
+ }
3038
+ catch {
3039
+ throw new Error('Invalid webhook payload: malformed JSON');
3040
+ }
3041
+ }
3042
+ else {
3043
+ data = payload;
3044
+ }
3018
3045
  if (!data.type || !data.data) {
3019
3046
  throw new Error('Invalid webhook event format');
3020
3047
  }
@@ -3404,7 +3431,6 @@ function createCacheStorage(config = {}) {
3404
3431
  case 'lfu':
3405
3432
  return new LFUCache(config);
3406
3433
  case 'ttl':
3407
- return new LRUCache(config);
3408
3434
  default:
3409
3435
  return new LRUCache(config);
3410
3436
  }
@@ -4219,7 +4245,7 @@ function createCachingPlugin(config = {}) {
4219
4245
  };
4220
4246
  }
4221
4247
  function createRateLimitPlugin(config = {}) {
4222
- const maxRequestsPerSecond = config.maxRequestsPerSecond || 10;
4248
+ const maxRequestsPerSecond = config.maxRequestsPerSecond || ETSY_RATE_LIMITS.MAX_REQUESTS_PER_SECOND;
4223
4249
  const requests = [];
4224
4250
  return {
4225
4251
  name: 'rateLimit',
@@ -4280,6 +4306,7 @@ exports.COMMON_SCOPE_COMBINATIONS = COMMON_SCOPE_COMBINATIONS;
4280
4306
  exports.CacheWithInvalidation = CacheWithInvalidation;
4281
4307
  exports.CreateListingSchema = CreateListingSchema;
4282
4308
  exports.DEFAULT_RETRY_CONFIG = DEFAULT_RETRY_CONFIG;
4309
+ exports.ETSY_RATE_LIMITS = ETSY_RATE_LIMITS;
4283
4310
  exports.ETSY_SCOPES = ETSY_SCOPES;
4284
4311
  exports.EncryptedFileTokenStorage = EncryptedFileTokenStorage;
4285
4312
  exports.EtsyApiError = EtsyApiError;