@swell/apps-sdk 1.0.130 → 1.0.133

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
@@ -7053,108 +7053,144 @@ var ThemeCache = class extends Cache {
7053
7053
  };
7054
7054
 
7055
7055
  // src/resources/product_helpers.ts
7056
- function calculateAddOptionsPrice(product, queryParams) {
7057
- const { option_values = "" } = queryParams;
7058
- const queryOptionValues = option_values;
7059
- const optionValues = queryOptionValues.split(",");
7060
- const addPrice = product.options?.reduce((acc, option) => {
7061
- if (!option.active || !option.values || option.values.length <= 0) {
7062
- return acc;
7063
- }
7064
- if (option.input_type !== "select") {
7065
- return acc;
7066
- }
7067
- for (const value of option.values) {
7068
- if (optionValues.includes(value.id)) {
7069
- return acc + (value.price || 0);
7070
- }
7071
- }
7072
- return acc + (option.values[0].price || 0);
7073
- }, 0);
7074
- return product.price + (addPrice || 0);
7075
- }
7076
- function calculateAddOptionsVariantPrice(product, variant, queryParams) {
7077
- const { option_values = "" } = queryParams;
7078
- const queryOptionValues = option_values;
7079
- const optionValues = queryOptionValues.split(",");
7080
- const addPrice = product.options?.reduce((acc, option) => {
7081
- if (option.variant || // skip variant options
7082
- !option.active || !option.values || option.values.length <= 0) {
7083
- return acc;
7084
- }
7085
- if (option.input_type !== "select") {
7086
- return acc;
7087
- }
7088
- for (const value of option.values) {
7089
- if (optionValues.includes(value.id)) {
7090
- return acc + (value.price || 0);
7091
- }
7092
- }
7093
- return acc + (option.values[0].price || 0);
7094
- }, 0);
7095
- let price = product.price;
7096
- if (variant.price !== null && variant.price !== void 0) {
7097
- price = variant.price;
7098
- }
7099
- return price + (addPrice || 0);
7100
- }
7101
7056
  function getAvailableVariants(product) {
7102
7057
  return (product.variants?.results?.slice()?.reverse() || []).filter(
7103
7058
  (variant) => variant.stock_status === "in_stock" || !variant.stock_status
7104
7059
  );
7105
7060
  }
