@profplum700/etsy-v3-api-client 2.4.2 → 2.4.3

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.
@@ -1395,6 +1395,24 @@ class EtsyClient {
1395
1395
  sleep(ms) {
1396
1396
  return new Promise(resolve => setTimeout(resolve, ms));
1397
1397
  }
1398
+ buildFormBody(params) {
1399
+ const body = new URLSearchParams();
1400
+ for (const [key, value] of Object.entries(params)) {
1401
+ if (value === undefined || value === null)
1402
+ continue;
1403
+ if (Array.isArray(value)) {
1404
+ for (const item of value) {
1405
+ if (item === undefined || item === null)
1406
+ continue;
1407
+ body.append(key, String(item));
1408
+ }
1409
+ }
1410
+ else {
1411
+ body.append(key, String(value));
1412
+ }
1413
+ }
1414
+ return body;
1415
+ }
1398
1416
  getApiKey() {
1399
1417
  if (this.sharedSecret) {
1400
1418
  return `${this.keystring}:${this.sharedSecret}`;
@@ -1454,19 +1472,37 @@ class EtsyClient {
1454
1472
  searchParams.set('sort_order', params.sort_order);
1455
1473
  if (params.includes)
1456
1474
  searchParams.set('includes', params.includes.join(','));
1475
+ if (params.legacy !== undefined) {
1476
+ searchParams.set('legacy', params.legacy.toString());
1477
+ }
1457
1478
  const response = await this.makeRequest(`/shops/${targetShopId}/listings?${searchParams.toString()}`);
1458
1479
  return response.results;
1459
1480
  }
1460
- async getListing(listingId, includes) {
1461
- const params = includes ? `?includes=${includes.join(',')}` : '';
1462
- return this.makeRequest(`/listings/${listingId}${params}`);
1481
+ async getListing(listingId, params) {
1482
+ const resolvedParams = Array.isArray(params)
1483
+ ? { includes: params }
1484
+ : params;
1485
+ const searchParams = new URLSearchParams();
1486
+ if (resolvedParams?.includes) {
1487
+ searchParams.set('includes', resolvedParams.includes.join(','));
1488
+ }
1489
+ if (resolvedParams?.language) {
1490
+ searchParams.set('language', resolvedParams.language);
1491
+ }
1492
+ if (resolvedParams?.legacy !== undefined) {
1493
+ searchParams.set('legacy', resolvedParams.legacy.toString());
1494
+ }
1495
+ if (resolvedParams?.allow_suggested_title !== undefined) {
1496
+ searchParams.set('allow_suggested_title', resolvedParams.allow_suggested_title.toString());
1497
+ }
1498
+ const query = searchParams.toString();
1499
+ const suffix = query ? `?${query}` : '';
1500
+ return this.makeRequest(`/listings/${listingId}${suffix}`);
1463
1501
  }
1464
1502
  async findAllListingsActive(params = {}) {
1465
1503
  const searchParams = new URLSearchParams();
1466
1504
  if (params.keywords)
1467
1505
  searchParams.set('keywords', params.keywords);
1468
- if (params.category)
1469
- searchParams.set('category', params.category);
1470
1506
  if (params.limit !== undefined)
1471
1507
  searchParams.set('limit', params.limit.toString());
1472
1508
  if (params.offset !== undefined)
@@ -1479,12 +1515,14 @@ class EtsyClient {
1479
1515
  searchParams.set('min_price', params.min_price.toString());
1480
1516
  if (params.max_price !== undefined)
1481
1517
  searchParams.set('max_price', params.max_price.toString());
1482
- if (params.tags)
1483
- searchParams.set('tags', params.tags.join(','));
1484
- if (params.location)
1485
- searchParams.set('location', params.location);
1518
+ if (params.taxonomy_id !== undefined) {
1519
+ searchParams.set('taxonomy_id', params.taxonomy_id.toString());
1520
+ }
1486
1521
  if (params.shop_location)
1487
1522
  searchParams.set('shop_location', params.shop_location);
1523
+ if (params.legacy !== undefined) {
1524
+ searchParams.set('legacy', params.legacy.toString());
1525
+ }
1488
1526
  const response = await this.makeRequest(`/listings/active?${searchParams.toString()}`);
1489
1527
  return response.results;
1490
1528
  }
@@ -1492,8 +1530,20 @@ class EtsyClient {
1492
1530
  const response = await this.makeRequest(`/listings/${listingId}/images`);
1493
1531
  return response.results;
1494
1532
  }
1495
- async getListingInventory(listingId) {
1496
- return this.makeRequest(`/listings/${listingId}/inventory`);
1533
+ async getListingInventory(listingId, params = {}) {
1534
+ const searchParams = new URLSearchParams();
1535
+ if (params.show_deleted !== undefined) {
1536
+ searchParams.set('show_deleted', params.show_deleted.toString());
1537
+ }
1538
+ if (params.includes) {
1539
+ searchParams.set('includes', params.includes);
1540
+ }
1541
+ if (params.legacy !== undefined) {
1542
+ searchParams.set('legacy', params.legacy.toString());
1543
+ }
1544
+ const query = searchParams.toString();
1545
+ const suffix = query ? `?${query}` : '';
1546
+ return this.makeRequest(`/listings/${listingId}/inventory${suffix}`);
1497
1547
  }
1498
1548
  async getReviewsByListing(listingId, params = {}) {
1499
1549
  const searchParams = new URLSearchParams();
@@ -1530,7 +1580,8 @@ class EtsyClient {
1530
1580
  return response.results;
1531
1581
  }
1532
1582
  async getUserShops() {
1533
- const response = await this.makeRequest('/users/me/shops');
1583
+ const user = await this.getUser();
1584
+ const response = await this.makeRequest(`/users/${user.user_id}/shops`);
1534
1585
  return response.results || [];
1535
1586
  }
1536
1587
  async updateShop(shopId, params, options) {
@@ -1547,21 +1598,27 @@ class EtsyClient {
1547
1598
  }
1548
1599
  }
1549
1600
  }
1601
+ const body = this.buildFormBody(params);
1550
1602
  return this.makeRequest(`/shops/${shopId}`, {
1551
1603
  method: 'PUT',
1552
- body: JSON.stringify(params)
1604
+ headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
1605
+ body: body.toString()
1553
1606
  }, false);
1554
1607
  }
1555
1608
  async createShopSection(shopId, params) {
1609
+ const body = this.buildFormBody(params);
1556
1610
  return this.makeRequest(`/shops/${shopId}/sections`, {
1557
1611
  method: 'POST',
1558
- body: JSON.stringify(params)
1612
+ headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
1613
+ body: body.toString()
1559
1614
  }, false);
1560
1615
  }
1561
1616
  async updateShopSection(shopId, sectionId, params) {
1617
+ const body = this.buildFormBody(params);
1562
1618
  return this.makeRequest(`/shops/${shopId}/sections/${sectionId}`, {
1563
1619
  method: 'PUT',
1564
- body: JSON.stringify(params)
1620
+ headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
1621
+ body: body.toString()
1565
1622
  }, false);
1566
1623
  }
1567
1624
  async deleteShopSection(shopId, sectionId) {
@@ -1581,9 +1638,13 @@ class EtsyClient {
1581
1638
  }
1582
1639
  }
1583
1640
  }
1584
- return this.makeRequest(`/shops/${shopId}/listings`, {
1641
+ const legacy = options?.legacy;
1642
+ const query = legacy !== undefined ? `?legacy=${legacy}` : '';
1643
+ const body = this.buildFormBody(params);
1644
+ return this.makeRequest(`/shops/${shopId}/listings${query}`, {
1585
1645
  method: 'POST',
1586
- body: JSON.stringify(params)
1646
+ headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
1647
+ body: body.toString()
1587
1648
  }, false);
1588
1649
  }
1589
1650
  async updateListing(shopId, listingId, params, options) {
@@ -1600,16 +1661,22 @@ class EtsyClient {
1600
1661
  }
1601
1662
  }
1602
1663
  }
1603
- return this.makeRequest(`/shops/${shopId}/listings/${listingId}`, {
1664
+ const legacy = options?.legacy;
1665
+ const query = legacy !== undefined ? `?legacy=${legacy}` : '';
1666
+ const body = this.buildFormBody(params);
1667
+ return this.makeRequest(`/shops/${shopId}/listings/${listingId}${query}`, {
1604
1668
  method: 'PATCH',
1605
- body: JSON.stringify(params)
1669
+ headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
1670
+ body: body.toString()
1606
1671
  }, false);
1607
1672
  }
1608
1673
  async deleteListing(listingId) {
1609
1674
  await this.makeRequest(`/listings/${listingId}`, { method: 'DELETE' }, false);
1610
1675
  }
1611
- async updateListingInventory(listingId, params) {
1612
- return this.makeRequest(`/listings/${listingId}/inventory`, {
1676
+ async updateListingInventory(listingId, params, options) {
1677
+ const legacy = options?.legacy;
1678
+ const query = legacy !== undefined ? `?legacy=${legacy}` : '';
1679
+ return this.makeRequest(`/listings/${listingId}/inventory${query}`, {
1613
1680
  method: 'PUT',
1614
1681
  body: JSON.stringify(params)
1615
1682
  }, false);
@@ -1642,8 +1709,8 @@ class EtsyClient {
1642
1709
  }
1643
1710
  return response.json();
1644
1711
  }
1645
- async getListingImage(shopId, listingId, imageId) {
1646
- return this.makeRequest(`/shops/${shopId}/listings/${listingId}/images/${imageId}`);
1712
+ async getListingImage(listingId, imageId) {
1713
+ return this.makeRequest(`/listings/${listingId}/images/${imageId}`);
1647
1714
  }
1648
1715
  async deleteListingImage(shopId, listingId, imageId) {
1649
1716
  await this.makeRequest(`/shops/${shopId}/listings/${listingId}/images/${imageId}`, { method: 'DELETE' }, false);
@@ -1690,20 +1757,40 @@ class EtsyClient {
1690
1757
  searchParams.set('was_shipped', params.was_shipped.toString());
1691
1758
  if (params?.was_delivered !== undefined)
1692
1759
  searchParams.set('was_delivered', params.was_delivered.toString());
1760
+ if (params?.was_canceled !== undefined)
1761
+ searchParams.set('was_canceled', params.was_canceled.toString());
1762
+ if (params?.legacy !== undefined)
1763
+ searchParams.set('legacy', params.legacy.toString());
1693
1764
  const response = await this.makeRequest(`/shops/${shopId}/receipts?${searchParams.toString()}`);
1694
1765
  return response.results;
1695
1766
  }
1696
- async getShopReceipt(shopId, receiptId) {
1697
- return this.makeRequest(`/shops/${shopId}/receipts/${receiptId}`);
1698
- }
1699
- async updateShopReceipt(shopId, receiptId, params) {
1700
- return this.makeRequest(`/shops/${shopId}/receipts/${receiptId}`, {
1767
+ async getShopReceipt(shopId, receiptId, params = {}) {
1768
+ const searchParams = new URLSearchParams();
1769
+ if (params.legacy !== undefined) {
1770
+ searchParams.set('legacy', params.legacy.toString());
1771
+ }
1772
+ const query = searchParams.toString();
1773
+ const suffix = query ? `?${query}` : '';
1774
+ return this.makeRequest(`/shops/${shopId}/receipts/${receiptId}${suffix}`);
1775
+ }
1776
+ async updateShopReceipt(shopId, receiptId, params, options) {
1777
+ const legacy = options?.legacy;
1778
+ const query = legacy !== undefined ? `?legacy=${legacy}` : '';
1779
+ const body = this.buildFormBody(params);
1780
+ return this.makeRequest(`/shops/${shopId}/receipts/${receiptId}${query}`, {
1701
1781
  method: 'PUT',
1702
- body: JSON.stringify(params)
1782
+ headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
1783
+ body: body.toString()
1703
1784
  }, false);
1704
1785
  }
1705
- async getShopReceiptTransactions(shopId, receiptId) {
1706
- const response = await this.makeRequest(`/shops/${shopId}/receipts/${receiptId}/transactions`);
1786
+ async getShopReceiptTransactions(shopId, receiptId, params = {}) {
1787
+ const searchParams = new URLSearchParams();
1788
+ if (params.legacy !== undefined) {
1789
+ searchParams.set('legacy', params.legacy.toString());
1790
+ }
1791
+ const query = searchParams.toString();
1792
+ const suffix = query ? `?${query}` : '';
1793
+ const response = await this.makeRequest(`/shops/${shopId}/receipts/${receiptId}/transactions${suffix}`);
1707
1794
  return response.results;
1708
1795
  }
1709
1796
  async getShopTransaction(shopId, transactionId) {
@@ -1714,37 +1801,54 @@ class EtsyClient {
1714
1801
  return response.results;
1715
1802
  }
1716
1803
  async createShopShippingProfile(shopId, params) {
1804
+ const body = this.buildFormBody(params);
1717
1805
  return this.makeRequest(`/shops/${shopId}/shipping-profiles`, {
1718
1806
  method: 'POST',
1719
- body: JSON.stringify(params)
1807
+ headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
1808
+ body: body.toString()
1720
1809
  }, false);
1721
1810
  }
1722
1811
  async getShopShippingProfile(shopId, profileId) {
1723
1812
  return this.makeRequest(`/shops/${shopId}/shipping-profiles/${profileId}`);
1724
1813
  }
1725
1814
  async updateShopShippingProfile(shopId, profileId, params) {
1815
+ const body = this.buildFormBody(params);
1726
1816
  return this.makeRequest(`/shops/${shopId}/shipping-profiles/${profileId}`, {
1727
1817
  method: 'PUT',
1728
- body: JSON.stringify(params)
1818
+ headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
1819
+ body: body.toString()
1729
1820
  }, false);
1730
1821
  }
1731
1822
  async deleteShopShippingProfile(shopId, profileId) {
1732
1823
  await this.makeRequest(`/shops/${shopId}/shipping-profiles/${profileId}`, { method: 'DELETE' }, false);
1733
1824
  }
1734
- async getShopShippingProfileDestinations(shopId, profileId) {
1735
- const response = await this.makeRequest(`/shops/${shopId}/shipping-profiles/${profileId}/destinations`);
1825
+ async getShopShippingProfileDestinations(shopId, profileId, params = {}) {
1826
+ const searchParams = new URLSearchParams();
1827
+ if (params.limit !== undefined) {
1828
+ searchParams.set('limit', params.limit.toString());
1829
+ }
1830
+ if (params.offset !== undefined) {
1831
+ searchParams.set('offset', params.offset.toString());
1832
+ }
1833
+ const query = searchParams.toString();
1834
+ const suffix = query ? `?${query}` : '';
1835
+ const response = await this.makeRequest(`/shops/${shopId}/shipping-profiles/${profileId}/destinations${suffix}`);
1736
1836
  return response.results;
1737
1837
  }
1738
1838
  async createShopShippingProfileDestination(shopId, profileId, params) {
1839
+ const body = this.buildFormBody(params);
1739
1840
  return this.makeRequest(`/shops/${shopId}/shipping-profiles/${profileId}/destinations`, {
1740
1841
  method: 'POST',
1741
- body: JSON.stringify(params)
1842
+ headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
1843
+ body: body.toString()
1742
1844
  }, false);
1743
1845
  }
1744
1846
  async updateShopShippingProfileDestination(shopId, profileId, destinationId, params) {
1847
+ const body = this.buildFormBody(params);
1745
1848
  return this.makeRequest(`/shops/${shopId}/shipping-profiles/${profileId}/destinations/${destinationId}`, {
1746
1849
  method: 'PUT',
1747
- body: JSON.stringify(params)
1850
+ headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
1851
+ body: body.toString()
1748
1852
  }, false);
1749
1853
  }
1750
1854
  async deleteShopShippingProfileDestination(shopId, profileId, destinationId) {
@@ -1754,16 +1858,16 @@ class EtsyClient {
1754
1858
  const response = await this.makeRequest(`/shops/${shopId}/shipping-profiles/${profileId}/upgrades`);
1755
1859
  return response.results;
1756
1860
  }
1757
- async createReceiptShipment(shopId, receiptId, params) {
1758
- return this.makeRequest(`/shops/${shopId}/receipts/${receiptId}/tracking`, {
1861
+ async createReceiptShipment(shopId, receiptId, params, options) {
1862
+ const legacy = options?.legacy;
1863
+ const query = legacy !== undefined ? `?legacy=${legacy}` : '';
1864
+ const body = this.buildFormBody(params);
1865
+ return this.makeRequest(`/shops/${shopId}/receipts/${receiptId}/tracking${query}`, {
1759
1866
  method: 'POST',
1760
- body: JSON.stringify(params)
1867
+ headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
1868
+ body: body.toString()
1761
1869
  }, false);
1762
1870
  }
1763
- async getShopReceiptShipments(shopId, receiptId) {
1764
- const response = await this.makeRequest(`/shops/${shopId}/receipts/${receiptId}/shipments`);
1765
- return response.results;
1766
- }
1767
1871
  async getShopPaymentAccountLedgerEntries(shopId, params) {
1768
1872
  const searchParams = new URLSearchParams();
1769
1873
  searchParams.set('min_created', params.min_created.toString());
@@ -1778,8 +1882,18 @@ class EtsyClient {
1778
1882
  async getShopPaymentAccountLedgerEntry(shopId, entryId) {
1779
1883
  return this.makeRequest(`/shops/${shopId}/payment-account/ledger-entries/${entryId}`);
1780
1884
  }
1885
+ async getPayments(shopId, paymentIds) {
1886
+ const searchParams = new URLSearchParams();
1887
+ searchParams.set('payment_ids', paymentIds.join(','));
1888
+ const response = await this.makeRequest(`/shops/${shopId}/payments?${searchParams.toString()}`);
1889
+ return response.results;
1890
+ }
1781
1891
  async getShopPayment(shopId, paymentId) {
1782
- return this.makeRequest(`/shops/${shopId}/payment-account/payments/${paymentId}`);
1892
+ const payments = await this.getPayments(shopId, [Number(paymentId)]);
1893
+ if (payments.length === 0) {
1894
+ throw new EtsyApiError('Payment not found', 404);
1895
+ }
1896
+ return payments[0];
1783
1897
  }
1784
1898
  async getBuyerTaxonomyNodes() {
1785
1899
  const response = await this.makeRequest('/buyer-taxonomy/nodes');
@@ -1796,8 +1910,8 @@ class EtsyClient {
1796
1910
  async updateListingProperty(params) {
1797
1911
  const { shopId, listingId, propertyId, valueIds, values, scaleId } = params;
1798
1912
  const body = new URLSearchParams();
1799
- valueIds.forEach(id => body.append('value_ids[]', id.toString()));
1800
- values.forEach(val => body.append('values[]', val));
1913
+ valueIds.forEach(id => body.append('value_ids', id.toString()));
1914
+ values.forEach(val => body.append('values', val));
1801
1915
  if (scaleId !== undefined) {
1802
1916
  body.append('scale_id', scaleId.toString());
1803
1917
  }