@swell/apps-sdk 1.0.168 → 1.0.170

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
@@ -1133,10 +1133,10 @@
1133
1133
  { content_type: { $regex: "^image/svg" } }
1134
1134
  ]
1135
1135
  },
1136
- // Do not return assets unless they end with .liquid.[ext] or css/js/svg
1136
+ // Do not return assets unless they end with .liquid or css/js/svg
1137
1137
  $or: [
1138
1138
  { file_path: { $regex: "^(?!theme/assets/)" } },
1139
- { file_path: { $regex: ".liquid.[a-zA-Z0-9]+$" } },
1139
+ { file_path: { $regex: ".liquid$" } },
1140
1140
  { file_path: { $regex: ".(css|js|svg)$" } }
1141
1141
  ]
1142
1142
  }
@@ -7659,6 +7659,33 @@
7659
7659
  }
7660
7660
  };
7661
7661
 
7662
+ // src/cache/content-cache.ts
7663
+ var DEFAULT_TTL2 = 90 * 24 * 60 * 60 * 1e3;
7664
+ var ContentCache = class {
7665
+ cache;
7666
+ defaultTtl;
7667
+ constructor(options) {
7668
+ const { defaultTtl, ...cacheOptions } = options || {};
7669
+ this.defaultTtl = defaultTtl ?? DEFAULT_TTL2;
7670
+ this.cache = new Cache({
7671
+ ttl: this.defaultTtl,
7672
+ ...cacheOptions
7673
+ });
7674
+ }
7675
+ /**
7676
+ * Get content from cache
7677
+ */
7678
+ async get(key) {
7679
+ return this.cache.get(key);
7680
+ }
7681
+ /**
7682
+ * Set content in cache
7683
+ */
7684
+ async set(key, value, ttl) {
7685
+ await this.cache.set(key, value, ttl ?? this.defaultTtl);
7686
+ }
7687
+ };
7688
+
7662
7689
  // src/resources/addresses.ts
7663
7690
  var SwellAddresses = class extends SwellStorefrontCollection {
7664
7691
  constructor(swell, query) {
@@ -15783,7 +15810,10 @@ ${formattedMessage}`;
15783
15810
  tags: [],
15784
15811
  template_suffix: defer(() => category.theme_template),
15785
15812
  title: defer(() => category.name),
15786
- url: deferWith(category, (category2) => `/collections/${category2.slug}`)
15813
+ url: deferWith(
15814
+ category,
15815
+ (category2) => category2.slug ? `/collections/${category2.slug}` : ""
15816
+ )
15787
15817
  });
15788
15818
  }
15789
15819
  function getFirstImage(category) {
@@ -15812,9 +15842,7 @@ ${formattedMessage}`;
15812
15842
  function getProducts(instance, object, mapper) {
15813
15843
  return deferWith(object, (object2) => {
15814
15844
  const { page, limit: limit2 } = instance.swell.queryParams;
15815
- const productQuery = {
15816
- $variants: true
15817
- };
15845
+ const productQuery = { $variants: true };
15818
15846
  if (typeof object2.id === "string" && object2.id !== "all") {
15819
15847
  productQuery.category = object2.id;
15820
15848
  }
@@ -15825,11 +15853,7 @@ ${formattedMessage}`;
15825
15853
  const products = new SwellStorefrontCollection(
15826
15854
  instance.swell,
15827
15855
  "products",
15828
- {
15829
- page,
15830
- limit: limit2,
15831
- ...filterQuery
15832
- },
15856
+ { page, limit: limit2, ...filterQuery },
15833
15857
  async function() {
15834
15858
  return this._defaultGetter().call(this);
15835
15859
  }
@@ -17336,10 +17360,9 @@ ${formattedMessage}`;
17336
17360
  ({ from }) => from === key || from.startsWith(keyObject)
17337
17361
  );
17338
17362
  if (resourceMap && value instanceof StorefrontResource) {
17339
- const resource = resourceMap.object(this, value);
17340
- const composed = Object.assign({}, value.toObject(), resource);
17341
- Object.setPrototypeOf(composed, Object.getPrototypeOf(resource));
17342
- pageData[resourceMap.to] = composed;
17363
+ const resourceProps = resourceMap.object(this, value);
17364
+ value.setCompatibilityProps(resourceProps);
17365
+ pageData[resourceMap.to] = value;
17343
17366
  }
17344
17367
  }
17345
17368
  }
@@ -18581,7 +18604,9 @@ ${injects.join("\n")}<\/script>`;
18581
18604
  scope[filepath] = yield (0, import_liquidjs25.evalToken)(this.withVar, ctx);
18582
18605
  }
18583
18606
  ctx.push(ctx.opts.jekyllInclude ? { include: scope } : scope);
18584
- const output = yield liquidSwell.getComponentPath(filepath).then((path) => liquidSwell.getThemeConfig(path)).then((themeConfig) => liquidSwell.renderTemplate(themeConfig, scope));
18607
+ const output = yield liquidSwell.getComponentPath(filepath).then((path) => liquidSwell.getThemeConfig(path)).then(
18608
+ (themeConfig) => liquidSwell.renderTemplate(themeConfig, ctx.getAll())
18609
+ );
18585
18610
  emitter.write(output);
18586
18611
  ctx.pop();
18587
18612
  ctx.restoreRegister(saved);
@@ -18675,7 +18700,7 @@ ${injects.join("\n")}<\/script>`;
18675
18700
  };
18676
18701
  function bindTags(liquidSwell) {
18677
18702
  Object.entries(tags).forEach(
18678
- ([tag, bind64]) => liquidSwell.registerTag(tag, bind64(liquidSwell))
18703
+ ([tag, bind65]) => liquidSwell.registerTag(tag, bind65(liquidSwell))
18679
18704
  );
18680
18705
  }
18681
18706
 
@@ -18837,7 +18862,7 @@ ${injects.join("\n")}<\/script>`;
18837
18862
  }
18838
18863
  }
18839
18864
  function isCustomDateFormat(format) {
18840
- return format.includes("%");
18865
+ return Boolean(format) && format.includes("%");
18841
18866
  }
18842
18867
  function applyStrftimeFormat(format, date) {
18843
18868
  return (0, import_strftime.default)(format, date);
@@ -19372,6 +19397,38 @@ ${injects.join("\n")}<\/script>`;
19372
19397
  };