7106
- function getSelectedSwellVariant(product, queryParams) {
7107
- const { variant: queryVariant, option_values } = queryParams;
7108
- const queryOptionValues = option_values;
7061
+ function isOptionValueAvailable(option, value, product, availableVariants) {
7062
+ if (!option.variant) {
7063
+ return true;
7064
+ }
7065
+ const hasVariants = product.variants?.results.length > 0;
7066
+ if (!hasVariants) {
7067
+ return true;
7068
+ }
7069
+ const variants = availableVariants || getAvailableVariants(product);
7070
+ return variants.some(
7071
+ (variant) => variant.option_value_ids.includes(value.id)
7072
+ );
7073
+ }
7074
+ function isOptionValueSelected(option, value, product, queryParams, selectedVariant) {
7075
+ let variant;
7076
+ if (option.variant) {
7077
+ variant = selectedVariant || getSelectedVariant(product, queryParams);
7078
+ }
7079
+ const selectedOptionValues = getSelectedVariantOptionValues(
7080
+ product,
7081
+ queryParams,
7082
+ variant
7083
+ );
7084
+ return selectedOptionValues.includes(value.id);
7085
+ }
7086
+ function getSelectedVariant(product, queryParams) {
7087
+ const {
7088
+ variant: queryVariant,
7089
+ option_values: queryOptionValues,
7090
+ selected_option_value: selectedOptionValue
7091
+ } = queryParams;
7109
7092
  const variants = getAvailableVariants(product);
7110
7093
  let selectedVariant = void 0;
7111
7094
  if (queryVariant) {
7112
7095
  selectedVariant = variants.find((variant) => variant.id === queryVariant);
7113
7096
  } else if (queryOptionValues) {
7114
7097
  const optionValues = queryOptionValues.split(",");
7115
- selectedVariant = variants.find(
7116
- (variant) => variant.option_value_ids.every(
7098
+ selectedVariant = variants.find((variant) => {
7099
+ if (variant.option_value_ids.length !== optionValues.length) {
7100
+ return false;
7101
+ }
7102
+ return variant.option_value_ids.every(
7117
7103
  (optionValueId) => optionValues.includes(optionValueId)
7118
- )
7119
- );
7104
+ );
7105
+ });
7106
+ if (!selectedVariant && selectedOptionValue) {
7107
+ selectedVariant = variants.filter(
7108
+ (variant) => variant.option_value_ids.includes(selectedOptionValue)
7109
+ )[0];
7110
+ }
7120
7111
  }
7121
7112
  return selectedVariant || variants?.[0] || void 0;
7122
7113
  }
7123
- function getSelectedVariant(product, queryParams) {
7124
- return getSelectedSwellVariant(
7114
+ function getSelectedVariantOptionValues(product, queryParams, variant) {
7115
+ if (variant) {
7116
+ return variant.option_value_ids;
7117
+ }
7118
+ const { option_values: queryOptionValues = "" } = queryParams;
7119
+ const optionValues = queryOptionValues.split(",");
7120
+ return (product.options || []).reduce((acc, option) => {
7121
+ if (!option.values) {
7122
+ return acc;
7123
+ }
7124
+ const hasOptionValues = option.values.length > 0;
7125
+ if (!option.active || !hasOptionValues) {
7126
+ return acc;
7127
+ }
7128
+ const value = option.values.find(
7129
+ (value2) => optionValues.includes(value2.id)
7130
+ );
7131
+ if (value) {
7132
+ acc.push(value.id);
7133
+ }
7134
+ return acc;
7135
+ }, []);
7136
+ }
7137
+ function getPurchaseOptions(product, queryParams) {
7138
+ if (!product?.purchase_options) {
7139
+ return null;
7140
+ }
7141
+ const { standard, subscription } = product.purchase_options;
7142
+ const selectedPurchaseOptionType = getSelectedPurchaseOptionType(
7125
7143
  product,
7126
7144
  queryParams
7127
7145
  );
7146
+ const purchaseOptions = {};
7147
+ if (standard) {
7148
+ purchaseOptions.standard = {
7149
+ ...standard,
7150
+ selected: selectedPurchaseOptionType === "standard"
7151
+ };
7152
+ }
7153
+ if (subscription) {
7154
+ const selectedPlan = getSelectedSubscriptionPurchaseOptionPlan(
7155
+ selectedPurchaseOptionType,
7156
+ subscription,
7157
+ queryParams
7158
+ );
7159
+ purchaseOptions.subscription = {
7160
+ ...subscription,
7161
+ selected: selectedPurchaseOptionType === "subscription",
7162
+ plans: subscription.plans.map((plan) => ({
7163
+ ...plan,
7164
+ selected: selectedPlan ? plan.id === selectedPlan.id : false
7165
+ }))
7166
+ };
7167
+ }
7168
+ return Object.keys(purchaseOptions).length > 0 ? purchaseOptions : null;
7128
7169
  }
7129
- function getSelectedOptionValues(product, queryParams) {
7130
- const variant = getSelectedSwellVariant(product, queryParams);
7131
- return getSelectedVariantOptionValues(product, variant, queryParams);
7170
+ function getSelectedPurchaseOptionType(product, queryParams) {
7171
+ const { purchase_options: purchaseOptions } = product;
7172
+ if (!purchaseOptions) {
7173
+ return null;
7174
+ }
7175
+ const { purchase_option: purchaseOption } = queryParams;
7176
+ const purchaseOptionType = purchaseOption?.type;
7177
+ if (purchaseOptionType && purchaseOptionType in purchaseOptions) {
7178
+ return purchaseOptionType;
7179
+ }
7180
+ return purchaseOptions.standard ? "standard" : "subscription";
7132
7181
  }
7133
- function getSelectedVariantOptionValues(product, variant, queryParams) {
7134
- const { option_values = "" } = queryParams;
7135
- const queryOptionValues = option_values;
7136
- const optionValues = queryOptionValues.split(",");
7137
- const selectedValues = variant ? [...variant.option_value_ids || []] : [];
7138
- const values = [];
7139
- for (const option of product.options || []) {
7140
- if (option.active && option.values && option.values.length > 0 && option.input_type === "select") {
7141
- let selectedByVariantId = "";
7142
- let selectedByOptionId = "";
7143
- for (const value of option.values) {
7144
- if (selectedValues.includes(value.id)) {
7145
- selectedByVariantId = value.id;
7146
- break;
7147
- }
7148
- if (optionValues.includes(value.id)) {
7149
- selectedByOptionId = value.id;
7150
- }
7151
- }
7152
- values.push(
7153
- selectedByVariantId || selectedByOptionId || option.values[0].id
7154
- );
7155
- }
7182
+ function getSelectedSubscriptionPurchaseOptionPlan(selectedPurchaseOptionType, subscriptionPurchaseOption, queryParams) {
7183
+ if (selectedPurchaseOptionType !== "subscription") {
7184
+ return null;
7156
7185
  }
7157
- return values;
7186
+ const { purchase_option: purchaseOption } = queryParams;
7187
+ let selectedPlan = null;
7188
+ if (purchaseOption?.plan_id) {
7189
+ selectedPlan = subscriptionPurchaseOption.plans.find(
7190
+ (plan) => plan.id === purchaseOption.plan_id
7191
+ );
7192
+ }
7193
+ return selectedPlan || subscriptionPurchaseOption.plans[0];
7158
7194
  }
7159
7195
 
7160
7196
  // src/resources/variant.ts
@@ -7168,11 +7204,10 @@ function transformSwellVariant(params, product, variant) {
7168
7204
  return {
7169
7205
  ...variant,
7170
7206
  // add swell properties there
7171
- price: calculateAddOptionsVariantPrice(product, variant, params),
7172
7207
  selected_option_values: getSelectedVariantOptionValues(
7173
7208
  product,
7174
- variant,
7175
- params
7209
+ params,
7210
+ variant
7176
7211
  )
7177
7212
  };
7178
7213
  }
@@ -7185,8 +7220,8 @@ function transformSwellProduct(params, product) {
7185
7220
  const newProduct = {
7186
7221
  ...product,
7187
7222
  // add swell properties there
7188
- price: calculateAddOptionsPrice(product, params),
7189
- selected_option_values: getSelectedOptionValues(product, params)
7223
+ selected_option_values: getSelectedVariantOptionValues(product, params),
7224
+ purchase_options: getPurchaseOptions(product, params)
7190
7225
  };
7191
7226
  if (Array.isArray(newProduct.variants?.results)) {
7192
7227
  newProduct.variants = {
@@ -7543,14 +7578,10 @@ var Swell = class _Swell {
7543
7578
  }
7544
7579
  };
7545
7580
  function getCacheKey(key, args) {
7546
- let cacheKey = key;
7547
7581
  if (Array.isArray(args) && args.length > 0) {
7548
- cacheKey += `_${JSON.stringify(args)}`;
7549
- }
7550
- if (cacheKey.length > 512) {
7551
- cacheKey = `${cacheKey.slice(0, 480)}${md5(cacheKey)}`;
7582
+ return `${key}_${md5(JSON.stringify(args))}`;
7552
7583
  }
7553
- return cacheKey;
7584
+ return key;
7554
7585
  }
7555
7586
  var SwellBackendAPI = class {
7556
7587
  apiHost = DEFAULT_API_HOST;
@@ -8058,11 +8089,7 @@ function getAllSectionComponentTemplates(allSections) {
8058
8089
  );
8059
8090
  }
8060
8091
  }
8061
- return list.filter((template) => {
8062
- return Object.keys(template.entry).some(
8063
- (key) => key !== "_id" && key !== "_component"
8064
- );
8065
- });
8092
+ return list;
8066
8093
  }
8067
8094
  function getEasyblocksPagePropsWithConfigs(themeGlobals, allSections, pageSections, layoutSectionGroups, pageId) {
8068
8095
  const rootComponent = {
@@ -14073,29 +14100,23 @@ function ShopifyProduct(instance, product, depth = 0) {
14073
14100
  if (!Array.isArray(product2.options)) {
14074
14101
  return {};
14075
14102
  }
14076
- let index = 0;
14103
+ const { queryParams } = instance.swell;
14104
+ const variants = getAvailableVariants(product2);
14105
+ const variant = getSelectedVariant(product2, queryParams);
14077
14106
  return product2.options.reduce(
14078
- (acc, option) => {
14079
- if (option.active && option.name) {
14080
- acc[option.name.toLowerCase()] = {
14081
- name: option.name,
14082
- position: ++index,
14083
- selected_value: void 0,
14084
- // variant_option: option.variant,
14085
- values: option.values?.map(
14086
- (value) => ShopifyProductOptionValue({
14087
- available: true,
14088
- id: value.id,
14089
- name: value.name,
14090
- product_url: void 0,
14091
- selected: false,
14092
- swatch: void 0,
14093
- variant: void 0
14094
- // addPrice: value.price,
14095
- })
14096
- ) ?? []
14097
- };
14107
+ (acc, option, index) => {
14108
+ if (!option.active || !option.name) {
14109
+ return acc;
14098
14110
  }
14111
+ acc[option.name.toLowerCase()] = getOption(
14112
+ option,
14113
+ index,
14114
+ product2,
14115
+ instance,
14116
+ depth,
14117
+ variants,
14118
+ variant
14119
+ );
14099
14120
  return acc;
14100
14121
  },
14101
14122
  {}
@@ -14107,33 +14128,20 @@ function ShopifyProduct(instance, product, depth = 0) {
14107
14128
  if (!Array.isArray(product2.options)) {
14108
14129
  return [];
14109
14130
  }
14110
- const variant = getSelectedVariant(product2, instance.swell.queryParams);
14111
- const optionValues = variant?.option_value_ids || [];
14112
- return product2.options.filter((option) => option.active && option.name).map((option, index) => {
14113
- return {
14114
- name: option.name,
14115
- position: index + 1,
14116
- selected_value: void 0,
14117
- // variant_option: option.variant,
14118
- values: option.values?.map(
14119
- (value) => ShopifyProductOptionValue({
14120
- available: true,
14121
- id: value.id,
14122
- name: value.name,
14123
- product_url: void 0,
14124
- selected: optionValues.includes(value.id),
14125
- swatch: void 0,
14126
- variant: ShopifyVariant(
14127
- instance,
14128
- variant || product2,
14129
- product2,
14130
- depth + 1
14131
- )
14132
- // addPrice: value.price,
14133
- })
14134
- )
14135
- };
14136
- });
14131
+ const { queryParams } = instance.swell;
14132
+ const variants = getAvailableVariants(product2);
14133
+ const variant = getSelectedVariant(product2, queryParams);
14134
+ return product2.options.filter((option) => option.active && option.name).map(
14135
+ (option, index) => getOption(
14136
+ option,
14137
+ index,
14138
+ product2,
14139
+ instance,
14140
+ depth,
14141
+ variants,
14142
+ variant
14143
+ )
14144
+ );
14137
14145
  }
14138
14146
  ),
14139
14147
  price: deferWith(product, (product2) => product2.price),
@@ -14234,6 +14242,38 @@ function ShopifyProductOptionValue(values) {
14234
14242
  function isLikeShopifyProduct(value) {
14235
14243
  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");
14236
14244
  }
14245
+ function getOption(option, index, product, instance, depth, variants, variant) {
14246
+ const { queryParams } = instance.swell;
14247
+ return {
14248
+ ...option,
14249
+ name: option.name,
14250
+ position: index + 1,
14251
+ selected_value: void 0,
14252
+ values: option.values?.map(
14253
+ (value) => ShopifyProductOptionValue({
14254
+ ...value,
14255
+ available: isOptionValueAvailable(option, value, product, variants),
14256
+ id: value.id,
14257
+ name: value.name,
14258
+ product_url: void 0,
14259
+ selected: isOptionValueSelected(
14260
+ option,
14261
+ value,
14262
+ product,
14263
+ queryParams,
14264
+ variant
14265
+ ),
14266
+ swatch: void 0,
14267
+ variant: ShopifyVariant(
14268
+ instance,
14269
+ variant || product,
14270
+ product,
14271
+ depth + 1
14272
+ )
14273
+ })
14274
+ )
14275
+ };
14276
+ }
14237
14277
 
14238
14278
  // src/compatibility/shopify-objects/line_item.ts
14239
14279
  function ShopifyLineItem(instance, item, cart, options = {}) {
@@ -14348,26 +14388,8 @@ function isTrialSubscriptionItem(item) {
14348
14388
  function resolveSubscription(item) {
14349
14389
  const purchaseOption = item?.purchase_option;
14350
14390
  if (purchaseOption?.type !== "subscription") {
14351
- return void 0;
14352
- }
14353
- const trialDays = purchaseOption.billing_schedule?.trial_days || 0;
14354
- const trialText = trialDays > 0 ? ` (Includes ${trialDays} trial day${trialDays === 1 ? "" : "s"})` : "";
14355
- const intervalCount = purchaseOption.billing_schedule?.interval_count || 1;
14356
- let intervalText = "day";
14357
- switch (purchaseOption.billing_schedule?.interval) {
14358
- case "weekly":
14359
- intervalText = "wk";
14360
- break;
14361
- case "monthly":
14362
- intervalText = "mo";
14363
- break;
14364
- case "yearly":
14365
- intervalText = "yr";
14366
- break;
14367
- default:
14391
+ return;
14368
14392
  }
14369
- const periodText = `${intervalCount > 1 ? intervalCount : ""}${intervalText}`;
14370
- const text = `${periodText}${trialText}`;
14371
14393
  return {
14372
14394
  checkout_charge_amount: item.price,
14373
14395
  compare_at_price: item.price,
@@ -14377,14 +14399,11 @@ function resolveSubscription(item) {
14377
14399
  remaining_balance_charge_amount: 0,
14378
14400
  selling_plan_group_id: purchaseOption.plan_id,
14379
14401
  selling_plan: {
14380
- id: 0,
14402
+ id: purchaseOption.plan_id,
14381
14403
  group_id: purchaseOption.plan_id,
14382
14404
  name: purchaseOption.plan_name,
14383
14405
  description: purchaseOption.plan_description,
14384
- // billing_schedule: purchaseOption.billing_schedule,
14385
14406
  options: [],
14386
- // provide as separate parts to properly render currency
14387
- // planPriceText: text,
14388
14407
  checkout_charge: { value: item.price, value_type: "price" },
14389
14408
  recurring_deliveries: item.delivery === "shipment",
14390
14409
  price_adjustments: [],
@@ -14509,10 +14528,7 @@ function ShopifyCart(instance, cart) {
14509
14528
  }),
14510
14529
  items_subtotal_price: defer(() => cart.sub_total),
14511
14530
  note: defer(() => cart.comments),
14512
- original_total_price: deferWith(
14513
- cart,
14514
- (cart2) => cart2.sub_total + cart2.item_discount
14515
- ),
14531
+ original_total_price: deferWith(cart, (cart2) => cart2.capture_total),
14516
14532
  requires_shipping: defer(() => Boolean(cart.shipment_delivery)),
14517
14533
  taxes_included: defer(() => Boolean(cart.item_tax_included)),
14518
14534
  total_discount: defer(() => cart.discount_total),
@@ -19012,6 +19028,7 @@ var SwellTheme3 = class {
19012
19028
  current: Number(this.swell.queryParams.page) || 1,
19013
19029
  url: this.swell.url.pathname,
19014
19030
  custom: !swellPage,
19031
+ title: swellPage?.label,
19015
19032
  slug: void 0,
19016
19033
  description: void 0,
19017
19034
  $locale: void 0
@@ -19031,9 +19048,17 @@ var SwellTheme3 = class {
19031
19048
  console.warn(err);
19032
19049
  }
19033
19050
  if (pageSchema?.page) {
19034
- const { slug, label, description, $locale } = pageSchema.page;
19051
+ const {
19052
+ title,
19053
+ label,
19054
+ // 'label' is deprecated, kept for compatibility
19055
+ description,
19056
+ slug,
19057
+ $locale
19058
+ } = pageSchema.page;
19059
+ page.label = page.label || title || label || "";
19060
+ page.title = title || page.label;
19035
19061
  page.slug = slug;
19036
- page.label = label || page.label;
19037
19062
  page.description = description;
19038
19063
  page.$locale = $locale;
19039
19064
  }
@@ -19750,9 +19775,9 @@ ${content.slice(pos)}`;
19750
19775
  this.setGlobals(pageData);
19751
19776
  }
19752
19777
  let pageConfig;
19753
- if (this.page?.id) {
19778
+ if (this.pageId) {
19754
19779
  pageConfig = await this.renderPageTemplate(
19755
- this.page.id,
19780
+ this.pageId,
19756
19781
  pageData,
19757
19782
  altTemplateId
19758
19783
  );