@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.cjs CHANGED
@@ -7179,108 +7179,144 @@ var ThemeCache = class extends Cache {
7179
7179
  };
7180
7180
 
7181
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
7182
  function getAvailableVariants(product) {
7228
7183
  return (product.variants?.results?.slice()?.reverse() || []).filter(
7229
7184
  (variant) => variant.stock_status === "in_stock" || !variant.stock_status
7230
7185
  );
7231
7186
  }
7232
- function getSelectedSwellVariant(product, queryParams) {
7233
- const { variant: queryVariant, option_values } = queryParams;
7234
- const queryOptionValues = option_values;
7187
+ function isOptionValueAvailable(option, value, product, availableVariants) {
7188
+ if (!option.variant) {
7189
+ return true;
7190
+ }
7191
+ const hasVariants = product.variants?.results.length > 0;
7192
+ if (!hasVariants) {
7193
+ return true;
7194
+ }
7195
+ const variants = availableVariants || getAvailableVariants(product);
7196
+ return variants.some(
7197
+ (variant) => variant.option_value_ids.includes(value.id)
7198
+ );
7199
+ }
7200
+ function isOptionValueSelected(option, value, product, queryParams, selectedVariant) {
7201
+ let variant;
7202
+ if (option.variant) {
7203
+ variant = selectedVariant || getSelectedVariant(product, queryParams);
7204
+ }
7205
+ const selectedOptionValues = getSelectedVariantOptionValues(
7206
+ product,
7207
+ queryParams,
7208
+ variant
7209
+ );
7210
+ return selectedOptionValues.includes(value.id);
7211
+ }
7212
+ function getSelectedVariant(product, queryParams) {
7213
+ const {
7214
+ variant: queryVariant,
7215
+ option_values: queryOptionValues,
7216
+ selected_option_value: selectedOptionValue
7217
+ } = queryParams;
7235
7218
  const variants = getAvailableVariants(product);
7236
7219
  let selectedVariant = void 0;
7237
7220
  if (queryVariant) {
7238
7221
  selectedVariant = variants.find((variant) => variant.id === queryVariant);
7239
7222
  } else if (queryOptionValues) {
7240
7223
  const optionValues = queryOptionValues.split(",");
7241
- selectedVariant = variants.find(
7242
- (variant) => variant.option_value_ids.every(
7224
+ selectedVariant = variants.find((variant) => {
7225
+ if (variant.option_value_ids.length !== optionValues.length) {
7226
+ return false;
7227
+ }
7228
+ return variant.option_value_ids.every(
7243
7229
  (optionValueId) => optionValues.includes(optionValueId)
7244
- )
7245
- );
7230
+ );
7231
+ });
7232
+ if (!selectedVariant && selectedOptionValue) {
7233
+ selectedVariant = variants.filter(
7234
+ (variant) => variant.option_value_ids.includes(selectedOptionValue)
7235
+ )[0];
7236
+ }
7246
7237
  }
7247
7238
  return selectedVariant || variants?.[0] || void 0;
7248
7239
  }
7249
- function getSelectedVariant(product, queryParams) {
7250
- return getSelectedSwellVariant(
7240
+ function getSelectedVariantOptionValues(product, queryParams, variant) {
7241
+ if (variant) {
7242
+ return variant.option_value_ids;
7243
+ }
7244
+ const { option_values: queryOptionValues = "" } = queryParams;
7245
+ const optionValues = queryOptionValues.split(",");
7246
+ return (product.options || []).reduce((acc, option) => {
7247
+ if (!option.values) {
7248
+ return acc;
7249
+ }
7250
+ const hasOptionValues = option.values.length > 0;
7251
+ if (!option.active || !hasOptionValues) {
7252
+ return acc;
7253
+ }
7254
+ const value = option.values.find(
7255
+ (value2) => optionValues.includes(value2.id)
7256
+ );
7257
+ if (value) {
7258
+ acc.push(value.id);
7259
+ }
7260
+ return acc;
7261
+ }, []);
7262
+ }
7263
+ function getPurchaseOptions(product, queryParams) {
7264
+ if (!product?.purchase_options) {
7265
+ return null;
7266
+ }
7267
+ const { standard, subscription } = product.purchase_options;
7268
+ const selectedPurchaseOptionType = getSelectedPurchaseOptionType(
7251
7269
  product,
7252
7270
  queryParams
7253
7271
  );
7272
+ const purchaseOptions = {};
7273
+ if (standard) {
7274
+ purchaseOptions.standard = {
7275
+ ...standard,
7276
+ selected: selectedPurchaseOptionType === "standard"
7277
+ };
7278
+ }
7279
+ if (subscription) {
7280
+ const selectedPlan = getSelectedSubscriptionPurchaseOptionPlan(
7281
+ selectedPurchaseOptionType,
7282
+ subscription,
7283
+ queryParams
7284
+ );
7285
+ purchaseOptions.subscription = {
7286
+ ...subscription,
7287
+ selected: selectedPurchaseOptionType === "subscription",
7288
+ plans: subscription.plans.map((plan) => ({
7289
+ ...plan,
7290
+ selected: selectedPlan ? plan.id === selectedPlan.id : false
7291
+ }))
7292
+ };
7293
+ }
7294
+ return Object.keys(purchaseOptions).length > 0 ? purchaseOptions : null;
7254
7295
  }
7255
- function getSelectedOptionValues(product, queryParams) {
7256
- const variant = getSelectedSwellVariant(product, queryParams);
7257
- return getSelectedVariantOptionValues(product, variant, queryParams);
7296
+ function getSelectedPurchaseOptionType(product, queryParams) {
7297
+ const { purchase_options: purchaseOptions } = product;
7298
+ if (!purchaseOptions) {
7299
+ return null;
7300
+ }
7301
+ const { purchase_option: purchaseOption } = queryParams;
7302
+ const purchaseOptionType = purchaseOption?.type;
7303
+ if (purchaseOptionType && purchaseOptionType in purchaseOptions) {
7304
+ return purchaseOptionType;
7305
+ }
7306
+ return purchaseOptions.standard ? "standard" : "subscription";
7258
7307
  }
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
- }
7308
+ function getSelectedSubscriptionPurchaseOptionPlan(selectedPurchaseOptionType, subscriptionPurchaseOption, queryParams) {
7309
+ if (selectedPurchaseOptionType !== "subscription") {
7310
+ return null;
7282
7311
  }
7283
- return values;
7312
+ const { purchase_option: purchaseOption } = queryParams;
7313
+ let selectedPlan = null;
7314
+ if (purchaseOption?.plan_id) {
7315
+ selectedPlan = subscriptionPurchaseOption.plans.find(
7316
+ (plan) => plan.id === purchaseOption.plan_id
7317
+ );
7318
+ }
7319
+ return selectedPlan || subscriptionPurchaseOption.plans[0];
7284
7320
  }
7285
7321
 
7286
7322
  // src/resources/variant.ts
@@ -7294,11 +7330,10 @@ function transformSwellVariant(params, product, variant) {
7294
7330
  return {
7295
7331
  ...variant,
7296
7332
  // add swell properties there
7297
- price: calculateAddOptionsVariantPrice(product, variant, params),
7298
7333
  selected_option_values: getSelectedVariantOptionValues(
7299
7334
  product,
7300
- variant,
7301
- params
7335
+ params,
7336
+ variant
7302
7337
  )
7303
7338
  };
7304
7339
  }
@@ -7311,8 +7346,8 @@ function transformSwellProduct(params, product) {
7311
7346
  const newProduct = {
7312
7347
  ...product,
7313
7348
  // add swell properties there
7314
- price: calculateAddOptionsPrice(product, params),
7315
- selected_option_values: getSelectedOptionValues(product, params)
7349
+ selected_option_values: getSelectedVariantOptionValues(product, params),
7350
+ purchase_options: getPurchaseOptions(product, params)
7316
7351
  };
7317
7352
  if (Array.isArray(newProduct.variants?.results)) {
7318
7353
  newProduct.variants = {
@@ -7669,14 +7704,10 @@ var Swell = class _Swell {
7669
7704
  }
7670
7705
  };
7671
7706
  function getCacheKey(key, args) {
7672
- let cacheKey = key;
7673
7707
  if (Array.isArray(args) && args.length > 0) {
7674
- cacheKey += `_${JSON.stringify(args)}`;
7675
- }
7676
- if (cacheKey.length > 512) {
7677
- cacheKey = `${cacheKey.slice(0, 480)}${md5(cacheKey)}`;
7708
+ return `${key}_${md5(JSON.stringify(args))}`;
7678
7709
  }
7679
- return cacheKey;
7710
+ return key;
7680
7711
  }
7681
7712
  var SwellBackendAPI = class {
7682
7713
  apiHost = DEFAULT_API_HOST;
@@ -8184,11 +8215,7 @@ function getAllSectionComponentTemplates(allSections) {
8184
8215
  );
8185
8216
  }
8186
8217
  }
8187
- return list.filter((template) => {
8188
- return Object.keys(template.entry).some(
8189
- (key) => key !== "_id" && key !== "_component"
8190
- );
8191
- });
8218
+ return list;
8192
8219
  }
8193
8220
  function getEasyblocksPagePropsWithConfigs(themeGlobals, allSections, pageSections, layoutSectionGroups, pageId) {
8194
8221
  const rootComponent = {
@@ -14199,29 +14226,23 @@ function ShopifyProduct(instance, product, depth = 0) {
14199
14226
  if (!Array.isArray(product2.options)) {
14200
14227
  return {};
14201
14228
  }
14202
- let index = 0;
14229
+ const { queryParams } = instance.swell;
14230
+ const variants = getAvailableVariants(product2);
14231
+ const variant = getSelectedVariant(product2, queryParams);
14203
14232
  return product2.options.reduce(
14204
- (acc, option) => {
14205
- if (option.active && option.name) {
14206
- acc[option.name.toLowerCase()] = {
14207
- name: option.name,
14208
- position: ++index,
14209
- selected_value: void 0,
14210
- // variant_option: option.variant,
14211
- values: option.values?.map(
14212
- (value) => ShopifyProductOptionValue({
14213
- available: true,
14214
- id: value.id,
14215
- name: value.name,
14216
- product_url: void 0,
14217
- selected: false,
14218
- swatch: void 0,
14219
- variant: void 0
14220
- // addPrice: value.price,
14221
- })
14222
- ) ?? []
14223
- };
14233
+ (acc, option, index) => {
14234
+ if (!option.active || !option.name) {
14235
+ return acc;
14224
14236
  }
14237
+ acc[option.name.toLowerCase()] = getOption(
14238
+ option,
14239
+ index,
14240
+ product2,
14241
+ instance,
14242
+ depth,
14243
+ variants,
14244
+ variant
14245
+ );
14225
14246
  return acc;
14226
14247
  },
14227
14248
  {}
@@ -14233,33 +14254,20 @@ function ShopifyProduct(instance, product, depth = 0) {
14233
14254
  if (!Array.isArray(product2.options)) {
14234
14255
  return [];
14235
14256
  }
14236
- const variant = getSelectedVariant(product2, instance.swell.queryParams);
14237
- const optionValues = variant?.option_value_ids || [];
14238
- return product2.options.filter((option) => option.active && option.name).map((option, index) => {
14239
- return {
14240
- name: option.name,
14241
- position: index + 1,
14242
- selected_value: void 0,
14243
- // variant_option: option.variant,
14244
- values: option.values?.map(
14245
- (value) => ShopifyProductOptionValue({
14246
- available: true,
14247
- id: value.id,
14248
- name: value.name,
14249
- product_url: void 0,
14250
- selected: optionValues.includes(value.id),
14251
- swatch: void 0,
14252
- variant: ShopifyVariant(
14253
- instance,
14254
- variant || product2,
14255
- product2,
14256
- depth + 1
14257
- )
14258
- // addPrice: value.price,
14259
- })
14260
- )
14261
- };
14262
- });
14257
+ const { queryParams } = instance.swell;
14258
+ const variants = getAvailableVariants(product2);
14259
+ const variant = getSelectedVariant(product2, queryParams);
14260
+ return product2.options.filter((option) => option.active && option.name).map(
14261
+ (option, index) => getOption(
14262
+ option,
14263
+ index,
14264
+ product2,
14265
+ instance,
14266
+ depth,
14267
+ variants,
14268
+ variant
14269
+ )
14270
+ );
14263
14271
  }
14264
14272
  ),
14265
14273
  price: deferWith(product, (product2) => product2.price),
@@ -14360,6 +14368,38 @@ function ShopifyProductOptionValue(values) {
14360
14368
  function isLikeShopifyProduct(value) {
14361
14369
  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");
14362
14370
  }
14371
+ function getOption(option, index, product, instance, depth, variants, variant) {
14372
+ const { queryParams } = instance.swell;
14373
+ return {
14374
+ ...option,
14375
+ name: option.name,
14376
+ position: index + 1,
14377
+ selected_value: void 0,
14378
+ values: option.values?.map(
14379
+ (value) => ShopifyProductOptionValue({
14380
+ ...value,
14381
+ available: isOptionValueAvailable(option, value, product, variants),
14382
+ id: value.id,
14383
+ name: value.name,
14384
+ product_url: void 0,
14385
+ selected: isOptionValueSelected(
14386
+ option,
14387
+ value,
14388
+ product,
14389
+ queryParams,
14390
+ variant
14391
+ ),
14392
+ swatch: void 0,
14393
+ variant: ShopifyVariant(
14394
+ instance,
14395
+ variant || product,
14396
+ product,
14397
+ depth + 1
14398
+ )
14399
+ })
14400
+ )
14401
+ };
14402
+ }
14363
14403
 
14364
14404
  // src/compatibility/shopify-objects/line_item.ts
14365
14405
  function ShopifyLineItem(instance, item, cart, options = {}) {
@@ -14474,26 +14514,8 @@ function isTrialSubscriptionItem(item) {
14474
14514
  function resolveSubscription(item) {
14475
14515
  const purchaseOption = item?.purchase_option;
14476
14516
  if (purchaseOption?.type !== "subscription") {
14477
- return void 0;
14478
- }
14479
- const trialDays = purchaseOption.billing_schedule?.trial_days || 0;
14480
- const trialText = trialDays > 0 ? ` (Includes ${trialDays} trial day${trialDays === 1 ? "" : "s"})` : "";
14481
- const intervalCount = purchaseOption.billing_schedule?.interval_count || 1;
14482
- let intervalText = "day";
14483
- switch (purchaseOption.billing_schedule?.interval) {
14484
- case "weekly":
14485
- intervalText = "wk";
14486
- break;
14487
- case "monthly":
14488
- intervalText = "mo";
14489
- break;
14490
- case "yearly":
14491
- intervalText = "yr";
14492
- break;
14493
- default:
14517
+ return;
14494
14518
  }
14495
- const periodText = `${intervalCount > 1 ? intervalCount : ""}${intervalText}`;
14496
- const text = `${periodText}${trialText}`;
14497
14519
  return {
14498
14520
  checkout_charge_amount: item.price,
14499
14521
  compare_at_price: item.price,
@@ -14503,14 +14525,11 @@ function resolveSubscription(item) {
14503
14525
  remaining_balance_charge_amount: 0,
14504
14526
  selling_plan_group_id: purchaseOption.plan_id,
14505
14527
  selling_plan: {
14506
- id: 0,
14528
+ id: purchaseOption.plan_id,
14507
14529
  group_id: purchaseOption.plan_id,
14508
14530
  name: purchaseOption.plan_name,
14509
14531
  description: purchaseOption.plan_description,
14510
- // billing_schedule: purchaseOption.billing_schedule,
14511
14532
  options: [],
14512
- // provide as separate parts to properly render currency
14513
- // planPriceText: text,
14514
14533
  checkout_charge: { value: item.price, value_type: "price" },
14515
14534
  recurring_deliveries: item.delivery === "shipment",
14516
14535
  price_adjustments: [],
@@ -14635,10 +14654,7 @@ function ShopifyCart(instance, cart) {
14635
14654
  }),
14636
14655
  items_subtotal_price: defer(() => cart.sub_total),
14637
14656
  note: defer(() => cart.comments),
14638
- original_total_price: deferWith(
14639
- cart,
14640
- (cart2) => cart2.sub_total + cart2.item_discount
14641
- ),
14657
+ original_total_price: deferWith(cart, (cart2) => cart2.capture_total),
14642
14658
  requires_shipping: defer(() => Boolean(cart.shipment_delivery)),
14643
14659
  taxes_included: defer(() => Boolean(cart.item_tax_included)),
14644
14660
  total_discount: defer(() => cart.discount_total),
@@ -19130,6 +19146,7 @@ var SwellTheme3 = class {
19130
19146
  current: Number(this.swell.queryParams.page) || 1,
19131
19147
  url: this.swell.url.pathname,
19132
19148
  custom: !swellPage,
19149
+ title: swellPage?.label,
19133
19150
  slug: void 0,
19134
19151
  description: void 0,
19135
19152
  $locale: void 0
@@ -19149,9 +19166,17 @@ var SwellTheme3 = class {
19149
19166
  console.warn(err);
19150
19167
  }
19151
19168
  if (pageSchema?.page) {
19152
- const { slug, label, description, $locale } = pageSchema.page;
19169
+ const {
19170
+ title,
19171
+ label,
19172
+ // 'label' is deprecated, kept for compatibility
19173
+ description,
19174
+ slug,
19175
+ $locale
19176
+ } = pageSchema.page;
19177
+ page.label = page.label || title || label || "";
19178
+ page.title = title || page.label;
19153
19179
  page.slug = slug;
19154
- page.label = label || page.label;
19155
19180
  page.description = description;
19156
19181
  page.$locale = $locale;
19157
19182
  }
@@ -19868,9 +19893,9 @@ ${content.slice(pos)}`;
19868
19893
  this.setGlobals(pageData);
19869
19894
  }
19870
19895
  let pageConfig;
19871
- if (this.page?.id) {
19896
+ if (this.pageId) {
19872
19897
  pageConfig = await this.renderPageTemplate(
19873
- this.page.id,
19898
+ this.pageId,
19874
19899
  pageData,
19875
19900
  altTemplateId
19876
19901
  );