19373
19398
  }
19374
19399
 
19400
+ // src/liquid/filters/shopify/img_url.ts
19401
+ function bind58(_liquidSwell) {
19402
+ return function filterImgUrl(input, ...params) {
19403
+ if (!input) return "";
19404
+ let url;
19405
+ if (typeof input === "object") {
19406
+ if (input.url) {
19407
+ url = input.url;
19408
+ } else {
19409
+ return "";
19410
+ }
19411
+ } else {
19412
+ url = String(input);
19413
+ }
19414
+ const query = [];
19415
+ params.forEach((param) => {
19416
+ if (!param) {
19417
+ return;
19418
+ }
19419
+ const [key, value] = param.includes(":") ? param.split(":").map((s) => s.trim()) : [param, void 0];
19420
+ if (/^w\d+$/.test(key)) {
19421
+ query.push(`width=${key.slice(1)}`);
19422
+ } else if (/^h\d+$/.test(key)) {
19423
+ query.push(`height=${key.slice(1)}`);
19424
+ } else if (key === "crop" && value) {
19425
+ query.push(`crop=${encodeURIComponent(value)}`);
19426
+ }
19427
+ });
19428
+ return query.length ? `${url}?${query.join("&")}` : url;
19429
+ };
19430
+ }
19431
+
19375
19432
  // src/liquid/filters/shopify/item_count_for_variant.ts
