@swell/apps-sdk 1.0.127 → 1.0.128

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
@@ -89,10 +89,151 @@
89
89
  var import_json52 = __toESM(__require("json5"), 1);
90
90
 
91
91
  // src/resources.ts
92
- var import_lodash_es = __require("lodash-es");
92
+ var import_lodash_es2 = __require("lodash-es");
93
93
 
94
94
  // src/liquid/utils.ts
95
95
  var import_liquidjs = __require("liquidjs");
96
+
97
+ // src/compatibility/shopify-objects/resource.ts
98
+ var import_lodash_es = __require("lodash-es");
99
+ var ShopifyResource = class _ShopifyResource {
100
+ props;
101
+ stringProp;
102
+ linkProps;
103
+ constructor(props, stringProp, linkProps) {
104
+ this.props = props;
105
+ this.stringProp = stringProp;
106
+ this.linkProps = linkProps;
107
+ return new Proxy(props, {
108
+ get(target, prop, receiver) {
109
+ const instance = target;
110
+ switch (prop) {
111
+ case "toJSON":
112
+ return props;
113
+ case "clone":
114
+ return () => {
115
+ return new _ShopifyResource(
116
+ (0, import_lodash_es.cloneDeep)(props),
117
+ (0, import_lodash_es.cloneDeep)(stringProp),
118
+ (0, import_lodash_es.cloneDeep)(linkProps)
119
+ );
120
+ };
121
+ case "toString": {
122
+ if (stringProp) {
123
+ return () => {
124
+ return props[stringProp];
125
+ };
126
+ }
127
+ break;
128
+ }
129
+ case Symbol.toPrimitive: {
130
+ return () => {
131
+ let prop2 = stringProp;
132
+ if (typeof prop2 === "number") {
133
+ prop2 = prop2.toString();
134
+ }
135
+ return this.get?.(instance, prop2 || "handle", receiver);
136
+ };
137
+ }
138
+ default:
139
+ break;
140
+ }
141
+ const value = instance[prop];
142
+ if (value instanceof DeferredShopifyResource) {
143
+ const promise = value.resolve().then(
144
+ (value2) => {
145
+ instance[prop] = value2;
146
+ return value2;
147
+ },
148
+ (err) => {
149
+ console.log(err);
150
+ instance[prop] = null;
151
+ return null;
152
+ }
153
+ );
154
+ instance[prop] = promise;
155
+ return promise;
156
+ }
157
+ return value;
158
+ },
159
+ getPrototypeOf() {
160
+ return _ShopifyResource.prototype;
161
+ }
162
+ });
163
+ }
164
+ valueOf() {
165
+ if (this.stringProp) {
166
+ return this.props[this.stringProp];
167
+ }
168
+ return this;
169
+ }
170
+ // For typescript
171
+ clone() {
172
+ return new _ShopifyResource(this.props);
173
+ }
174
+ };
175
+ var DeferredShopifyResource = class {
176
+ handler;
177
+ result;
178
+ constructor(handler) {
179
+ this.result = void 0;
180
+ this.handler = handler;
181
+ }
182
+ async resolve() {
183
+ if (this.handler !== import_lodash_es.noop) {
184
+ const handler = this.handler;
185
+ this.handler = import_lodash_es.noop;
186
+ this.result = Promise.resolve().then(handler).then((value) => {
187
+ this.result = value;
188
+ return value;
189
+ });
190
+ }
191
+ return this.result;
192
+ }
193
+ };
194
+ function defer(handler) {
195
+ return new DeferredShopifyResource(handler);
196
+ }
197
+ function isResolvable(asyncProp) {
198
+ return isObject(asyncProp) && (isLikePromise(asyncProp) || typeof asyncProp._resolve === "function");
199
+ }
200
+ function isStorefrontResource(resource) {
201
+ return isObject(resource) && typeof resource._resolve === "function";
202
+ }
203
+ async function resolveAsyncProp(asyncProp) {
204
+ if (Array.isArray(asyncProp)) {
205
+ return Promise.all(
206
+ asyncProp.map(
207
+ (prop) => isStorefrontResource(prop) ? prop._resolve() : prop
208
+ )
209
+ );
210
+ }
211
+ if (isStorefrontResource(asyncProp)) {
212
+ return asyncProp._resolve();
213
+ }
214
+ return asyncProp;
215
+ }
216
+ function handleDeferredProp(asyncProp, handler) {
217
+ return resolveAsyncProp(asyncProp).then((value) => {
218
+ if (Array.isArray(asyncProp) && Array.isArray(value)) {
219
+ return handler(...value.map((prop) => prop || {}));
220
+ }
221
+ if (isResolvable(value)) {
222
+ return handleDeferredProp(value, handler);
223
+ }
224
+ return handler(value || {});
225
+ }).catch((err) => {
226
+ console.log(err);
227
+ return null;
228
+ });
229
+ }
230
+ function deferWith(asyncProp, handler) {
231
+ return new DeferredShopifyResource(
232
+ () => handleDeferredProp(asyncProp, handler)
233
+ );
234
+ }
235
+
236
+ // src/liquid/utils.ts
96
237
  var ForloopDrop = class extends import_liquidjs.Drop {
97
238
  i;
98
239
  length;
@@ -259,18 +400,24 @@
259
400
  async function resolveAllKeys(value, references = /* @__PURE__ */ new WeakSet()) {
260
401
  await forEachKeyDeep(value, async (key, value2) => {
261
402
  if (!isObject(value2)) {
262
- return;
403
+ return true;
263
404
  }
264
405
  const val = value2[key];
265
406
  if (isLikePromise(val)) {
266
407
  value2[key] = await val;
267
408
  await resolveAllKeys(value2[key], references);
268
409
  } else if (isObject(val)) {
410
+ if (val instanceof DeferredShopifyResource) {
411
+ value2[key] = await val.resolve();
412
+ await resolveAllKeys(value2[key], references);
413
+ return true;
414
+ }
269
415
  if (references.has(val)) {
270
416
  return false;
271
417
  }
272
418
  references.add(val);
273
419
  }
420
+ return true;
274
421
  });
275
422
  }
276
423
  async function forEachKeyDeep(obj, fn) {
@@ -596,7 +743,7 @@
596
743
  this._getter
597
744
  );
598
745
  if (this._isResultResolved()) {
599
- cloned._result = (0, import_lodash_es.cloneDeep)(this._result);
746
+ cloned._result = (0, import_lodash_es2.cloneDeep)(this._result);
600
747
  }
601
748
  if (this._compatibilityProps) {
602
749
  cloned.setCompatibilityProps(this._compatibilityProps);
@@ -625,7 +772,7 @@
625
772
  }
626
773
  });
