@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.js CHANGED
@@ -535,12 +535,18 @@
535
535
  }
536
536
  return instance[prop];
537
537
  }
538
+ // add additional properties to the loaded result
539
+ _transformResult(result) {
540
+ return result;
541
+ }
538
542
  async _get(..._args) {
539
543
  if (this._getter) {
540
544
  const getter = this._getter.bind(
541
545
  this
542
546
  );
543
547
  return Promise.resolve().then(getter).then((result) => {
548
+ return this._transformResult(result);
549
+ }).then((result) => {
544
550
  this._result = result;
545
551
  if (result) {
546
552
  Object.assign(this, result);
@@ -616,6 +622,8 @@
616
622
  function cloneStorefrontResource(input) {
617
623
  const resourceName = input._resourceName;
618
624
  const clone = new StorefrontResource(input._getter);
625
+ clone._params = input._params;
626
+ clone._transformResult = input._transformResult.bind(clone);
619
627
  Object.defineProperty(clone.constructor, "name", {
620
628
  value: resourceName
621
629
  });
@@ -786,10 +794,12 @@
786
794
  };
787
795
  var SwellStorefrontRecord = class extends SwellStorefrontResource {
788
796
  _id;
797
+ _params;
789
798
  constructor(swell, collection, id, query = {}, getter) {
790
799
  super(swell, collection, getter);
791
800
  this._id = id;
792
801
  this._query = query;
802
+ this._params = {};
793
803
  if (!getter) {
794
804
  this._setGetter(this._defaultGetter());
795
805
  }
@@ -827,6 +837,8 @@
827
837
  ],
828
838
  getter
829
839
  ).then((result) => {
840
+ return this._transformResult(result);
841
+ }).then((result) => {
830
842
  this._result = result;
831
843
  if (result) {
832
844
  Object.assign(this, result);
@@ -7067,6 +7079,169 @@
7067
7079
  }
7068
7080
  };
7069
7081
 
7082
+ // src/resources/product_helpers.ts
7083
+ function calculateAddOptionsPrice(product, queryParams) {
7084
+ const { option_values = "" } = queryParams;
7085
+ const queryOptionValues = option_values;
7086
+ const optionValues = queryOptionValues.split(",");
7087
+ const addPrice = product.options?.reduce((acc, option) => {
7088
+ if (!option.active || !option.values || option.values.length <= 0) {
7089
+ return acc;
7090
+ }
7091
+ if (option.input_type !== "select") {
7092
+ return acc;
7093
+ }
7094
+ for (const value of option.values) {
7095
+ if (optionValues.includes(value.id)) {
7096
+ return acc + (value.price || 0);
7097
+ }
7098
+ }
7099
+ return acc + (option.values[0].price || 0);
7100
+ }, 0);
7101
+ return product.price + (addPrice || 0);
7102
+ }
7103
+ function calculateAddOptionsVariantPrice(product, variant, queryParams) {
7104
+ const { option_values = "" } = queryParams;
7105
+ const queryOptionValues = option_values;
7106
+ const optionValues = queryOptionValues.split(",");
7107
+ const addPrice = product.options?.reduce((acc, option) => {
7108
+ if (option.variant || // skip variant options
7109
+ !option.active || !option.values || option.values.length <= 0) {
7110
+ return acc;
7111
+ }
7112
+ if (option.input_type !== "select") {
7113
+ return acc;
7114
+ }
7115
+ for (const value of option.values) {
7116
+ if (optionValues.includes(value.id)) {
7117
+ return acc + (value.price || 0);
7118
+ }
7119
+ }
7120
+ return acc + (option.values[0].price || 0);
7121
+ }, 0);
7122
+ let price = product.price;
7123
+ if (variant.price !== null && variant.price !== void 0) {
7124
+ price = variant.price;
7125
+ }
7126
+ return price + (addPrice || 0);
7127
+ }
7128
+ function getAvailableVariants(product) {
7129
+ return (product.variants?.results?.slice()?.reverse() || []).filter(
7130
+ (variant) => variant.stock_status === "in_stock" || !variant.stock_status
7131
+ );
7132
+ }
7133
+ function getSelectedSwellVariant(product, queryParams) {
7134
+ const { variant: queryVariant, option_values } = queryParams;
7135
+ const queryOptionValues = option_values;
7136
+ const variants = getAvailableVariants(product);
7137
+ let selectedVariant = void 0;
7138
+ if (queryVariant) {
7139
+ selectedVariant = variants.find((variant) => variant.id === queryVariant);
7140
+ } else if (queryOptionValues) {
7141
+ const optionValues = queryOptionValues.split(",");
7142
+ selectedVariant = variants.find(
7143
+ (variant) => variant.option_value_ids.every(
7144
+ (optionValueId) => optionValues.includes(optionValueId)
7145
+ )
7146
+ );
7147
+ }
7148
+ return selectedVariant || variants?.[0] || void 0;
7149
+ }
7150
+ function getSelectedVariant(product, queryParams) {
7151
+ return getSelectedSwellVariant(
7152
+ product,
7153
+ queryParams
7154
+ );
7155
+ }
7156
+ function getSelectedOptionValues(product, queryParams) {
7157
+ const variant = getSelectedSwellVariant(product, queryParams);
7158
+ return getSelectedVariantOptionValues(product, variant, queryParams);
7159
+ }
7160
+ function getSelectedVariantOptionValues(product, variant, queryParams) {
7161
+ const { option_values = "" } = queryParams;
7162
+ const queryOptionValues = option_values;
7163
+ const optionValues = queryOptionValues.split(",");
7164
+ const selectedValues = variant ? [...variant.option_value_ids || []] : [];
7165
+ const values = [];
7166
+ for (const option of product.options || []) {
7167
+ if (option.active && option.values && option.values.length > 0 && option.input_type === "select") {
7168
+ let selectedByVariantId = "";
7169
+ let selectedByOptionId = "";
7170
+ for (const value of option.values) {
7171
+ if (selectedValues.includes(value.id)) {
7172
+ selectedByVariantId = value.id;
7173
+ break;
7174
+ }
7175
+ if (optionValues.includes(value.id)) {
7176
+ selectedByOptionId = value.id;
7177
+ }
7178
+ }
7179
+ values.push(
7180
+ selectedByVariantId || selectedByOptionId || option.values[0].id
7181
+ );
7182
+ }
7183
+ }
7184
+ return values;
7185
+ }
7186
+
7187
+ // src/resources/variant.ts
7188
+ function transformSwellVariant(params, product, variant) {
7189
+ if (!product) {
7190
+ return product;
7191
+ }
7192
+ if (!variant) {
7193
+ return variant;
7194
+ }
7195
+ return {
7196
+ ...variant,
7197
+ // add swell properties there
7198
+ price: calculateAddOptionsVariantPrice(product, variant, params),
7199
+ selected_option_values: getSelectedVariantOptionValues(
7200
+ product,
7201
+ variant,
7202
+ params
7203
+ )
7204
+ };
7205
+ }
7206
+
7207
+ // src/resources/product.ts
7208
+ function transformSwellProduct(params, product) {
7209
+ if (!product) {
7210
+ return product;
7211
+ }
7212
+ const newProduct = {
7213
+ ...product,
7214
+ // add swell properties there
7215
+ price: calculateAddOptionsPrice(product, params),
7216
+ selected_option_values: getSelectedOptionValues(product, params)
7217
+ };
7218
+ if (Array.isArray(newProduct.variants?.results)) {
7219
+ newProduct.variants = {
7220
+ ...newProduct.variants,
7221
+ results: newProduct.variants.results.map(
7222
+ (variant) => transformSwellVariant(params, product, variant)
7223
+ )
7224
+ };
7225
+ }
7226
+ return newProduct;
7227
+ }
7228
+ var SwellProduct = class extends SwellStorefrontRecord {
7229
+ _params;
7230
+ constructor(swell, id, query = {}, getter) {
7231
+ super(swell, "products", id, query, getter);
7232
+ this._params = swell.queryParams;
7233
+ return this._getProxy();
7234
+ }
7235
+ // add swell properties to the resolved object
7236
+ _transformResult(result) {
7237
+ const res = transformSwellProduct(
7238
+ this._params,
7239
+ result
7240
+ );
7241
+ return res;
7242
+ }
7243
+ };
7244
+
7070
7245
  // src/api.ts
7071
7246
  var DEFAULT_API_HOST = "https://api.schema.io";
7072
7247
  var resourceCaches = /* @__PURE__ */ new Map();
@@ -7502,39 +7677,6 @@ ${formattedMessage}`;
7502
7677
  }
7503
7678
  };
7504
7679
 
7505
- // src/products.ts
7506
- function getProducts(swell, query) {
7507
- return new SwellStorefrontCollection(swell, "products", query);
7508
- }
7509
- function getProduct(swell, id, query) {
7510
- return new SwellStorefrontRecord(swell, "products", id, query);
7511
- }
7512
- function getProductsFiltered(swell, {
7513
- search,
7514
- filter,
7515
- sort
7516
- }) {
7517
- return new SwellStorefrontCollection(swell, "products", {
7518
- search,
7519
- filter,
7520
- sort
7521
- });
7522
- }
7523
-
7524
- // src/categories.ts
7525
- function getCategories(swell, query) {
7526
- return new SwellStorefrontCollection(swell, "categories", query);
7527
- }
7528
- function getCategory(swell, id, query) {
7529
- return new SwellStorefrontRecord(swell, "categories", id, query);
7530
- }
7531
- async function getCategoryWithProducts(swell, id, query) {
7532
- const category = getCategory(swell, id, query);
7533
- const categoryId = await category.id;
7534
- category.products = getProducts(swell, { category: categoryId });
7535
- return category;
7536
- }
7537
-
7538
7680
  // src/content.ts
7539
7681
  function getContentModel(swell, name) {
7540
7682
  return swell.getCachedResource(
@@ -13727,11 +13869,15 @@ ${formattedMessage}`;
13727
13869
  if (variant instanceof ShopifyResource) {
13728
13870
  return variant.clone();
13729
13871
  }
13872
+ let swellVariant = {};
13730
13873
  if (variant instanceof StorefrontResource) {
13731
13874
  variant = cloneStorefrontResource(variant);
13875
+ } else {
13876
+ swellVariant = { ...variant };
13732
13877
  }
13733
13878
  const product = productIn || variant.product || {};
13734
13879
  return new ShopifyResource({
13880
+ ...swellVariant,
13735
13881
  available: deferWith(
13736
13882
  variant,
13737
13883
  (variant2) => Boolean(variant2.stock_status === "in_stock" || !variant2.stock_status)
@@ -13777,25 +13923,19 @@ ${formattedMessage}`;
13777
13923
  metafields: {},
13778
13924
  next_incoming_date: void 0,
13779
13925
  options: getOptions(product, variant),
13780
- // @ts-expect-error: move this to swell product class
13781
- selected_option_values: deferWith(
13782
- [product, variant],
13783
- (product2, variant2) => getSelectedVariantOptionValues(
13784
- product2,
13785
- variant2,
13786
- instance.swell.queryParams
13787
- )
13788
- ),
13789
13926
  option1: getOptionByIndex(product, variant, 0),
13790
13927
  // Deprecated by Shopify
13791
13928
  option2: getOptionByIndex(product, variant, 1),
13792
13929
  // Deprecated by Shopify
13793
13930
  option3: getOptionByIndex(product, variant, 2),
13794
13931
  // Deprecated by Shopify
13795
- price: deferWith(
13796
- [product, variant],
13797
- (product2, variant2) => getVariantPrice(product2, variant2, instance.swell.queryParams)
13798
- ),
13932
+ price: deferWith([product, variant], (product2, variant2) => {
13933
+ let price = product2.price;
13934
+ if (variant2.price !== null && variant2.price !== void 0) {
13935
+ price = variant2.price;
13936
+ }
13937
+ return price;
13938
+ }),
13799
13939
  product: deferWith(product, (product2) => {
13800
13940
  return ShopifyProduct(
13801
13941
  instance,
@@ -13882,30 +14022,6 @@ ${formattedMessage}`;
13882
14022
  }
13883
14023
  );
13884
14024
  }
13885
- function getVariantPrice(product, variant, queryParams) {
13886
- const { option_values: queryOptionValues = "" } = queryParams;
13887
- const optionValues = queryOptionValues.split(",");
13888
- const addPrice = product.options?.reduce((acc, option) => {
13889
- if (option.variant || // skip variant options
13890
- !option.active || !option.values || option.values.length <= 0) {
13891
- return acc;
13892
- }
13893
- if (option.input_type !== "select") {
13894
- return acc;
13895
- }
13896
- for (const value of option.values) {
13897
- if (optionValues.includes(value.id)) {
13898
- return acc + (value.price || 0);
13899
- }
13900
- }
13901
- return acc + (option.values[0].price || 0);
13902
- }, 0);
13903
- let price = product.price;
13904
- if (variant.price !== null && variant.price !== void 0) {
13905
- price = variant.price;
13906
- }
13907
- return price + (addPrice || 0);
13908
- }
13909
14025
 
13910
14026
  // src/compatibility/shopify-objects/product.ts
13911
14027
  function ShopifyProduct(instance, product, depth = 0) {
@@ -13981,12 +14097,6 @@ ${formattedMessage}`;
13981
14097
  }
13982
14098
  return product2.options.filter((option) => option.active && option.name).map((option) => option.name);
13983
14099
  }),
13984
- // all options values including non-variant
13985
- // @ts-expect-error: move this to swell product class
13986
- selected_option_values: deferWith(
13987
- product,
13988
- (product2) => getSelectedOptionValues(product2, instance.swell.queryParams)
13989
- ),
13990
14100
  options_by_name: deferWith(product, (product2) => {
13991
14101
  if (!Array.isArray(product2.options)) {
13992
14102
  return {};
@@ -14054,10 +14164,7 @@ ${formattedMessage}`;
14054
14164
  });
14055
14165
  }
14056
14166
  ),
14057
- price: deferWith(
14058
- product,
14059
- (product2) => calculateAddOptionsPrice(product2, instance.swell.queryParams)
14060
- ),
14167
+ price: deferWith(product, (product2) => product2.price),
14061
14168
  price_max: deferWith(product, (product2) => {
14062
14169
  if (!Array.isArray(product2.variants?.results)) {
14063
14170
  return product2.price;
@@ -14092,6 +14199,8 @@ ${formattedMessage}`;
14092
14199
  product,
14093
14200
  (product2) => product2.prices?.length > 0
14094
14201
  ),
14202
+ // ShopifyProduct does not have this property
14203
+ // @ts-expect-error property
14095
14204
  quantity_rule: deferWith(product, (product2) => {
14096
14205
  let inventory = product2.stock_level || 0;
14097
14206
  if (inventory < 0) {
@@ -14150,77 +14259,6 @@ ${formattedMessage}`;
14150
14259
  function ShopifyProductOptionValue(values) {
14151
14260
  return new ShopifyResource(values, "name");
14152
14261
  }
14153
- function getSelectedVariant(product, queryParams) {
14154
- const { variant: queryVariant, option_values: queryOptionValues } = queryParams;
14155
- const variants = getAvailableVariants(product);
14156
- let selectedVariant = void 0;
14157
- if (queryVariant) {
14158
- selectedVariant = variants.find(
14159
- (variant) => variant.id === queryVariant
14160
- );
14161
- } else if (queryOptionValues) {
14162
- const optionValues = queryOptionValues.split(",");
14163
- selectedVariant = variants.find(
14164
- (variant) => variant.option_value_ids.every(
14165
- (optionValueId) => optionValues.includes(optionValueId)
14166
- )
14167
- );
14168
- }
14169
- return selectedVariant || variants?.[0] || void 0;
14170
- }
14171
- function getAvailableVariants(product) {
14172
- return (product.variants?.results?.slice()?.reverse() || []).filter(
14173
- (variant) => variant.stock_status === "in_stock" || !variant.stock_status
14174
- );
14175
- }
14176
- function calculateAddOptionsPrice(product, queryParams) {
14177
- const { option_values: queryOptionValues = "" } = queryParams;
14178
- const optionValues = queryOptionValues.split(",");
14179
- const addPrice = product.options?.reduce((acc, option) => {
14180
- if (!option.active || !option.values || option.values.length <= 0) {
14181
- return acc;
14182
- }
14183
- if (option.input_type !== "select") {
14184
- return acc;
14185
- }
14186
- for (const value of option.values) {
14187
- if (optionValues.includes(value.id)) {
14188
- return acc + (value.price || 0);
14189
- }
14190
- }
14191
- return acc + (option.values[0].price || 0);
14192
- }, 0);
14193
- return product.price + (addPrice || 0);
14194
- }
14195
- function getSelectedOptionValues(product, queryParams) {
14196
- const variant = getSelectedVariant(product, queryParams);
14197
- return getSelectedVariantOptionValues(product, variant, queryParams);
14198
- }
14199
- function getSelectedVariantOptionValues(product, variant, queryParams) {
14200
- const { option_values: queryOptionValues = "" } = queryParams;
14201
- const optionValues = queryOptionValues.split(",");
14202
- const selectedValues = variant ? [...variant.option_value_ids || []] : [];
14203
- const values = [];
14204
- for (const option of product.options || []) {
14205
- if (option.active && option.values?.length > 0 && option.input_type === "select") {
14206
- let selectedByVariantId = "";
14207
- let selectedByOptionId = "";
14208
- for (const value of option.values) {
14209
- if (selectedValues.includes(value.id)) {
14210
- selectedByVariantId = value.id;
14211
- break;
14212
- }
14213
- if (optionValues.includes(value.id)) {
14214
- selectedByOptionId = value.id;
14215
- }
14216
- }
14217
- values.push(
14218
- selectedByVariantId || selectedByOptionId || option.values[0].id
14219
- );
14220
- }
14221
- }
14222
- return values;
14223
- }
14224
14262
  function isLikeShopifyProduct(value) {
14225
14263
  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");
14226
14264
  }