@01.software/sdk 0.38.0 → 0.40.0
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 +169 -54
- package/dist/analytics/react.cjs.map +1 -1
- package/dist/analytics/react.js.map +1 -1
- package/dist/analytics.cjs.map +1 -1
- package/dist/analytics.js.map +1 -1
- package/dist/client.cjs +1194 -5
- package/dist/client.cjs.map +1 -1
- package/dist/client.d.cts +8 -7
- package/dist/client.d.ts +8 -7
- package/dist/client.js +1194 -5
- package/dist/client.js.map +1 -1
- package/dist/{collection-client-BroIWHY1.d.ts → collection-client-CKFa99C6.d.ts} +16 -10
- package/dist/{collection-client-B0J9wMNE.d.cts → collection-client-VYwYi6u6.d.cts} +16 -10
- package/dist/const-4BUtUdGU.d.cts +32 -0
- package/dist/const-ymprfiPf.d.ts +32 -0
- package/dist/errors.cjs +4 -1
- package/dist/errors.cjs.map +1 -1
- package/dist/errors.js +4 -1
- package/dist/errors.js.map +1 -1
- package/dist/{index-BOLQxveo.d.cts → index-Dquv4xTL.d.cts} +3 -3
- package/dist/{index-CSwR2HSg.d.ts → index-w36lpSkU.d.ts} +3 -3
- package/dist/index.cjs +2796 -2651
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +8 -8
- package/dist/index.d.ts +8 -8
- package/dist/index.js +2796 -2651
- package/dist/index.js.map +1 -1
- package/dist/{payload-types-m3jjhxk9.d.cts → payload-types-DC0xzR9i.d.cts} +470 -265
- package/dist/{payload-types-m3jjhxk9.d.ts → payload-types-DC0xzR9i.d.ts} +470 -265
- package/dist/query.cjs +241 -58
- package/dist/query.cjs.map +1 -1
- package/dist/query.d.cts +151 -26
- package/dist/query.d.ts +151 -26
- package/dist/query.js +241 -58
- package/dist/query.js.map +1 -1
- package/dist/realtime.cjs +5 -1
- package/dist/realtime.cjs.map +1 -1
- package/dist/realtime.d.cts +2 -2
- package/dist/realtime.d.ts +2 -2
- package/dist/realtime.js +5 -1
- package/dist/realtime.js.map +1 -1
- package/dist/server.cjs +1117 -1
- package/dist/server.cjs.map +1 -1
- package/dist/server.d.cts +7 -7
- package/dist/server.d.ts +7 -7
- package/dist/server.js +1117 -1
- package/dist/server.js.map +1 -1
- package/dist/storefront-cache.cjs +144 -0
- package/dist/storefront-cache.cjs.map +1 -0
- package/dist/storefront-cache.d.cts +24 -0
- package/dist/storefront-cache.d.ts +24 -0
- package/dist/storefront-cache.js +121 -0
- package/dist/storefront-cache.js.map +1 -0
- package/dist/{types-Cmrd1ezc.d.ts → types-Bh2p-EMc.d.ts} +5 -1
- package/dist/{types-D0ubzQw0.d.cts → types-BvpjooU-.d.cts} +5 -1
- package/dist/{types-D2xYdz4P.d.ts → types-D5uHrPLr.d.cts} +264 -96
- package/dist/{types-CIGscmus.d.cts → types-Umd-YTjM.d.ts} +264 -96
- package/dist/ui/canvas/server.cjs +5 -1
- package/dist/ui/canvas/server.cjs.map +1 -1
- package/dist/ui/canvas/server.js +5 -1
- package/dist/ui/canvas/server.js.map +1 -1
- package/dist/ui/canvas.cjs +5 -1
- package/dist/ui/canvas.cjs.map +1 -1
- package/dist/ui/canvas.js +5 -1
- package/dist/ui/canvas.js.map +1 -1
- package/dist/ui/form.d.cts +1 -1
- package/dist/ui/form.d.ts +1 -1
- package/dist/ui/video.d.cts +1 -1
- package/dist/ui/video.d.ts +1 -1
- package/dist/webhook.d.cts +4 -4
- package/dist/webhook.d.ts +4 -4
- package/package.json +13 -3
- package/dist/const-6XHz_jej.d.ts +0 -32
- package/dist/const-B5KT72c7.d.cts +0 -32
package/dist/client.js
CHANGED
|
@@ -600,12 +600,35 @@ function productDetailCatalogQuery(params) {
|
|
|
600
600
|
}
|
|
601
601
|
return `/api/products/detail/catalog?${search}`;
|
|
602
602
|
}
|
|
603
|
-
function listingGroupsQuery(params) {
|
|
604
|
-
return `/api/products/listing-groups?ids=${params.productIds.map(encodeURIComponent).join(",")}`;
|
|
605
|
-
}
|
|
606
603
|
function listingGroupsCatalogQuery(params) {
|
|
607
604
|
return `/api/products/listing-groups/catalog?ids=${params.productIds.map(encodeURIComponent).join(",")}`;
|
|
608
605
|
}
|
|
606
|
+
function appendListingGroupsQuerySearchParams(search, options) {
|
|
607
|
+
if (options?.page != null) search.set("page", String(options.page));
|
|
608
|
+
if (options?.limit != null) search.set("limit", String(options.limit));
|
|
609
|
+
if (options?.sort != null) {
|
|
610
|
+
const sort = options.sort;
|
|
611
|
+
search.set(
|
|
612
|
+
"sort",
|
|
613
|
+
Array.isArray(sort) ? sort.join(",") : sort
|
|
614
|
+
);
|
|
615
|
+
}
|
|
616
|
+
if (options?.where != null) {
|
|
617
|
+
search.set("whereJson", JSON.stringify(options.where));
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
function listingGroupsQueryUrl(options) {
|
|
621
|
+
const search = new URLSearchParams();
|
|
622
|
+
appendListingGroupsQuerySearchParams(search, options);
|
|
623
|
+
const query = search.toString();
|
|
624
|
+
return `/api/products/listing-groups/query${query ? `?${query}` : ""}`;
|
|
625
|
+
}
|
|
626
|
+
function listingGroupsQueryCatalogUrl(options) {
|
|
627
|
+
const search = new URLSearchParams();
|
|
628
|
+
appendListingGroupsQuerySearchParams(search, options);
|
|
629
|
+
const query = search.toString();
|
|
630
|
+
return `/api/products/listing-groups/query/catalog${query ? `?${query}` : ""}`;
|
|
631
|
+
}
|
|
609
632
|
function stockSnapshotQuery(params) {
|
|
610
633
|
return `/api/products/stock?variantIds=${params.variantIds.map(encodeURIComponent).join(",")}`;
|
|
611
634
|
}
|
|
@@ -745,7 +768,20 @@ var HttpClient = class {
|
|
|
745
768
|
};
|
|
746
769
|
|
|
747
770
|
// src/core/collection/query-builder.ts
|
|
771
|
+
var PUBLIC_READ_DEPTH_ERROR = "Publishable collection reads require depth: 0. Use a shaped commerce endpoint or server client for populated data.";
|
|
772
|
+
var PUBLIC_READ_JOINS_ERROR = "Publishable collection reads require joins: false. Use a shaped commerce endpoint or server client for joined data.";
|
|
773
|
+
var PUBLIC_READ_POPULATE_ERROR = "Publishable collection reads do not support populate. Use a shaped commerce endpoint or server client for populated data.";
|
|
748
774
|
function withDefaultPublicReadOptions(options) {
|
|
775
|
+
const rawOptions = options;
|
|
776
|
+
if (rawOptions?.depth !== void 0 && rawOptions.depth !== 0) {
|
|
777
|
+
throw createValidationError(PUBLIC_READ_DEPTH_ERROR);
|
|
778
|
+
}
|
|
779
|
+
if (rawOptions?.joins !== void 0 && rawOptions.joins !== false) {
|
|
780
|
+
throw createValidationError(PUBLIC_READ_JOINS_ERROR);
|
|
781
|
+
}
|
|
782
|
+
if (rawOptions?.populate !== void 0) {
|
|
783
|
+
throw createValidationError(PUBLIC_READ_POPULATE_ERROR);
|
|
784
|
+
}
|
|
749
785
|
return {
|
|
750
786
|
...options,
|
|
751
787
|
depth: options?.depth ?? 0,
|
|
@@ -772,7 +808,7 @@ var ReadOnlyCollectionQueryBuilder = class {
|
|
|
772
808
|
async count(options) {
|
|
773
809
|
return this.api.requestCount(
|
|
774
810
|
`/api/${String(this.collection)}/count`,
|
|
775
|
-
options
|
|
811
|
+
withDefaultPublicReadOptions(options)
|
|
776
812
|
);
|
|
777
813
|
}
|
|
778
814
|
};
|
|
@@ -1208,6 +1244,55 @@ var CommunityClient = class extends CustomerScopedApi {
|
|
|
1208
1244
|
}
|
|
1209
1245
|
};
|
|
1210
1246
|
|
|
1247
|
+
// src/core/content/content-client.ts
|
|
1248
|
+
var PublicContentApi = class extends CustomerScopedApi {
|
|
1249
|
+
get(endpoint) {
|
|
1250
|
+
return this.request(endpoint, { method: "GET" });
|
|
1251
|
+
}
|
|
1252
|
+
};
|
|
1253
|
+
function appendCommonParams(search, options, defaultSort) {
|
|
1254
|
+
search.set("sort", options?.sort ?? defaultSort);
|
|
1255
|
+
if (options?.page != null) search.set("page", String(options.page));
|
|
1256
|
+
if (options?.limit != null) search.set("limit", String(options.limit));
|
|
1257
|
+
}
|
|
1258
|
+
function linksListPath(options) {
|
|
1259
|
+
const search = new URLSearchParams();
|
|
1260
|
+
appendCommonParams(search, options, "-publishedAt,-createdAt");
|
|
1261
|
+
if (options?.categoryId) search.set("categoryId", options.categoryId);
|
|
1262
|
+
if (options?.categorySlug) {
|
|
1263
|
+
search.set("categorySlug", options.categorySlug);
|
|
1264
|
+
}
|
|
1265
|
+
if (options?.tagId) search.set("tagId", options.tagId);
|
|
1266
|
+
if (options?.tagSlug) search.set("tagSlug", options.tagSlug);
|
|
1267
|
+
if (options?.featured != null) search.set("featured", String(options.featured));
|
|
1268
|
+
return `/api/links/storefront?${search}`;
|
|
1269
|
+
}
|
|
1270
|
+
function galleryItemsListPath(options) {
|
|
1271
|
+
const search = new URLSearchParams();
|
|
1272
|
+
appendCommonParams(search, options, "manual");
|
|
1273
|
+
if (options?.galleryId) search.set("galleryId", options.galleryId);
|
|
1274
|
+
if (options?.gallerySlug) search.set("gallerySlug", options.gallerySlug);
|
|
1275
|
+
return `/api/gallery-items/storefront?${search}`;
|
|
1276
|
+
}
|
|
1277
|
+
var ContentClient = class {
|
|
1278
|
+
constructor(options) {
|
|
1279
|
+
const api = new PublicContentApi("ContentClient", {
|
|
1280
|
+
publishableKey: options.publishableKey,
|
|
1281
|
+
apiUrl: options.apiUrl,
|
|
1282
|
+
onRequestId: options.onRequestId,
|
|
1283
|
+
requiresCredential: false
|
|
1284
|
+
});
|
|
1285
|
+
this.links = {
|
|
1286
|
+
list: (params) => api.get(linksListPath(params))
|
|
1287
|
+
};
|
|
1288
|
+
this.galleryItems = {
|
|
1289
|
+
list: (params) => api.get(
|
|
1290
|
+
galleryItemsListPath(params)
|
|
1291
|
+
)
|
|
1292
|
+
};
|
|
1293
|
+
}
|
|
1294
|
+
};
|
|
1295
|
+
|
|
1211
1296
|
// src/core/customer/customer-auth.ts
|
|
1212
1297
|
var DEFAULT_TIMEOUT2 = 15e3;
|
|
1213
1298
|
function safeGetItem(key) {
|
|
@@ -1366,6 +1451,19 @@ var CustomerAuth = class {
|
|
|
1366
1451
|
const params = new URLSearchParams();
|
|
1367
1452
|
if (options?.page) params.set("page", String(options.page));
|
|
1368
1453
|
if (options?.limit) params.set("limit", String(options.limit));
|
|
1454
|
+
if (options?.displayFinancialStatus) {
|
|
1455
|
+
params.set("displayFinancialStatus", options.displayFinancialStatus);
|
|
1456
|
+
}
|
|
1457
|
+
if (options?.displayFulfillmentStatus) {
|
|
1458
|
+
params.set("displayFulfillmentStatus", options.displayFulfillmentStatus);
|
|
1459
|
+
}
|
|
1460
|
+
if (options?.returnStatus) params.set("returnStatus", options.returnStatus);
|
|
1461
|
+
if (options?.primaryDisplayStatus) {
|
|
1462
|
+
params.set("primaryDisplayStatus", options.primaryDisplayStatus);
|
|
1463
|
+
}
|
|
1464
|
+
if (options?.returnDisplayStatus) {
|
|
1465
|
+
params.set("returnDisplayStatus", options.returnDisplayStatus);
|
|
1466
|
+
}
|
|
1369
1467
|
if (options?.status) params.set("status", options.status);
|
|
1370
1468
|
const qs = params.toString();
|
|
1371
1469
|
return this.requestJson(`/api/customers/me/orders${qs ? `?${qs}` : ""}`, {
|
|
@@ -1463,6 +1561,1019 @@ var CartApi = class extends CustomerScopedApi {
|
|
|
1463
1561
|
}
|
|
1464
1562
|
};
|
|
1465
1563
|
|
|
1564
|
+
// src/utils/product-selection-media.ts
|
|
1565
|
+
function selectedSwatchMediaItemId(swatch) {
|
|
1566
|
+
if (!swatch || swatch.type !== "media") return null;
|
|
1567
|
+
const id = swatch.mediaItemId;
|
|
1568
|
+
if (id == null || id === "") return null;
|
|
1569
|
+
return String(id);
|
|
1570
|
+
}
|
|
1571
|
+
function getMediaId(value) {
|
|
1572
|
+
if (typeof value === "string" || typeof value === "number") {
|
|
1573
|
+
return String(value);
|
|
1574
|
+
}
|
|
1575
|
+
if (typeof value === "object" && value !== null && "id" in value) {
|
|
1576
|
+
const id = value.id;
|
|
1577
|
+
if (typeof id === "string" || typeof id === "number") return String(id);
|
|
1578
|
+
}
|
|
1579
|
+
return null;
|
|
1580
|
+
}
|
|
1581
|
+
function mediaArray(value) {
|
|
1582
|
+
if (!Array.isArray(value)) return [];
|
|
1583
|
+
return value.filter((entry) => entry != null);
|
|
1584
|
+
}
|
|
1585
|
+
function uniqueWithPrimaryFirst(primary, items) {
|
|
1586
|
+
const unique = /* @__PURE__ */ new Map();
|
|
1587
|
+
for (const item of items) {
|
|
1588
|
+
const id = getMediaId(item);
|
|
1589
|
+
const key = id ?? `inline:${unique.size}`;
|
|
1590
|
+
if (!unique.has(key)) unique.set(key, item);
|
|
1591
|
+
}
|
|
1592
|
+
if (primary) {
|
|
1593
|
+
const primaryId = getMediaId(primary);
|
|
1594
|
+
const prefixed = /* @__PURE__ */ new Map();
|
|
1595
|
+
const primaryKey = primaryId ?? "inline:primary";
|
|
1596
|
+
prefixed.set(primaryKey, primary);
|
|
1597
|
+
for (const [key, value] of unique.entries()) {
|
|
1598
|
+
if (!prefixed.has(key)) prefixed.set(key, value);
|
|
1599
|
+
}
|
|
1600
|
+
return Array.from(prefixed.values());
|
|
1601
|
+
}
|
|
1602
|
+
return Array.from(unique.values());
|
|
1603
|
+
}
|
|
1604
|
+
function buildPoolById(pool) {
|
|
1605
|
+
const poolById = /* @__PURE__ */ new Map();
|
|
1606
|
+
for (const item of pool) {
|
|
1607
|
+
const id = getMediaId(item);
|
|
1608
|
+
if (id) poolById.set(id, item);
|
|
1609
|
+
}
|
|
1610
|
+
return poolById;
|
|
1611
|
+
}
|
|
1612
|
+
function resolveVariantImageItems(variant, poolById) {
|
|
1613
|
+
if (!variant || !Array.isArray(variant.images)) return [];
|
|
1614
|
+
const resolved = [];
|
|
1615
|
+
for (const entry of variant.images) {
|
|
1616
|
+
if (entry == null) continue;
|
|
1617
|
+
if (typeof entry === "string" || typeof entry === "number") {
|
|
1618
|
+
const pooled = poolById.get(String(entry));
|
|
1619
|
+
if (pooled) resolved.push(pooled);
|
|
1620
|
+
continue;
|
|
1621
|
+
}
|
|
1622
|
+
const mediaId = getMediaId(entry);
|
|
1623
|
+
if (mediaId) {
|
|
1624
|
+
const pooled = poolById.get(mediaId);
|
|
1625
|
+
if (pooled) {
|
|
1626
|
+
resolved.push(pooled);
|
|
1627
|
+
}
|
|
1628
|
+
continue;
|
|
1629
|
+
}
|
|
1630
|
+
}
|
|
1631
|
+
return resolved;
|
|
1632
|
+
}
|
|
1633
|
+
function resolveOptionSwatchPrimary(selectedOptionValues, poolById) {
|
|
1634
|
+
if (!selectedOptionValues?.length) return null;
|
|
1635
|
+
for (const optionValue of selectedOptionValues) {
|
|
1636
|
+
const swatch = optionValue?.swatch;
|
|
1637
|
+
const swatchPointer = selectedSwatchMediaItemId(swatch);
|
|
1638
|
+
if (swatchPointer) {
|
|
1639
|
+
const pooled = poolById.get(swatchPointer);
|
|
1640
|
+
if (pooled) return pooled;
|
|
1641
|
+
}
|
|
1642
|
+
const inline = swatch?.inlineMedia;
|
|
1643
|
+
if (inline != null && typeof inline === "object") {
|
|
1644
|
+
return inline;
|
|
1645
|
+
}
|
|
1646
|
+
}
|
|
1647
|
+
return null;
|
|
1648
|
+
}
|
|
1649
|
+
function resolveProductSelectionMedia(input) {
|
|
1650
|
+
const pool = mediaArray(input.productMediaPool);
|
|
1651
|
+
const poolById = buildPoolById(pool);
|
|
1652
|
+
const selectedVariantImages = resolveVariantImageItems(
|
|
1653
|
+
input.selectedVariant,
|
|
1654
|
+
poolById
|
|
1655
|
+
);
|
|
1656
|
+
if (selectedVariantImages.length > 0) {
|
|
1657
|
+
const primaryImage = selectedVariantImages[0] ?? null;
|
|
1658
|
+
return {
|
|
1659
|
+
primaryImage,
|
|
1660
|
+
images: uniqueWithPrimaryFirst(primaryImage, selectedVariantImages),
|
|
1661
|
+
source: "variant_media_selected"
|
|
1662
|
+
};
|
|
1663
|
+
}
|
|
1664
|
+
if (input.selectedVariant == null && (input.matchingVariants?.length ?? 0) > 0) {
|
|
1665
|
+
const mergedMatchingImages = [];
|
|
1666
|
+
for (const matchingVariant of input.matchingVariants ?? []) {
|
|
1667
|
+
mergedMatchingImages.push(
|
|
1668
|
+
...resolveVariantImageItems(matchingVariant, poolById)
|
|
1669
|
+
);
|
|
1670
|
+
}
|
|
1671
|
+
if (mergedMatchingImages.length > 0) {
|
|
1672
|
+
const primaryImage = mergedMatchingImages[0] ?? null;
|
|
1673
|
+
return {
|
|
1674
|
+
primaryImage,
|
|
1675
|
+
images: uniqueWithPrimaryFirst(primaryImage, mergedMatchingImages),
|
|
1676
|
+
source: "variant_media_matching"
|
|
1677
|
+
};
|
|
1678
|
+
}
|
|
1679
|
+
}
|
|
1680
|
+
const optionSwatchPrimary = resolveOptionSwatchPrimary(
|
|
1681
|
+
input.selectedOptionValues,
|
|
1682
|
+
poolById
|
|
1683
|
+
);
|
|
1684
|
+
if (optionSwatchPrimary) {
|
|
1685
|
+
return {
|
|
1686
|
+
primaryImage: optionSwatchPrimary,
|
|
1687
|
+
images: [optionSwatchPrimary],
|
|
1688
|
+
source: "option_swatch"
|
|
1689
|
+
};
|
|
1690
|
+
}
|
|
1691
|
+
return {
|
|
1692
|
+
primaryImage: null,
|
|
1693
|
+
images: [],
|
|
1694
|
+
source: "none"
|
|
1695
|
+
};
|
|
1696
|
+
}
|
|
1697
|
+
function resolveFeaturedImagePointer(input) {
|
|
1698
|
+
const pool = mediaArray(input.productMediaPool);
|
|
1699
|
+
const resolvedPointer = getMediaId(input.resolvedPrimary);
|
|
1700
|
+
if (resolvedPointer && input.resolvedSource !== "product_pool" && input.resolvedSource !== "none") {
|
|
1701
|
+
return resolvedPointer;
|
|
1702
|
+
}
|
|
1703
|
+
const poolById = buildPoolById(pool);
|
|
1704
|
+
const featuredImagePointer = getMediaId(input.featuredImage);
|
|
1705
|
+
if (featuredImagePointer && poolById.has(featuredImagePointer)) {
|
|
1706
|
+
return featuredImagePointer;
|
|
1707
|
+
}
|
|
1708
|
+
if (pool.length > 0) {
|
|
1709
|
+
const firstPoolId = getMediaId(pool[0]);
|
|
1710
|
+
if (firstPoolId) return firstPoolId;
|
|
1711
|
+
}
|
|
1712
|
+
return null;
|
|
1713
|
+
}
|
|
1714
|
+
|
|
1715
|
+
// src/utils/ecommerce.ts
|
|
1716
|
+
var DEFAULT_PRODUCT_SELECTION_URL_EMIT = "slug-compat";
|
|
1717
|
+
function resolveProductSelectionUrlEmit(emit) {
|
|
1718
|
+
return emit ?? DEFAULT_PRODUCT_SELECTION_URL_EMIT;
|
|
1719
|
+
}
|
|
1720
|
+
function appendSlugCompatSelectionParam(params, matrix, optionId, valueId) {
|
|
1721
|
+
const option = matrix.optionById.get(optionId);
|
|
1722
|
+
const value = matrix.valueById.get(valueId);
|
|
1723
|
+
if (!option?.slug || !value?.slug) return false;
|
|
1724
|
+
const slugMatches = option.values.filter(
|
|
1725
|
+
(candidate) => candidate.slug === value.slug
|
|
1726
|
+
);
|
|
1727
|
+
if (slugMatches.length !== 1) return false;
|
|
1728
|
+
params.append(`opt.${option.slug}`, value.slug);
|
|
1729
|
+
return true;
|
|
1730
|
+
}
|
|
1731
|
+
function appendCanonicalSelectionParam(params, optionId, valueId) {
|
|
1732
|
+
params.append(`opt.${optionId}`, valueId);
|
|
1733
|
+
}
|
|
1734
|
+
var ProductSelectionCodecError = class extends Error {
|
|
1735
|
+
constructor(message) {
|
|
1736
|
+
super(message);
|
|
1737
|
+
this.code = "ambiguous_product_selection_query";
|
|
1738
|
+
this.name = "ProductSelectionCodecError";
|
|
1739
|
+
}
|
|
1740
|
+
};
|
|
1741
|
+
function getRelationID(value) {
|
|
1742
|
+
if (typeof value === "string") return value;
|
|
1743
|
+
if (typeof value === "number") return String(value);
|
|
1744
|
+
if (value && typeof value === "object" && "id" in value) {
|
|
1745
|
+
const id = value.id;
|
|
1746
|
+
if (typeof id === "string") return id;
|
|
1747
|
+
if (typeof id === "number") return String(id);
|
|
1748
|
+
}
|
|
1749
|
+
return void 0;
|
|
1750
|
+
}
|
|
1751
|
+
function extractEntityId(value) {
|
|
1752
|
+
if (typeof value === "string") return value;
|
|
1753
|
+
if (typeof value === "number") return String(value);
|
|
1754
|
+
if (value && typeof value === "object" && "id" in value) {
|
|
1755
|
+
if (typeof value.id === "string") return value.id;
|
|
1756
|
+
if (typeof value.id === "number") return String(value.id);
|
|
1757
|
+
}
|
|
1758
|
+
return null;
|
|
1759
|
+
}
|
|
1760
|
+
function resolveGenericListingPrimaryImage(product, resolvedPrimary, resolvedSource) {
|
|
1761
|
+
return resolveFeaturedImagePointer({
|
|
1762
|
+
productMediaPool: product?.images ?? [],
|
|
1763
|
+
featuredImage: product?.featuredImage ?? null,
|
|
1764
|
+
resolvedPrimary,
|
|
1765
|
+
resolvedSource
|
|
1766
|
+
});
|
|
1767
|
+
}
|
|
1768
|
+
function normalizeProductOptionValueSwatch(swatch) {
|
|
1769
|
+
if (swatch == null) return null;
|
|
1770
|
+
if (typeof swatch !== "object") return null;
|
|
1771
|
+
const raw = swatch;
|
|
1772
|
+
const mediaItemId = extractEntityId(raw.mediaItemId);
|
|
1773
|
+
const color = typeof raw.color === "string" ? raw.color.trim() : "";
|
|
1774
|
+
const hasColor = color.length > 0;
|
|
1775
|
+
if (raw.type === "media" || mediaItemId && raw.type !== "color") {
|
|
1776
|
+
if (!mediaItemId || hasColor) return null;
|
|
1777
|
+
return {
|
|
1778
|
+
type: "media",
|
|
1779
|
+
mediaItemId,
|
|
1780
|
+
color: null
|
|
1781
|
+
};
|
|
1782
|
+
}
|
|
1783
|
+
if (raw.type === "color" || hasColor) {
|
|
1784
|
+
if (mediaItemId || !hasColor) return null;
|
|
1785
|
+
return { type: "color", color, mediaItemId: null };
|
|
1786
|
+
}
|
|
1787
|
+
return null;
|
|
1788
|
+
}
|
|
1789
|
+
function matrixOrder(index) {
|
|
1790
|
+
return String(index).padStart(6, "0");
|
|
1791
|
+
}
|
|
1792
|
+
function buildProductOptionMatrixFromDetail(detail) {
|
|
1793
|
+
const normalizedOptions = detail.options.map((option, optionIndex) => ({
|
|
1794
|
+
id: String(option.id),
|
|
1795
|
+
title: option.title || String(option.id),
|
|
1796
|
+
slug: option.slug,
|
|
1797
|
+
order: matrixOrder(optionIndex),
|
|
1798
|
+
values: option.values.map((value, valueIndex) => ({
|
|
1799
|
+
id: String(value.id),
|
|
1800
|
+
optionId: String(option.id),
|
|
1801
|
+
optionSlug: option.slug,
|
|
1802
|
+
label: value.value || value.slug || String(value.id),
|
|
1803
|
+
slug: value.slug,
|
|
1804
|
+
swatch: normalizeProductOptionValueSwatch(value.swatch),
|
|
1805
|
+
order: matrixOrder(valueIndex)
|
|
1806
|
+
}))
|
|
1807
|
+
}));
|
|
1808
|
+
const optionById = new Map(
|
|
1809
|
+
normalizedOptions.map((option) => [option.id, option])
|
|
1810
|
+
);
|
|
1811
|
+
const optionBySlug = new Map(
|
|
1812
|
+
normalizedOptions.map((option) => [option.slug, option])
|
|
1813
|
+
);
|
|
1814
|
+
const valueById = /* @__PURE__ */ new Map();
|
|
1815
|
+
const valueToOptionId = /* @__PURE__ */ new Map();
|
|
1816
|
+
const valueToOptionSlug = /* @__PURE__ */ new Map();
|
|
1817
|
+
for (const option of normalizedOptions) {
|
|
1818
|
+
for (const value of option.values) {
|
|
1819
|
+
valueById.set(value.id, value);
|
|
1820
|
+
valueToOptionId.set(value.id, option.id);
|
|
1821
|
+
valueToOptionSlug.set(value.id, option.slug);
|
|
1822
|
+
}
|
|
1823
|
+
}
|
|
1824
|
+
const optionIds = normalizedOptions.map((option) => option.id);
|
|
1825
|
+
const optionSlugs = normalizedOptions.map((option) => option.slug);
|
|
1826
|
+
const normalizedVariants = detail.variants.map((variant) => {
|
|
1827
|
+
const optionValueByOptionId = /* @__PURE__ */ new Map();
|
|
1828
|
+
const optionValueByOptionSlug = /* @__PURE__ */ new Map();
|
|
1829
|
+
for (const rawValue of variant.optionValues) {
|
|
1830
|
+
const optionId = String(rawValue.optionId);
|
|
1831
|
+
const valueId = String(rawValue.valueId);
|
|
1832
|
+
const optionSlug = rawValue.optionSlug;
|
|
1833
|
+
if (!optionById.has(optionId)) continue;
|
|
1834
|
+
if (valueToOptionId.get(valueId) !== optionId) continue;
|
|
1835
|
+
if (optionValueByOptionId.has(optionId)) continue;
|
|
1836
|
+
optionValueByOptionId.set(optionId, valueId);
|
|
1837
|
+
if (optionSlug && !optionValueByOptionSlug.has(optionSlug)) {
|
|
1838
|
+
optionValueByOptionSlug.set(optionSlug, valueId);
|
|
1839
|
+
}
|
|
1840
|
+
}
|
|
1841
|
+
const optionValueIds = optionIds.map((optionId) => optionValueByOptionId.get(optionId)).filter((valueId) => Boolean(valueId));
|
|
1842
|
+
return {
|
|
1843
|
+
id: String(variant.id),
|
|
1844
|
+
optionValueIds,
|
|
1845
|
+
optionValueByOptionId,
|
|
1846
|
+
optionValueByOptionSlug,
|
|
1847
|
+
source: variant
|
|
1848
|
+
};
|
|
1849
|
+
});
|
|
1850
|
+
return {
|
|
1851
|
+
options: normalizedOptions,
|
|
1852
|
+
optionIds,
|
|
1853
|
+
optionSlugs,
|
|
1854
|
+
optionById,
|
|
1855
|
+
optionBySlug,
|
|
1856
|
+
valueById,
|
|
1857
|
+
valueToOptionId,
|
|
1858
|
+
valueToOptionSlug,
|
|
1859
|
+
variants: normalizedVariants
|
|
1860
|
+
};
|
|
1861
|
+
}
|
|
1862
|
+
function getVariantSelection(matrix, variantId) {
|
|
1863
|
+
if (variantId == null) return void 0;
|
|
1864
|
+
const id = String(variantId);
|
|
1865
|
+
return matrix.variants.find((variant) => variant.id === id);
|
|
1866
|
+
}
|
|
1867
|
+
function hasExplicitSelection(selection) {
|
|
1868
|
+
return Boolean(
|
|
1869
|
+
selection.variantId != null || selection.search || selection.valueIds || Object.keys(selection.byOptionId ?? {}).length > 0 || Object.keys(selection.byOptionSlug ?? {}).length > 0
|
|
1870
|
+
);
|
|
1871
|
+
}
|
|
1872
|
+
function assignSelectedValue(matrix, selectedByOptionId, optionId, valueId) {
|
|
1873
|
+
if (valueId == null) return false;
|
|
1874
|
+
const normalizedValueId = String(valueId);
|
|
1875
|
+
if (matrix.valueToOptionId.get(normalizedValueId) !== optionId) return false;
|
|
1876
|
+
selectedByOptionId.set(optionId, normalizedValueId);
|
|
1877
|
+
return true;
|
|
1878
|
+
}
|
|
1879
|
+
function assignSelectedValueSlugByOptionId(matrix, selectedByOptionId, optionId, valueSlug) {
|
|
1880
|
+
if (!valueSlug) return false;
|
|
1881
|
+
const option = matrix.optionById.get(optionId);
|
|
1882
|
+
if (!option) return false;
|
|
1883
|
+
const values = option.values.filter(
|
|
1884
|
+
(candidate) => candidate.slug === valueSlug
|
|
1885
|
+
);
|
|
1886
|
+
if (values.length > 1) {
|
|
1887
|
+
throw new ProductSelectionCodecError(
|
|
1888
|
+
`Ambiguous product selection value slug "${valueSlug}" for option "${optionId}". Use opt.<optionId>=<valueId>.`
|
|
1889
|
+
);
|
|
1890
|
+
}
|
|
1891
|
+
const value = values[0];
|
|
1892
|
+
if (!value) return false;
|
|
1893
|
+
selectedByOptionId.set(optionId, value.id);
|
|
1894
|
+
return true;
|
|
1895
|
+
}
|
|
1896
|
+
function assignSelectedValueSlugByOptionSlug(matrix, selectedByOptionId, optionSlug, valueSlug) {
|
|
1897
|
+
if (!valueSlug) return false;
|
|
1898
|
+
const option = matrix.optionBySlug.get(optionSlug);
|
|
1899
|
+
if (!option) return false;
|
|
1900
|
+
const values = option.values.filter(
|
|
1901
|
+
(candidate) => candidate.slug === valueSlug
|
|
1902
|
+
);
|
|
1903
|
+
if (values.length > 1) {
|
|
1904
|
+
throw new ProductSelectionCodecError(
|
|
1905
|
+
`Ambiguous product selection value slug "${valueSlug}" for option "${optionSlug}". Use opt.<optionId>=<valueId>.`
|
|
1906
|
+
);
|
|
1907
|
+
}
|
|
1908
|
+
const value = values[0];
|
|
1909
|
+
if (!value) return false;
|
|
1910
|
+
selectedByOptionId.set(option.id, value.id);
|
|
1911
|
+
return true;
|
|
1912
|
+
}
|
|
1913
|
+
function toSearchParams(search) {
|
|
1914
|
+
if (!search) return new URLSearchParams();
|
|
1915
|
+
if (search instanceof URLSearchParams) return new URLSearchParams(search);
|
|
1916
|
+
if (search instanceof URL) return new URLSearchParams(search.searchParams);
|
|
1917
|
+
const trimmed = search.trim();
|
|
1918
|
+
if (!trimmed) return new URLSearchParams();
|
|
1919
|
+
try {
|
|
1920
|
+
if (/^[a-z][a-z0-9+.-]*:\/\//i.test(trimmed)) {
|
|
1921
|
+
return new URL(trimmed).searchParams;
|
|
1922
|
+
}
|
|
1923
|
+
} catch {
|
|
1924
|
+
return new URLSearchParams();
|
|
1925
|
+
}
|
|
1926
|
+
return new URLSearchParams(
|
|
1927
|
+
trimmed.startsWith("?") ? trimmed.slice(1) : trimmed
|
|
1928
|
+
);
|
|
1929
|
+
}
|
|
1930
|
+
function slugLike(value) {
|
|
1931
|
+
return value.trim().toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "");
|
|
1932
|
+
}
|
|
1933
|
+
function assertNoAmbiguousSelectionParams(matrix, params) {
|
|
1934
|
+
const knownSelectionKeys = /* @__PURE__ */ new Set();
|
|
1935
|
+
const hasVariantParam = params.has("variant");
|
|
1936
|
+
const hasOptionParams = Array.from(params.keys()).some(
|
|
1937
|
+
(key) => key.startsWith("opt.")
|
|
1938
|
+
);
|
|
1939
|
+
if (hasVariantParam && hasOptionParams) {
|
|
1940
|
+
throw new ProductSelectionCodecError(
|
|
1941
|
+
"Product selection URL cannot mix variant=<variantId> with opt.<optionId>=<valueId> params."
|
|
1942
|
+
);
|
|
1943
|
+
}
|
|
1944
|
+
for (const option of matrix.options) {
|
|
1945
|
+
knownSelectionKeys.add(slugLike(option.slug));
|
|
1946
|
+
knownSelectionKeys.add(slugLike(option.title));
|
|
1947
|
+
for (const value of option.values) {
|
|
1948
|
+
if (value.slug) knownSelectionKeys.add(slugLike(value.slug));
|
|
1949
|
+
}
|
|
1950
|
+
}
|
|
1951
|
+
for (const [key, value] of params.entries()) {
|
|
1952
|
+
if (key.startsWith("opt.")) {
|
|
1953
|
+
const optionToken = key.slice(4);
|
|
1954
|
+
if (!optionToken || !matrix.optionBySlug.has(optionToken) && !matrix.optionById.has(optionToken)) {
|
|
1955
|
+
throw new ProductSelectionCodecError(
|
|
1956
|
+
`Unknown product selection query parameter "${key}". Use opt.<optionId>=<valueId>.`
|
|
1957
|
+
);
|
|
1958
|
+
}
|
|
1959
|
+
if (!value) {
|
|
1960
|
+
throw new ProductSelectionCodecError(
|
|
1961
|
+
`Product selection query parameter "${key}" requires a value ID or compatibility value slug.`
|
|
1962
|
+
);
|
|
1963
|
+
}
|
|
1964
|
+
continue;
|
|
1965
|
+
}
|
|
1966
|
+
if (key === "variant") {
|
|
1967
|
+
if (!value) {
|
|
1968
|
+
throw new ProductSelectionCodecError(
|
|
1969
|
+
'Product selection query parameter "variant" requires a variant ID.'
|
|
1970
|
+
);
|
|
1971
|
+
}
|
|
1972
|
+
if (!getVariantSelection(matrix, value)) {
|
|
1973
|
+
throw new ProductSelectionCodecError(
|
|
1974
|
+
`Unknown product selection variant "${value}".`
|
|
1975
|
+
);
|
|
1976
|
+
}
|
|
1977
|
+
continue;
|
|
1978
|
+
}
|
|
1979
|
+
const keyToken = slugLike(key);
|
|
1980
|
+
if (knownSelectionKeys.has(keyToken) || value === "" && knownSelectionKeys.has(keyToken)) {
|
|
1981
|
+
throw new ProductSelectionCodecError(
|
|
1982
|
+
`Ambiguous product selection query parameter "${key}". Use opt.<optionId>=<valueId>.`
|
|
1983
|
+
);
|
|
1984
|
+
}
|
|
1985
|
+
}
|
|
1986
|
+
}
|
|
1987
|
+
function emitCompatibilityOptionIdParam(options, event) {
|
|
1988
|
+
try {
|
|
1989
|
+
if (options?.onCompatibilityOptionIdParam) {
|
|
1990
|
+
options.onCompatibilityOptionIdParam(event);
|
|
1991
|
+
return;
|
|
1992
|
+
}
|
|
1993
|
+
options?.onLegacyOptionIdParam?.(event);
|
|
1994
|
+
} catch {
|
|
1995
|
+
}
|
|
1996
|
+
}
|
|
1997
|
+
function assignSearchSelection(matrix, selectedByOptionId, search, options) {
|
|
1998
|
+
if (!search) return null;
|
|
1999
|
+
const params = toSearchParams(search);
|
|
2000
|
+
assertNoAmbiguousSelectionParams(matrix, params);
|
|
2001
|
+
const variantParam = params.get("variant");
|
|
2002
|
+
if (variantParam != null) {
|
|
2003
|
+
const variantSelection = getVariantSelection(matrix, variantParam);
|
|
2004
|
+
if (!variantSelection) {
|
|
2005
|
+
throw new ProductSelectionCodecError(
|
|
2006
|
+
`Unknown product selection variant "${variantParam}".`
|
|
2007
|
+
);
|
|
2008
|
+
}
|
|
2009
|
+
for (const [optionId, valueId] of variantSelection.optionValueByOptionId) {
|
|
2010
|
+
selectedByOptionId.set(optionId, valueId);
|
|
2011
|
+
}
|
|
2012
|
+
return variantSelection.id;
|
|
2013
|
+
}
|
|
2014
|
+
for (const [key, valueToken] of params.entries()) {
|
|
2015
|
+
if (!key.startsWith("opt.")) continue;
|
|
2016
|
+
const optionToken = key.slice(4);
|
|
2017
|
+
const optionById = matrix.optionById.get(optionToken);
|
|
2018
|
+
if (optionById) {
|
|
2019
|
+
if (assignSelectedValue(
|
|
2020
|
+
matrix,
|
|
2021
|
+
selectedByOptionId,
|
|
2022
|
+
optionById.id,
|
|
2023
|
+
valueToken
|
|
2024
|
+
)) {
|
|
2025
|
+
continue;
|
|
2026
|
+
}
|
|
2027
|
+
const before = selectedByOptionId.get(optionById.id);
|
|
2028
|
+
if (assignSelectedValueSlugByOptionId(
|
|
2029
|
+
matrix,
|
|
2030
|
+
selectedByOptionId,
|
|
2031
|
+
optionById.id,
|
|
2032
|
+
valueToken
|
|
2033
|
+
) && selectedByOptionId.get(optionById.id) !== before) {
|
|
2034
|
+
emitCompatibilityOptionIdParam(options, {
|
|
2035
|
+
optionId: optionById.id,
|
|
2036
|
+
optionSlug: optionById.slug,
|
|
2037
|
+
valueSlug: valueToken,
|
|
2038
|
+
searchParam: key
|
|
2039
|
+
});
|
|
2040
|
+
continue;
|
|
2041
|
+
}
|
|
2042
|
+
throw new ProductSelectionCodecError(
|
|
2043
|
+
`Unknown product selection value "${valueToken}" for option "${optionToken}". Use opt.<optionId>=<valueId>.`
|
|
2044
|
+
);
|
|
2045
|
+
}
|
|
2046
|
+
const optionBySlug = matrix.optionBySlug.get(optionToken);
|
|
2047
|
+
if (optionBySlug) {
|
|
2048
|
+
if (assignSelectedValueSlugByOptionSlug(
|
|
2049
|
+
matrix,
|
|
2050
|
+
selectedByOptionId,
|
|
2051
|
+
optionBySlug.slug,
|
|
2052
|
+
valueToken
|
|
2053
|
+
)) {
|
|
2054
|
+
continue;
|
|
2055
|
+
}
|
|
2056
|
+
if (matrix.valueById.has(valueToken)) {
|
|
2057
|
+
throw new ProductSelectionCodecError(
|
|
2058
|
+
`Unknown product selection value "${valueToken}" for option "${optionToken}". Use opt.<optionId>=<valueId>.`
|
|
2059
|
+
);
|
|
2060
|
+
}
|
|
2061
|
+
throw new ProductSelectionCodecError(
|
|
2062
|
+
`Unknown product selection value "${valueToken}" for option "${optionToken}". Use opt.<optionSlug>=<valueSlug> for compatibility URLs.`
|
|
2063
|
+
);
|
|
2064
|
+
}
|
|
2065
|
+
}
|
|
2066
|
+
return null;
|
|
2067
|
+
}
|
|
2068
|
+
function normalizeProductSelectionFromMatrix(matrix, selection = {}, options) {
|
|
2069
|
+
const selectedByOptionId = /* @__PURE__ */ new Map();
|
|
2070
|
+
const variantSelection = getVariantSelection(matrix, selection.variantId);
|
|
2071
|
+
const variantId = variantSelection?.id ?? null;
|
|
2072
|
+
if (variantSelection) {
|
|
2073
|
+
for (const [optionId, valueId] of variantSelection.optionValueByOptionId) {
|
|
2074
|
+
selectedByOptionId.set(optionId, valueId);
|
|
2075
|
+
}
|
|
2076
|
+
}
|
|
2077
|
+
for (const rawValueId of selection.valueIds ?? []) {
|
|
2078
|
+
const valueId = getRelationID(rawValueId);
|
|
2079
|
+
if (!valueId) continue;
|
|
2080
|
+
const optionId = matrix.valueToOptionId.get(valueId);
|
|
2081
|
+
if (!optionId) continue;
|
|
2082
|
+
selectedByOptionId.set(optionId, valueId);
|
|
2083
|
+
}
|
|
2084
|
+
const searchVariantId = assignSearchSelection(
|
|
2085
|
+
matrix,
|
|
2086
|
+
selectedByOptionId,
|
|
2087
|
+
selection.search,
|
|
2088
|
+
options
|
|
2089
|
+
);
|
|
2090
|
+
for (const [rawOptionId, rawSelection] of Object.entries(
|
|
2091
|
+
selection.byOptionId ?? {}
|
|
2092
|
+
)) {
|
|
2093
|
+
const optionId = String(rawOptionId);
|
|
2094
|
+
if (!matrix.optionById.has(optionId)) continue;
|
|
2095
|
+
if (rawSelection && typeof rawSelection === "object" && "valueId" in rawSelection && rawSelection.valueId != null) {
|
|
2096
|
+
assignSelectedValue(
|
|
2097
|
+
matrix,
|
|
2098
|
+
selectedByOptionId,
|
|
2099
|
+
optionId,
|
|
2100
|
+
rawSelection.valueId
|
|
2101
|
+
);
|
|
2102
|
+
continue;
|
|
2103
|
+
}
|
|
2104
|
+
if (typeof rawSelection === "string" || typeof rawSelection === "number") {
|
|
2105
|
+
assignSelectedValue(matrix, selectedByOptionId, optionId, rawSelection);
|
|
2106
|
+
continue;
|
|
2107
|
+
}
|
|
2108
|
+
if (rawSelection && typeof rawSelection === "object" && "valueSlug" in rawSelection) {
|
|
2109
|
+
assignSelectedValueSlugByOptionId(
|
|
2110
|
+
matrix,
|
|
2111
|
+
selectedByOptionId,
|
|
2112
|
+
optionId,
|
|
2113
|
+
rawSelection.valueSlug
|
|
2114
|
+
);
|
|
2115
|
+
}
|
|
2116
|
+
}
|
|
2117
|
+
for (const [rawOptionSlug, rawSelection] of Object.entries(
|
|
2118
|
+
selection.byOptionSlug ?? {}
|
|
2119
|
+
)) {
|
|
2120
|
+
const optionSlug = String(rawOptionSlug);
|
|
2121
|
+
if (!matrix.optionBySlug.has(optionSlug)) continue;
|
|
2122
|
+
if (rawSelection && typeof rawSelection === "object" && "valueId" in rawSelection && rawSelection.valueId != null) {
|
|
2123
|
+
const option = matrix.optionBySlug.get(optionSlug);
|
|
2124
|
+
if (option) {
|
|
2125
|
+
assignSelectedValue(
|
|
2126
|
+
matrix,
|
|
2127
|
+
selectedByOptionId,
|
|
2128
|
+
option.id,
|
|
2129
|
+
rawSelection.valueId
|
|
2130
|
+
);
|
|
2131
|
+
}
|
|
2132
|
+
continue;
|
|
2133
|
+
}
|
|
2134
|
+
if (rawSelection && typeof rawSelection === "object" && "valueSlug" in rawSelection) {
|
|
2135
|
+
assignSelectedValueSlugByOptionSlug(
|
|
2136
|
+
matrix,
|
|
2137
|
+
selectedByOptionId,
|
|
2138
|
+
optionSlug,
|
|
2139
|
+
rawSelection.valueSlug
|
|
2140
|
+
);
|
|
2141
|
+
continue;
|
|
2142
|
+
}
|
|
2143
|
+
if (typeof rawSelection === "string" || typeof rawSelection === "number") {
|
|
2144
|
+
assignSelectedValueSlugByOptionSlug(
|
|
2145
|
+
matrix,
|
|
2146
|
+
selectedByOptionId,
|
|
2147
|
+
optionSlug,
|
|
2148
|
+
String(rawSelection)
|
|
2149
|
+
);
|
|
2150
|
+
}
|
|
2151
|
+
}
|
|
2152
|
+
const byOptionId = Object.fromEntries(
|
|
2153
|
+
matrix.optionIds.map((optionId) => [optionId, selectedByOptionId.get(optionId)]).filter((entry) => Boolean(entry[1]))
|
|
2154
|
+
);
|
|
2155
|
+
const byOptionSlug = Object.fromEntries(
|
|
2156
|
+
matrix.options.map((option) => {
|
|
2157
|
+
const valueId = selectedByOptionId.get(option.id);
|
|
2158
|
+
const value = valueId ? matrix.valueById.get(valueId) : void 0;
|
|
2159
|
+
return [option.slug, value?.slug ?? void 0];
|
|
2160
|
+
}).filter((entry) => Boolean(entry[1]))
|
|
2161
|
+
);
|
|
2162
|
+
return {
|
|
2163
|
+
byOptionSlug,
|
|
2164
|
+
byOptionId,
|
|
2165
|
+
valueIds: matrix.optionIds.map((optionId) => byOptionId[optionId]).filter((valueId) => Boolean(valueId)),
|
|
2166
|
+
variantId: searchVariantId ?? variantId
|
|
2167
|
+
};
|
|
2168
|
+
}
|
|
2169
|
+
function stringifyProductSelection(detail, selection = {}, options) {
|
|
2170
|
+
const matrix = buildProductOptionMatrixFromDetail(detail);
|
|
2171
|
+
const normalized = normalizeProductSelectionFromMatrix(
|
|
2172
|
+
matrix,
|
|
2173
|
+
selection,
|
|
2174
|
+
options
|
|
2175
|
+
);
|
|
2176
|
+
const params = new URLSearchParams();
|
|
2177
|
+
if (hasExplicitSelection(selection)) {
|
|
2178
|
+
const matchingVariants = getMatchingVariantEntries(matrix, normalized);
|
|
2179
|
+
const exactVariant = getExactSelectedVariantEntry(
|
|
2180
|
+
matrix,
|
|
2181
|
+
normalized,
|
|
2182
|
+
matchingVariants
|
|
2183
|
+
);
|
|
2184
|
+
if (exactVariant) {
|
|
2185
|
+
params.set("variant", exactVariant.id);
|
|
2186
|
+
return params.toString();
|
|
2187
|
+
}
|
|
2188
|
+
}
|
|
2189
|
+
const emit = resolveProductSelectionUrlEmit(options?.emit);
|
|
2190
|
+
for (const optionId of matrix.optionIds) {
|
|
2191
|
+
const valueId = normalized.byOptionId[optionId];
|
|
2192
|
+
if (!valueId) continue;
|
|
2193
|
+
if (!matrix.optionById.has(optionId) || !matrix.valueById.has(valueId)) {
|
|
2194
|
+
continue;
|
|
2195
|
+
}
|
|
2196
|
+
if (emit === "slug-compat") {
|
|
2197
|
+
if (appendSlugCompatSelectionParam(params, matrix, optionId, valueId)) {
|
|
2198
|
+
continue;
|
|
2199
|
+
}
|
|
2200
|
+
}
|
|
2201
|
+
appendCanonicalSelectionParam(params, optionId, valueId);
|
|
2202
|
+
}
|
|
2203
|
+
return params.toString();
|
|
2204
|
+
}
|
|
2205
|
+
function selectedEntries(selection) {
|
|
2206
|
+
return Object.entries(selection.byOptionId);
|
|
2207
|
+
}
|
|
2208
|
+
function getMatchingVariantEntries(matrix, selection) {
|
|
2209
|
+
const entries = selectedEntries(selection);
|
|
2210
|
+
if (entries.length === 0) return matrix.variants;
|
|
2211
|
+
return matrix.variants.filter(
|
|
2212
|
+
(variant) => entries.every(
|
|
2213
|
+
([optionId, valueId]) => variant.optionValueByOptionId.get(optionId) === valueId
|
|
2214
|
+
)
|
|
2215
|
+
);
|
|
2216
|
+
}
|
|
2217
|
+
function getExactSelectedVariantEntry(matrix, selection, matchingVariants) {
|
|
2218
|
+
if (matrix.optionIds.length === 0) {
|
|
2219
|
+
return getVariantSelection(matrix, selection.variantId) ?? (matchingVariants.length === 1 ? matchingVariants[0] ?? null : null);
|
|
2220
|
+
}
|
|
2221
|
+
const allOptionsSelected = matrix.optionIds.every(
|
|
2222
|
+
(optionId) => Boolean(selection.byOptionId[optionId])
|
|
2223
|
+
);
|
|
2224
|
+
if (!allOptionsSelected) return null;
|
|
2225
|
+
return matchingVariants.find(
|
|
2226
|
+
(variant) => matrix.optionIds.every(
|
|
2227
|
+
(optionId) => variant.optionValueByOptionId.get(optionId) === selection.byOptionId[optionId]
|
|
2228
|
+
)
|
|
2229
|
+
) ?? null;
|
|
2230
|
+
}
|
|
2231
|
+
function isPresentMedia(value) {
|
|
2232
|
+
return value != null;
|
|
2233
|
+
}
|
|
2234
|
+
function mediaArray2(values) {
|
|
2235
|
+
if (!Array.isArray(values)) return [];
|
|
2236
|
+
return values.filter(isPresentMedia);
|
|
2237
|
+
}
|
|
2238
|
+
function getProductHrefSlug(product) {
|
|
2239
|
+
if ("product" in product && product.product?.slug) {
|
|
2240
|
+
return product.product.slug;
|
|
2241
|
+
}
|
|
2242
|
+
if ("slug" in product && product.slug) return product.slug;
|
|
2243
|
+
throw new ProductSelectionCodecError(
|
|
2244
|
+
"Product slug is required to build a product href."
|
|
2245
|
+
);
|
|
2246
|
+
}
|
|
2247
|
+
function joinProductPath(basePath, slug, trailingSlash) {
|
|
2248
|
+
const base = basePath.replace(/\/+$/, "");
|
|
2249
|
+
const encodedSlug = encodeURIComponent(slug);
|
|
2250
|
+
return `${base}/${encodedSlug}${trailingSlash ? "/" : ""}`;
|
|
2251
|
+
}
|
|
2252
|
+
function getProductHrefGroupSelection(group, matrix) {
|
|
2253
|
+
if (!group) return null;
|
|
2254
|
+
if (group.variantId != null) return { variantId: group.variantId };
|
|
2255
|
+
const optionId = group.optionId != null ? String(group.optionId) : group.optionSlug ? matrix?.optionBySlug.get(group.optionSlug)?.id : void 0;
|
|
2256
|
+
if (!optionId) return null;
|
|
2257
|
+
const option = matrix?.optionById.get(optionId);
|
|
2258
|
+
const optionValueId = group.optionValueId != null ? String(group.optionValueId) : group.optionValueSlug && option ? option.values.find((value) => value.slug === group.optionValueSlug)?.id : void 0;
|
|
2259
|
+
if (!optionValueId) return null;
|
|
2260
|
+
return { byOptionId: { [optionId]: optionValueId } };
|
|
2261
|
+
}
|
|
2262
|
+
function getProductHrefCodecOptions(options) {
|
|
2263
|
+
return options.emit != null ? { emit: options.emit } : void 0;
|
|
2264
|
+
}
|
|
2265
|
+
function appendGroupByOptionIdHrefParams(params, group, groupSelection, options) {
|
|
2266
|
+
const emit = resolveProductSelectionUrlEmit(options.emit);
|
|
2267
|
+
const byOptionId = groupSelection.byOptionId ?? {};
|
|
2268
|
+
const entries = Object.entries(byOptionId);
|
|
2269
|
+
if (entries.length === 0) return false;
|
|
2270
|
+
for (const [optionId, valueId] of entries) {
|
|
2271
|
+
const optionSlug = group?.optionSlug ?? options.matrix?.optionById.get(optionId)?.slug ?? null;
|
|
2272
|
+
const valueSlug = group?.optionValueSlug ?? options.matrix?.valueById.get(String(valueId))?.slug ?? null;
|
|
2273
|
+
if (emit === "slug-compat" && optionSlug && valueSlug && entries.length === 1) {
|
|
2274
|
+
params.set(`opt.${optionSlug}`, valueSlug);
|
|
2275
|
+
return true;
|
|
2276
|
+
}
|
|
2277
|
+
if (emit === "slug-compat" && options.matrix && appendSlugCompatSelectionParam(
|
|
2278
|
+
params,
|
|
2279
|
+
options.matrix,
|
|
2280
|
+
optionId,
|
|
2281
|
+
String(valueId)
|
|
2282
|
+
)) {
|
|
2283
|
+
continue;
|
|
2284
|
+
}
|
|
2285
|
+
appendCanonicalSelectionParam(params, optionId, String(valueId));
|
|
2286
|
+
}
|
|
2287
|
+
return params.size > 0;
|
|
2288
|
+
}
|
|
2289
|
+
function getPreferCompleteVariantFromHintSelection(group, options) {
|
|
2290
|
+
if (!options.preferCompleteVariantFromHint) return null;
|
|
2291
|
+
if (group?.optionValueId != null || group?.optionValueSlug) return null;
|
|
2292
|
+
const hintVariantId = group?.listing?.selectionHintVariant;
|
|
2293
|
+
if (hintVariantId == null) return null;
|
|
2294
|
+
return { variantId: hintVariantId };
|
|
2295
|
+
}
|
|
2296
|
+
function buildProductHref(product, group, options = {}) {
|
|
2297
|
+
const path = joinProductPath(
|
|
2298
|
+
options.basePath ?? "/products",
|
|
2299
|
+
getProductHrefSlug(product),
|
|
2300
|
+
options.trailingSlash ?? false
|
|
2301
|
+
);
|
|
2302
|
+
const params = new URLSearchParams();
|
|
2303
|
+
if (options.detail && options.selection) {
|
|
2304
|
+
const selection = stringifyProductSelection(
|
|
2305
|
+
options.detail,
|
|
2306
|
+
options.selection,
|
|
2307
|
+
getProductHrefCodecOptions(options)
|
|
2308
|
+
);
|
|
2309
|
+
return selection ? `${path}?${selection}` : path;
|
|
2310
|
+
}
|
|
2311
|
+
const preferVariantSelection = getPreferCompleteVariantFromHintSelection(
|
|
2312
|
+
group,
|
|
2313
|
+
options
|
|
2314
|
+
);
|
|
2315
|
+
if (preferVariantSelection) {
|
|
2316
|
+
if (options.detail) {
|
|
2317
|
+
const selection = stringifyProductSelection(
|
|
2318
|
+
options.detail,
|
|
2319
|
+
preferVariantSelection,
|
|
2320
|
+
getProductHrefCodecOptions(options)
|
|
2321
|
+
);
|
|
2322
|
+
return selection ? `${path}?${selection}` : path;
|
|
2323
|
+
}
|
|
2324
|
+
if (preferVariantSelection.variantId != null) {
|
|
2325
|
+
params.set("variant", String(preferVariantSelection.variantId));
|
|
2326
|
+
return `${path}?${params.toString()}`;
|
|
2327
|
+
}
|
|
2328
|
+
}
|
|
2329
|
+
const groupSelection = getProductHrefGroupSelection(group, options.matrix);
|
|
2330
|
+
if (groupSelection) {
|
|
2331
|
+
if (options.detail) {
|
|
2332
|
+
const selection = stringifyProductSelection(
|
|
2333
|
+
options.detail,
|
|
2334
|
+
groupSelection,
|
|
2335
|
+
getProductHrefCodecOptions(options)
|
|
2336
|
+
);
|
|
2337
|
+
return selection ? `${path}?${selection}` : path;
|
|
2338
|
+
}
|
|
2339
|
+
if (groupSelection.variantId != null) {
|
|
2340
|
+
params.set("variant", String(groupSelection.variantId));
|
|
2341
|
+
return `${path}?${params.toString()}`;
|
|
2342
|
+
}
|
|
2343
|
+
if (appendGroupByOptionIdHrefParams(params, group, groupSelection, options)) {
|
|
2344
|
+
return `${path}?${params.toString()}`;
|
|
2345
|
+
}
|
|
2346
|
+
}
|
|
2347
|
+
if (group?.optionValueSlug) {
|
|
2348
|
+
const optionSlug = group.optionSlug ?? (group.optionId != null ? options.matrix?.optionById.get(String(group.optionId))?.slug : void 0);
|
|
2349
|
+
if (optionSlug) {
|
|
2350
|
+
params.set(`opt.${optionSlug}`, group.optionValueSlug);
|
|
2351
|
+
}
|
|
2352
|
+
}
|
|
2353
|
+
return params.size > 0 ? `${path}?${params.toString()}` : path;
|
|
2354
|
+
}
|
|
2355
|
+
function compareVariantOrder(a, b) {
|
|
2356
|
+
const aOrder = Number(a._order ?? Number.MAX_SAFE_INTEGER);
|
|
2357
|
+
const bOrder = Number(b._order ?? Number.MAX_SAFE_INTEGER);
|
|
2358
|
+
if (Number.isFinite(aOrder) && Number.isFinite(bOrder) && aOrder !== bOrder) {
|
|
2359
|
+
return aOrder - bOrder;
|
|
2360
|
+
}
|
|
2361
|
+
const aId = String(getRelationID(a.id) ?? "");
|
|
2362
|
+
const bId = String(getRelationID(b.id) ?? "");
|
|
2363
|
+
return aId.localeCompare(bId);
|
|
2364
|
+
}
|
|
2365
|
+
function isVariantAvailableForSale(variant) {
|
|
2366
|
+
if (variant.isActive === false) return false;
|
|
2367
|
+
if (variant.isUnlimited) return true;
|
|
2368
|
+
return (variant.stock ?? 0) - (variant.reservedStock ?? 0) > 0;
|
|
2369
|
+
}
|
|
2370
|
+
function sortVariantsForMediaSelection(variants) {
|
|
2371
|
+
const orderedVariants = [...variants].sort(compareVariantOrder);
|
|
2372
|
+
const activeVariants = orderedVariants.filter(
|
|
2373
|
+
(variant) => variant.isActive !== false
|
|
2374
|
+
);
|
|
2375
|
+
const availableVariants = activeVariants.filter(isVariantAvailableForSale);
|
|
2376
|
+
const unavailableActiveVariants = activeVariants.filter(
|
|
2377
|
+
(variant) => !isVariantAvailableForSale(variant)
|
|
2378
|
+
);
|
|
2379
|
+
return [...availableVariants, ...unavailableActiveVariants];
|
|
2380
|
+
}
|
|
2381
|
+
function variantHasPoolMedia(variant, productMediaPool) {
|
|
2382
|
+
if (!variant?.images?.length) return false;
|
|
2383
|
+
return variant.images.some((image) => {
|
|
2384
|
+
const imageId = getRelationID(image);
|
|
2385
|
+
return Boolean(
|
|
2386
|
+
imageId && productMediaPool.some((poolItem) => getRelationID(poolItem) === imageId)
|
|
2387
|
+
);
|
|
2388
|
+
});
|
|
2389
|
+
}
|
|
2390
|
+
function getMinMax(values) {
|
|
2391
|
+
const numbers = values.filter(
|
|
2392
|
+
(value) => typeof value === "number"
|
|
2393
|
+
);
|
|
2394
|
+
if (numbers.length === 0) {
|
|
2395
|
+
return { min: null, max: null };
|
|
2396
|
+
}
|
|
2397
|
+
return {
|
|
2398
|
+
min: Math.min(...numbers),
|
|
2399
|
+
max: Math.max(...numbers)
|
|
2400
|
+
};
|
|
2401
|
+
}
|
|
2402
|
+
function buildProductListingGroupProjection(product, variants) {
|
|
2403
|
+
const orderedVariants = [...variants].sort(compareVariantOrder);
|
|
2404
|
+
const activeVariants = orderedVariants.filter(
|
|
2405
|
+
(variant) => variant.isActive !== false
|
|
2406
|
+
);
|
|
2407
|
+
const availableVariants = activeVariants.filter(isVariantAvailableForSale);
|
|
2408
|
+
const selectionHintVariant = availableVariants[0] ?? activeVariants[0] ?? null;
|
|
2409
|
+
const { min: minPrice, max: maxPrice } = getMinMax(
|
|
2410
|
+
activeVariants.map((variant) => variant.price)
|
|
2411
|
+
);
|
|
2412
|
+
const { min: minCompareAtPrice, max: maxCompareAtPrice } = getMinMax(
|
|
2413
|
+
activeVariants.map((variant) => variant.compareAtPrice)
|
|
2414
|
+
);
|
|
2415
|
+
const productMediaPool = product?.images ?? [];
|
|
2416
|
+
const selectionHintHasPoolMedia = variantHasPoolMedia(
|
|
2417
|
+
selectionHintVariant,
|
|
2418
|
+
productMediaPool
|
|
2419
|
+
);
|
|
2420
|
+
const mediaSelectionVariants = sortVariantsForMediaSelection(variants);
|
|
2421
|
+
const resolvedProductMedia = resolveProductSelectionMedia({
|
|
2422
|
+
productMediaPool,
|
|
2423
|
+
selectedVariant: selectionHintVariant && selectionHintHasPoolMedia ? {
|
|
2424
|
+
id: selectionHintVariant.id,
|
|
2425
|
+
images: selectionHintVariant.images
|
|
2426
|
+
} : null,
|
|
2427
|
+
matchingVariants: selectionHintHasPoolMedia ? [] : mediaSelectionVariants
|
|
2428
|
+
});
|
|
2429
|
+
return {
|
|
2430
|
+
selectionHintVariant: getRelationID(selectionHintVariant?.id) ?? null,
|
|
2431
|
+
primaryImage: resolveGenericListingPrimaryImage(
|
|
2432
|
+
product,
|
|
2433
|
+
resolvedProductMedia.primaryImage,
|
|
2434
|
+
resolvedProductMedia.source
|
|
2435
|
+
),
|
|
2436
|
+
minPrice,
|
|
2437
|
+
maxPrice,
|
|
2438
|
+
minCompareAtPrice,
|
|
2439
|
+
maxCompareAtPrice,
|
|
2440
|
+
isPriceRange: minPrice !== null && maxPrice !== null ? minPrice !== maxPrice : false,
|
|
2441
|
+
availableForSale: availableVariants.length > 0
|
|
2442
|
+
};
|
|
2443
|
+
}
|
|
2444
|
+
function buildProductListingCard(item, options = {}) {
|
|
2445
|
+
const product = item.product;
|
|
2446
|
+
const groups = item.groups;
|
|
2447
|
+
const variants = getProductListingCardVariants(item);
|
|
2448
|
+
const projectedListing = buildProductListingGroupProjection(product, variants);
|
|
2449
|
+
const selectionHintVariant = getRelationID(product.selectedOrFirstAvailableVariant) ?? projectedListing.selectionHintVariant;
|
|
2450
|
+
const representativeVariant = findListingCardRepresentativeVariant(
|
|
2451
|
+
variants,
|
|
2452
|
+
selectionHintVariant
|
|
2453
|
+
);
|
|
2454
|
+
const productMediaPool = mediaArray2(product.images);
|
|
2455
|
+
const featuredImageId = getRelationID(product.featuredImage);
|
|
2456
|
+
const featuredImageFromPool = featuredImageId ? productMediaPool.find((item2) => getRelationID(item2) === featuredImageId) : void 0;
|
|
2457
|
+
const listingPrimaryMedia = featuredImageFromPool ?? product.featuredImage ?? productMediaPool[0] ?? null;
|
|
2458
|
+
const priceRange = resolveListingCardPriceRange(
|
|
2459
|
+
product,
|
|
2460
|
+
projectedListing,
|
|
2461
|
+
groups
|
|
2462
|
+
);
|
|
2463
|
+
const availableForSale = product.availableForSale != null ? product.availableForSale : groups.length > 0 ? groups.some((group) => group.listing.availableForSale) : projectedListing.availableForSale;
|
|
2464
|
+
const swatches = groups.length > 1 ? groups.map((group) => buildListingSwatch(product, group, options)) : [];
|
|
2465
|
+
return {
|
|
2466
|
+
id: String(product.id),
|
|
2467
|
+
href: buildProductHref(
|
|
2468
|
+
{ slug: product.slug },
|
|
2469
|
+
{ listing: { selectionHintVariant } },
|
|
2470
|
+
options
|
|
2471
|
+
),
|
|
2472
|
+
title: product.title,
|
|
2473
|
+
representativeVariant,
|
|
2474
|
+
primaryImage: listingPrimaryMedia,
|
|
2475
|
+
priceRange,
|
|
2476
|
+
availableForSale,
|
|
2477
|
+
swatches
|
|
2478
|
+
};
|
|
2479
|
+
}
|
|
2480
|
+
function getProductListingCardVariants(item) {
|
|
2481
|
+
const productVariants = item.product.variants?.docs;
|
|
2482
|
+
if (Array.isArray(productVariants) && productVariants.length > 0) {
|
|
2483
|
+
return productVariants;
|
|
2484
|
+
}
|
|
2485
|
+
const variants = [];
|
|
2486
|
+
const seen = /* @__PURE__ */ new Set();
|
|
2487
|
+
for (const group of item.groups) {
|
|
2488
|
+
for (const variant of group.variants) {
|
|
2489
|
+
const id = getRelationID(variant.id);
|
|
2490
|
+
if (id && seen.has(id)) continue;
|
|
2491
|
+
if (id) seen.add(id);
|
|
2492
|
+
variants.push(variant);
|
|
2493
|
+
}
|
|
2494
|
+
}
|
|
2495
|
+
return variants;
|
|
2496
|
+
}
|
|
2497
|
+
function findListingCardRepresentativeVariant(variants, representativeVariantId) {
|
|
2498
|
+
if (representativeVariantId == null) return null;
|
|
2499
|
+
return variants.find(
|
|
2500
|
+
(variant) => getRelationID(variant.id) === representativeVariantId
|
|
2501
|
+
) ?? null;
|
|
2502
|
+
}
|
|
2503
|
+
function hasCompleteProductPriceRange(priceRange) {
|
|
2504
|
+
return priceRange?.minVariantPrice?.amount != null && priceRange?.maxVariantPrice?.amount != null;
|
|
2505
|
+
}
|
|
2506
|
+
function resolveListingCardPriceRange(product, projectedListing, groups) {
|
|
2507
|
+
if (hasCompleteProductPriceRange(product.priceRange)) {
|
|
2508
|
+
const groupRange = groups.length > 0 ? aggregateListingPriceRange(groups) : null;
|
|
2509
|
+
const minCompareAtPrice = product.compareAtPriceRange?.minVariantPrice?.amount ?? groupRange?.minCompareAtPrice ?? null;
|
|
2510
|
+
const maxCompareAtPrice = product.compareAtPriceRange?.maxVariantPrice?.amount ?? groupRange?.maxCompareAtPrice ?? null;
|
|
2511
|
+
return {
|
|
2512
|
+
minPrice: product.priceRange.minVariantPrice.amount,
|
|
2513
|
+
maxPrice: product.priceRange.maxVariantPrice.amount,
|
|
2514
|
+
minCompareAtPrice,
|
|
2515
|
+
maxCompareAtPrice,
|
|
2516
|
+
isPriceRange: product.priceRange.isPriceRange ?? product.priceRange.minVariantPrice.amount !== product.priceRange.maxVariantPrice.amount
|
|
2517
|
+
};
|
|
2518
|
+
}
|
|
2519
|
+
if (groups.length > 0) {
|
|
2520
|
+
return aggregateListingPriceRange(groups);
|
|
2521
|
+
}
|
|
2522
|
+
return {
|
|
2523
|
+
minPrice: projectedListing.minPrice,
|
|
2524
|
+
maxPrice: projectedListing.maxPrice,
|
|
2525
|
+
minCompareAtPrice: projectedListing.minCompareAtPrice,
|
|
2526
|
+
maxCompareAtPrice: projectedListing.maxCompareAtPrice,
|
|
2527
|
+
isPriceRange: projectedListing.isPriceRange
|
|
2528
|
+
};
|
|
2529
|
+
}
|
|
2530
|
+
function aggregateListingPriceRange(groups) {
|
|
2531
|
+
const minPrice = minOfNullable(groups.map((g) => g.listing.minPrice));
|
|
2532
|
+
const maxPrice = maxOfNullable(groups.map((g) => g.listing.maxPrice));
|
|
2533
|
+
const minCompareAtPrice = minOfNullable(
|
|
2534
|
+
groups.map((g) => g.listing.minCompareAtPrice)
|
|
2535
|
+
);
|
|
2536
|
+
const maxCompareAtPrice = maxOfNullable(
|
|
2537
|
+
groups.map((g) => g.listing.maxCompareAtPrice)
|
|
2538
|
+
);
|
|
2539
|
+
const isPriceRange = minPrice !== null && maxPrice !== null && minPrice !== maxPrice;
|
|
2540
|
+
return {
|
|
2541
|
+
minPrice,
|
|
2542
|
+
maxPrice,
|
|
2543
|
+
minCompareAtPrice,
|
|
2544
|
+
maxCompareAtPrice,
|
|
2545
|
+
isPriceRange
|
|
2546
|
+
};
|
|
2547
|
+
}
|
|
2548
|
+
function buildListingSwatch(product, group, options) {
|
|
2549
|
+
return {
|
|
2550
|
+
optionId: group.optionId,
|
|
2551
|
+
optionValueId: group.optionValueId,
|
|
2552
|
+
label: group.optionValueLabel,
|
|
2553
|
+
swatch: group.optionValueSwatch ?? null,
|
|
2554
|
+
href: buildProductHref(
|
|
2555
|
+
{ slug: product.slug },
|
|
2556
|
+
{
|
|
2557
|
+
optionId: group.optionId,
|
|
2558
|
+
optionSlug: group.optionSlug,
|
|
2559
|
+
optionValueId: group.optionValueId,
|
|
2560
|
+
optionValueSlug: group.optionValueSlug,
|
|
2561
|
+
listing: group.listing
|
|
2562
|
+
},
|
|
2563
|
+
options
|
|
2564
|
+
),
|
|
2565
|
+
availableForSale: group.listing.availableForSale
|
|
2566
|
+
};
|
|
2567
|
+
}
|
|
2568
|
+
function minOfNullable(values) {
|
|
2569
|
+
const numbers = values.filter((v) => v !== null);
|
|
2570
|
+
return numbers.length === 0 ? null : Math.min(...numbers);
|
|
2571
|
+
}
|
|
2572
|
+
function maxOfNullable(values) {
|
|
2573
|
+
const numbers = values.filter((v) => v !== null);
|
|
2574
|
+
return numbers.length === 0 ? null : Math.max(...numbers);
|
|
2575
|
+
}
|
|
2576
|
+
|
|
1466
2577
|
// src/core/api/product-api.ts
|
|
1467
2578
|
var PRODUCT_DETAIL_UNAVAILABLE_REASONS = /* @__PURE__ */ new Set([
|
|
1468
2579
|
"not_found",
|
|
@@ -1486,6 +2597,68 @@ function productDetailResultFromError(error) {
|
|
|
1486
2597
|
if (!reason) return void 0;
|
|
1487
2598
|
return { found: false, reason };
|
|
1488
2599
|
}
|
|
2600
|
+
function nonEmpty(values) {
|
|
2601
|
+
return values?.length ? values : void 0;
|
|
2602
|
+
}
|
|
2603
|
+
function buildProductListingPageWhere(params) {
|
|
2604
|
+
const clauses = [];
|
|
2605
|
+
const search = params.search?.trim();
|
|
2606
|
+
if (search) {
|
|
2607
|
+
clauses.push({
|
|
2608
|
+
or: [
|
|
2609
|
+
{ title: { like: search } },
|
|
2610
|
+
{ slug: { like: search } },
|
|
2611
|
+
{ handle: { like: search } }
|
|
2612
|
+
]
|
|
2613
|
+
});
|
|
2614
|
+
}
|
|
2615
|
+
const filters = params.filters;
|
|
2616
|
+
const ids = nonEmpty(filters?.ids);
|
|
2617
|
+
if (ids) clauses.push({ id: { in: ids } });
|
|
2618
|
+
const slugs = nonEmpty(filters?.slugs);
|
|
2619
|
+
if (slugs) clauses.push({ slug: { in: slugs } });
|
|
2620
|
+
const handles = nonEmpty(filters?.handles);
|
|
2621
|
+
if (handles) clauses.push({ handle: { in: handles } });
|
|
2622
|
+
const categoryIds = nonEmpty(filters?.categoryIds);
|
|
2623
|
+
if (categoryIds) clauses.push({ categories: { in: categoryIds } });
|
|
2624
|
+
const tagIds = nonEmpty(filters?.tagIds);
|
|
2625
|
+
if (tagIds) clauses.push({ tags: { in: tagIds } });
|
|
2626
|
+
const minPrice = filters?.price?.min;
|
|
2627
|
+
if (minPrice != null) {
|
|
2628
|
+
clauses.push({
|
|
2629
|
+
"priceRange.maxVariantPrice.amount": { greater_than_equal: minPrice }
|
|
2630
|
+
});
|
|
2631
|
+
}
|
|
2632
|
+
const maxPrice = filters?.price?.max;
|
|
2633
|
+
if (maxPrice != null) {
|
|
2634
|
+
clauses.push({
|
|
2635
|
+
"priceRange.minVariantPrice.amount": { less_than_equal: maxPrice }
|
|
2636
|
+
});
|
|
2637
|
+
}
|
|
2638
|
+
if (filters?.availableForSale != null) {
|
|
2639
|
+
clauses.push({
|
|
2640
|
+
availableForSale: { equals: filters.availableForSale }
|
|
2641
|
+
});
|
|
2642
|
+
}
|
|
2643
|
+
if (clauses.length === 0) return void 0;
|
|
2644
|
+
if (clauses.length === 1) return clauses[0];
|
|
2645
|
+
return { and: clauses };
|
|
2646
|
+
}
|
|
2647
|
+
function buildProductListingPageUrl(params) {
|
|
2648
|
+
const options = {
|
|
2649
|
+
page: params.page,
|
|
2650
|
+
limit: params.limit,
|
|
2651
|
+
sort: params.sort,
|
|
2652
|
+
where: buildProductListingPageWhere(params)
|
|
2653
|
+
};
|
|
2654
|
+
return params.mode === "full" ? listingGroupsQueryUrl(options) : listingGroupsQueryCatalogUrl(options);
|
|
2655
|
+
}
|
|
2656
|
+
function withProductListingCards(response, options) {
|
|
2657
|
+
return {
|
|
2658
|
+
...response,
|
|
2659
|
+
cards: response.docs.map((item) => buildProductListingCard(item, options))
|
|
2660
|
+
};
|
|
2661
|
+
}
|
|
1489
2662
|
|
|
1490
2663
|
// src/core/api/order-api.ts
|
|
1491
2664
|
function idempotencyRequestOptions(idempotencyKey) {
|
|
@@ -1528,10 +2701,20 @@ var CommerceClient = class {
|
|
|
1528
2701
|
this.product = {
|
|
1529
2702
|
stockCheck: (params) => api.post("/api/products/stock-check", params),
|
|
1530
2703
|
stockSnapshot: (params) => api.get(stockSnapshotQuery(params)),
|
|
1531
|
-
listingGroups: (params) => api.get(
|
|
2704
|
+
listingGroups: (params) => api.get(
|
|
2705
|
+
listingGroupsCatalogQuery(params)
|
|
2706
|
+
),
|
|
1532
2707
|
listingGroupsCatalog: (params) => api.get(
|
|
1533
2708
|
listingGroupsCatalogQuery(params)
|
|
1534
2709
|
),
|
|
2710
|
+
listingPage: async (params = {}) => {
|
|
2711
|
+
const catalogParams = {
|
|
2712
|
+
...params,
|
|
2713
|
+
mode: "catalog"
|
|
2714
|
+
};
|
|
2715
|
+
const response = await api.get(buildProductListingPageUrl(catalogParams));
|
|
2716
|
+
return withProductListingCards(response, catalogParams);
|
|
2717
|
+
},
|
|
1535
2718
|
detail: async (params) => {
|
|
1536
2719
|
try {
|
|
1537
2720
|
const product = await api.get(productDetailQuery(params));
|
|
@@ -1736,6 +2919,11 @@ var Client = class {
|
|
|
1736
2919
|
onUnauthorized,
|
|
1737
2920
|
onRequestId
|
|
1738
2921
|
});
|
|
2922
|
+
this.content = new ContentClient({
|
|
2923
|
+
publishableKey: this.config.publishableKey,
|
|
2924
|
+
apiUrl: this.config.apiUrl,
|
|
2925
|
+
onRequestId
|
|
2926
|
+
});
|
|
1739
2927
|
this.events = new EventsClient({
|
|
1740
2928
|
publishableKey: this.config.publishableKey,
|
|
1741
2929
|
apiUrl: this.config.apiUrl,
|
|
@@ -1764,6 +2952,7 @@ function createClient(options) {
|
|
|
1764
2952
|
}
|
|
1765
2953
|
export {
|
|
1766
2954
|
Client,
|
|
2955
|
+
ContentClient,
|
|
1767
2956
|
createClient
|
|
1768
2957
|
};
|
|
1769
2958
|
//# sourceMappingURL=client.js.map
|