627
774
  if (this._isResultResolved()) {
628
- const result = (0, import_lodash_es.cloneDeep)(this._result);
775
+ const result = (0, import_lodash_es2.cloneDeep)(this._result);
629
776
  if (result) {
630
777
  const compatibilityProps = compatibilityGetter(result);
631
778
  Object.assign(cloned, result);
@@ -789,145 +936,6 @@
789
936
  }
790
937
  };
791
938
 
792
- // src/compatibility/shopify-objects/resource.ts
793
- var import_lodash_es2 = __require("lodash-es");
794
- var ShopifyResource = class _ShopifyResource {
795
- props;
796
- stringProp;
797
- linkProps;
798
- constructor(props, stringProp, linkProps) {
799
- this.props = props;
800
- this.stringProp = stringProp;
801
- this.linkProps = linkProps;
802
- return new Proxy(props, {
803
- get(target, prop, receiver) {
804
- const instance = target;
805
- switch (prop) {
806
- case "toJSON":
807
- return props;
808
- case "clone":
809
- return () => {
810
- return new _ShopifyResource(
811
- (0, import_lodash_es2.cloneDeep)(props),
812
- (0, import_lodash_es2.cloneDeep)(stringProp),
813
- (0, import_lodash_es2.cloneDeep)(linkProps)
814
- );
815
- };
816
- case "toString": {
817
- if (stringProp) {
818
- return () => {
819
- return props[stringProp];
820
- };
821
- }
822
- break;
823
- }
824
- case Symbol.toPrimitive: {
825
- return () => {
826
- let prop2 = stringProp;
827
- if (typeof prop2 === "number") {
828
- prop2 = prop2.toString();
829
- }
830
- return this.get?.(instance, prop2 || "handle", receiver);
831
- };
832
- }
833
- default:
834
- break;
835
- }
836
- const value = instance[prop];
837
- if (value instanceof DeferredShopifyResource) {
838
- const promise = value.resolve().then(
839
- (value2) => {
840
- instance[prop] = value2;
841
- return value2;
842
- },
843
- (err) => {
844
- console.log(err);
845
- instance[prop] = null;
846
- return null;
847
- }
848
- );
849
- instance[prop] = promise;
850
- return promise;
851
- }
852
- return value;
853
- },
854
- getPrototypeOf() {
855
- return _ShopifyResource.prototype;
856
- }
857
- });
858
- }
859
- valueOf() {
860
- if (this.stringProp) {
861
- return this.props[this.stringProp];
862
- }
863
- return this;
864
- }
865
- // For typescript
866
- clone() {
867
- return new _ShopifyResource(this.props);
868
- }
869
- };
870
- var DeferredShopifyResource = class {
871
- handler;
872
- result;
873
- constructor(handler) {
874
- this.result = void 0;
875
- this.handler = handler;
876
- }
877
- async resolve() {
878
- if (this.handler !== import_lodash_es2.noop) {
879
- const handler = this.handler;
880
- this.handler = import_lodash_es2.noop;
881
- this.result = Promise.resolve().then(handler).then((value) => {
882
- this.result = value;
883
- return value;
884
- });
885
- }
886
- return this.result;
887
- }
888
- };
889
- function defer(handler) {
890
- return new DeferredShopifyResource(handler);
891
- }
892
- function isResolvable(asyncProp) {
893
- return isObject(asyncProp) && (isLikePromise(asyncProp) || typeof asyncProp._resolve === "function");
894
- }
895
- function isStorefrontResource(resource) {
896
- return isObject(resource) && typeof resource._resolve === "function";
897
- }
898
- async function resolveAsyncProp(asyncProp) {
899
- if (Array.isArray(asyncProp)) {
900
- return Promise.all(
901
- asyncProp.map(
902
- (prop) => isStorefrontResource(prop) ? prop._resolve() : prop
903
- )
904
- );
905
- }
906
- if (isStorefrontResource(asyncProp)) {
907
- return asyncProp._resolve();
908
- }
909
- return asyncProp;
910
- }
911
- function handleDeferredProp(asyncProp, handler) {
912
- return resolveAsyncProp(asyncProp).then((value) => {
913
- if (Array.isArray(asyncProp) && Array.isArray(value)) {
914
- return handler(...value.map((prop) => prop || {}));
915
- }
916
- if (isResolvable(value)) {
917
- return handleDeferredProp(value, handler);
918
- }
919
- return handler(value || {});
920
- }).catch((err) => {
921
- console.log(err);
922
- return null;
923
- });
924
- }
925
- function deferWith(asyncProp, handler) {
926
- return new DeferredShopifyResource(
927
- () => handleDeferredProp(asyncProp, handler)
928
- );
929
- }
930
-
931
939
  // src/constants.ts
