@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/README.md CHANGED
@@ -138,7 +138,7 @@ class Swell {
138
138
  post<T>(url: string, data: SwellData): Promise<T>;
139
139
  put<T>(url: string, data: SwellData): Promise<T>;
140
140
  delete<T>(url: string, data?: SwellData): Promise<T>;
141
- getCachedResource<T>(key: string, args: unknown[], handler: () => T): Promise<T>;
141
+ getCachedResource<T>(key: string, args: unknown[], handler: () => T, isCacheble = true): Promise<T>;
142
142
  }
143
143
  ```
144
144
 
package/dist/index.cjs CHANGED
@@ -649,6 +649,12 @@ async function forEachKeyDeep(obj, fn) {
649
649
  }
650
650
 
651
651
  // src/resources.ts
652
+ var NOT_CACHEBLE_COLLECTIONS = Object.freeze(
653
+ /* @__PURE__ */ new Set(["accounts:addresses", "accounts:orders", "accounts:subscriptions"])
654
+ );
655
+ function isResourceCacheble(name) {
656
+ return !NOT_CACHEBLE_COLLECTIONS.has(name);
657
+ }
652
658
  var MAX_QUERY_PAGE_LIMIT = 100;
653
659
  var DEFAULT_QUERY_PAGE_LIMIT = 15;
654
660
  var StorefrontResource = class {
@@ -935,7 +941,8 @@ var SwellStorefrontCollection = class _SwellStorefrontCollection extends SwellSt
935
941
  this._swell.queryParams,
936
942
  this._getterHash
937
943
  ],
938
- getter
944
+ getter,
945
+ isResourceCacheble(this._collection)
939
946
  ).then((result) => {
940
947
  this._result = result;
941
948
  if (result) {
@@ -1049,7 +1056,8 @@ var SwellStorefrontRecord = class extends SwellStorefrontResource {
1049
1056
  this._swell.queryParams,
1050
1057
  this._getterHash
1051
1058
  ],
1052
- getter
1059
+ getter,
1060
+ isResourceCacheble(this._collection)
1053
1061
  ).then((result) => {
1054
1062
  return this._transformResult(result);
1055
1063
  }).then((result) => {
@@ -7216,16 +7224,18 @@ var Cache = class {
7216
7224
  *
7217
7225
  * This will always return the cached value immediately if exists
7218
7226
  */
7219
- async fetchSWR(key, fetchFn, ttl = DEFAULT_SWR_TTL) {
7227
+ async fetchSWR(key, fetchFn, ttl = DEFAULT_SWR_TTL, isCacheble = true) {
7220
7228
  const trace = createTraceId();
7221
7229
  logger.debug("[SDK] Cache fetch start", { key, trace });
7222
- const cacheValue = await this.client.get(key);
7230
+ const cacheValue = isCacheble ? await this.client.get(key) : void 0;
7223
7231
  let promise = SWR_PROMISE_MAP.get(key);
7224
7232
  if (promise === void 0) {
7225
7233
  promise = Promise.resolve().then(fetchFn).then(resolveAsyncResources).then(async (value) => {
7226
7234
  const isNull = value === null || value === void 0;
7227
- await this.client.set(key, isNull ? NULL_VALUE : value, ttl);
7228
- logger.debug("[SDK] Cache update done", { key, trace });
7235
+ if (isCacheble) {
7236
+ await this.client.set(key, isNull ? NULL_VALUE : value, ttl);
7237
+ logger.debug("[SDK] Cache update done", { key, trace });
7238
+ }
7229
7239
  return value;
7230
7240
  }).finally(() => {
7231
7241
  SWR_PROMISE_MAP.delete(key);
@@ -7376,9 +7386,25 @@ var SwellPage = class extends SwellStorefrontRecord {
7376
7386
  };
7377
7387
 
7378
7388
  // src/resources/product_helpers.ts
7389
+ function isGiftcard(product) {
7390
+ return product.type === "giftcard";
7391
+ }
7392
+ function isOptionAvailable(product, option) {
7393
+ if (isGiftcard(product)) {
7394
+ return true;
7395
+ }
7396
+ return Boolean(option.active && option.name);
7397
+ }
7398
+ function isProductAvailable(product, variant) {
7399
+ if (product.stock_purchasable) {
7400
+ return true;
7401
+ }
7402
+ const stockStatus = (variant || product).stock_status;
7403
+ return !stockStatus || stockStatus === "in_stock";
7404
+ }
7379
7405
  function getAvailableVariants(product) {
7380
7406
  return (product.variants?.results?.slice()?.reverse() || []).filter(
7381
- (variant) => variant.stock_status === "in_stock" || !variant.stock_status
7407
+ (variant) => isProductAvailable(product, variant)
7382
7408
  );
7383
7409
  }
7384
7410
  function isOptionValueAvailable(option, value, product, availableVariants) {
@@ -7445,7 +7471,7 @@ function getSelectedVariantOptionValues(product, queryParams, variant) {
7445
7471
  return acc;
7446
7472
  }
7447
7473
  const hasOptionValues = option.values.length > 0;
7448
- if (!option.active || !hasOptionValues) {
7474
+ if (!isOptionAvailable(product, option) || !hasOptionValues) {
7449
7475
  return acc;
7450
7476
  }
7451
7477
  const value = option.values.find(
@@ -7731,9 +7757,14 @@ var Swell = class _Swell {
7731
7757
  * Fetches a resource.
7732
7758
  * First attempts to fetch from cache.
7733
7759
  */
7734
- async getCachedResource(key, args, handler) {
7760
+ async getCachedResource(key, args, handler, isCacheble = true) {
7735
7761
  const cacheKey = getCacheKey(key, [this.instanceId, args]);
7736
- return this.getResourceCache().fetchSWR(cacheKey, handler);
7762
+ return this.getResourceCache().fetchSWR(
7763
+ cacheKey,
7764
+ handler,
7765
+ void 0,
7766
+ isCacheble
7767
+ );
7737
7768
  }
7738
7769
  async getAppSettings() {
7739
7770
  const settings = await this.get(
@@ -7745,15 +7776,16 @@ var Swell = class _Swell {
7745
7776
  return settings || {};
7746
7777
  }
7747
7778
  async getStorefrontSettings(force = false) {
7779
+ const storefrontSettings = this.storefront.settings;
7748
7780
  try {
7749
- const { settings, menus, payments, subscriptions, session } = await this.storefront.request(
7781
+ const allSettings = await this.storefront.request(
7750
7782
  "get",
7751
7783
  "/settings/all",
7752
7784
  void 0,
7753
7785
  force ? { $cache: false } : void 0,
7754
7786
  { force }
7755
7787
  );
7756
- const storefrontSettings = this.storefront.settings;
7788
+ const { settings, menus, payments, subscriptions, session } = allSettings;
7757
7789
  storefrontSettings.localizedState = {};
7758
7790
  storefrontSettings.set({
7759
7791
  value: settings
@@ -7781,7 +7813,7 @@ var Swell = class _Swell {
7781
7813
  }
7782
7814
  logger.error(err);
7783
7815
  }
7784
- return this.storefront.settings.get();
7816
+ return storefrontSettings;
7785
7817
  }
7786
7818
  getStorefrontMenus() {
7787
7819
  const menus = this.storefront.settings.getState(
@@ -14257,8 +14289,8 @@ function ShopifyVariant(instance, variant, productIn, depth = 0) {
14257
14289
  return new ShopifyResource({
14258
14290
  ...swellVariant,
14259
14291
  available: deferWith(
14260
- variant,
14261
- (variant2) => Boolean(variant2.stock_status === "in_stock" || !variant2.stock_status)
14292
+ [product, variant],
14293
+ (product2, variant2) => isProductAvailable(product2, variant2)
14262
14294
  ),
14263
14295
  barcode: void 0,
14264
14296
  compare_at_price: defer(() => variant.orig_price),
@@ -14347,7 +14379,7 @@ function ShopifyVariant(instance, variant, productIn, depth = 0) {
14347
14379
  requires_selling_plan: false,
14348
14380
  requires_shipping: deferWith(
14349
14381
  product,
14350
- (product2) => Boolean(product2.delivery?.contains("shipment"))
14382
+ (product2) => Boolean(product2.delivery?.includes("shipment"))
14351
14383
  ),
14352
14384
  selected: false,
14353
14385
  selected_selling_plan_allocation: void 0,
@@ -14416,7 +14448,7 @@ function ShopifyProduct(instance, product, depth = 0) {
14416
14448
  return new ShopifyResource({
14417
14449
  available: deferWith(
14418
14450
  product,
14419
- (product2) => product2.stock_status === "in_stock" || !product2.stock_status
14451
+ (product2) => isProductAvailable(product2)
14420
14452
  ),
14421
14453
  collections: [],
14422
14454
  // TODO: need to support this in the resource class somehow
@@ -14441,7 +14473,7 @@ function ShopifyProduct(instance, product, depth = 0) {
14441
14473
  const variant = getSelectedVariant(product2, {});
14442
14474
  return variant ? ShopifyVariant(instance, variant, product2, depth + 1) : void 0;
14443
14475
  }),
14444
- "gift_card?": deferWith(product, (product2) => product2.type === "giftcard"),
14476
+ "gift_card?": deferWith(product, isGiftcard),
14445
14477
  handle: defer(() => product.slug),
14446
14478
  // indicates that product has any options
14447
14479
  has_only_default_variant: deferWith(
@@ -14473,7 +14505,9 @@ function ShopifyProduct(instance, product, depth = 0) {
14473
14505
  if (!Array.isArray(product2.options)) {
14474
14506
  return [];
14475
14507
  }
14476
- return product2.options.filter((option) => option.active && option.name).map((option) => option.name);
14508
+ return product2.options.filter(
14509
+ (option) => isOptionAvailable(product2, option)
14510
+ ).map((option) => option.name);
14477
14511
  }),
14478
14512
  options_by_name: deferWith(product, (product2) => {
14479
14513
  if (!Array.isArray(product2.options)) {
@@ -14484,7 +14518,7 @@ function ShopifyProduct(instance, product, depth = 0) {
14484
14518
  const variant = getSelectedVariant(product2, queryParams);
14485
14519
  return product2.options.reduce(
14486
14520
  (acc, option, index) => {
14487
- if (!option.active || !option.name) {
14521
+ if (!isOptionAvailable(product2, option)) {
14488
14522
  return acc;
14489
14523
  }
14490
14524
  acc[option.name.toLowerCase()] = getOption(
@@ -14510,7 +14544,7 @@ function ShopifyProduct(instance, product, depth = 0) {
14510
14544
  const { queryParams } = instance.swell;
14511
14545
  const variants = getAvailableVariants(product2);
14512
14546
  const variant = getSelectedVariant(product2, queryParams);
14513
- return product2.options.filter((option) => option.active && option.name).map(
14547
+ return product2.options.filter((option) => isOptionAvailable(product2, option)).map(
14514
14548
  (option, index) => getOption(
14515
14549
  option,
14516
14550
  index,
@@ -14683,7 +14717,7 @@ function ShopifyLineItem(instance, item, cart, options = {}) {
14683
14717
  : undefined, */
14684
14718
  fulfillment_service: "manual",
14685
14719
  // TODO
14686
- gift_card: item.delivery === "giftcard",
14720
+ gift_card: isGiftcard(item.product),
14687
14721
  grams: item.shipment_weight,
14688
14722
  id: item.id,
14689
14723
  image: deferWith(
@@ -15213,6 +15247,9 @@ function ShopifyAddress(instance, address, account) {
15213
15247
  if (address instanceof StorefrontResource) {
15214
15248
  address = cloneStorefrontResource(address);
15215
15249
  }
15250
+ if (!address) {
15251
+ address = {};
15252
+ }
15216
15253
  return new ShopifyResource({
15217
15254
  address1: defer(() => address.address1),
15218
15255
  address2: defer(() => address.address2),
@@ -16687,6 +16724,10 @@ ${injects.join("\n")}</script>`;
16687
16724
  pageId = "account/order";
