@plasmicpkgs/commerce-swell 0.0.2 → 0.0.6

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/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Plasmic
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -4,13 +4,13 @@ Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
6
6
 
7
- var registerComponent = _interopDefault(require('@plasmicapp/host/registerComponent'));
8
7
  var React = require('react');
9
8
  var React__default = _interopDefault(React);
10
9
  var commerce = require('@plasmicpkgs/commerce');
11
10
  var swell = _interopDefault(require('swell-js'));
12
11
  var Cookies = _interopDefault(require('js-cookie'));
13
- var debounce = _interopDefault(require('lodash.debounce'));
12
+ var debounce = _interopDefault(require('lodash/debounce'));
13
+ var registerGlobalContext = _interopDefault(require('@plasmicapp/host/registerGlobalContext'));
14
14
 
15
15
  /*
16
16
  Forked from https://github.com/vercel/commerce/tree/main/packages/swell/src
@@ -181,9 +181,9 @@ var runtime = (function (exports) {
181
181
  // This is a polyfill for %IteratorPrototype% for environments that
182
182
  // don't natively support it.
183
183
  var IteratorPrototype = {};
184
- IteratorPrototype[iteratorSymbol] = function () {
184
+ define(IteratorPrototype, iteratorSymbol, function () {
185
185
  return this;
186
- };
186
+ });
187
187
 
188
188
  var getProto = Object.getPrototypeOf;
189
189
  var NativeIteratorPrototype = getProto && getProto(getProto(values([])));
@@ -197,8 +197,9 @@ var runtime = (function (exports) {
197
197
 
198
198
  var Gp = GeneratorFunctionPrototype.prototype =
199
199
  Generator.prototype = Object.create(IteratorPrototype);
200
- GeneratorFunction.prototype = Gp.constructor = GeneratorFunctionPrototype;
201
- GeneratorFunctionPrototype.constructor = GeneratorFunction;
200
+ GeneratorFunction.prototype = GeneratorFunctionPrototype;
201
+ define(Gp, "constructor", GeneratorFunctionPrototype);
202
+ define(GeneratorFunctionPrototype, "constructor", GeneratorFunction);
202
203
  GeneratorFunction.displayName = define(
203
204
  GeneratorFunctionPrototype,
204
205
  toStringTagSymbol,
@@ -312,9 +313,9 @@ var runtime = (function (exports) {
312
313
  }
313
314
 
314
315
  defineIteratorMethods(AsyncIterator.prototype);
315
- AsyncIterator.prototype[asyncIteratorSymbol] = function () {
316
+ define(AsyncIterator.prototype, asyncIteratorSymbol, function () {
316
317
  return this;
317
- };
318
+ });
318
319
  exports.AsyncIterator = AsyncIterator;
319
320
 
320
321
  // Note that simple async functions are implemented on top of
@@ -507,13 +508,13 @@ var runtime = (function (exports) {
507
508
  // iterator prototype chain incorrectly implement this, causing the Generator
508
509
  // object to not be returned from this call. This ensures that doesn't happen.
509
510
  // See https://github.com/facebook/regenerator/issues/274 for more details.
510
- Gp[iteratorSymbol] = function() {
511
+ define(Gp, iteratorSymbol, function() {
511
512
  return this;
512
- };
513
+ });
513
514
 
514
- Gp.toString = function() {
515
+ define(Gp, "toString", function() {
515
516
  return "[object Generator]";
516
- };
517
+ });
517
518
 
518
519
  function pushTryEntry(locs) {
519
520
  var entry = { tryLoc: locs[0] };
@@ -832,17 +833,24 @@ try {
832
833
  } catch (accidentalStrictMode) {
833
834
  // This module should not be running in strict mode, so the above
834
835
  // assignment should always work unless something is misconfigured. Just
835
- // in case runtime.js accidentally runs in strict mode, we can escape
836
+ // in case runtime.js accidentally runs in strict mode, in modern engines
837
+ // we can explicitly access globalThis. In older engines we can escape
836
838
  // strict mode using a global Function call. This could conceivably fail
837
839
  // if a Content Security Policy forbids using Function, but in that case
838
840
  // the proper solution is to fix the accidental strict mode problem. If
839
841
  // you've misconfigured your bundler to force strict mode and applied a
840
842
  // CSP to forbid Function, and you're not willing to fix either of those
841
843
  // problems, please detail your unique predicament in a GitHub issue.
842
- Function("r", "regeneratorRuntime = r")(runtime);
844
+ if (typeof globalThis === "object") {
845
+ globalThis.regeneratorRuntime = runtime;
846
+ } else {
847
+ Function("r", "regeneratorRuntime = r")(runtime);
848
+ }
843
849
  }
844
850
  });
845
851
 
852
+ var _excluded = ["file"];
853
+
846
854
  var normalizeProductOption = function normalizeProductOption(_ref2) {
847
855
  var id = _ref2.id,
848
856
  _ref2$name = _ref2.name,
@@ -879,7 +887,7 @@ var normalizeProductImages = function normalizeProductImages(images) {
879
887
 
880
888
  return images == null ? void 0 : images.map(function (_ref3) {
881
889
  var file = _ref3.file,
882
- rest = _objectWithoutPropertiesLoose(_ref3, ["file"]);
890
+ rest = _objectWithoutPropertiesLoose(_ref3, _excluded);
883
891
 
884
892
  return _extends({
885
893
  url: (file == null ? void 0 : file.url) + '',
@@ -1530,7 +1538,7 @@ var handler$4 = {
1530
1538
  },
1531
1539
  fetcher: function fetcher(_ref) {
1532
1540
  return _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee() {
1533
- var input, fetch, sortMap, categoryId, search, _input$sort, sort, count, mappedSort, _yield$fetch, results, found, products;
1541
+ var input, fetch, sortMap, categoryId, brandId, search, _input$sort, sort, count, mappedSort, _yield$fetch, results, found, products;
1534
1542
 
1535
1543
  return runtime_1.wrap(function _callee$(_context) {
1536
1544
  while (1) {
@@ -1538,7 +1546,7 @@ var handler$4 = {
1538
1546
  case 0:
1539
1547
  input = _ref.input, fetch = _ref.fetch;
1540
1548
  sortMap = new Map([['latest-desc', ''], ['price-asc', 'price_asc'], ['price-desc', 'price_desc'], ['trending-desc', 'popularity']]);
1541
- categoryId = input.categoryId, search = input.search, _input$sort = input.sort, sort = _input$sort === void 0 ? 'latest-desc' : _input$sort, count = input.count;
1549
+ categoryId = input.categoryId, brandId = input.brandId, search = input.search, _input$sort = input.sort, sort = _input$sort === void 0 ? 'latest-desc' : _input$sort, count = input.count;
1542
1550
  mappedSort = sortMap.get(sort);
1543
1551
  _context.next = 6;
1544
1552
  return fetch({
@@ -1546,6 +1554,7 @@ var handler$4 = {
1546
1554
  method: 'list',
1547
1555
  variables: {
1548
1556
  category: categoryId,
1557
+ brand: brandId,
1549
1558
  search: search,
1550
1559
  sort: mappedSort,
1551
1560
  expand: ["variants"],
@@ -1590,6 +1599,168 @@ var handler$4 = {
1590
1599
  }
1591
1600
  };
1592
1601
 
1602
+ var handler$5 = {
1603
+ fetchOptions: {
1604
+ query: 'categories',
1605
+ method: 'get'
1606
+ },
1607
+ fetcher: function fetcher(_ref) {
1608
+ return _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee() {
1609
+ var _data$results$map;
1610
+
1611
+ var fetch, data;
1612
+ return runtime_1.wrap(function _callee$(_context) {
1613
+ while (1) {
1614
+ switch (_context.prev = _context.next) {
1615
+ case 0:
1616
+ fetch = _ref.fetch;
1617
+ _context.next = 3;
1618
+ return fetch({
1619
+ query: 'categories',
1620
+ method: 'get'
1621
+ });
1622
+
1623
+ case 3:
1624
+ data = _context.sent;
1625
+ return _context.abrupt("return", (_data$results$map = data.results.map(function (_ref2) {
1626
+ var id = _ref2.id,
1627
+ name = _ref2.name,
1628
+ slug = _ref2.slug;
1629
+ return {
1630
+ id: id,
1631
+ name: name,
1632
+ slug: slug,
1633
+ path: "/" + slug
1634
+ };
1635
+ })) != null ? _data$results$map : []);
1636
+
1637
+ case 5:
1638
+ case "end":
1639
+ return _context.stop();
1640
+ }
1641
+ }
1642
+ }, _callee);
1643
+ }))();
1644
+ },
1645
+ useHook: function useHook(_ref3) {
1646
+ var useData = _ref3.useData;
1647
+ return function (input) {
1648
+ var response = useData({
1649
+ swrOptions: _extends({
1650
+ revalidateOnFocus: false
1651
+ }, input == null ? void 0 : input.swrOptions)
1652
+ });
1653
+ return React.useMemo(function () {
1654
+ return Object.create(response, {
1655
+ isEmpty: {
1656
+ get: function get() {
1657
+ var _response$data$length, _response$data;
1658
+
1659
+ return ((_response$data$length = (_response$data = response.data) == null ? void 0 : _response$data.length) != null ? _response$data$length : 0) <= 0;
1660
+ },
1661
+ enumerable: true
1662
+ }
1663
+ });
1664
+ }, [response]);
1665
+ };
1666
+ }
1667
+ };
1668
+
1669
+ var handler$6 = {
1670
+ fetchOptions: {
1671
+ query: 'attributes',
1672
+ method: 'get'
1673
+ },
1674
+ fetcher: function fetcher(_ref) {
1675
+ return _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee() {
1676
+ var _yield$fetch$values, _yield$fetch;
1677
+
1678
+ var fetch, vendors;
1679
+ return runtime_1.wrap(function _callee$(_context) {
1680
+ while (1) {
1681
+ switch (_context.prev = _context.next) {
1682
+ case 0:
1683
+ fetch = _ref.fetch;
1684
+ _context.next = 3;
1685
+ return fetch({
1686
+ query: 'attributes',
1687
+ method: 'get',
1688
+ variables: 'brand'
1689
+ });
1690
+
1691
+ case 3:
1692
+ _context.t0 = _yield$fetch = _context.sent;
1693
+
1694
+ if (!(_context.t0 == null)) {
1695
+ _context.next = 8;
1696
+ break;
1697
+ }
1698
+
1699
+ _context.t1 = void 0;
1700
+ _context.next = 9;
1701
+ break;
1702
+
1703
+ case 8:
1704
+ _context.t1 = _yield$fetch.values;
1705
+
1706
+ case 9:
1707
+ _context.t2 = _yield$fetch$values = _context.t1;
1708
+
1709
+ if (!(_context.t2 != null)) {
1710
+ _context.next = 14;
1711
+ break;
1712
+ }
1713
+
1714
+ _context.t3 = _yield$fetch$values;
1715
+ _context.next = 15;
1716
+ break;
1717
+
1718
+ case 14:
1719
+ _context.t3 = [];
1720
+
1721
+ case 15:
1722
+ vendors = _context.t3;
1723
+ console.log("dale", "useBrands", vendors);
1724
+ return _context.abrupt("return", Array.from(new Set(vendors).values()).map(function (v) {
1725
+ return {
1726
+ entityId: v,
1727
+ name: v,
1728
+ path: "brands/" + v
1729
+ };
1730
+ }));
1731
+
1732
+ case 18:
1733
+ case "end":
1734
+ return _context.stop();
1735
+ }
1736
+ }
1737
+ }, _callee);
1738
+ }))();
1739
+ },
1740
+ useHook: function useHook(_ref2) {
1741
+ var useData = _ref2.useData;
1742
+ return function (input) {
1743
+ var response = useData({
1744
+ swrOptions: _extends({
1745
+ revalidateOnFocus: false
1746
+ }, input == null ? void 0 : input.swrOptions)
1747
+ });
1748
+ return React.useMemo(function () {
1749
+ return Object.create(response, {
1750
+ isEmpty: {
1751
+ get: function get() {
1752
+ var _response$data$length, _response$data;
1753
+
1754
+ return ((_response$data$length = (_response$data = response.data) == null ? void 0 : _response$data.length) != null ? _response$data$length : 0) <= 0;
1755
+ },
1756
+ enumerable: true
1757
+ }
1758
+ });
1759
+ }, [response]);
1760
+ };
1761
+ }
1762
+ };
1763
+
1593
1764
  var fetcher = /*#__PURE__*/function () {
1594
1765
  var _ref2 = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee2(_ref) {
1595
1766
  var _ref$method, method, variables, query, callSwell, _callSwell;
@@ -1692,6 +1863,10 @@ var getSwellProvider = function getSwellProvider(storeId, publicKey) {
1692
1863
  },
1693
1864
  products: {
1694
1865
  useSearch: handler$4
1866
+ },
1867
+ site: {
1868
+ useCategories: handler$5,
1869
+ useBrands: handler$6
1695
1870
  }
1696
1871
  };
1697
1872
  };
