@tanqory/theme-kit 0.1.0 → 2.0.0-alpha.3
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/LICENSE +21 -0
- package/README.md +98 -10
- package/dist/abi/canvas-live.d.ts +49 -0
- package/dist/abi/canvas-live.js +88 -0
- package/dist/abi/cart.d.ts +48 -0
- package/dist/abi/cart.js +256 -0
- package/dist/abi/convert.d.ts +126 -0
- package/dist/abi/convert.js +493 -0
- package/dist/abi/data-api.d.ts +11 -0
- package/dist/abi/data-api.js +178 -0
- package/dist/abi/jcs.d.ts +6 -0
- package/dist/abi/jcs.js +28 -0
- package/dist/abi/page-data.d.ts +198 -0
- package/dist/abi/page-data.js +577 -0
- package/dist/abi/page.d.ts +23 -0
- package/dist/abi/page.js +46 -0
- package/dist/abi/referrals.d.ts +23 -0
- package/dist/abi/referrals.js +89 -0
- package/dist/abi/rewards.d.ts +27 -0
- package/dist/abi/rewards.js +82 -0
- package/dist/abi/seo.d.ts +47 -0
- package/dist/abi/seo.js +68 -0
- package/dist/abi/shell.d.ts +22 -0
- package/dist/abi/shell.js +10 -0
- package/dist/abi/wire.d.ts +38 -0
- package/dist/abi/wire.js +84 -0
- package/dist/build.d.ts +113 -0
- package/dist/build.js +294 -0
- package/dist/client.d.ts +19 -0
- package/dist/client.js +76 -0
- package/dist/contract.d.ts +6 -0
- package/dist/contract.js +6 -0
- package/dist/headless/ai.d.ts +132 -0
- package/dist/headless/ai.js +99 -0
- package/dist/headless/hooks.d.ts +273 -0
- package/dist/headless/hooks.js +533 -0
- package/dist/headless/i18n.d.ts +13 -0
- package/dist/headless/i18n.js +45 -0
- package/dist/headless/index.d.ts +36 -0
- package/dist/headless/index.js +38 -0
- package/dist/headless/money.d.ts +25 -0
- package/dist/headless/money.js +61 -0
- package/dist/headless/page.d.ts +43 -0
- package/dist/headless/page.js +53 -0
- package/dist/headless/platform.d.ts +55 -0
- package/dist/headless/platform.js +77 -0
- package/dist/headless/primitives/cart.d.ts +90 -0
- package/dist/headless/primitives/cart.js +153 -0
- package/dist/headless/primitives/collection.d.ts +68 -0
- package/dist/headless/primitives/collection.js +80 -0
- package/dist/headless/primitives/product.d.ts +101 -0
- package/dist/headless/primitives/product.js +117 -0
- package/dist/headless/primitives/storefront.d.ts +106 -0
- package/dist/headless/primitives/storefront.js +186 -0
- package/dist/headless/primitives/util.d.ts +19 -0
- package/dist/headless/primitives/util.js +42 -0
- package/dist/headless/strings.d.ts +9 -0
- package/dist/headless/strings.js +186 -0
- package/dist/headless/structured-data.d.ts +31 -0
- package/dist/headless/structured-data.js +57 -0
- package/dist/headless/theme.d.ts +99 -0
- package/dist/headless/theme.js +54 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.js +18 -553
- package/dist/runtime/data/StoreProvider.d.ts +56 -0
- package/dist/runtime/data/StoreProvider.js +308 -0
- package/dist/runtime/data/handle.d.ts +14 -0
- package/dist/runtime/data/handle.js +16 -0
- package/dist/runtime/data/routes.d.ts +87 -0
- package/dist/runtime/data/routes.js +154 -0
- package/dist/runtime/data/to-ui.d.ts +237 -0
- package/dist/runtime/data/to-ui.js +759 -0
- package/dist/runtime/entries/register.d.ts +9 -0
- package/dist/runtime/entries/register.js +9 -0
- package/dist/runtime/entries/sections.d.ts +16 -0
- package/dist/runtime/entries/sections.js +19 -0
- package/dist/{types → runtime/kit}/SectionTree.d.ts +2 -1
- package/dist/runtime/kit/SectionTree.js +100 -0
- package/dist/runtime/kit/analytics.d.ts +63 -0
- package/dist/runtime/kit/analytics.js +233 -0
- package/dist/runtime/kit/cart.d.ts +167 -0
- package/dist/runtime/kit/cart.js +544 -0
- package/dist/{types → runtime/kit}/composition.d.ts +1 -1
- package/dist/runtime/kit/composition.js +3 -0
- package/dist/{types → runtime/kit}/config.d.ts +2 -2
- package/dist/runtime/kit/config.js +6 -0
- package/dist/runtime/kit/consent.d.ts +29 -0
- package/dist/runtime/kit/consent.js +159 -0
- package/dist/runtime/kit/contract/content.d.ts +164 -0
- package/dist/runtime/kit/contract/content.js +83 -0
- package/dist/runtime/kit/contract/field-types.d.ts +290 -0
- package/dist/runtime/kit/contract/field-types.js +129 -0
- package/dist/runtime/kit/contract/groups.d.ts +230 -0
- package/dist/runtime/kit/contract/groups.js +402 -0
- package/dist/runtime/kit/contract/index.d.ts +33 -0
- package/dist/runtime/kit/contract/index.js +33 -0
- package/dist/runtime/kit/contract/transform.d.ts +82 -0
- package/dist/runtime/kit/contract/transform.js +133 -0
- package/dist/runtime/kit/contract/validate.d.ts +67 -0
- package/dist/runtime/kit/contract/validate.js +388 -0
- package/dist/{types → runtime/kit}/data.d.ts +276 -7
- package/dist/runtime/kit/data.js +1117 -0
- package/dist/{types → runtime/kit}/defineSection.d.ts +1 -1
- package/dist/runtime/kit/defineSection.js +10 -0
- package/dist/{types → runtime/kit}/dynamic-source.d.ts +4 -4
- package/dist/runtime/kit/dynamic-source.js +143 -0
- package/dist/runtime/kit/index.d.ts +24 -0
- package/dist/runtime/kit/index.js +24 -0
- package/dist/{types → runtime/kit}/jsx-to-json.d.ts +1 -1
- package/dist/runtime/kit/jsx-to-json.js +28 -0
- package/dist/{types → runtime/kit}/mount.d.ts +25 -4
- package/dist/runtime/kit/mount.js +124 -0
- package/dist/{types → runtime/kit}/registry.d.ts +1 -1
- package/dist/runtime/kit/registry.js +12 -0
- package/dist/{types → runtime/kit}/ssg.d.ts +9 -3
- package/dist/runtime/kit/ssg.js +56 -0
- package/dist/{types → runtime/kit}/storefront.d.ts +206 -7
- package/dist/runtime/kit/storefront.js +723 -0
- package/dist/{types → runtime/kit}/theme-context.d.ts +8 -2
- package/dist/runtime/kit/theme-context.js +25 -0
- package/dist/runtime/kit/types.d.ts +142 -0
- package/dist/runtime/kit/types.js +1 -0
- package/dist/runtime/platform/TrackingPixels.d.ts +14 -0
- package/dist/runtime/platform/TrackingPixels.js +63 -0
- package/dist/runtime/platform/environment.d.ts +68 -0
- package/dist/runtime/platform/environment.js +85 -0
- package/dist/runtime/platform/locale.d.ts +27 -0
- package/dist/runtime/platform/locale.js +42 -0
- package/dist/runtime/platform/route-analytics.d.ts +15 -0
- package/dist/runtime/platform/route-analytics.js +72 -0
- package/dist/runtime/platform/safe-href.d.ts +25 -0
- package/dist/runtime/platform/safe-href.js +69 -0
- package/dist/runtime/platform/safe-values.d.ts +37 -0
- package/dist/runtime/platform/safe-values.js +140 -0
- package/dist/runtime/render/SectionRenderer.d.ts +4 -0
- package/dist/runtime/render/SectionRenderer.js +111 -0
- package/dist/runtime/render/listing-query.d.ts +47 -0
- package/dist/runtime/render/listing-query.js +114 -0
- package/dist/runtime/render/localized-copy.d.ts +86 -0
- package/dist/runtime/render/localized-copy.js +129 -0
- package/dist/runtime/render/product-listing.d.ts +11 -0
- package/dist/runtime/render/product-listing.js +240 -0
- package/dist/runtime/render/section-actions.d.ts +44 -0
- package/dist/runtime/render/section-actions.js +169 -0
- package/dist/runtime/render/starter.d.ts +11 -0
- package/dist/runtime/render/starter.js +81 -0
- package/dist/runtime/seo/head.d.ts +87 -0
- package/dist/runtime/seo/head.js +224 -0
- package/dist/runtime/seo/structured-data.d.ts +22 -0
- package/dist/runtime/seo/structured-data.js +115 -0
- package/dist/runtime/shell/PageShell.d.ts +18 -0
- package/dist/runtime/shell/PageShell.js +113 -0
- package/dist/runtime/shell/RuntimeBridge.d.ts +14 -0
- package/dist/runtime/shell/RuntimeBridge.js +82 -0
- package/dist/runtime/shell/overlay-state.d.ts +31 -0
- package/dist/runtime/shell/overlay-state.js +59 -0
- package/dist/runtime/shell/preview-mode.d.ts +2 -0
- package/dist/runtime/shell/preview-mode.js +11 -0
- package/dist/runtime/shell/spa-router.d.ts +14 -0
- package/dist/runtime/shell/spa-router.js +76 -0
- package/dist/runtime/shell/url-redirects.d.ts +6 -0
- package/dist/runtime/shell/url-redirects.js +37 -0
- package/dist/runtime/slots.d.ts +74 -0
- package/dist/runtime/slots.js +16 -0
- package/dist/runtime.d.ts +34 -0
- package/dist/runtime.js +40 -0
- package/dist/sdk/index.d.ts +24 -0
- package/dist/sdk/index.js +24 -0
- package/dist/sdk/referrals.d.ts +62 -0
- package/dist/sdk/referrals.js +20 -0
- package/dist/sdk/rewards.d.ts +85 -0
- package/dist/sdk/rewards.js +29 -0
- package/dist/sdk/runtime-context.d.ts +120 -0
- package/dist/sdk/runtime-context.js +73 -0
- package/dist/sdk/schema.d.ts +197 -0
- package/dist/sdk/schema.js +121 -0
- package/dist/sdk/server-render.d.ts +8 -0
- package/dist/sdk/server-render.js +12 -0
- package/dist/sdk/store-lookup.d.ts +27 -0
- package/dist/sdk/store-lookup.js +29 -0
- package/dist/sdk/store.d.ts +115 -0
- package/dist/sdk/store.js +1 -0
- package/dist/sdk/sx.d.ts +40 -0
- package/dist/sdk/sx.js +112 -0
- package/dist/sdk/theme.d.ts +152 -0
- package/dist/sdk/theme.js +10 -0
- package/dist/sdk/types.d.ts +395 -0
- package/dist/sdk/types.js +7 -0
- package/dist/server.d.ts +65 -0
- package/dist/server.js +370 -0
- package/docs/headless-api.md +220 -0
- package/package.json +77 -23
- package/dist/index.js.map +0 -1
- package/dist/ssg-BZ5FYTw7.js +0 -1802
- package/dist/ssg-BZ5FYTw7.js.map +0 -1
- package/dist/ssg.js +0 -8
- package/dist/ssg.js.map +0 -1
- package/dist/types/cart.d.ts +0 -86
- package/dist/types/editor.d.ts +0 -5
- package/dist/types/index.d.ts +0 -20
- package/dist/types/preview-bridge.d.ts +0 -9
- package/dist/types/types.d.ts +0 -44
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Theme ABI v2 domain records → the shapes Nova's sections already read (`lib/tanqory/data.tsx`,
|
|
3
|
+
* `lib/tanqory/storefront.ts`). Pure functions, no I/O, no module state: the same code maps a
|
|
4
|
+
* server `ctx.data` result and a browser `/_tq/data/*` answer, so SSR and hydration agree.
|
|
5
|
+
*
|
|
6
|
+
* Where the ABI carries less than Nova's v1 data layer did (no cookie-banner config, no digital
|
|
7
|
+
* wallets), the Nova field is left absent and the section's own "not
|
|
8
|
+
* available" branch renders — never a fixture, never a guess. `Shop.socialLinks` and
|
|
9
|
+
* `paymentSettings.acceptedCardBrands` (storefront-query rev 2.20) map when the store has them.
|
|
10
|
+
*
|
|
11
|
+
* Opt-in ABI members (storefront-query rev 2.19): `Collection.filters` (facets) and
|
|
12
|
+
* `Product.collections` (the breadcrumb's collection) map when the platform sent them.
|
|
13
|
+
* Stock levels (storefront-query rev 2.21, capability `data:inventory`): `ProductVariant.quantityAvailable`
|
|
14
|
+
* (0–999, 999 = "999 or more") is present only when the merchant shows stock levels and the variant is
|
|
15
|
+
* tracked and stops selling at zero. It maps to `inventoryQuantity` under `DENY`; absent stays absent.
|
|
16
|
+
* See MISMATCHES.md for each field that has no ABI source.
|
|
17
|
+
*/
|
|
18
|
+
import type { Filter as AbiFilter, Article as AbiArticle, Blog as AbiBlog, Cart as AbiCart, Collection as AbiCollection, Image as AbiImage, Menu as AbiMenu, Page as AbiPage, Policy as AbiPolicy, Product as AbiProduct, Shop as AbiShop, SearchResult as AbiSearchResult } from '@tanqory/theme-abi';
|
|
19
|
+
import type { Collection, ImageRef, Page, Product } from '../runtime/kit/data.js';
|
|
20
|
+
import type { Article, Blog, Filter, Menu, SearchResults, Shop, ShopPolicy } from '../runtime/kit/storefront.js';
|
|
21
|
+
export declare function image(i: AbiImage | null | undefined): ImageRef | null;
|
|
22
|
+
/** Plain text of an HTML body, for the surfaces that show text only (cards, meta description). */
|
|
23
|
+
export declare function plainText(html: string | null | undefined): string;
|
|
24
|
+
export declare function product(p: AbiProduct): Product;
|
|
25
|
+
export declare function filters(list: readonly AbiFilter[] | undefined): Filter[];
|
|
26
|
+
/** The listing's filter objects (`{productVendor}`, `{available}`, `{price: {min, max}}`, …) → ABI `filters`. Unknown shapes are dropped. */
|
|
27
|
+
export declare function abiFilters(list: readonly Record<string, unknown>[] | undefined): {
|
|
28
|
+
key: string;
|
|
29
|
+
value: string;
|
|
30
|
+
}[];
|
|
31
|
+
export declare function collection(c: AbiCollection): Collection;
|
|
32
|
+
export declare function page(p: AbiPage): Page;
|
|
33
|
+
export declare function article(a: AbiArticle): Article;
|
|
34
|
+
export declare function blog(b: AbiBlog): Blog;
|
|
35
|
+
export declare function menu(m: AbiMenu): Menu;
|
|
36
|
+
export declare function policy(p: AbiPolicy): ShopPolicy;
|
|
37
|
+
export declare function shop(s: AbiShop, policies?: readonly AbiPolicy[], country?: string): Shop;
|
|
38
|
+
/** D-7 `Shop.contact` → the shop's public email / phone, each only when set. */
|
|
39
|
+
export declare function shopContact(raw: unknown): {
|
|
40
|
+
email?: string;
|
|
41
|
+
phone?: string;
|
|
42
|
+
};
|
|
43
|
+
/** D-2 `Shop.cookieBanner` → the banner config Nova renders. Null when absent or not an object (old data = today's banner). */
|
|
44
|
+
export declare function cookieBanner(raw: unknown): NonNullable<Shop['consentBanner']> | null;
|
|
45
|
+
/** D-6 `Shop.policyLinks` → the store's published policies (title + site path). Null when absent. */
|
|
46
|
+
export declare function policyLinks(raw: unknown): ShopPolicy[] | null;
|
|
47
|
+
/**
|
|
48
|
+
* D-5 `Shop.freeShipping` → the lowest "free over" threshold for the shopper's country, or null when the
|
|
49
|
+
* country's zone has none (or the platform sent nothing). The amount is already in the market's currency.
|
|
50
|
+
*/
|
|
51
|
+
export declare function freeShippingFor(raw: unknown, country: string): {
|
|
52
|
+
amount: string;
|
|
53
|
+
currencyCode: string;
|
|
54
|
+
} | null;
|
|
55
|
+
/**
|
|
56
|
+
* `Shop.seo` (the home page's title, meta description and social image — an optional ABI field the
|
|
57
|
+
* platform may not send yet), read defensively: trimmed non-empty text, an absolute `https:` image
|
|
58
|
+
* URL, positive integer sizes. Anything else is "not set". Null when nothing is set.
|
|
59
|
+
*/
|
|
60
|
+
export declare function shopSeo(raw: unknown): NonNullable<Shop['seo']> | null;
|
|
61
|
+
/**
|
|
62
|
+
* `Shop.socialLinks` → Nova's: only absolute `https:` URLs with a network slug, one link per network
|
|
63
|
+
* (the first wins). Anything else is dropped — a footer link must never point somewhere unsafe.
|
|
64
|
+
*/
|
|
65
|
+
export declare function socialLinks(list: AbiShop['socialLinks']): {
|
|
66
|
+
platform: string;
|
|
67
|
+
url: string;
|
|
68
|
+
}[];
|
|
69
|
+
export declare function searchResults(r: AbiSearchResult): SearchResults;
|
|
70
|
+
/**
|
|
71
|
+
* The cart-level amount taken off between the subtotal and the total.
|
|
72
|
+
*
|
|
73
|
+
* The ABI cart has no discount allocations, only `cost.subtotalAmount`, `cost.totalTaxAmount` and
|
|
74
|
+
* `cost.totalAmount`. Before checkout those are related by
|
|
75
|
+
* `total = subtotal − cart discount + tax`, so the discount is what reconciles the three figures
|
|
76
|
+
* the summary prints. Without it the cart showed "Subtotal $64.00 · Tax $0.00 · Estimated total
|
|
77
|
+
* $60.80" with nothing explaining the $3.20.
|
|
78
|
+
*
|
|
79
|
+
* Computed in minor units at the subtotal's own precision (never `toFixed(2)` — JPY has none, KWD
|
|
80
|
+
* three). Null when nothing was taken off, when the figures are not one currency, or when an
|
|
81
|
+
* amount is not a number: a guessed discount is worse than none.
|
|
82
|
+
*/
|
|
83
|
+
export declare function cartDiscount(c: AbiCart): {
|
|
84
|
+
amount: string;
|
|
85
|
+
currencyCode: string;
|
|
86
|
+
} | null;
|
|
87
|
+
/** The ABI cart → the lines/totals CartProvider renders (lib/tanqory/cart.tsx CartState fields). */
|
|
88
|
+
export declare function cartState(c: AbiCart): {
|
|
89
|
+
id: string;
|
|
90
|
+
version: number;
|
|
91
|
+
lines: {
|
|
92
|
+
lineSubtotal: import("@tanqory/theme-abi").Money;
|
|
93
|
+
productHandle: string;
|
|
94
|
+
attributes: {
|
|
95
|
+
key: string;
|
|
96
|
+
value: string;
|
|
97
|
+
}[];
|
|
98
|
+
availableForSale?: boolean | undefined;
|
|
99
|
+
compareAtPrice?: import("@tanqory/theme-abi").Money | undefined;
|
|
100
|
+
image: ImageRef | null;
|
|
101
|
+
price: import("@tanqory/theme-abi").Money;
|
|
102
|
+
selectedOptions: {
|
|
103
|
+
name: string;
|
|
104
|
+
value: string;
|
|
105
|
+
}[];
|
|
106
|
+
variantTitle?: string | undefined;
|
|
107
|
+
id: string;
|
|
108
|
+
variantId: string;
|
|
109
|
+
quantity: number;
|
|
110
|
+
title: string;
|
|
111
|
+
}[];
|
|
112
|
+
subtotal: import("@tanqory/theme-abi").Money;
|
|
113
|
+
total: import("@tanqory/theme-abi").Money;
|
|
114
|
+
tax: import("@tanqory/theme-abi").Money | null;
|
|
115
|
+
note: string | null;
|
|
116
|
+
totalQuantity: number;
|
|
117
|
+
checkoutUrl: string;
|
|
118
|
+
discountCodes: {
|
|
119
|
+
code: string;
|
|
120
|
+
applicable: boolean;
|
|
121
|
+
}[];
|
|
122
|
+
discountAmount: {
|
|
123
|
+
amount: string;
|
|
124
|
+
currencyCode: string;
|
|
125
|
+
} | null;
|
|
126
|
+
};
|
|
@@ -0,0 +1,493 @@
|
|
|
1
|
+
const MENU_TYPES = new Set([
|
|
2
|
+
'FRONTPAGE', 'COLLECTION', 'COLLECTIONS', 'PRODUCT', 'CATALOG', 'PAGE', 'BLOG', 'ARTICLE', 'SEARCH', 'SHOP_POLICY', 'HTTP',
|
|
3
|
+
]);
|
|
4
|
+
export function image(i) {
|
|
5
|
+
if (!i?.url)
|
|
6
|
+
return null;
|
|
7
|
+
return {
|
|
8
|
+
url: i.url,
|
|
9
|
+
...(i.altText ? { altText: i.altText } : {}),
|
|
10
|
+
...(typeof i.width === 'number' ? { width: i.width } : {}),
|
|
11
|
+
...(typeof i.height === 'number' ? { height: i.height } : {}),
|
|
12
|
+
...(i.width && i.height ? { aspectRatio: i.width / i.height } : {}),
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
/** Plain text of an HTML body, for the surfaces that show text only (cards, meta description). */
|
|
16
|
+
export function plainText(html) {
|
|
17
|
+
return String(html ?? '')
|
|
18
|
+
.replace(/<[^>]*>/g, ' ')
|
|
19
|
+
.replace(/ /g, ' ')
|
|
20
|
+
.replace(/&/g, '&')
|
|
21
|
+
.replace(/</g, '<')
|
|
22
|
+
.replace(/>/g, '>')
|
|
23
|
+
.replace(/"/g, '"')
|
|
24
|
+
.replace(/'/g, "'")
|
|
25
|
+
.replace(/\s+/g, ' ')
|
|
26
|
+
.trim();
|
|
27
|
+
}
|
|
28
|
+
/** `quantityAvailable` when the platform sent a usable one (an integer 0–999), else undefined. */
|
|
29
|
+
function stock(v) {
|
|
30
|
+
const q = v.quantityAvailable;
|
|
31
|
+
return typeof q === 'number' && Number.isInteger(q) && q >= 0 && q <= 999 ? q : undefined;
|
|
32
|
+
}
|
|
33
|
+
function variant(v) {
|
|
34
|
+
const q = stock(v);
|
|
35
|
+
return {
|
|
36
|
+
id: v.id,
|
|
37
|
+
title: v.title,
|
|
38
|
+
price: v.price,
|
|
39
|
+
compareAtPrice: v.compareAtPrice ?? null,
|
|
40
|
+
availableForSale: v.availableForSale,
|
|
41
|
+
selectedOptions: v.selectedOptions.map((o) => ({ name: o.name, value: o.value })),
|
|
42
|
+
image: image(v.image),
|
|
43
|
+
sku: v.sku ?? null,
|
|
44
|
+
// The platform sends a count only for a variant that stops selling at zero — DENY.
|
|
45
|
+
...(q !== undefined ? { inventoryQuantity: q, inventoryPolicy: 'DENY' } : {}),
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
export function product(p) {
|
|
49
|
+
const variants = p.variants?.nodes ?? [];
|
|
50
|
+
const first = variants.find((v) => v.availableForSale) ?? variants[0];
|
|
51
|
+
const min = p.priceRange.minVariantPrice;
|
|
52
|
+
const was = p.compareAtPriceRange?.maxVariantPrice;
|
|
53
|
+
return {
|
|
54
|
+
id: p.id,
|
|
55
|
+
handle: p.handle,
|
|
56
|
+
title: p.title,
|
|
57
|
+
templateSuffix: p.templateSuffix ?? null,
|
|
58
|
+
price: min,
|
|
59
|
+
compareAtPrice: was && Number(was.amount) > Number(min.amount) ? was : null,
|
|
60
|
+
featuredImage: image(p.featuredImage),
|
|
61
|
+
...(first ? { variantId: first.id } : {}),
|
|
62
|
+
availableForSale: p.availableForSale,
|
|
63
|
+
vendor: p.vendor ?? null,
|
|
64
|
+
productType: p.productType ?? null,
|
|
65
|
+
tags: [...p.tags],
|
|
66
|
+
description: plainText(p.descriptionHtml),
|
|
67
|
+
descriptionHtml: p.descriptionHtml,
|
|
68
|
+
images: p.images.map(image).filter((i) => i !== null),
|
|
69
|
+
options: p.options.map((o) => ({ name: o.name, values: [...o.values] })),
|
|
70
|
+
variants: variants.map(variant),
|
|
71
|
+
seo: { title: p.seo?.title ?? null, description: p.seo?.description ?? null },
|
|
72
|
+
...(p.metafields ? { metafields: Object.fromEntries(Object.entries(p.metafields).map(([k, v]) => [k, v == null ? null : String(v)])) } : {}),
|
|
73
|
+
...(p.collections ? { collections: p.collections.map((c) => ({ handle: c.handle, title: c.title })) } : {}),
|
|
74
|
+
...lowStock(p.lowStockThreshold),
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
/** `Product.lowStockThreshold` (storefront-query rev 2.26): the product's own low-stock threshold, when the merchant set one (D-8). */
|
|
78
|
+
function lowStock(v) {
|
|
79
|
+
return typeof v === 'number' && Number.isInteger(v) && v >= 0 && v <= 9999 ? { lowStockThreshold: v } : {};
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* ABI facet → the storefront filter Nova's listing reads (`platform/runtime/render/product-listing.ts`
|
|
83
|
+
* `RawFilter`): `input` is the JSON of the filter object the listing hands back to
|
|
84
|
+
* `collectionProducts({ filters })`, which `abiFilters` turns into the ABI `{key, value}` entries.
|
|
85
|
+
* A PRICE_RANGE facet becomes one value whose input carries the store's bounds (`{price: {min, max}}`).
|
|
86
|
+
*/
|
|
87
|
+
const FILTER_INPUT_KEY = { vendor: 'productVendor', productType: 'productType', tag: 'tag' };
|
|
88
|
+
export function filters(list) {
|
|
89
|
+
const out = [];
|
|
90
|
+
for (const f of list ?? []) {
|
|
91
|
+
if (f.type === 'PRICE_RANGE') {
|
|
92
|
+
if (!f.range)
|
|
93
|
+
continue;
|
|
94
|
+
const min = Number(f.range.min.amount);
|
|
95
|
+
const max = Number(f.range.max.amount);
|
|
96
|
+
if (!Number.isFinite(min) || !Number.isFinite(max))
|
|
97
|
+
continue;
|
|
98
|
+
out.push({ id: f.id, label: f.label, type: 'PRICE_RANGE', values: [{ id: f.id, label: f.label, count: 0, input: JSON.stringify({ price: { min, max } }) }] });
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
const values = [];
|
|
102
|
+
for (const v of f.values) {
|
|
103
|
+
const { key, value } = v.input;
|
|
104
|
+
const input = key === 'available' ? { available: value === 'true' } : FILTER_INPUT_KEY[key] ? { [FILTER_INPUT_KEY[key]]: value } : null;
|
|
105
|
+
if (input)
|
|
106
|
+
values.push({ id: v.id, label: v.label, count: v.count, input: JSON.stringify(input) });
|
|
107
|
+
}
|
|
108
|
+
if (values.length)
|
|
109
|
+
out.push({ id: f.id, label: f.label, type: f.type, values });
|
|
110
|
+
}
|
|
111
|
+
return out;
|
|
112
|
+
}
|
|
113
|
+
/** A decimal amount the data plane accepts (`^(0|[1-9][0-9]{0,14})(\.[0-9]{1,4})?$`), or null. */
|
|
114
|
+
function amount(n) {
|
|
115
|
+
if (typeof n !== 'number' || !Number.isFinite(n) || n < 0 || n >= 1e15)
|
|
116
|
+
return null;
|
|
117
|
+
return Number.isInteger(n) ? String(n) : n.toFixed(4).replace(/\.?0+$/, '');
|
|
118
|
+
}
|
|
119
|
+
/** The listing's filter objects (`{productVendor}`, `{available}`, `{price: {min, max}}`, …) → ABI `filters`. Unknown shapes are dropped. */
|
|
120
|
+
export function abiFilters(list) {
|
|
121
|
+
const out = [];
|
|
122
|
+
for (const f of list ?? []) {
|
|
123
|
+
if (typeof f.available === 'boolean')
|
|
124
|
+
out.push({ key: 'available', value: String(f.available) });
|
|
125
|
+
else if (typeof f.productVendor === 'string')
|
|
126
|
+
out.push({ key: 'vendor', value: f.productVendor });
|
|
127
|
+
else if (typeof f.productType === 'string')
|
|
128
|
+
out.push({ key: 'productType', value: f.productType });
|
|
129
|
+
else if (typeof f.tag === 'string')
|
|
130
|
+
out.push({ key: 'tag', value: f.tag });
|
|
131
|
+
else if (f.price && typeof f.price === 'object') {
|
|
132
|
+
const { min, max } = f.price;
|
|
133
|
+
const lo = amount(min);
|
|
134
|
+
const hi = amount(max);
|
|
135
|
+
if (lo !== null)
|
|
136
|
+
out.push({ key: 'price.min', value: lo });
|
|
137
|
+
if (hi !== null)
|
|
138
|
+
out.push({ key: 'price.max', value: hi });
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
return out;
|
|
142
|
+
}
|
|
143
|
+
export function collection(c) {
|
|
144
|
+
return {
|
|
145
|
+
id: c.id,
|
|
146
|
+
handle: c.handle,
|
|
147
|
+
title: c.title,
|
|
148
|
+
templateSuffix: c.templateSuffix ?? null,
|
|
149
|
+
// Nova renders a collection description as text (CollectionBanner, MainCollection).
|
|
150
|
+
description: plainText(c.descriptionHtml) || null,
|
|
151
|
+
image: c.image?.url ? { url: c.image.url, ...(c.image.altText ? { altText: c.image.altText } : {}) } : null,
|
|
152
|
+
products: (c.products?.nodes ?? []).map(product),
|
|
153
|
+
...(typeof c.products?.totalCount === 'number' ? { productsCount: c.products.totalCount } : {}),
|
|
154
|
+
seo: { title: c.seo?.title ?? null, description: c.seo?.description ?? null },
|
|
155
|
+
...(c.filters ? { filters: filters(c.filters) } : {}),
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
export function page(p) {
|
|
159
|
+
return {
|
|
160
|
+
handle: p.handle,
|
|
161
|
+
title: p.title,
|
|
162
|
+
body: p.body,
|
|
163
|
+
bodySummary: plainText(p.body).slice(0, 160),
|
|
164
|
+
templateSuffix: p.templateSuffix ?? null,
|
|
165
|
+
seo: { title: p.seo?.title ?? null, description: p.seo?.description ?? null },
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
export function article(a) {
|
|
169
|
+
return {
|
|
170
|
+
id: a.id,
|
|
171
|
+
handle: a.handle,
|
|
172
|
+
title: a.title,
|
|
173
|
+
excerpt: a.excerpt ?? null,
|
|
174
|
+
contentHtml: a.contentHtml,
|
|
175
|
+
image: image(a.image),
|
|
176
|
+
author: a.author?.name ?? null,
|
|
177
|
+
publishedAt: a.publishedAt,
|
|
178
|
+
tags: [...a.tags],
|
|
179
|
+
blogHandle: a.blog?.handle,
|
|
180
|
+
seo: { title: a.seo?.title ?? null, description: a.seo?.description ?? null },
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
export function blog(b) {
|
|
184
|
+
return {
|
|
185
|
+
id: b.id,
|
|
186
|
+
handle: b.handle,
|
|
187
|
+
title: b.title,
|
|
188
|
+
articles: (b.articles?.nodes ?? []).map((a) => ({ ...article(a), blogHandle: a.blog?.handle ?? b.handle })),
|
|
189
|
+
seo: { title: b.seo?.title ?? null, description: b.seo?.description ?? null },
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
function menuItem(i) {
|
|
193
|
+
return {
|
|
194
|
+
id: i.id,
|
|
195
|
+
title: i.title,
|
|
196
|
+
url: i.url,
|
|
197
|
+
type: (MENU_TYPES.has(i.type) ? i.type : 'HTTP'),
|
|
198
|
+
items: i.items.map(menuItem),
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
export function menu(m) {
|
|
202
|
+
return { id: `menu:${m.handle}`, handle: m.handle, title: m.title, items: m.items.map(menuItem) };
|
|
203
|
+
}
|
|
204
|
+
/** Nova's `shop.policies` slots, keyed by the policy handles the platform uses. */
|
|
205
|
+
const POLICY_SLOT = {
|
|
206
|
+
'privacy-policy': 'privacy',
|
|
207
|
+
'refund-policy': 'refund',
|
|
208
|
+
'terms-of-service': 'termsOfService',
|
|
209
|
+
'shipping-policy': 'shipping',
|
|
210
|
+
'subscription-policy': 'subscription',
|
|
211
|
+
};
|
|
212
|
+
export function policy(p) {
|
|
213
|
+
return { handle: p.handle, title: p.title, url: p.url, body: p.body };
|
|
214
|
+
}
|
|
215
|
+
export function shop(s, policies = [], country = '') {
|
|
216
|
+
const slots = {};
|
|
217
|
+
for (const p of policies) {
|
|
218
|
+
const slot = POLICY_SLOT[p.handle];
|
|
219
|
+
slots[slot ?? p.handle] = policy(p);
|
|
220
|
+
}
|
|
221
|
+
const primary = s.brand?.colors?.primary;
|
|
222
|
+
const social = socialLinks(s.socialLinks);
|
|
223
|
+
let host = '';
|
|
224
|
+
try {
|
|
225
|
+
host = new URL(s.primaryDomain.url).host;
|
|
226
|
+
}
|
|
227
|
+
catch {
|
|
228
|
+
host = '';
|
|
229
|
+
}
|
|
230
|
+
const seo = shopSeo(s.seo);
|
|
231
|
+
const extra = s;
|
|
232
|
+
const contact = shopContact(extra.contact);
|
|
233
|
+
const banner = cookieBanner(extra.cookieBanner);
|
|
234
|
+
const links = policyLinks(extra.policyLinks);
|
|
235
|
+
const freeShipping = freeShippingFor(extra.freeShipping, country);
|
|
236
|
+
return {
|
|
237
|
+
name: s.name,
|
|
238
|
+
description: s.description ?? null,
|
|
239
|
+
...(seo ? { seo } : {}),
|
|
240
|
+
...contact,
|
|
241
|
+
...(extra.taxesIncluded === true ? { taxesIncluded: true } : {}),
|
|
242
|
+
...(freeShipping ? { freeShippingThreshold: freeShipping } : {}),
|
|
243
|
+
...(banner ? { consentBanner: banner } : {}),
|
|
244
|
+
...(extra.accounts && typeof extra.accounts === 'object' && extra.accounts.signInLinks === false ? { signInLinks: false } : {}),
|
|
245
|
+
...(links ? { policyLinks: links } : {}),
|
|
246
|
+
brand: s.brand
|
|
247
|
+
? {
|
|
248
|
+
logo: image(s.brand.logo),
|
|
249
|
+
squareLogo: image(s.brand.squareLogo),
|
|
250
|
+
...(primary ? { colors: { primary: [{ background: primary }], secondary: [] } } : {}),
|
|
251
|
+
}
|
|
252
|
+
: null,
|
|
253
|
+
policies: slots,
|
|
254
|
+
primaryDomain: { host, url: s.primaryDomain.url, sslEnabled: s.primaryDomain.url.startsWith('https:') },
|
|
255
|
+
...(social.length ? { socialLinks: social } : {}),
|
|
256
|
+
paymentSettings: { acceptedCardBrands: [...(s.paymentSettings.acceptedCardBrands ?? [])], supportedDigitalWallets: [], currencyCode: s.paymentSettings.currencyCode },
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
const text = (v, max = 2000) => (typeof v === 'string' && v.trim() ? v.trim().slice(0, max) : undefined);
|
|
260
|
+
/** D-7 `Shop.contact` → the shop's public email / phone, each only when set. */
|
|
261
|
+
export function shopContact(raw) {
|
|
262
|
+
if (!raw || typeof raw !== 'object')
|
|
263
|
+
return {};
|
|
264
|
+
const r = raw;
|
|
265
|
+
const email = text(r.email, 255);
|
|
266
|
+
const phone = text(r.phone, 50);
|
|
267
|
+
return {
|
|
268
|
+
...(email && /^[^\s@<>"]+@[^\s@<>"]+\.[^\s@<>"]+$/.test(email) ? { email } : {}),
|
|
269
|
+
...(phone && /^[0-9+()\-. ]+$/.test(phone) ? { phone } : {}),
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
/** A link target the banner may use: a site path or an absolute https URL. */
|
|
273
|
+
function safeHref(v) {
|
|
274
|
+
const t = text(v, 2048);
|
|
275
|
+
if (!t)
|
|
276
|
+
return undefined;
|
|
277
|
+
if (t.startsWith('/') && !t.startsWith('//'))
|
|
278
|
+
return t;
|
|
279
|
+
try {
|
|
280
|
+
const u = new URL(t);
|
|
281
|
+
return u.protocol === 'https:' && u.hostname ? u.href : undefined;
|
|
282
|
+
}
|
|
283
|
+
catch {
|
|
284
|
+
return undefined;
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
const POSITIONS = new Set(['bottom', 'bottom-left', 'bottom-right', 'bottom-center', 'center']);
|
|
288
|
+
const BANNER_TEXT = ['title', 'body', 'privacyLinkLabel', 'acceptLabel', 'declineLabel', 'manageLabel'];
|
|
289
|
+
const PREFS_TEXT = [
|
|
290
|
+
'title', 'introTitle', 'introBody', 'acceptAllLabel', 'declineAllLabel', 'saveLabel', 'bulletTitle',
|
|
291
|
+
'requiredTitle', 'requiredDescription', 'personalizationTitle', 'personalizationDescription',
|
|
292
|
+
'marketingTitle', 'marketingDescription', 'analyticsTitle', 'analyticsDescription',
|
|
293
|
+
];
|
|
294
|
+
function texts(raw, keys) {
|
|
295
|
+
const out = {};
|
|
296
|
+
if (!raw || typeof raw !== 'object')
|
|
297
|
+
return out;
|
|
298
|
+
for (const k of keys) {
|
|
299
|
+
const v = text(raw[k]);
|
|
300
|
+
if (v)
|
|
301
|
+
out[k] = v;
|
|
302
|
+
}
|
|
303
|
+
return out;
|
|
304
|
+
}
|
|
305
|
+
/** D-2 `Shop.cookieBanner` → the banner config Nova renders. Null when absent or not an object (old data = today's banner). */
|
|
306
|
+
export function cookieBanner(raw) {
|
|
307
|
+
if (!raw || typeof raw !== 'object')
|
|
308
|
+
return null;
|
|
309
|
+
const r = raw;
|
|
310
|
+
const position = typeof r.position === 'string' && POSITIONS.has(r.position) ? r.position : 'bottom';
|
|
311
|
+
const colorTheme = r.colorTheme === 'dark' || r.colorTheme === 'custom' ? r.colorTheme : 'light';
|
|
312
|
+
const regions = Array.isArray(r.regions) ? r.regions.filter((x) => typeof x === 'string' && /^[A-Z]{2}(-[A-Z0-9]{1,3})?$/.test(x)) : [];
|
|
313
|
+
const privacyLinkUrl = safeHref(r.privacyLinkUrl);
|
|
314
|
+
const t = texts(r.text, BANNER_TEXT);
|
|
315
|
+
const p = texts(r.preferences, PREFS_TEXT);
|
|
316
|
+
const prefs = r.preferences && typeof r.preferences === 'object' ? r.preferences : null;
|
|
317
|
+
const bullets = prefs?.bulletsEnabled === true && Array.isArray(prefs.bullets) ? prefs.bullets.map((b) => text(b, 500)).filter((b) => Boolean(b)).slice(0, 3) : [];
|
|
318
|
+
return {
|
|
319
|
+
enabled: r.enabled === true,
|
|
320
|
+
// "Use automated settings" — never saved means on (contract rev 2).
|
|
321
|
+
automated: r.automated !== false,
|
|
322
|
+
position,
|
|
323
|
+
colorTheme,
|
|
324
|
+
regions,
|
|
325
|
+
...(privacyLinkUrl ? { privacyLinkUrl } : {}),
|
|
326
|
+
text: t,
|
|
327
|
+
preferences: { ...p, ...(bullets.length ? { bullets } : {}) },
|
|
328
|
+
};
|
|
329
|
+
}
|
|
330
|
+
/** D-6 `Shop.policyLinks` → the store's published policies (title + site path). Null when absent. */
|
|
331
|
+
export function policyLinks(raw) {
|
|
332
|
+
if (!Array.isArray(raw))
|
|
333
|
+
return null;
|
|
334
|
+
const out = [];
|
|
335
|
+
for (const l of raw) {
|
|
336
|
+
const handle = typeof l?.handle === 'string' && /^[a-z0-9][a-z0-9-]{0,63}$/.test(l.handle) ? l.handle : '';
|
|
337
|
+
const title = text(l?.title, 200);
|
|
338
|
+
if (!handle || !title || out.some((o) => o.handle === handle))
|
|
339
|
+
continue;
|
|
340
|
+
out.push({ handle, title, url: `/policies/${handle}` });
|
|
341
|
+
}
|
|
342
|
+
return out;
|
|
343
|
+
}
|
|
344
|
+
/**
|
|
345
|
+
* D-5 `Shop.freeShipping` → the lowest "free over" threshold for the shopper's country, or null when the
|
|
346
|
+
* country's zone has none (or the platform sent nothing). The amount is already in the market's currency.
|
|
347
|
+
*/
|
|
348
|
+
export function freeShippingFor(raw, country) {
|
|
349
|
+
if (!Array.isArray(raw) || !country)
|
|
350
|
+
return null;
|
|
351
|
+
const c = country.toUpperCase();
|
|
352
|
+
for (const e of raw) {
|
|
353
|
+
if (!e || !Array.isArray(e.countries) || !e.countries.includes(c))
|
|
354
|
+
continue;
|
|
355
|
+
const m = e.minimumSubtotal;
|
|
356
|
+
if (m && typeof m.amount === 'string' && /^\d{1,15}(\.\d{1,4})?$/.test(m.amount) && typeof m.currencyCode === 'string' && /^[A-Z]{3}$/.test(m.currencyCode)) {
|
|
357
|
+
return { amount: m.amount, currencyCode: m.currencyCode };
|
|
358
|
+
}
|
|
359
|
+
return null;
|
|
360
|
+
}
|
|
361
|
+
return null;
|
|
362
|
+
}
|
|
363
|
+
/**
|
|
364
|
+
* `Shop.seo` (the home page's title, meta description and social image — an optional ABI field the
|
|
365
|
+
* platform may not send yet), read defensively: trimmed non-empty text, an absolute `https:` image
|
|
366
|
+
* URL, positive integer sizes. Anything else is "not set". Null when nothing is set.
|
|
367
|
+
*/
|
|
368
|
+
export function shopSeo(raw) {
|
|
369
|
+
if (!raw || typeof raw !== 'object')
|
|
370
|
+
return null;
|
|
371
|
+
const r = raw;
|
|
372
|
+
const text = (v, max) => (typeof v === 'string' && v.trim() ? v.trim().slice(0, max) : undefined);
|
|
373
|
+
const size = (v) => (typeof v === 'number' && Number.isInteger(v) && v > 0 ? v : undefined);
|
|
374
|
+
const title = text(r.title, 300);
|
|
375
|
+
const description = text(r.description, 1000);
|
|
376
|
+
let image;
|
|
377
|
+
const img = r.image;
|
|
378
|
+
if (img && typeof img === 'object' && typeof img.url === 'string') {
|
|
379
|
+
try {
|
|
380
|
+
const u = new URL(img.url);
|
|
381
|
+
if (u.protocol === 'https:' && u.hostname) {
|
|
382
|
+
const alt = text(img.altText, 500);
|
|
383
|
+
const width = size(img.width);
|
|
384
|
+
const height = size(img.height);
|
|
385
|
+
image = { url: u.href, ...(alt ? { altText: alt } : {}), ...(width ? { width } : {}), ...(height ? { height } : {}) };
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
catch {
|
|
389
|
+
/* not a URL */
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
if (!title && !description && !image)
|
|
393
|
+
return null;
|
|
394
|
+
return { ...(title ? { title } : {}), ...(description ? { description } : {}), ...(image ? { image } : {}) };
|
|
395
|
+
}
|
|
396
|
+
/**
|
|
397
|
+
* `Shop.socialLinks` → Nova's: only absolute `https:` URLs with a network slug, one link per network
|
|
398
|
+
* (the first wins). Anything else is dropped — a footer link must never point somewhere unsafe.
|
|
399
|
+
*/
|
|
400
|
+
export function socialLinks(list) {
|
|
401
|
+
const out = [];
|
|
402
|
+
for (const l of list ?? []) {
|
|
403
|
+
const platform = typeof l?.platform === 'string' ? l.platform.trim().toLowerCase() : '';
|
|
404
|
+
if (!/^[a-z0-9][a-z0-9_-]{0,31}$/.test(platform) || out.some((o) => o.platform === platform))
|
|
405
|
+
continue;
|
|
406
|
+
let url;
|
|
407
|
+
try {
|
|
408
|
+
url = new URL(String(l.url));
|
|
409
|
+
}
|
|
410
|
+
catch {
|
|
411
|
+
continue;
|
|
412
|
+
}
|
|
413
|
+
if (url.protocol !== 'https:' || !url.hostname)
|
|
414
|
+
continue;
|
|
415
|
+
out.push({ platform, url: url.href });
|
|
416
|
+
}
|
|
417
|
+
return out;
|
|
418
|
+
}
|
|
419
|
+
export function searchResults(r) {
|
|
420
|
+
return {
|
|
421
|
+
totalCount: (r.products.totalCount ?? r.products.nodes.length) + (r.pages.totalCount ?? r.pages.nodes.length) + (r.articles.totalCount ?? r.articles.nodes.length),
|
|
422
|
+
products: r.products.nodes.map(product),
|
|
423
|
+
pages: r.pages.nodes.map((p) => ({ handle: p.handle, title: p.title })),
|
|
424
|
+
articles: r.articles.nodes.map(article),
|
|
425
|
+
filters: [],
|
|
426
|
+
pageInfo: {
|
|
427
|
+
hasNextPage: r.products.pageInfo.hasNextPage,
|
|
428
|
+
hasPreviousPage: false,
|
|
429
|
+
startCursor: null,
|
|
430
|
+
endCursor: r.products.pageInfo.endCursor ?? null,
|
|
431
|
+
},
|
|
432
|
+
};
|
|
433
|
+
}
|
|
434
|
+
/**
|
|
435
|
+
* The cart-level amount taken off between the subtotal and the total.
|
|
436
|
+
*
|
|
437
|
+
* The ABI cart has no discount allocations, only `cost.subtotalAmount`, `cost.totalTaxAmount` and
|
|
438
|
+
* `cost.totalAmount`. Before checkout those are related by
|
|
439
|
+
* `total = subtotal − cart discount + tax`, so the discount is what reconciles the three figures
|
|
440
|
+
* the summary prints. Without it the cart showed "Subtotal $64.00 · Tax $0.00 · Estimated total
|
|
441
|
+
* $60.80" with nothing explaining the $3.20.
|
|
442
|
+
*
|
|
443
|
+
* Computed in minor units at the subtotal's own precision (never `toFixed(2)` — JPY has none, KWD
|
|
444
|
+
* three). Null when nothing was taken off, when the figures are not one currency, or when an
|
|
445
|
+
* amount is not a number: a guessed discount is worse than none.
|
|
446
|
+
*/
|
|
447
|
+
export function cartDiscount(c) {
|
|
448
|
+
const { subtotalAmount: sub, totalAmount: total } = c.cost;
|
|
449
|
+
const tax = c.cost.totalTaxAmount ?? null;
|
|
450
|
+
if (!sub || !total || sub.currencyCode !== total.currencyCode)
|
|
451
|
+
return null;
|
|
452
|
+
if (tax && tax.currencyCode !== sub.currencyCode)
|
|
453
|
+
return null;
|
|
454
|
+
const decimals = Math.max(...[sub, total, ...(tax ? [tax] : [])].map((m) => (m.amount.split('.')[1] ?? '').length));
|
|
455
|
+
const scale = 10 ** decimals;
|
|
456
|
+
const minor = (m) => Math.round(Number(m.amount) * scale);
|
|
457
|
+
const off = minor(sub) + (tax ? minor(tax) : 0) - minor(total);
|
|
458
|
+
if (!Number.isFinite(off) || off <= 0)
|
|
459
|
+
return null;
|
|
460
|
+
return { amount: (off / scale).toFixed(decimals), currencyCode: sub.currencyCode };
|
|
461
|
+
}
|
|
462
|
+
/** The ABI cart → the lines/totals CartProvider renders (lib/tanqory/cart.tsx CartState fields). */
|
|
463
|
+
export function cartState(c) {
|
|
464
|
+
return {
|
|
465
|
+
id: c.id,
|
|
466
|
+
version: c.version,
|
|
467
|
+
lines: c.lines.map((l) => ({
|
|
468
|
+
id: l.id,
|
|
469
|
+
variantId: l.merchandise.id,
|
|
470
|
+
quantity: l.quantity,
|
|
471
|
+
title: l.merchandise.product.title,
|
|
472
|
+
...(l.merchandise.title && l.merchandise.title !== 'Default Title' ? { variantTitle: l.merchandise.title } : {}),
|
|
473
|
+
image: image(l.merchandise.image ?? l.merchandise.product.featuredImage),
|
|
474
|
+
price: l.merchandise.price,
|
|
475
|
+
// The merchandise is a whole ProductVariant: its options, compare-at and availability are the
|
|
476
|
+
// line's (the cart shows "Queen · Oat", the struck compare-at and "No longer available").
|
|
477
|
+
selectedOptions: (l.merchandise.selectedOptions ?? []).map((o) => ({ name: o.name, value: o.value })),
|
|
478
|
+
...(l.merchandise.compareAtPrice ? { compareAtPrice: l.merchandise.compareAtPrice } : {}),
|
|
479
|
+
...(typeof l.merchandise.availableForSale === 'boolean' ? { availableForSale: l.merchandise.availableForSale } : {}),
|
|
480
|
+
lineSubtotal: l.cost.subtotalAmount,
|
|
481
|
+
productHandle: l.merchandise.product.handle,
|
|
482
|
+
attributes: Object.entries(l.attributes).map(([key, value]) => ({ key, value })),
|
|
483
|
+
})),
|
|
484
|
+
subtotal: c.cost.subtotalAmount,
|
|
485
|
+
total: c.cost.totalAmount,
|
|
486
|
+
tax: c.cost.totalTaxAmount ?? null,
|
|
487
|
+
note: c.note ?? null,
|
|
488
|
+
totalQuantity: c.totalQuantity,
|
|
489
|
+
checkoutUrl: c.checkoutUrl,
|
|
490
|
+
discountCodes: c.discountCodes.map((d) => ({ code: d.code, applicable: d.applicable })),
|
|
491
|
+
discountAmount: cartDiscount(c),
|
|
492
|
+
};
|
|
493
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { DataApi } from '../runtime/kit/data.js';
|
|
2
|
+
import type { PageData } from './page-data.js';
|
|
3
|
+
import type { AbiRemote } from './wire.js';
|
|
4
|
+
export declare const SEARCH_SORTS: readonly ["relevance", ...string[]];
|
|
5
|
+
export declare function searchOrder(sort: string | null | undefined): {
|
|
6
|
+
sortKey: 'PRICE' | 'TITLE' | 'CREATED_AT';
|
|
7
|
+
reverse: boolean;
|
|
8
|
+
} | null;
|
|
9
|
+
/** The resource-cache key of a search (platform/runtime/data/StoreProvider.tsx keys its loads the same way). */
|
|
10
|
+
export declare const searchKey: (query: string, sort?: string | null) => string;
|
|
11
|
+
export declare function createDataApi(pd: PageData, remote: AbiRemote | null): DataApi;
|