@swell/apps-sdk 1.0.128 → 1.0.129

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
@@ -67,6 +67,7 @@ __export(index_exports, {
67
67
  Swell: () => Swell,
68
68
  SwellBackendAPI: () => SwellBackendAPI,
69
69
  SwellError: () => SwellError,
70
+ SwellProduct: () => SwellProduct,
70
71
  SwellStorefrontCollection: () => SwellStorefrontCollection,
71
72
  SwellStorefrontPagination: () => SwellStorefrontPagination,
72
73
  SwellStorefrontRecord: () => SwellStorefrontRecord,
@@ -93,9 +94,6 @@ __export(index_exports, {
93
94
  getAllSections: () => getAllSections,
94
95
  getBlog: () => getBlog,
95
96
  getBlogs: () => getBlogs,
96
- getCategories: () => getCategories,
97
- getCategory: () => getCategory,
98
- getCategoryWithProducts: () => getCategoryWithProducts,
99
97
  getContentEntry: () => getContentEntry,
100
98
  getContentList: () => getContentList,
101
99
  getContentModel: () => getContentModel,
@@ -112,9 +110,6 @@ __export(index_exports, {
112
110
  getMenuItemValueId: () => getMenuItemValueId,
113
111
  getPage: () => getPage,
114
112
  getPageSections: () => getPageSections,
115
- getProduct: () => getProduct,
116
- getProducts: () => getProducts,
117
- getProductsFiltered: () => getProductsFiltered,
118
113
  getSectionGroupProp: () => getSectionGroupProp,
119
114
  getSectionSettingsFromProps: () => getSectionSettingsFromProps,
120
115
  getThemeSettingsFromProps: () => getThemeSettingsFromProps,
@@ -639,12 +634,18 @@ var StorefrontResource = class {
639
634
  }
640
635
  return instance[prop];
641
636
  }
637
+ // add additional properties to the loaded result
638
+ _transformResult(result) {
639
+ return result;
640
+ }
642
641
  async _get(..._args) {
643
642
  if (this._getter) {
644
643
  const getter = this._getter.bind(
645
644
  this
646
645
  );
647
646
  return Promise.resolve().then(getter).then((result) => {
647
+ return this._transformResult(result);
648
+ }).then((result) => {
648
649
  this._result = result;
649
650
  if (result) {
650
651
  Object.assign(this, result);
@@ -720,6 +721,8 @@ var StorefrontResource = class {
720
721
  function cloneStorefrontResource(input) {
721
722
  const resourceName = input._resourceName;
722
723
  const clone = new StorefrontResource(input._getter);
724
+ clone._params = input._params;
725
+ clone._transformResult = input._transformResult.bind(clone);
723
726
  Object.defineProperty(clone.constructor, "name", {
724
727
  value: resourceName
725
728
  });
@@ -890,10 +893,12 @@ var SwellStorefrontCollection = class _SwellStorefrontCollection extends SwellSt
890
893
  };
891
894
  var SwellStorefrontRecord = class extends SwellStorefrontResource {
892
895
  _id;
896
+ _params;
893
897
  constructor(swell, collection, id, query = {}, getter) {
894
898
  super(swell, collection, getter);
895
899
  this._id = id;
896
900
  this._query = query;
901
+ this._params = {};
897
902
  if (!getter) {
898
903
  this._setGetter(this._defaultGetter());
899
904
  }
@@ -931,6 +936,8 @@ var SwellStorefrontRecord = class extends SwellStorefrontResource {
931
936
  ],
932
937
  getter
933
938
  ).then((result) => {
939
+ return this._transformResult(result);
940
+ }).then((result) => {
934
941
  this._result = result;
935
942
  if (result) {
936
943
  Object.assign(this, result);
@@ -7171,6 +7178,169 @@ var ThemeCache = class extends Cache {
7171
7178
  }
7172
7179
  };
7173
7180
 
7181
+ // src/resources/product_helpers.ts
7182
+ function calculateAddOptionsPrice(product, queryParams) {
7183
+ const { option_values = "" } = queryParams;
7184
+ const queryOptionValues = option_values;
7185
+ const optionValues = queryOptionValues.split(",");
7186
+ const addPrice = product.options?.reduce((acc, option) => {
7187
+ if (!option.active || !option.values || option.values.length <= 0) {
7188
+ return acc;
7189
+ }
7190
+ if (option.input_type !== "select") {
7191
+ return acc;
7192
+ }
7193
+ for (const value of option.values) {
7194
+ if (optionValues.includes(value.id)) {
7195
+ return acc + (value.price || 0);
7196
+ }
7197
+ }
7198
+ return acc + (option.values[0].price || 0);
7199
+ }, 0);
7200
+ return product.price + (addPrice || 0);
7201
+ }
7202
+ function calculateAddOptionsVariantPrice(product, variant, queryParams) {
7203
+ const { option_values = "" } = queryParams;
7204
+ const queryOptionValues = option_values;
7205
+ const optionValues = queryOptionValues.split(",");
7206
+ const addPrice = product.options?.reduce((acc, option) => {
7207
+ if (option.variant || // skip variant options
7208
+ !option.active || !option.values || option.values.length <= 0) {
7209
+ return acc;
7210
+ }
7211
+ if (option.input_type !== "select") {
7212
+ return acc;
7213
+ }
7214
+ for (const value of option.values) {
7215
+ if (optionValues.includes(value.id)) {
7216
+ return acc + (value.price || 0);
7217
+ }
7218
+ }
7219
+ return acc + (option.values[0].price || 0);
7220
+ }, 0);
7221
+ let price = product.price;
7222
+ if (variant.price !== null && variant.price !== void 0) {
7223
+ price = variant.price;
7224
+ }
7225
+ return price + (addPrice || 0);
7226
+ }
7227
+ function getAvailableVariants(product) {
7228
+ return (product.variants?.results?.slice()?.reverse() || []).filter(
7229
+ (variant) => variant.stock_status === "in_stock" || !variant.stock_status
7230
+ );
7231
+ }
7232
+ function getSelectedSwellVariant(product, queryParams) {
7233
+ const { variant: queryVariant, option_values } = queryParams;
7234
+ const queryOptionValues = option_values;
7235
+ const variants = getAvailableVariants(product);
7236
+ let selectedVariant = void 0;
7237
+ if (queryVariant) {
7238
+ selectedVariant = variants.find((variant) => variant.id === queryVariant);
7239
+ } else if (queryOptionValues) {
7240
+ const optionValues = queryOptionValues.split(",");
7241
+ selectedVariant = variants.find(
7242
+ (variant) => variant.option_value_ids.every(
7243
+ (optionValueId) => optionValues.includes(optionValueId)
7244
+ )
7245
+ );
7246
+ }
7247
+ return selectedVariant || variants?.[0] || void 0;
7248
+ }
7249
+ function getSelectedVariant(product, queryParams) {
7250
+ return getSelectedSwellVariant(
7251
+ product,
7252
+ queryParams
7253
+ );
7254
+ }
7255
+ function getSelectedOptionValues(product, queryParams) {
7256
+ const variant = getSelectedSwellVariant(product, queryParams);
7257
+ return getSelectedVariantOptionValues(product, variant, queryParams);
7258
+ }
7259
+ function getSelectedVariantOptionValues(product, variant, queryParams) {
7260
+ const { option_values = "" } = queryParams;
7261
+ const queryOptionValues = option_values;
7262
+ const optionValues = queryOptionValues.split(",");
7263
+ const selectedValues = variant ? [...variant.option_value_ids || []] : [];
7264
+ const values = [];
7265
+ for (const option of product.options || []) {
7266
+ if (option.active && option.values && option.values.length > 0 && option.input_type === "select") {
7267
+ let selectedByVariantId = "";
7268
+ let selectedByOptionId = "";
7269
+ for (const value of option.values) {
7270
+ if (selectedValues.includes(value.id)) {
7271
+ selectedByVariantId = value.id;
7272
+ break;
7273
+ }
7274
+ if (optionValues.includes(value.id)) {
7275
+ selectedByOptionId = value.id;
7276
+ }
7277
+ }
7278
+ values.push(
7279
+ selectedByVariantId || selectedByOptionId || option.values[0].id
7280
+ );
7281
+ }
7282
+ }
7283
+ return values;
7284
+ }
7285
+
7286
+ // src/resources/variant.ts
7287
+ function transformSwellVariant(params, product, variant) {
7288
+ if (!product) {
7289
+ return product;
7290
+ }
7291
+ if (!variant) {
7292
+ return variant;
7293
+ }
7294
+ return {
7295
+ ...variant,
7296
+ // add swell properties there
7297
+ price: calculateAddOptionsVariantPrice(product, variant, params),
7298
+ selected_option_values: getSelectedVariantOptionValues(
7299
+ product,
7300
+ variant,
7301
+ params
7302
+ )
7303
+ };
7304
+ }
7305
+
7306
+ // src/resources/product.ts
7307
+ function transformSwellProduct(params, product) {
7308
+ if (!product) {
7309
+ return product;
7310
+ }
7311
+ const newProduct = {
7312
+ ...product,
7313
+ // add swell properties there
7314
+ price: calculateAddOptionsPrice(product, params),
7315
+ selected_option_values: getSelectedOptionValues(product, params)
7316
+ };
7317
+ if (Array.isArray(newProduct.variants?.results)) {
7318
+ newProduct.variants = {
7319
+ ...newProduct.variants,
7320
+ results: newProduct.variants.results.map(
7321
+ (variant) => transformSwellVariant(params, product, variant)
7322
+ )
7323
+ };
7324
+ }
7325
+ return newProduct;
7326
+ }
7327
+ var SwellProduct = class extends SwellStorefrontRecord {
7328
+ _params;
7329
+ constructor(swell, id, query = {}, getter) {
7330
+ super(swell, "products", id, query, getter);
7331
+ this._params = swell.queryParams;
7332
+ return this._getProxy();
7333
+ }
7334
+ // add swell properties to the resolved object
7335
+ _transformResult(result) {
7336
+ const res = transformSwellProduct(
7337
+ this._params,
7338
+ result
7339
+ );
7340
+ return res;
7341
+ }
7342
+ };
7343
+
7174
7344
  // src/api.ts
7175
7345
  var DEFAULT_API_HOST = "https://api.schema.io";
7176
7346
  var resourceCaches = /* @__PURE__ */ new Map();
@@ -7606,39 +7776,6 @@ ${formattedMessage}`;
7606
7776
  }
7607
7777
  };
7608
7778
 
7609
- // src/products.ts
7610
- function getProducts(swell, query) {
7611
- return new SwellStorefrontCollection(swell, "products", query);
7612
- }
7613
- function getProduct(swell, id, query) {
7614
- return new SwellStorefrontRecord(swell, "products", id, query);
7615
- }
7616
- function getProductsFiltered(swell, {
7617
- search,
7618
- filter,
7619
- sort
7620
- }) {
7621
- return new SwellStorefrontCollection(swell, "products", {
7622
- search,
7623
- filter,
7624
- sort
7625
- });
7626
- }
7627
-
7628
- // src/categories.ts
7629
- function getCategories(swell, query) {
7630
- return new SwellStorefrontCollection(swell, "categories", query);
7631
- }
7632
- function getCategory(swell, id, query) {
7633
- return new SwellStorefrontRecord(swell, "categories", id, query);
7634
- }
7635
- async function getCategoryWithProducts(swell, id, query) {
7636
- const category = getCategory(swell, id, query);
7637
- const categoryId = await category.id;
7638
- category.products = getProducts(swell, { category: categoryId });
7639
- return category;
7640
- }
7641
-
7642
7779
  // src/content.ts
7643
7780
  function getContentModel(swell, name) {
7644
7781
  return swell.getCachedResource(
@@ -13831,11 +13968,15 @@ function ShopifyVariant(instance, variant, productIn, depth = 0) {
13831
13968
  if (variant instanceof ShopifyResource) {
13832
13969
  return variant.clone();
13833
13970
  }
13971
+ let swellVariant = {};
13834
13972
  if (variant instanceof StorefrontResource) {
13835
13973
  variant = cloneStorefrontResource(variant);
13974
+ } else {
13975
+ swellVariant = { ...variant };
13836
13976
  }
13837
13977
  const product = productIn || variant.product || {};
13838
13978
  return new ShopifyResource({
13979
+ ...swellVariant,
13839
13980
  available: deferWith(
13840
13981
  variant,
13841
13982
  (variant2) => Boolean(variant2.stock_status === "in_stock" || !variant2.stock_status)
@@ -13881,25 +14022,19 @@ function ShopifyVariant(instance, variant, productIn, depth = 0) {
13881
14022
  metafields: {},
13882
14023
  next_incoming_date: void 0,
13883
14024
  options: getOptions(product, variant),
13884
- // @ts-expect-error: move this to swell product class
13885
- selected_option_values: deferWith(
13886
- [product, variant],
13887
- (product2, variant2) => getSelectedVariantOptionValues(
13888
- product2,
13889
- variant2,
13890
- instance.swell.queryParams
13891
- )
13892
- ),
13893
14025
  option1: getOptionByIndex(product, variant, 0),
13894
14026
  // Deprecated by Shopify
13895
14027
  option2: getOptionByIndex(product, variant, 1),
13896
14028
  // Deprecated by Shopify
13897
14029
  option3: getOptionByIndex(product, variant, 2),
13898
14030
  // Deprecated by Shopify
13899
- price: deferWith(
13900
- [product, variant],
13901
- (product2, variant2) => getVariantPrice(product2, variant2, instance.swell.queryParams)
13902
- ),
14031
+ price: deferWith([product, variant], (product2, variant2) => {
14032
+ let price = product2.price;
14033
+ if (variant2.price !== null && variant2.price !== void 0) {
14034
+ price = variant2.price;
14035
+ }
14036
+ return price;
14037
+ }),
13903
14038
  product: deferWith(product, (product2) => {
13904
14039
  return ShopifyProduct(
13905
14040
  instance,
@@ -13986,30 +14121,6 @@ function getOptionByIndex(product, variant, index) {
13986
14121
  }
13987
14122
  );
13988
14123
  }
13989
- function getVariantPrice(product, variant, queryParams) {
13990
- const { option_values: queryOptionValues = "" } = queryParams;
13991
- const optionValues = queryOptionValues.split(",");
13992
- const addPrice = product.options?.reduce((acc, option) => {
13993
- if (option.variant || // skip variant options
13994
- !option.active || !option.values || option.values.length <= 0) {
13995
- return acc;
13996
- }
13997
- if (option.input_type !== "select") {
13998
- return acc;
13999
- }
14000
- for (const value of option.values) {
14001
- if (optionValues.includes(value.id)) {
14002
- return acc + (value.price || 0);
14003
- }
14004
- }
14005
- return acc + (option.values[0].price || 0);
14006
- }, 0);
14007
- let price = product.price;
14008
- if (variant.price !== null && variant.price !== void 0) {
14009
- price = variant.price;
14010
- }
14011
- return price + (addPrice || 0);
14012
- }
14013
14124
 
14014
14125
  // src/compatibility/shopify-objects/product.ts
14015
14126
  function ShopifyProduct(instance, product, depth = 0) {
@@ -14085,12 +14196,6 @@ function ShopifyProduct(instance, product, depth = 0) {
14085
14196
  }
14086
14197
  return product2.options.filter((option) => option.active && option.name).map((option) => option.name);
14087
14198
  }),
14088
- // all options values including non-variant
14089
- // @ts-expect-error: move this to swell product class
14090
- selected_option_values: deferWith(
14091
- product,
14092
- (product2) => getSelectedOptionValues(product2, instance.swell.queryParams)
14093
- ),
14094
14199
  options_by_name: deferWith(product, (product2) => {
14095
14200
  if (!Array.isArray(product2.options)) {
14096
14201
  return {};
@@ -14158,10 +14263,7 @@ function ShopifyProduct(instance, product, depth = 0) {
14158
14263
  });
14159
14264
  }
14160
14265
  ),
14161
- price: deferWith(
14162
- product,
14163
- (product2) => calculateAddOptionsPrice(product2, instance.swell.queryParams)
14164
- ),
14266
+ price: deferWith(product, (product2) => product2.price),
14165
14267
  price_max: deferWith(product, (product2) => {
14166
14268
  if (!Array.isArray(product2.variants?.results)) {
14167
14269
  return product2.price;
@@ -14196,6 +14298,8 @@ function ShopifyProduct(instance, product, depth = 0) {
14196
14298
  product,
14197
14299
  (product2) => product2.prices?.length > 0
14198
14300
  ),
14301
+ // ShopifyProduct does not have this property
14302
+ // @ts-expect-error property
14199
14303
  quantity_rule: deferWith(product, (product2) => {
14200
14304
  let inventory = product2.stock_level || 0;
14201
14305
  if (inventory < 0) {
@@ -14254,77 +14358,6 @@ function ShopifyProduct(instance, product, depth = 0) {
14254
14358
  function ShopifyProductOptionValue(values) {
14255
14359
  return new ShopifyResource(values, "name");
14256
14360
  }
14257
- function getSelectedVariant(product, queryParams) {
14258
- const { variant: queryVariant, option_values: queryOptionValues } = queryParams;
14259
- const variants = getAvailableVariants(product);
14260
- let selectedVariant = void 0;
14261
- if (queryVariant) {
14262
- selectedVariant = variants.find(
14263
- (variant) => variant.id === queryVariant
14264
- );
14265
- } else if (queryOptionValues) {
14266
- const optionValues = queryOptionValues.split(",");
14267
- selectedVariant = variants.find(
14268
- (variant) => variant.option_value_ids.every(
14269
- (optionValueId) => optionValues.includes(optionValueId)
14270
- )
14271
- );
14272
- }
14273
- return selectedVariant || variants?.[0] || void 0;
14274
- }
14275
- function getAvailableVariants(product) {
14276
- return (product.variants?.results?.slice()?.reverse() || []).filter(
14277
- (variant) => variant.stock_status === "in_stock" || !variant.stock_status
14278
- );
14279
- }
14280
- function calculateAddOptionsPrice(product, queryParams) {
14281
- const { option_values: queryOptionValues = "" } = queryParams;
14282
- const optionValues = queryOptionValues.split(",");
14283
- const addPrice = product.options?.reduce((acc, option) => {
14284
- if (!option.active || !option.values || option.values.length <= 0) {
14285
- return acc;
14286
- }
14287
- if (option.input_type !== "select") {
14288
- return acc;
14289
- }
14290
- for (const value of option.values) {
14291
- if (optionValues.includes(value.id)) {
14292
- return acc + (value.price || 0);
14293
- }
14294
- }
14295
- return acc + (option.values[0].price || 0);
14296
- }, 0);
14297
- return product.price + (addPrice || 0);
14298
- }
14299
- function getSelectedOptionValues(product, queryParams) {
14300
- const variant = getSelectedVariant(product, queryParams);
14301
- return getSelectedVariantOptionValues(product, variant, queryParams);
14302
- }
14303
- function getSelectedVariantOptionValues(product, variant, queryParams) {
14304
- const { option_values: queryOptionValues = "" } = queryParams;
14305
- const optionValues = queryOptionValues.split(",");
14306
- const selectedValues = variant ? [...variant.option_value_ids || []] : [];
14307
- const values = [];
14308
- for (const option of product.options || []) {
14309
- if (option.active && option.values?.length > 0 && option.input_type === "select") {
14310
- let selectedByVariantId = "";
14311
- let selectedByOptionId = "";
14312
- for (const value of option.values) {
14313
- if (selectedValues.includes(value.id)) {
14314
- selectedByVariantId = value.id;
14315
- break;
14316
- }
14317
- if (optionValues.includes(value.id)) {
14318
- selectedByOptionId = value.id;
14319
- }
14320
- }
14321
- values.push(
14322
- selectedByVariantId || selectedByOptionId || option.values[0].id
14323
- );
14324
- }
14325
- }
14326
- return values;
14327
- }
14328
14361
  function isLikeShopifyProduct(value) {
14329
14362
  return typeof value === "object" && value !== null && Object.hasOwn(value, "variants") && Object.hasOwn(value, "gift_card?") && Object.hasOwn(value, "price_varies") && Object.hasOwn(value, "has_only_default_variant");
14330
14363
  }
@@ -20843,6 +20876,7 @@ function getResourceQuery(slug, query) {
20843
20876
  Swell,
20844
20877
  SwellBackendAPI,
20845
20878
  SwellError,
20879
+ SwellProduct,
20846
20880
  SwellStorefrontCollection,
20847
20881
  SwellStorefrontPagination,
20848
20882
  SwellStorefrontRecord,
@@ -20869,9 +20903,6 @@ function getResourceQuery(slug, query) {
20869
20903
  getAllSections,
20870
20904
  getBlog,
20871
20905
  getBlogs,
20872
- getCategories,
20873
- getCategory,
20874
- getCategoryWithProducts,
20875
20906
  getContentEntry,
20876
20907
  getContentList,
20877
20908
  getContentModel,
@@ -20888,9 +20919,6 @@ function getResourceQuery(slug, query) {
20888
20919
  getMenuItemValueId,
20889
20920
  getPage,
20890
20921
  getPageSections,
20891
- getProduct,
20892
- getProducts,
20893
- getProductsFiltered,
20894
20922
  getSectionGroupProp,
20895
20923
  getSectionSettingsFromProps,
20896
20924
  getThemeSettingsFromProps,