16688
16725
  urlParams.id = segment3;
16689
16726
  break;
16727
+ case "subscriptions":
16728
+ pageId = "account/subscription";
16729
+ urlParams.id = segment3;
16730
+ break;
16690
16731
  case "register":
16691
16732
  pageId = "account/login";
16692
16733
  break;
@@ -19434,14 +19475,15 @@ var SwellTheme3 = class {
19434
19475
  logger.debug("[SDK] Theme init start", { page: pageId, trace });
19435
19476
  await this.themeLoader.init(this.themeConfigs || void 0);
19436
19477
  logger.debug("[SDK] ThemeLoader init done", { page: pageId, trace });
19437
- const { store, session, menus, geo, configs } = await this.getSettingsAndConfigs();
19478
+ const { store, session, menus, geo, configs, storefrontSettings } = await this.getSettingsAndConfigs();
19438
19479
  logger.debug("[SDK] Theme settings load done", { page: pageId, trace });
19439
19480
  const { settings, request, page, cart, account, customer } = await this.resolvePageData(store, configs, pageId, altTemplate);
19440
19481
  logger.debug("[SDK] Theme page data load done", { page: pageId, trace });
19441
19482
  this.page = page;
19442
19483
  const globals = {
19443
19484
  ...this.globalData,
19444
- store,
19485
+ // return all storefront settings in the store
19486
+ store: { ...storefrontSettings, ...store },
19445
19487
  settings,
19446
19488
  session,
19447
19489
  request,
@@ -19507,7 +19549,10 @@ var SwellTheme3 = class {
19507
19549
  {}
19508
19550
  )
19509
19551
  };
19510
- const session = await this.swell.storefront.settings.session();
19552
+ const [session, storeSettings] = await Promise.all([
19553
+ storefrontSettings.session(),
19554
+ storefrontSettings.get()
19555
+ ]);
19511
19556
  if (configs.translations) {
19512
19557
  configs.language = configs.translations;
19513
19558
  }
@@ -19520,11 +19565,13 @@ var SwellTheme3 = class {
19520
19565
  await this.setCompatibilityConfigs(configs);
19521
19566
  const menus = await this.resolveMenuSettings();
19522
19567
  return {
19523
- store: storefrontSettings?.store,
19568
+ store: storeSettings?.store,
19524
19569
  session,
19525
19570
  menus,
19526
19571
  geo,
19527
- configs
19572
+ configs,
19573
+ // all settings
19574
+ storefrontSettings
19528
19575
  };
19529
19576
  }
19530
19577
  async resolvePageData(store, configs, pageId, altTemplate) {
@@ -19613,7 +19660,8 @@ var SwellTheme3 = class {
19613
19660
  this.fetchSingletonResourceCached(
19614
19661
  "account",
19615
19662
  () => this.fetchAccount(),
19616
- () => null
19663
+ () => null,
19664
+ false
19617
19665
  )
19618
19666
  ]);
19619
19667
  if (!cart) {
@@ -19633,7 +19681,7 @@ var SwellTheme3 = class {
19633
19681
  // Shopify only
19634
19682
  };
19635
19683
  }
19636
- async fetchSingletonResourceCached(key, handler, defaultValue) {
19684
+ async fetchSingletonResourceCached(key, handler, defaultValue, isCacheble = true) {
19637
19685
  const cacheKey = this.swell.storefront.session.getCookie();
19638
19686
  if (!cacheKey) {
19639
19687
  return defaultValue();
@@ -19641,7 +19689,8 @@ var SwellTheme3 = class {
19641
19689
  const result = await this.swell.getCachedResource(
19642
19690
  `${key}-${cacheKey}`,
19643
19691
  [],
19644
- handler
19692
+ handler,
19693
+ isCacheble
19645
19694
  );
19646
19695
  return result ?? defaultValue();
19647
19696
  }
@@ -21172,6 +21221,7 @@ function createStorefrontRecord(resource, swell, path, parent_slug, parent_query
21172
21221
  }
21173
21222
  function createCollection(resource, swell, path, parent_slug, parent_query) {
21174
21223
  const query = getResourceQuery(parent_slug, parent_query);
21224
+ query.$resource_path = path;
21175
21225
  return new SwellStorefrontCollection(
21176
21226
  swell,
21177
21227
  resource,