932
940
  var FILE_DATA_INCLUDE_QUERY = {
933
941
  url: "/:themes:configs/{id}/file/data",
@@ -13637,6 +13645,9 @@ ${formattedMessage}`;
13637
13645
  "moderated?": false
13638
13646
  });
13639
13647
  }
13648
+ function isLikeShopifyArticle(value) {
13649
+ return typeof value === "object" && value !== null && Object.hasOwn(value, "title") && Object.hasOwn(value, "content") && Object.hasOwn(value, "comments") && Object.hasOwn(value, "moderated?") && Object.hasOwn(value, "published_at") && Object.hasOwn(value, "excerpt_or_content");
13650
+ }
13640
13651
 
13641
13652
  // src/compatibility/shopify-objects/blog.ts
13642
13653
  function ShopifyBlog(instance, blogCategory) {
@@ -13934,7 +13945,7 @@ ${formattedMessage}`;
13934
13945
  // not used
13935
13946
  first_available_variant: deferWith(product, (product2) => {
13936
13947
  const variant = getSelectedVariant(product2, {});
13937
- return ShopifyVariant(instance, variant || product2, product2, depth + 1);
13948
+ return variant ? ShopifyVariant(instance, variant, product2, depth + 1) : void 0;
13938
13949
  }),
13939
13950
  "gift_card?": deferWith(product, (product2) => product2.type === "giftcard"),
13940
13951
  handle: defer(() => product.slug),
@@ -14047,26 +14058,32 @@ ${formattedMessage}`;
14047
14058
  product,
14048
14059
  (product2) => calculateAddOptionsPrice(product2, instance.swell.queryParams)
14049
14060
  ),
14050
- price_max: deferWith(
14051
- product,
14052
- (product2) => product2.variants?.results?.reduce(
14061
+ price_max: deferWith(product, (product2) => {
14062
+ if (!Array.isArray(product2.variants?.results)) {
14063
+ return product2.price;
14064
+ }
14065
+ return product2.variants.results.reduce(
14053
14066
  (max, variant) => Math.max(max, variant.price),
14054
14067
  0
14055
- )
14056
- ),
14057
- price_min: deferWith(
14058
- product,
14059
- (product2) => product2.variants?.results?.reduce(
14068
+ );
14069
+ }),
14070
+ price_min: deferWith(product, (product2) => {
14071
+ if (!Array.isArray(product2.variants?.results)) {
14072
+ return product2.price;
14073
+ }
14074
+ return product2.variants.results.reduce(
14060
14075
  (min, variant) => Math.min(min, variant.price),
14061
14076
  Infinity
14062
- )
14063
- ),
14064
- price_varies: deferWith(
14065
- product,
14066
- (product2) => product2.variants?.results?.some(
14077
+ );
14078
+ }),
14079
+ price_varies: deferWith(product, (product2) => {
14080
+ if (!Array.isArray(product2.variants?.results)) {
14081
+ return false;
14082
+ }
14083
+ return product2.variants.results.some(
14067
14084
  (variant) => variant.price !== product2.price
14068
- )
14069
- ),
14085
+ );
14086
+ }),
14070
14087
  published_at: deferWith(
14071
14088
  product,
14072
14089
  (product2) => product2.date_updated || product2.date_created
@@ -14102,11 +14119,8 @@ ${formattedMessage}`;
14102
14119
  selected_or_first_available_variant: deferWith(
14103
14120
  product,
14104
14121
  (product2) => {
14105
- let variant = getSelectedVariant(product2, instance.swell.queryParams);
14106
- if (!variant) {
14107
- variant = product2;
14108
- }
14109
- return ShopifyVariant(instance, variant, product2, depth + 1);
14122
+ const variant = getSelectedVariant(product2, instance.swell.queryParams);
14123
+ return variant ? ShopifyVariant(instance, variant, product2, depth + 1) : void 0;
14110
14124
  }
14111
14125
  ),
14112
14126
  selected_selling_plan: void 0,
@@ -14127,6 +14141,9 @@ ${formattedMessage}`;
14127
14141
  ).reverse();
14128
14142
  return variants;
14129
14143
  }),
14144
+ variants_count: deferWith(product, (product2) => {
14145
+ return product2.variants?.count || 0;
14146
+ }),
14130
14147
  vendor: void 0
14131
14148
  });
14132
14149
  }
@@ -14204,6 +14221,9 @@ ${formattedMessage}`;
14204
14221
  }
