@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.js CHANGED
@@ -542,6 +542,12 @@
542
542
  }
543
543
 
544
544
  // src/resources.ts
545
+ var NOT_CACHEBLE_COLLECTIONS = Object.freeze(
546
+ /* @__PURE__ */ new Set(["accounts:addresses", "accounts:orders", "accounts:subscriptions"])
547
+ );
548
+ function isResourceCacheble(name) {
549
+ return !NOT_CACHEBLE_COLLECTIONS.has(name);
550
+ }
545
551
  var MAX_QUERY_PAGE_LIMIT = 100;
546
552
  var DEFAULT_QUERY_PAGE_LIMIT = 15;
547
553
  var StorefrontResource = class {
@@ -828,7 +834,8 @@
828
834
  this._swell.queryParams,
829
835
  this._getterHash
830
836
  ],
831
- getter
837
+ getter,
838
+ isResourceCacheble(this._collection)
832
839
  ).then((result) => {
833
840
  this._result = result;
834
841
  if (result) {
@@ -942,7 +949,8 @@
942
949
  this._swell.queryParams,
943
950
  this._getterHash
944
951
  ],
945
- getter
952
+ getter,
953
+ isResourceCacheble(this._collection)
946
954
  ).then((result) => {
947
955
  return this._transformResult(result);
948
956
  }).then((result) => {
@@ -7109,16 +7117,18 @@
7109
7117
  *
7110
7118
  * This will always return the cached value immediately if exists
7111
7119
  */
7112
- async fetchSWR(key, fetchFn, ttl = DEFAULT_SWR_TTL) {
7120
+ async fetchSWR(key, fetchFn, ttl = DEFAULT_SWR_TTL, isCacheble = true) {
7113
7121
  const trace = createTraceId();
7114
7122
  logger.debug("[SDK] Cache fetch start", { key, trace });
7115
- const cacheValue = await this.client.get(key);
7123
+ const cacheValue = isCacheble ? await this.client.get(key) : void 0;
7116
7124
  let promise = SWR_PROMISE_MAP.get(key);
7117
7125
  if (promise === void 0) {
7118
7126
  promise = Promise.resolve().then(fetchFn).then(resolveAsyncResources).then(async (value) => {
7119
7127
  const isNull = value === null || value === void 0;
7120
- await this.client.set(key, isNull ? NULL_VALUE : value, ttl);
7121
- logger.debug("[SDK] Cache update done", { key, trace });
7128
+ if (isCacheble) {
7129
+ await this.client.set(key, isNull ? NULL_VALUE : value, ttl);
7130
+ logger.debug("[SDK] Cache update done", { key, trace });
7131
+ }
7122
7132
  return value;
7123
7133
  }).finally(() => {
7124
7134
  SWR_PROMISE_MAP.delete(key);
@@ -7269,9 +7279,25 @@
7269
7279
  };
7270
7280
 
7271
7281
  // src/resources/product_helpers.ts
7282
+ function isGiftcard(product) {
7283
+ return product.type === "giftcard";
7284
+ }
7285
+ function isOptionAvailable(product, option) {
7286
+ if (isGiftcard(product)) {
7287
+ return true;
7288
+ }
7289
+ return Boolean(option.active && option.name);
7290
+ }
7291
+ function isProductAvailable(product, variant) {
7292
+ if (product.stock_purchasable) {
7293
+ return true;
7294
+ }
7295
+ const stockStatus = (variant || product).stock_status;
7296
+ return !stockStatus || stockStatus === "in_stock";
7297
+ }
7272
7298
  function getAvailableVariants(product) {
7273
7299
  return (product.variants?.results?.slice()?.reverse() || []).filter(
7274
- (variant) => variant.stock_status === "in_stock" || !variant.stock_status
7300
+ (variant) => isProductAvailable(product, variant)
7275
7301
  );
7276
7302
  }
7277
7303
  function isOptionValueAvailable(option, value, product, availableVariants) {
@@ -7338,7 +7364,7 @@
7338
7364
  return acc;
7339
7365
  }
7340
7366
  const hasOptionValues = option.values.length > 0;
7341
- if (!option.active || !hasOptionValues) {
7367
+ if (!isOptionAvailable(product, option) || !hasOptionValues) {
7342
7368
  return acc;
7343
7369
  }
7344
7370
  const value = option.values.find(
@@ -7624,9 +7650,14 @@
7624
7650
  * Fetches a resource.
7625
7651
  * First attempts to fetch from cache.
7626
7652
  */
7627
- async getCachedResource(key, args, handler) {
7653
+ async getCachedResource(key, args, handler, isCacheble = true) {
7628
7654
  const cacheKey = getCacheKey(key, [this.instanceId, args]);
7629
- return this.getResourceCache().fetchSWR(cacheKey, handler);
7655
+ return this.getResourceCache().fetchSWR(
7656
+ cacheKey,
7657
+ handler,
7658
+ void 0,
7659
+ isCacheble
7660
+ );
7630
7661
  }
7631
7662
  async getAppSettings() {
7632
7663
  const settings = await this.get(
@@ -7638,15 +7669,16 @@
7638
7669
  return settings || {};
7639
7670
  }
7640
7671
  async getStorefrontSettings(force = false) {
7672
+ const storefrontSettings = this.storefront.settings;
7641
7673
  try {
7642
- const { settings, menus, payments, subscriptions, session } = await this.storefront.request(
7674
+ const allSettings = await this.storefront.request(
7643
7675
  "get",
7644
7676
  "/settings/all",
7645
7677
  void 0,
7646
7678
  force ? { $cache: false } : void 0,
7647
7679
  { force }
7648
7680
  );
7649
- const storefrontSettings = this.storefront.settings;
7681
+ const { settings, menus, payments, subscriptions, session } = allSettings;
7650
7682
  storefrontSettings.localizedState = {};
7651
7683
  storefrontSettings.set({
7652
7684
  value: settings
@@ -7674,7 +7706,7 @@
7674
7706
  }
7675
7707
  logger.error(err);
7676
7708
  }
7677
- return this.storefront.settings.get();
7709
+ return storefrontSettings;
7678
7710
  }
7679
7711
  getStorefrontMenus() {
7680
7712
  const menus = this.storefront.settings.getState(
@@ -14150,8 +14182,8 @@ ${formattedMessage}`;
14150
14182
  return new ShopifyResource({
14151
14183
  ...swellVariant,
14152
14184
  available: deferWith(
14153
- variant,
14154
- (variant2) => Boolean(variant2.stock_status === "in_stock" || !variant2.stock_status)
14185
+ [product, variant],
14186
+ (product2, variant2) => isProductAvailable(product2, variant2)
14155
14187
  ),
14156
14188
  barcode: void 0,
14157
14189
  compare_at_price: defer(() => variant.orig_price),
@@ -14240,7 +14272,7 @@ ${formattedMessage}`;
14240
14272
  requires_selling_plan: false,
14241
14273
  requires_shipping: deferWith(
14242
14274
  product,
14243
- (product2) => Boolean(product2.delivery?.contains("shipment"))
14275
+ (product2) => Boolean(product2.delivery?.includes("shipment"))
14244
14276
  ),
14245
14277
  selected: false,
14246
14278
  selected_selling_plan_allocation: void 0,
@@ -14309,7 +14341,7 @@ ${formattedMessage}`;
14309
14341
  return new ShopifyResource({
14310
14342
  available: deferWith(
14311
14343
  product,
14312
- (product2) => product2.stock_status === "in_stock" || !product2.stock_status
14344
+ (product2) => isProductAvailable(product2)
14313
14345
  ),
14314
14346
  collections: [],
14315
14347
  // TODO: need to support this in the resource class somehow
@@ -14334,7 +14366,7 @@ ${formattedMessage}`;
14334
14366
  const variant = getSelectedVariant(product2, {});
14335
14367
  return variant ? ShopifyVariant(instance, variant, product2, depth + 1) : void 0;
14336
14368
  }),
14337
- "gift_card?": deferWith(product, (product2) => product2.type === "giftcard"),
14369
+ "gift_card?": deferWith(product, isGiftcard),
14338
14370
  handle: defer(() => product.slug),
14339
14371
  // indicates that product has any options
14340
14372
  has_only_default_variant: deferWith(
@@ -14366,7 +14398,9 @@ ${formattedMessage}`;
14366
14398
  if (!Array.isArray(product2.options)) {
14367
14399
  return [];
14368
14400
  }
14369
- return product2.options.filter((option) => option.active && option.name).map((option) => option.name);
14401
+ return product2.options.filter(
14402
+ (option) => isOptionAvailable(product2, option)
14403
+ ).map((option) => option.name);
14370
14404
  }),
14371
14405
  options_by_name: deferWith(product, (product2) => {
14372
14406
  if (!Array.isArray(product2.options)) {
@@ -14377,7 +14411,7 @@ ${formattedMessage}`;
14377
14411
  const variant = getSelectedVariant(product2, queryParams);
14378
14412
  return product2.options.reduce(
14379
14413
  (acc, option, index) => {
14380
- if (!option.active || !option.name) {
14414
+ if (!isOptionAvailable(product2, option)) {
14381
14415
  return acc;
14382
14416
  }
14383
14417
  acc[option.name.toLowerCase()] = getOption(
@@ -14403,7 +14437,7 @@ ${formattedMessage}`;
14403
14437
  const { queryParams } = instance.swell;
14404
14438
  const variants = getAvailableVariants(product2);
14405
14439
  const variant = getSelectedVariant(product2, queryParams);
14406
- return product2.options.filter((option) => option.active && option.name).map(
14440
+ return product2.options.filter((option) => isOptionAvailable(product2, option)).map(
14407
14441
  (option, index) => getOption(
14408
14442
  option,
14409
14443
  index,
@@ -14576,7 +14610,7 @@ ${formattedMessage}`;
14576
14610
  : undefined, */
14577
14611
  fulfillment_service: "manual",
14578
14612
  // TODO
14579
- gift_card: item.delivery === "giftcard",
14613
+ gift_card: isGiftcard(item.product),
14580
14614
  grams: item.shipment_weight,
14581
14615
  id: item.id,
14582
14616
  image: deferWith(
@@ -15106,6 +15140,9 @@ ${formattedMessage}`;
15106
15140
  if (address instanceof StorefrontResource) {
15107
15141
  address = cloneStorefrontResource(address);
15108
15142
  }
15143
+ if (!address) {
15144
+ address = {};
15145
+ }
15109
15146
  return new ShopifyResource({
15110
15147
  address1: defer(() => address.address1),
15111
15148
  address2: defer(() => address.address2),
@@ -16580,6 +16617,10 @@ ${injects.join("\n")}<\/script>`;
16580
16617
  pageId = "account/order";
16581
16618
  urlParams.id = segment3;
16582
16619
  break;
16620
+ case "subscriptions":
16621
+ pageId = "account/subscription";
16622
+ urlParams.id = segment3;
16623
+ break;
16583
16624
  case "register":
16584
16625
  pageId = "account/login";
16585
16626
  break;
@@ -19327,14 +19368,15 @@ ${injects.join("\n")}<\/script>`;
19327
19368
  logger.debug("[SDK] Theme init start", { page: pageId, trace });
19328
19369
  await this.themeLoader.init(this.themeConfigs || void 0);
19329
19370
  logger.debug("[SDK] ThemeLoader init done", { page: pageId, trace });
19330
- const { store, session, menus, geo, configs } = await this.getSettingsAndConfigs();
19371
+ const { store, session, menus, geo, configs, storefrontSettings } = await this.getSettingsAndConfigs();
19331
19372
  logger.debug("[SDK] Theme settings load done", { page: pageId, trace });
19332
19373
  const { settings, request, page, cart, account, customer } = await this.resolvePageData(store, configs, pageId, altTemplate);
19333
19374
  logger.debug("[SDK] Theme page data load done", { page: pageId, trace });
19334
19375
  this.page = page;
19335
19376
  const globals = {
19336
19377
  ...this.globalData,
19337
- store,
19378
+ // return all storefront settings in the store
19379
+ store: { ...storefrontSettings, ...store },
19338
19380
  settings,
19339
19381
  session,
19340
19382
  request,
@@ -19400,7 +19442,10 @@ ${injects.join("\n")}<\/script>`;
19400
19442
  {}
19401
19443
  )
19402
19444
  };
19403
- const session = await this.swell.storefront.settings.session();
19445
+ const [session, storeSettings] = await Promise.all([
19446
+ storefrontSettings.session(),
19447
+ storefrontSettings.get()
19448
+ ]);
19404
19449
  if (configs.translations) {
19405
19450
  configs.language = configs.translations;
19406
19451
  }
@@ -19413,11 +19458,13 @@ ${injects.join("\n")}<\/script>`;
19413
19458
  await this.setCompatibilityConfigs(configs);
19414
19459
  const menus = await this.resolveMenuSettings();
19415
19460
  return {
19416
- store: storefrontSettings?.store,
19461
+ store: storeSettings?.store,
19417
19462
  session,
19418
19463
  menus,
19419
19464
  geo,
19420
- configs
19465
+ configs,
19466
+ // all settings
19467
+ storefrontSettings
19421
19468
  };
19422
19469
  }
19423
19470
  async resolvePageData(store, configs, pageId, altTemplate) {
@@ -19506,7 +19553,8 @@ ${injects.join("\n")}<\/script>`;
19506
19553
  this.fetchSingletonResourceCached(
19507
19554
  "account",
19508
19555
  () => this.fetchAccount(),
19509
- () => null
19556
+ () => null,
19557
+ false
19510
19558
  )
19511
19559
  ]);
19512
19560
  if (!cart) {
@@ -19526,7 +19574,7 @@ ${injects.join("\n")}<\/script>`;
19526
19574
  // Shopify only
19527
19575
  };
19528
19576
  }
19529
- async fetchSingletonResourceCached(key, handler, defaultValue) {
19577
+ async fetchSingletonResourceCached(key, handler, defaultValue, isCacheble = true) {
19530
19578
  const cacheKey = this.swell.storefront.session.getCookie();
19531
19579
  if (!cacheKey) {
19532
19580
  return defaultValue();
@@ -19534,7 +19582,8 @@ ${injects.join("\n")}<\/script>`;
19534
19582
  const result = await this.swell.getCachedResource(
19535
19583
  `${key}-${cacheKey}`,
19536
19584
  [],
19537
- handler
19585
+ handler,
19586
+ isCacheble
19538
19587
  );
19539
19588
  return result ?? defaultValue();
19540
19589
  }
@@ -21065,6 +21114,7 @@ ${this.shopifyCompatibility.getContentForHeader()}`;
21065
21114
  }
21066
21115
  function createCollection(resource, swell, path, parent_slug, parent_query) {
21067
21116
  const query = getResourceQuery(parent_slug, parent_query);
21117
+ query.$resource_path = path;
21068
21118
  return new SwellStorefrontCollection(
21069
21119
  swell,
21070
21120
  resource,