@swell/apps-sdk 1.0.143 → 1.0.145

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
@@ -515,6 +515,12 @@ async function forEachKeyDeep(obj, fn) {
515
515
  }
516
516
 
517
517
  // src/resources.ts
518
+ var NOT_CACHEBLE_COLLECTIONS = Object.freeze(
519
+ /* @__PURE__ */ new Set(["accounts:addresses", "accounts:orders", "accounts:subscriptions"])
520
+ );
521
+ function isResourceCacheble(name) {
522
+ return !NOT_CACHEBLE_COLLECTIONS.has(name);
523
+ }
518
524
  var MAX_QUERY_PAGE_LIMIT = 100;
519
525
  var DEFAULT_QUERY_PAGE_LIMIT = 15;
520
526
  var StorefrontResource = class {
@@ -801,7 +807,8 @@ var SwellStorefrontCollection = class _SwellStorefrontCollection extends SwellSt
801
807
  this._swell.queryParams,
802
808
  this._getterHash
803
809
  ],
804
- getter
810
+ getter,
811
+ isResourceCacheble(this._collection)
805
812
  ).then((result) => {
806
813
  this._result = result;
807
814
  if (result) {
@@ -915,7 +922,8 @@ var SwellStorefrontRecord = class extends SwellStorefrontResource {
915
922
  this._swell.queryParams,
916
923
  this._getterHash
917
924
  ],
918
- getter
925
+ getter,
926
+ isResourceCacheble(this._collection)
919
927
  ).then((result) => {
920
928
  return this._transformResult(result);
921
929
  }).then((result) => {
@@ -7082,16 +7090,18 @@ var Cache = class {
7082
7090
  *
7083
7091
  * This will always return the cached value immediately if exists
7084
7092
  */
7085
- async fetchSWR(key, fetchFn, ttl = DEFAULT_SWR_TTL) {
7093
+ async fetchSWR(key, fetchFn, ttl = DEFAULT_SWR_TTL, isCacheble = true) {
7086
7094
  const trace = createTraceId();
7087
7095
  logger.debug("[SDK] Cache fetch start", { key, trace });
7088
- const cacheValue = await this.client.get(key);
7096
+ const cacheValue = isCacheble ? await this.client.get(key) : void 0;
7089
7097
  let promise = SWR_PROMISE_MAP.get(key);
7090
7098
  if (promise === void 0) {
7091
7099
  promise = Promise.resolve().then(fetchFn).then(resolveAsyncResources).then(async (value) => {
7092
7100
  const isNull = value === null || value === void 0;
7093
- await this.client.set(key, isNull ? NULL_VALUE : value, ttl);
7094
- logger.debug("[SDK] Cache update done", { key, trace });
7101
+ if (isCacheble) {
7102
+ await this.client.set(key, isNull ? NULL_VALUE : value, ttl);
7103
+ logger.debug("[SDK] Cache update done", { key, trace });
7104
+ }
7095
7105
  return value;
7096
7106
  }).finally(() => {
7097
7107
  SWR_PROMISE_MAP.delete(key);
@@ -7242,9 +7252,25 @@ var SwellPage = class extends SwellStorefrontRecord {
7242
7252
  };
7243
7253
 
7244
7254
  // src/resources/product_helpers.ts
7255
+ function isGiftcard(product) {
7256
+ return product.type === "giftcard";
7257
+ }
7258
+ function isOptionAvailable(product, option) {
7259
+ if (isGiftcard(product)) {
7260
+ return true;
7261
+ }
7262
+ return Boolean(option.active && option.name);
7263
+ }
7264
+ function isProductAvailable(product, variant) {
7265
+ if (product.stock_purchasable) {
7266
+ return true;
7267
+ }
7268
+ const stockStatus = (variant || product).stock_status;
7269
+ return !stockStatus || stockStatus === "in_stock";
7270
+ }
7245
7271
  function getAvailableVariants(product) {
7246
7272
  return (product.variants?.results?.slice()?.reverse() || []).filter(
7247
- (variant) => variant.stock_status === "in_stock" || !variant.stock_status
7273
+ (variant) => isProductAvailable(product, variant)
7248
7274
  );
7249
7275
  }
7250
7276
  function isOptionValueAvailable(option, value, product, availableVariants) {
@@ -7311,7 +7337,7 @@ function getSelectedVariantOptionValues(product, queryParams, variant) {
7311
7337
  return acc;
7312
7338
  }
7313
7339
  const hasOptionValues = option.values.length > 0;
7314
- if (!option.active || !hasOptionValues) {
7340
+ if (!isOptionAvailable(product, option) || !hasOptionValues) {
7315
7341
  return acc;
7316
7342
  }
7317
7343
  const value = option.values.find(
@@ -7597,9 +7623,14 @@ var Swell = class _Swell {
7597
7623
  * Fetches a resource.
7598
7624
  * First attempts to fetch from cache.
7599
7625
  */
7600
- async getCachedResource(key, args, handler) {
7626
+ async getCachedResource(key, args, handler, isCacheble = true) {
7601
7627
  const cacheKey = getCacheKey(key, [this.instanceId, args]);
7602
- return this.getResourceCache().fetchSWR(cacheKey, handler);
7628
+ return this.getResourceCache().fetchSWR(
7629
+ cacheKey,
7630
+ handler,
7631
+ void 0,
7632
+ isCacheble
7633
+ );
7603
7634
  }
7604
7635
  async getAppSettings() {
7605
7636
  const settings = await this.get(
@@ -7611,15 +7642,16 @@ var Swell = class _Swell {
7611
7642
  return settings || {};
7612
7643
  }
7613
7644
  async getStorefrontSettings(force = false) {
7645
+ const storefrontSettings = this.storefront.settings;
7614
7646
  try {
7615
- const { settings, menus, payments, subscriptions, session } = await this.storefront.request(
7647
+ const allSettings = await this.storefront.request(
7616
7648
  "get",
7617
7649
  "/settings/all",
7618
7650
  void 0,
7619
7651
  force ? { $cache: false } : void 0,
7620
7652
  { force }
7621
7653
  );
7622
- const storefrontSettings = this.storefront.settings;
7654
+ const { settings, menus, payments, subscriptions, session } = allSettings;
7623
7655
  storefrontSettings.localizedState = {};
7624
7656
  storefrontSettings.set({
7625
7657
  value: settings
@@ -7647,7 +7679,7 @@ var Swell = class _Swell {
7647
7679
  }
7648
7680
  logger.error(err);
7649
7681
  }
7650
- return this.storefront.settings.get();
7682
+ return storefrontSettings;
7651
7683
  }
7652
7684
  getStorefrontMenus() {
7653
7685
  const menus = this.storefront.settings.getState(
@@ -14123,8 +14155,8 @@ function ShopifyVariant(instance, variant, productIn, depth = 0) {
14123
14155
  return new ShopifyResource({
14124
14156
  ...swellVariant,
14125
14157
  available: deferWith(
14126
- variant,
14127
- (variant2) => Boolean(variant2.stock_status === "in_stock" || !variant2.stock_status)
14158
+ [product, variant],
14159
+ (product2, variant2) => isProductAvailable(product2, variant2)
14128
14160
  ),
14129
14161
  barcode: void 0,
14130
14162
  compare_at_price: defer(() => variant.orig_price),
@@ -14213,7 +14245,7 @@ function ShopifyVariant(instance, variant, productIn, depth = 0) {
14213
14245
  requires_selling_plan: false,
14214
14246
  requires_shipping: deferWith(
14215
14247
  product,
14216
- (product2) => Boolean(product2.delivery?.contains("shipment"))
14248
+ (product2) => Boolean(product2.delivery?.includes("shipment"))
14217
14249
  ),
14218
14250
  selected: false,
14219
14251
  selected_selling_plan_allocation: void 0,
@@ -14282,7 +14314,7 @@ function ShopifyProduct(instance, product, depth = 0) {
14282
14314
  return new ShopifyResource({
14283
14315
  available: deferWith(
14284
14316
  product,
14285
- (product2) => product2.stock_status === "in_stock" || !product2.stock_status
14317
+ (product2) => isProductAvailable(product2)
14286
14318
  ),
14287
14319
  collections: [],
14288
14320
  // TODO: need to support this in the resource class somehow
@@ -14307,7 +14339,7 @@ function ShopifyProduct(instance, product, depth = 0) {
14307
14339
  const variant = getSelectedVariant(product2, {});
14308
14340
  return variant ? ShopifyVariant(instance, variant, product2, depth + 1) : void 0;
14309
14341
  }),
14310
- "gift_card?": deferWith(product, (product2) => product2.type === "giftcard"),
14342
+ "gift_card?": deferWith(product, isGiftcard),
14311
14343
  handle: defer(() => product.slug),
14312
14344
  // indicates that product has any options
14313
14345
  has_only_default_variant: deferWith(
@@ -14339,7 +14371,9 @@ function ShopifyProduct(instance, product, depth = 0) {
14339
14371
  if (!Array.isArray(product2.options)) {
14340
14372
  return [];
14341
14373
  }
14342
- return product2.options.filter((option) => option.active && option.name).map((option) => option.name);
14374
+ return product2.options.filter(
14375
+ (option) => isOptionAvailable(product2, option)
14376
+ ).map((option) => option.name);
14343
14377
  }),
14344
14378
  options_by_name: deferWith(product, (product2) => {
14345
14379
  if (!Array.isArray(product2.options)) {
@@ -14350,7 +14384,7 @@ function ShopifyProduct(instance, product, depth = 0) {
14350
14384
  const variant = getSelectedVariant(product2, queryParams);
14351
14385
  return product2.options.reduce(
14352
14386
  (acc, option, index) => {
14353
- if (!option.active || !option.name) {
14387
+ if (!isOptionAvailable(product2, option)) {
14354
14388
  return acc;
14355
14389
  }
14356
14390
  acc[option.name.toLowerCase()] = getOption(
@@ -14376,7 +14410,7 @@ function ShopifyProduct(instance, product, depth = 0) {
14376
14410
  const { queryParams } = instance.swell;
14377
14411
  const variants = getAvailableVariants(product2);
14378
14412
  const variant = getSelectedVariant(product2, queryParams);
14379
- return product2.options.filter((option) => option.active && option.name).map(
14413
+ return product2.options.filter((option) => isOptionAvailable(product2, option)).map(
14380
14414
  (option, index) => getOption(
14381
14415
  option,
14382
14416
  index,
@@ -14549,7 +14583,7 @@ function ShopifyLineItem(instance, item, cart, options = {}) {
14549
14583
  : undefined, */
14550
14584
  fulfillment_service: "manual",
14551
14585
  // TODO
14552
- gift_card: item.delivery === "giftcard",
14586
+ gift_card: isGiftcard(item.product),
14553
14587
  grams: item.shipment_weight,
14554
14588
  id: item.id,
14555
14589
  image: deferWith(
@@ -15079,6 +15113,9 @@ function ShopifyAddress(instance, address, account) {
15079
15113
  if (address instanceof StorefrontResource) {
15080
15114
  address = cloneStorefrontResource(address);
15081
15115
  }
15116
+ if (!address) {
15117
+ address = {};
15118
+ }
15082
15119
  return new ShopifyResource({
15083
15120
  address1: defer(() => address.address1),
15084
15121
  address2: defer(() => address.address2),
@@ -16553,6 +16590,10 @@ ${injects.join("\n")}</script>`;
16553
16590
  pageId = "account/order";
16554
16591
  urlParams.id = segment3;
16555
16592
  break;
16593
+ case "subscriptions":
16594
+ pageId = "account/subscription";
16595
+ urlParams.id = segment3;
16596
+ break;
16556
16597
  case "register":
16557
16598
  pageId = "account/login";
16558
16599
  break;
@@ -19308,14 +19349,15 @@ var SwellTheme3 = class {
19308
19349
  logger.debug("[SDK] Theme init start", { page: pageId, trace });
19309
19350
  await this.themeLoader.init(this.themeConfigs || void 0);
19310
19351
  logger.debug("[SDK] ThemeLoader init done", { page: pageId, trace });
19311
- const { store, session, menus, geo, configs } = await this.getSettingsAndConfigs();
19352
+ const { store, session, menus, geo, configs, storefrontSettings } = await this.getSettingsAndConfigs();
19312
19353
  logger.debug("[SDK] Theme settings load done", { page: pageId, trace });
19313
19354
  const { settings, request, page, cart, account, customer } = await this.resolvePageData(store, configs, pageId, altTemplate);
19314
19355
  logger.debug("[SDK] Theme page data load done", { page: pageId, trace });
19315
19356
  this.page = page;
19316
19357
  const globals = {
19317
19358
  ...this.globalData,
19318
- store,
19359
+ // return all storefront settings in the store
19360
+ store: { ...storefrontSettings, ...store },
19319
19361
  settings,
19320
19362
  session,
19321
19363
  request,
@@ -19381,7 +19423,10 @@ var SwellTheme3 = class {
19381
19423
  {}
19382
19424
  )
19383
19425
  };
19384
- const session = await this.swell.storefront.settings.session();
19426
+ const [session, storeSettings] = await Promise.all([
19427
+ storefrontSettings.session(),
19428
+ storefrontSettings.get()
19429
+ ]);
19385
19430
  if (configs.translations) {
19386
19431
  configs.language = configs.translations;
19387
19432
  }
@@ -19394,11 +19439,13 @@ var SwellTheme3 = class {
19394
19439
  await this.setCompatibilityConfigs(configs);
19395
19440
  const menus = await this.resolveMenuSettings();
19396
19441
  return {
19397
- store: storefrontSettings?.store,
19442
+ store: storeSettings?.store,
19398
19443
  session,
19399
19444
  menus,
19400
19445
  geo,
19401
- configs
19446
+ configs,
19447
+ // all settings
19448
+ storefrontSettings
19402
19449
  };
19403
19450
  }
19404
19451
  async resolvePageData(store, configs, pageId, altTemplate) {
@@ -19487,7 +19534,8 @@ var SwellTheme3 = class {
19487
19534
  this.fetchSingletonResourceCached(
19488
19535
  "account",
19489
19536
  () => this.fetchAccount(),
19490
- () => null
19537
+ () => null,
19538
+ false
19491
19539
  )
19492
19540
  ]);
19493
19541
  if (!cart) {
@@ -19507,7 +19555,7 @@ var SwellTheme3 = class {
19507
19555
  // Shopify only
19508
19556
  };
19509
19557
  }
19510
- async fetchSingletonResourceCached(key, handler, defaultValue) {
19558
+ async fetchSingletonResourceCached(key, handler, defaultValue, isCacheble = true) {
19511
19559
  const cacheKey = this.swell.storefront.session.getCookie();
19512
19560
  if (!cacheKey) {
19513
19561
  return defaultValue();
@@ -19515,7 +19563,8 @@ var SwellTheme3 = class {
19515
19563
  const result = await this.swell.getCachedResource(
19516
19564
  `${key}-${cacheKey}`,
19517
19565
  [],
19518
- handler
19566
+ handler,
19567
+ isCacheble
19519
19568
  );
19520
19569
  return result ?? defaultValue();
19521
19570
  }
@@ -21046,6 +21095,7 @@ function createStorefrontRecord(resource, swell, path, parent_slug, parent_query
21046
21095
  }
21047
21096
  function createCollection(resource, swell, path, parent_slug, parent_query) {
21048
21097
  const query = getResourceQuery(parent_slug, parent_query);
21098
+ query.$resource_path = path;
21049
21099
  return new SwellStorefrontCollection(
21050
21100
  swell,
21051
21101
  resource,