@swell/apps-sdk 1.0.142 → 1.0.143

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.mjs CHANGED
@@ -7402,6 +7402,16 @@ function transformSwellVariant(params, product, variant) {
7402
7402
  }
7403
7403
 
7404
7404
  // src/resources/product.ts
7405
+ var SORT_OPTIONS = [
7406
+ { value: "", name: "Featured" },
7407
+ { value: "popularity", name: "Popularity", query: "popularity desc" },
7408
+ { value: "price_asc", name: "Price, low to high", query: "price asc" },
7409
+ { value: "price_desc", name: "Price, high to low", query: "price desc" },
7410
+ { value: "date_asc", name: "Date, old to new", query: "date asc" },
7411
+ { value: "date_desc", name: "Date, new to old", query: "date desc" },
7412
+ { value: "name_asc", name: "Product name, A-Z", query: "name asc" },
7413
+ { value: "name_desc", name: "Product name, Z-A", query: "name desc" }
7414
+ ];
7405
7415
  function transformSwellProduct(params, product) {
7406
7416
  if (!product) {
7407
7417
  return product;
@@ -7438,6 +7448,28 @@ var SwellProduct = class extends SwellStorefrontRecord {
7438
7448
  return res;
7439
7449
  }
7440
7450
  };
7451
+ function productQueryWithFilters(swell, query = {}) {
7452
+ const sortBy = swell.queryParams.sort || "";
7453
+ const filters2 = Object.entries(swell.queryParams).reduce(
7454
+ (acc, [key, value]) => {
7455
+ if (key.startsWith("filter_")) {
7456
+ const qkey = key.replace("filter_", "");
7457
+ if (value?.gte !== void 0 || value?.lte !== void 0) {
7458
+ acc[qkey] = [value.gte || 0, value.lte || void 0];
7459
+ } else {
7460
+ acc[qkey] = value;
7461
+ }
7462
+ }
7463
+ return acc;
7464
+ },
7465
+ {}
7466
+ );
7467
+ return {
7468
+ sort: SORT_OPTIONS.find((option) => option.value === sortBy)?.query || void 0,
7469
+ $filters: filters2,
7470
+ ...query
7471
+ };
7472
+ }
7441
7473
 
7442
7474
  // src/api.ts
7443
7475
  var DEFAULT_API_HOST = "https://api.schema.io";
@@ -14878,9 +14910,11 @@ function ShopifyCollection(instance, category) {
14878
14910
  if (category instanceof StorefrontResource) {
14879
14911
  category = cloneStorefrontResource(category);
14880
14912
  }
14913
+ const productMapper = (product) => ShopifyProduct(instance, product);
14881
14914
  const resolveProducts = makeProductsCollectionResolve(
14915
+ instance,
14882
14916
  category,
14883
- (product) => ShopifyProduct(instance, product)
14917
+ productMapper
14884
14918
  );
14885
14919
  return new ShopifyResource({
14886
14920
  all_products_count: defer(
@@ -14932,8 +14966,9 @@ function ShopifyCollection(instance, category) {
14932
14966
  category,
14933
14967
  (category2) => getFirstImage(instance, category2)
14934
14968
  ),
14935
- filters: defer(
14936
- async () => ((await resolveProducts())?.filter_options ?? []).map(
14969
+ filters: deferWith(
14970
+ category,
14971
+ (category2) => (category2?.filter_options ?? []).map(
14937
14972
  (filter) => ShopifyFilter(instance, filter)
14938
14973
  )
14939
14974
  ),
@@ -14943,9 +14978,7 @@ function ShopifyCollection(instance, category) {
14943
14978
  metafields: {},
14944
14979
  next_product: void 0,
14945
14980
  previous_product: void 0,
14946
- products: defer(async () => {
14947
- return (await resolveProducts())?.results ?? [];
14948
- }),
14981
+ products: getProducts(instance, category, productMapper),
14949
14982
  products_count: defer(
14950
14983
  async () => (await resolveProducts())?.results?.length || 0
14951
14984
  ),
@@ -14984,28 +15017,35 @@ function convertToShopifySorting(value) {
14984
15017
  return "manual";
14985
15018
  }
14986
15019
  }
14987
- function makeProductsCollectionResolve(object, mapper) {
14988
- const productResults = deferWith(object, (object2) => {
14989
- if (object2.products) {
14990
- if (object2.products instanceof SwellStorefrontCollection) {
14991
- return object2.products._cloneWithCompatibilityResult((products) => {
14992
- return {
14993
- ...products,
14994
- results: products.results.map(mapper)
14995
- };
14996
- });
15020
+ function getProducts(instance, object, mapper) {
15021
+ return deferWith(object, (object2) => {
15022
+ const { page, limit: limit2 } = instance.swell.queryParams;
15023
+ const categoryFilter = object2.id && object2.id !== "all" ? object2.id : void 0;
15024
+ const productQuery = categoryFilter ? { category: categoryFilter, $variants: true } : { $variants: true };
15025
+ const filterQuery = productQueryWithFilters(instance.swell, productQuery);
15026
+ const products = new SwellStorefrontCollection(
15027
+ instance.swell,
15028
+ "products",
15029
+ {
15030
+ page,
15031
+ limit: limit2,
15032
+ ...filterQuery
15033
+ },
15034
+ async function() {
15035
+ return this._defaultGetter().call(this);
14997
15036
  }
14998
- if (Array.isArray(object2.products?.results)) {
14999
- return {
15000
- ...object2.products,
15001
- results: object2.products.results.map(mapper)
15002
- };
15037
+ );
15038
+ return products._cloneWithCompatibilityResult(
15039
+ (products2) => {
15040
+ return { ...products2, results: products2.results.map(mapper) };
15003
15041
  }
15004
- }
15005
- return null;
15042
+ );
15006
15043
  });
15044
+ }
15045
+ function makeProductsCollectionResolve(instance, object, mapper) {
15046
+ const products = getProducts(instance, object, mapper);
15007
15047
  async function resolveProducts() {
15008
- const resolved = await productResults.resolve();
15048
+ const resolved = await products.resolve();
15009
15049
  if (resolved && "_resolve" in resolved) {
15010
15050
  return resolved._resolve();
15011
15051
  }
@@ -15769,11 +15809,15 @@ function ShopifySearch(instance, search) {
15769
15809
  if (search instanceof ShopifyResource) {
15770
15810
  return search.clone();
15771
15811
  }
15772
- const resolveProducts = makeProductsCollectionResolve(search, (product) => {
15773
- const shopifyProduct = ShopifyProduct(instance, product);
15774
- shopifyProduct.object_type = "product";
15775
- return shopifyProduct;
15776
- });
15812
+ const resolveProducts = makeProductsCollectionResolve(
15813
+ instance,
15814
+ search,
15815
+ (product) => {
15816
+ const shopifyProduct = ShopifyProduct(instance, product);
15817
+ shopifyProduct.object_type = "product";
15818
+ return shopifyProduct;
15819
+ }
15820
+ );
15777
15821
  return new ShopifyResource({
15778
15822
  default_sort_by: deferWith(
15779
15823
  search,