14205
14222
  return values;
14206
14223
  }
14224
+ function isLikeShopifyProduct(value) {
14225
+ 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");
14226
+ }
14207
14227
 
14208
14228
  // src/compatibility/shopify-objects/line_item.ts
14209
14229
  function ShopifyLineItem(instance, item, cart, options = {}) {
@@ -17272,7 +17292,7 @@ ${injects.join("\n")}<\/script>`;
17272
17292
  };
17273
17293
  function bindTags(liquidSwell) {
17274
17294
  Object.entries(tags).forEach(
17275
- ([tag, bind61]) => liquidSwell.registerTag(tag, bind61(liquidSwell))
17295
+ ([tag, bind62]) => liquidSwell.registerTag(tag, bind62(liquidSwell))
17276
17296
  );
17277
17297
  }
17278
17298
 
@@ -18094,8 +18114,86 @@ ${injects.join("\n")}<\/script>`;
18094
18114
  };
18095
18115
  }
18096
18116
 
18097
- // src/liquid/filters/inline_editable.ts
18117
+ // src/liquid/filters/shopify/structured_data.ts
18098
18118
  function bind60(_liquidSwell) {
18119
+ return async function filterStructuredData(input) {
18120
+ let value = input;
18121
+ if (value instanceof StorefrontResource) {
18122
+ value = await value.resolve();
18123
+ }
18124
+ await resolveAllKeys(value);
18125
+ if (isObject2(value)) {
18126
+ if (isLikeShopifyProduct(value)) {
18127
+ return JSON.stringify(
18128
+ value.variants_count > 0 ? convertToSchemaOrgProductGroup(value) : convertToSchemaOrgProduct(
18129
+ value,
18130
+ value
18131
+ )
18132
+ );
18133
+ }
18134
+ if (isLikeShopifyArticle(value)) {
18135
+ return JSON.stringify(convertToSchemaOrgArticle(value));
18136
+ }
18137
+ }
18138
+ return value;
18139
+ };
18140
+ }
18141
+ function convertToSchemaOrgArticle(article) {
18142
+ const schemaOrgArticle = {
18143
+ "@context": "https://schema.org",
18144
+ "@type": "Article",
18145
+ "@id": article.url,
18146
+ url: article.url,
18147
+ name: article.title,
18148
+ author: article.author,
18149
+ image: article.image?.src.url || void 0,
18150
+ abstract: article.excerpt,
18151
+ keywords: article.tags,
18152
+ articleBody: article.content,
18153
+ dateCreated: article.created_at,
18154
+ dateModified: article.updated_at,
18155
+ datePublished: article.published_at
18156
+ };
18157
+ return schemaOrgArticle;
18158
+ }
18159
+ function convertToSchemaOrgProduct(variant, product) {
18160
+ const schemaOrgProduct = {
18161
+ "@context": "https://schema.org",
18162
+ "@type": "Product",
18163
+ "@id": variant.url,
18164
+ url: variant.url,
18165
+ sku: variant.sku,
18166
+ name: variant.title,
18167
+ image: variant.featured_image?.src.url || void 0,
18168
+ keywords: product.tags,
18169
+ description: product.description,
18170
+ offers: {
18171
+ "@type": "Offer",
18172
+ availability: variant.available ? "https://schema.org/InStock" : "https://schema.org/OutOfStock",
18173
+ price: variant.price,
18174
+ priceCurrency: "USD"
18175
+ }
18176
+ };
18177
+ return schemaOrgProduct;
18178
+ }
18179
+ function convertToSchemaOrgProductGroup(product) {
18180
+ const schemaOrgProductGroup = {
18181
+ "@context": "https://schema.org",
18182
+ "@type": "ProductGroup",
18183
+ productGroupID: String(product.id),
18184
+ url: product.url,
18185
+ name: product.title,
18186
+ description: product.description,
18187
+ variesBy: product.options,
18188
+ hasVariant: product.variants.map(
18189
+ (variant) => convertToSchemaOrgProduct(variant, product)
18190
+ )
18191
+ };
18192
+ return schemaOrgProductGroup;
18193
+ }
18194
+
18195
+ // src/liquid/filters/inline_editable.ts
18196
+ function bind61(_liquidSwell) {
18099
18197
  return (value, key) => {
18100
18198
  if (typeof value === "object" && "value" in value) {
18101
18199
  value = value.value;
@@ -18154,8 +18252,9 @@ ${injects.join("\n")}<\/script>`;
18154
18252
  payment_terms: bind57,
18155
18253
  placeholder_svg_tag: bind58,
18156
18254
  shopify_asset_url: bind59,
18255
+ structured_data: bind60,
18157
18256
  // Swell only
18158
- inline_editable: bind60
18257
+ inline_editable: bind61
18159
18258
  };
18160
18259
  function bindFilters(liquidSwell) {
18161
18260
  for (const [tag, handler] of Object.entries(filters)) {
@@ -18169,8 +18268,8 @@ ${injects.join("\n")}<\/script>`;
18169
18268
  }
18170
18269
  }
18171
18270
  }
18172
- function bindWithResolvedProps(liquidSwell, bind61, resolve = []) {
18173
- const handler = bind61(liquidSwell);
18271
+ function bindWithResolvedProps(liquidSwell, bind62, resolve = []) {
18272
+ const handler = bind62(liquidSwell);
18174
18273
  if (!Array.isArray(resolve)) {
18175
18274
  return handler;
18176
18275
  }