@@ -1711,9 +1886,6 @@ var commerceProviderMeta = {
1711
1886
  name: "plasmic-commerce-swell-provider",
1712
1887
  displayName: "Swell Provider",
1713
1888
  props: {
1714
- children: {
1715
- type: "slot"
1716
- },
1717
1889
  storeId: "string",
1718
1890
  publicKey: "string"
1719
1891
  },
@@ -1725,20 +1897,13 @@ function CommerceProviderComponent(props) {
1725
1897
  var storeId = props.storeId,
1726
1898
  publicKey = props.publicKey,
1727
1899
  children = props.children;
1728
-
1729
- if (!storeId) {
1730
- return React__default.createElement("p", null, " You must set the store id ");
1731
- } else if (!publicKey) {
1732
- return React__default.createElement("p", null, " You must set the public key ");
1733
- }
1734
-
1735
1900
  var CommerceProvider = getCommerceProvider(storeId, publicKey);
1736
1901
  return React__default.createElement(CommerceProvider, null, children);
1737
1902
  }
1738
1903
 
1739
1904
  function registerCommerceProvider(loader, customCommerceProviderMeta) {
1740
1905
  var doRegisterComponent = function doRegisterComponent() {
1741
- return loader ? loader.registerComponent.apply(loader, arguments) : registerComponent.apply(void 0, arguments);
1906
+ return loader ? loader.registerGlobalContext.apply(loader, arguments) : registerGlobalContext.apply(void 0, arguments);
1742
1907
  };
1743
1908
 
1744
1909
  doRegisterComponent(CommerceProviderComponent, customCommerceProviderMeta != null ? customCommerceProviderMeta : commerceProviderMeta);