19376
19433
  var item_count_for_variant_default = {
19377
19434
  bind(_liquidSwell) {
@@ -19386,14 +19443,14 @@ ${injects.join("\n")}<\/script>`;
19386
19443
  };
19387
19444
 
19388
19445
  // src/liquid/filters/shopify/payment_button.ts
19389
- function bind58(_liquidSwell) {
19446
+ function bind59(_liquidSwell) {
19390
19447
  return (form) => {
19391
19448
  return null;
19392
19449
  };
19393
19450
  }
19394
19451
 
19395
19452
  // src/liquid/filters/shopify/payment_terms.ts
19396
- function bind59(_liquidSwell) {
19453
+ function bind60(_liquidSwell) {
19397
19454
  return (form) => {
19398
19455
  return null;
19399
19456
  };
@@ -19495,7 +19552,7 @@ ${injects.join("\n")}<\/script>`;
19495
19552
  var placeholder_svgs_default = svgs;
19496
19553
 
19497
19554
  // src/liquid/filters/shopify/placeholder_svg_tag.ts
19498
- function bind60(_liquidSwell) {
19555
+ function bind61(_liquidSwell) {
19499
19556
  return function filterPlaceholderSvgTag(name, className) {
19500
19557
  const svg = placeholder_svgs_default[name];
19501
19558
  if (typeof svg === "object" && svg !== null) {
@@ -19506,7 +19563,7 @@ ${injects.join("\n")}<\/script>`;
19506
19563
  }
19507
19564
 
19508
19565
  // src/liquid/filters/shopify/shopify_asset_url.ts
19509
- function bind61(_liquidSwell) {
19566
+ function bind62(_liquidSwell) {
19510
19567
  return function filterShopifyAssetUrl(input) {
19511
19568
  if (typeof input === "string") {
19512
19569
  switch (input) {
@@ -19531,7 +19588,7 @@ ${injects.join("\n")}<\/script>`;
19531
19588
  }
19532
19589
 
19533
19590
  // src/liquid/filters/shopify/structured_data.ts
19534
- function bind62(_liquidSwell) {
19591
+ function bind63(_liquidSwell) {
19535
19592
  return async function filterStructuredData(input) {
19536
19593
  let value = input;
19537
19594
  if (value instanceof StorefrontResource) {
@@ -19609,7 +19666,7 @@ ${injects.join("\n")}<\/script>`;
19609
19666
  }
19610
19667
 
19611
19668
  // src/liquid/filters/inline_editable.ts
19612
- function bind63(_liquidSwell) {
19669
+ function bind64(_liquidSwell) {
19613
19670
  return (value, key) => {
19614
19671
  if (typeof value === "object" && "value" in value) {
19615
19672
  value = value.value;
@@ -19667,14 +19724,15 @@ ${injects.join("\n")}<\/script>`;
19667
19724
  // Shopify compatibility only
19668
19725
  asset_img_url: bind56,
19669
19726
  hex_to_rgba: bind57,
19727
+ img_url: bind58,
19670
19728
  item_count_for_variant: item_count_for_variant_default,
19671
- payment_button: bind58,
19672
- payment_terms: bind59,
19673
- placeholder_svg_tag: bind60,
19674
- shopify_asset_url: bind61,
19675
- structured_data: bind62,
19729
+ payment_button: bind59,
19730
+ payment_terms: bind60,
19731
+ placeholder_svg_tag: bind61,
19732
+ shopify_asset_url: bind62,
19733
+ structured_data: bind63,
19676
19734
  // Swell only
19677
- inline_editable: bind63
19735
+ inline_editable: bind64
19678
19736
  };
19679
19737
  function bindFilters(liquidSwell) {
19680
19738
  for (const [tag, handler] of Object.entries(filters)) {
@@ -19688,8 +19746,8 @@ ${injects.join("\n")}<\/script>`;
19688
19746
  }
19689
19747
  }
19690
19748
  }
19691
- function bindWithResolvedProps(liquidSwell, bind64, resolve = []) {
19692
- const handler = bind64(liquidSwell);
19749
+ function bindWithResolvedProps(liquidSwell, bind65, resolve = []) {
19750
+ const handler = bind65(liquidSwell);
19693
19751
  if (!Array.isArray(resolve)) {
19694
19752
  return handler;
19695
19753
  }
@@ -20499,6 +20557,7 @@ ${injects.join("\n")}<\/script>`;
20499
20557
  globals;
20500
20558
  forms;
20501
20559
  resources;
20560
+ dynamicAssetUrl;
20502
20561
  liquidSwell;
20503
20562
  themeLoader;
20504
20563
  page;
@@ -20512,13 +20571,20 @@ ${injects.join("\n")}<\/script>`;
20512
20571
  themeSettingFilePath = "theme/config/theme.json";
20513
20572
  pageSectionGroups = null;
20514
20573
  constructor(swell, options = {}) {
20515
- const { forms, resources, globals, shopifyCompatibilityClass } = options;
20574
+ const {
20575
+ forms,
20576
+ resources,
20577
+ globals,
20578
+ dynamicAssetUrl,
20579
+ shopifyCompatibilityClass
20580
+ } = options;
20516
20581
  this.swell = swell;
20517
20582
  this.props = this.getSwellAppThemeProps(swell.config);
20518
20583
  this.shopifyCompatibilityConfig = swell.shopifyCompatibilityConfig || null;
20519
20584
  this.globals = globals || {};
20520
20585
  this.forms = forms;
20521
20586
  this.resources = resources;
20587
+ this.dynamicAssetUrl = dynamicAssetUrl;
20522
20588
  this.shopifyCompatibilityClass = shopifyCompatibilityClass || ShopifyCompatibility2;
20523
20589
  this.liquidSwell = new LiquidSwell30({
20524
20590
  theme: this,
@@ -20583,10 +20649,7 @@ ${injects.join("\n")}<\/script>`;
20583
20649
  geo,
20584
20650
  configs,
20585
20651
  language: configs?.language,
20586
- ...pageRecord ? getRecordGlobals(this, pageRecord) : {
20587
- page_title: page.title,
20588
- page_description: page.description
20589
- },
20652
+ ...pageRecord ? getRecordGlobals(this, pageRecord) : { page_title: page.title, page_description: page.description },
20590
20653
  all_country_option_tags: countryOptions,
20591
20654
  country_option_tags: countryOptions,
20592
20655
  canonical_url: `${store.url}${this.swell.url?.pathname || ""}`,
@@ -20614,13 +20677,8 @@ ${injects.join("\n")}<\/script>`;
20614
20677
  if (this.shopifyCompatibility) {
20615
20678
  this.shopifyCompatibility.adaptGlobals(globals, this.globals);
20616
20679
  }
20617
- this.globals = {
20618
- ...this.globals,
20619
- ...globals
20620
- };
20621
- this.liquidSwell.options.globals = {
20622
- ...this.globals
20623
- };
20680
+ this.globals = { ...this.globals, ...globals };
20681
+ this.liquidSwell.options.globals = { ...this.globals };
20624
20682
  }
20625
20683
  async getSettingsAndConfigs() {
20626
20684
  const geo = GEO_DATA;
@@ -20872,10 +20930,7 @@ ${injects.join("\n")}<\/script>`;
20872
20930
  return Object.keys(serializedFormData).length > 0 ? serializedFormData : null;
20873
20931
  }
20874
20932
  setGlobalData(data = {}) {
20875
- this.globalData = {
20876
- ...this.globalData,
20877
- ...data
20878
- };
20933
+ this.globalData = { ...this.globalData, ...data };
20879
20934
  this.setGlobals(this.globalData);
20880
20935
  }
20881
20936
  serializeGlobalData() {
@@ -21194,11 +21249,31 @@ ${injects.join("\n")}<\/script>`;
21194
21249
  async getAssetConfig(assetName) {
21195
21250
  return await this.getThemeConfig(`theme/assets/${assetName}`) ?? await this.getThemeConfig(`assets/${assetName}`) ?? null;
21196
21251
  }
21252
+ async getDynamicAssetUrl(filePath) {
21253
+ if (!this.dynamicAssetUrl) {
21254
+ return null;
21255
+ }
21256
+ const assetName = `${filePath}.liquid`;
21257
+ const assetConfig = await this.getAssetConfig(assetName);
21258
+ if (!assetConfig) {
21259
+ return null;
21260
+ }
21261
+ if (!this.dynamicAssetUrl.endsWith("/")) {
21262
+ this.dynamicAssetUrl += "/";
21263
+ }
21264
+ const settingsConfig = this._getTemplateConfigByType(
21265
+ "config",
21266
+ "settings_data",
21267
+ "json"
21268
+ );
21269
+ const settingsHash = settingsConfig?.hash;
21270
+ return settingsHash ? `${this.dynamicAssetUrl}v/${settingsHash}/${assetName}` : `${this.dynamicAssetUrl}${assetName}`;
21271
+ }
21197
21272
  async getAssetUrl(filePath) {
21198
21273
  const assetConfig = await this.getAssetConfig(filePath);
21199
21274
  const file = assetConfig?.file;
21200
21275
  if (!file) {
21201
- return null;
21276
+ return this.getDynamicAssetUrl(filePath);
21202
21277
  }
21203
21278
  const fileUrl = file.url || null;
21204
21279
  if (!fileUrl) {
@@ -21286,10 +21361,7 @@ ${injects.join("\n")}<\/script>`;
21286
21361
  if (schemaStartIndex === -1 || schemaEndIndex === -1) {
21287
21362
  return null;
21288
21363
  }
21289
- return {
21290
- ...config,
21291
- file_data: schemaTag + schemaData + schemaEndTag
21292
- };
21364
+ return { ...config, file_data: schemaTag + schemaData + schemaEndTag };
21293
21365
  }
21294
21366
  async renderThemeTemplate(filePath, data) {
21295
21367
  const config = await this.getThemeTemplateConfig(filePath);
@@ -21465,9 +21537,7 @@ ${content.slice(pos)}`;
21465
21537
  const pageSectionGroup = {
21466
21538
  // use original pageId to return exactly the requested section id
21467
21539
  id: originalPageId,
21468
- sections: {
21469
- [sectionKey]: oldSections[sectionKey]
21470
- }
21540
+ sections: { [sectionKey]: oldSections[sectionKey] }
21471
21541
  };
21472
21542
  const [pageSection] = await this.renderPageSections(
21473
21543
  pageSectionGroup,
@@ -21614,10 +21684,7 @@ ${this.shopifyCompatibility.getContentForHeader()}`;
21614
21684
  }
21615
21685
  return {
21616
21686
  ...block,
21617
- settings: {
21618
- ...blockDefaults,
21619
- ...block.settings || void 0
21620
- }
21687
+ settings: { ...blockDefaults, ...block.settings || void 0 }
21621
21688
  };
21622
21689
  }
21623
21690
  );
@@ -21754,10 +21821,7 @@ ${this.shopifyCompatibility.getContentForHeader()}`;
21754
21821
  )}</style>`;
21755
21822
  }
21756
21823
  }
21757
- return {
21758
- ...sectionConfig,
21759
- output
21760
- };
21824
+ return { ...sectionConfig, output };
21761
21825
  })
21762
21826
  );
21763
21827
  }
@@ -21820,10 +21884,7 @@ ${this.shopifyCompatibility.getContentForHeader()}`;
21820
21884
  return settings;
21821
21885
  }
21822
21886
  const editorSettings = [
21823
- {
21824
- label: schema.label,
21825
- fields: schema.fields
21826
- }
21887
+ { label: schema.label, fields: schema.fields }
21827
21888
  ];
21828
21889
  let blocks = settings.section.blocks?.filter(
21829
21890
  (block) => block.disabled !== true
@@ -22656,7 +22717,6 @@ ${this.shopifyCompatibility.getContentForHeader()}`;
22656
22717
  }
22657
22718
  generateVersionHash(headers) {
22658
22719
  const swellData = this.extractSwellData(headers);
22659
- const acceptLang = headers.get("accept-language") || "";
22660
22720
  const versionFactors = {
22661
22721
  store: headers.get("swell-storefront-id") || "",
22662
22722
  app: (headers.get("swell-app-id") || "") + "@" + (headers.get("host") || ""),
@@ -22664,7 +22724,7 @@ ${this.shopifyCompatibility.getContentForHeader()}`;
22664
22724
  theme: headers.get("swell-theme-version-hash") || "",
22665
22725
  modified: headers.get("swell-cache-modified") || "",
22666
22726
  currency: swellData["swell-currency"] || "USD",
22667
- locale: headers.get("x-locale") || acceptLang.split(",")[0].trim().toLowerCase() || "default",
22727
+ locale: headers.get("x-locale") || swellData["swell-locale"] || "en-US",
22668
22728
  context: headers.get("swell-storefront-context"),
22669
22729
  epoch: this.epoch
22670
22730
  };