@tanqory/theme-kit 0.1.0 → 2.0.0-alpha.19
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 +128 -10
- package/dist/abi/canvas-live.d.ts +72 -0
- package/dist/abi/canvas-live.js +159 -0
- package/dist/abi/cart.d.ts +60 -0
- package/dist/abi/cart.js +396 -0
- package/dist/abi/convert.d.ts +360 -0
- package/dist/abi/convert.js +1330 -0
- package/dist/abi/data-api.d.ts +15 -0
- package/dist/abi/data-api.js +213 -0
- package/dist/abi/jcs.d.ts +6 -0
- package/dist/abi/jcs.js +28 -0
- package/dist/abi/labels.d.ts +3 -0
- package/dist/abi/labels.js +34 -0
- package/dist/abi/page-data.d.ts +264 -0
- package/dist/abi/page-data.js +746 -0
- package/dist/abi/page.d.ts +28 -0
- package/dist/abi/page.js +57 -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 +23 -0
- package/dist/abi/shell.js +10 -0
- package/dist/abi/state-pack.d.ts +17 -0
- package/dist/abi/state-pack.js +43 -0
- package/dist/abi/wire.d.ts +46 -0
- package/dist/abi/wire.js +114 -0
- package/dist/build.d.ts +121 -0
- package/dist/build.js +401 -0
- package/dist/chunks.d.ts +23 -0
- package/dist/chunks.js +41 -0
- package/dist/client.d.ts +26 -0
- package/dist/client.js +101 -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 +276 -0
- package/dist/headless/hooks.js +544 -0
- package/dist/headless/i18n.d.ts +13 -0
- package/dist/headless/i18n.js +45 -0
- package/dist/headless/index.d.ts +44 -0
- package/dist/headless/index.js +48 -0
- package/dist/headless/lazy-component.d.ts +72 -0
- package/dist/headless/lazy-component.js +225 -0
- package/dist/headless/lazy-section.d.ts +38 -0
- package/dist/headless/lazy-section.js +97 -0
- package/dist/headless/model.d.ts +73 -0
- package/dist/headless/model.js +115 -0
- package/dist/headless/money.d.ts +17 -0
- package/dist/headless/money.js +63 -0
- package/dist/headless/page.d.ts +45 -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 +187 -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 +198 -0
- package/dist/headless/structured-data.d.ts +31 -0
- package/dist/headless/structured-data.js +57 -0
- package/dist/headless/tanqory-money.generated.d.ts +493 -0
- package/dist/headless/tanqory-money.generated.js +507 -0
- package/dist/headless/theme.d.ts +109 -0
- package/dist/headless/theme.js +62 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.js +21 -553
- package/dist/runtime/data/StoreProvider.d.ts +84 -0
- package/dist/runtime/data/StoreProvider.js +380 -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 +158 -0
- package/dist/runtime/data/to-ui.d.ts +286 -0
- package/dist/runtime/data/to-ui.js +1009 -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 +250 -0
- package/dist/runtime/kit/cart.d.ts +222 -0
- package/dist/runtime/kit/cart.js +545 -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 +213 -0
- package/dist/runtime/kit/contract/content.js +119 -0
- package/dist/runtime/kit/contract/field-types.d.ts +360 -0
- package/dist/runtime/kit/contract/field-types.js +216 -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 +77 -0
- package/dist/runtime/kit/contract/validate.js +520 -0
- package/dist/runtime/kit/data.d.ts +732 -0
- package/dist/runtime/kit/data.js +1131 -0
- package/dist/{types → runtime/kit}/defineSection.d.ts +1 -1
- package/dist/runtime/kit/defineSection.js +10 -0
- package/dist/runtime/kit/dynamic-source.d.ts +118 -0
- package/dist/runtime/kit/dynamic-source.js +311 -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/runtime/kit/storefront.d.ts +652 -0
- 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 +155 -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 +199 -0
- package/dist/runtime/render/listing-query.d.ts +52 -0
- package/dist/runtime/render/listing-query.js +122 -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 +243 -0
- package/dist/runtime/render/resolved.d.ts +47 -0
- package/dist/runtime/render/resolved.js +80 -0
- package/dist/runtime/render/section-actions.d.ts +67 -0
- package/dist/runtime/render/section-actions.js +205 -0
- package/dist/runtime/render/starter.d.ts +11 -0
- package/dist/runtime/render/starter.js +96 -0
- package/dist/runtime/seo/head.d.ts +115 -0
- package/dist/runtime/seo/head.js +281 -0
- package/dist/runtime/seo/structured-data.d.ts +83 -0
- package/dist/runtime/seo/structured-data.js +252 -0
- package/dist/runtime/shell/PageShell.d.ts +18 -0
- package/dist/runtime/shell/PageShell.js +118 -0
- package/dist/runtime/shell/RuntimeBridge.d.ts +18 -0
- package/dist/runtime/shell/RuntimeBridge.js +124 -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/app-block.d.ts +29 -0
- package/dist/sdk/app-block.js +22 -0
- package/dist/sdk/font-hooks.d.ts +7 -0
- package/dist/sdk/font-hooks.js +21 -0
- package/dist/sdk/fonts.d.ts +144 -0
- package/dist/sdk/fonts.js +241 -0
- package/dist/sdk/index.d.ts +32 -0
- package/dist/sdk/index.js +36 -0
- package/dist/sdk/motion-hooks.d.ts +23 -0
- package/dist/sdk/motion-hooks.js +46 -0
- package/dist/sdk/motion.d.ts +78 -0
- package/dist/sdk/motion.js +292 -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 +230 -0
- package/dist/sdk/runtime-context.js +73 -0
- package/dist/sdk/schema.d.ts +249 -0
- package/dist/sdk/schema.js +168 -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 +32 -0
- package/dist/sdk/store-lookup.js +48 -0
- package/dist/sdk/store.d.ts +164 -0
- package/dist/sdk/store.js +5 -0
- package/dist/sdk/sx.d.ts +45 -0
- package/dist/sdk/sx.js +117 -0
- package/dist/sdk/theme.d.ts +212 -0
- package/dist/sdk/theme.js +27 -0
- package/dist/sdk/types.d.ts +789 -0
- package/dist/sdk/types.js +7 -0
- package/dist/server.d.ts +112 -0
- package/dist/server.js +589 -0
- package/dist/vite-chunks.d.ts +10 -0
- package/dist/vite-chunks.js +44 -0
- package/docs/headless-api.md +250 -0
- package/package.json +72 -26
- 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/data.d.ts +0 -357
- package/dist/types/dynamic-source.d.ts +0 -84
- 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/storefront.d.ts +0 -333
- package/dist/types/types.d.ts +0 -44
|
@@ -0,0 +1,723 @@
|
|
|
1
|
+
const EMPTY_PAGE_INFO = {
|
|
2
|
+
hasNextPage: false,
|
|
3
|
+
hasPreviousPage: false,
|
|
4
|
+
startCursor: null,
|
|
5
|
+
endCursor: null,
|
|
6
|
+
};
|
|
7
|
+
function normalizePageInfo(n) {
|
|
8
|
+
if (!n)
|
|
9
|
+
return EMPTY_PAGE_INFO;
|
|
10
|
+
return {
|
|
11
|
+
hasNextPage: Boolean(n.hasNextPage),
|
|
12
|
+
hasPreviousPage: Boolean(n.hasPreviousPage),
|
|
13
|
+
startCursor: n.startCursor ?? null,
|
|
14
|
+
endCursor: n.endCursor ?? null,
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
function normalizeFilters(arr) {
|
|
18
|
+
return (arr ?? []).map((f) => ({
|
|
19
|
+
id: f.id,
|
|
20
|
+
label: f.label,
|
|
21
|
+
type: f.type,
|
|
22
|
+
...(f.presentation ? { presentation: f.presentation } : {}),
|
|
23
|
+
values: (f.values ?? []).map((v) => ({
|
|
24
|
+
id: v.id,
|
|
25
|
+
label: v.label,
|
|
26
|
+
count: Number(v.count ?? 0),
|
|
27
|
+
input: typeof v.input === 'string' ? v.input : JSON.stringify(v.input),
|
|
28
|
+
})),
|
|
29
|
+
}));
|
|
30
|
+
}
|
|
31
|
+
// ───────────────────────── GraphQL fragments ─────────────────────
|
|
32
|
+
const MENU_FIELDS = /* GraphQL */ `
|
|
33
|
+
fragment MenuFields on Menu {
|
|
34
|
+
id
|
|
35
|
+
handle
|
|
36
|
+
title
|
|
37
|
+
items {
|
|
38
|
+
...MenuItemFields
|
|
39
|
+
items { ...MenuItemFields items { ...MenuItemFields } }
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
fragment MenuItemFields on MenuItem { id title url type resourceId }
|
|
43
|
+
`;
|
|
44
|
+
const ARTICLE_FIELDS = /* GraphQL */ `
|
|
45
|
+
fragment ArticleFields on Article {
|
|
46
|
+
id
|
|
47
|
+
handle
|
|
48
|
+
title
|
|
49
|
+
excerpt
|
|
50
|
+
contentHtml
|
|
51
|
+
image { url altText width height }
|
|
52
|
+
author: authorV2 { name }
|
|
53
|
+
publishedAt
|
|
54
|
+
tags
|
|
55
|
+
seo { title description keywords }
|
|
56
|
+
templateSuffix
|
|
57
|
+
}
|
|
58
|
+
`;
|
|
59
|
+
const PRODUCT_CARD = /* GraphQL */ `
|
|
60
|
+
fragment ProductCard on Product {
|
|
61
|
+
id
|
|
62
|
+
handle
|
|
63
|
+
title
|
|
64
|
+
availableForSale
|
|
65
|
+
vendor
|
|
66
|
+
productType
|
|
67
|
+
tags
|
|
68
|
+
featuredImage { url altText width height }
|
|
69
|
+
priceRange { minVariantPrice { amount currencyCode } maxVariantPrice { amount currencyCode } }
|
|
70
|
+
compareAtPriceRange { minVariantPrice { amount currencyCode } maxVariantPrice { amount currencyCode } }
|
|
71
|
+
firstAvailableVariant { id }
|
|
72
|
+
}
|
|
73
|
+
`;
|
|
74
|
+
const CUSTOMER_FIELDS = /* GraphQL */ `
|
|
75
|
+
fragment AddressFields on MailingAddress {
|
|
76
|
+
id firstName lastName company address1 address2 city province country zip phone
|
|
77
|
+
formatted(withName: true)
|
|
78
|
+
}
|
|
79
|
+
fragment CustomerFields on Customer {
|
|
80
|
+
id email firstName lastName displayName phone acceptsMarketing numberOfOrders tags
|
|
81
|
+
defaultAddress { ...AddressFields }
|
|
82
|
+
addresses(first: 20) { nodes { ...AddressFields } }
|
|
83
|
+
storeCreditAccounts { id balance { amount currencyCode } }
|
|
84
|
+
}
|
|
85
|
+
`;
|
|
86
|
+
const ORDER_FIELDS = /* GraphQL */ `
|
|
87
|
+
fragment OrderFields on Order {
|
|
88
|
+
id name orderNumber processedAt financialStatus fulfillmentStatus statusUrl
|
|
89
|
+
email phone cancelled cancelReason
|
|
90
|
+
currentTotalPrice { amount currencyCode }
|
|
91
|
+
subtotalPrice { amount currencyCode }
|
|
92
|
+
totalTax { amount currencyCode }
|
|
93
|
+
totalShippingPrice { amount currencyCode }
|
|
94
|
+
totalPrice { amount currencyCode }
|
|
95
|
+
shippingAddress { id firstName lastName company address1 address2 city province country zip phone }
|
|
96
|
+
billingAddress { id firstName lastName company address1 address2 city province country zip phone }
|
|
97
|
+
fulfillments(first: 5) { trackingCompany trackingInfo { number url } }
|
|
98
|
+
transactions { id kind status amount { amount currencyCode } gateway paymentMethod processedAt }
|
|
99
|
+
giftCards { id lastCharacters balance { amount currencyCode } initialValue { amount currencyCode } enabled expiresOn }
|
|
100
|
+
lineItems(first: 50) {
|
|
101
|
+
nodes {
|
|
102
|
+
title variantTitle quantity
|
|
103
|
+
discountedTotalPrice { amount currencyCode }
|
|
104
|
+
variant { image { url altText width height } }
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
`;
|
|
109
|
+
// ───────────────────────── Normalizers ───────────────────────────
|
|
110
|
+
const ZERO = { amount: '0', currencyCode: 'USD' };
|
|
111
|
+
function img(node) {
|
|
112
|
+
if (!node || !node.url)
|
|
113
|
+
return null;
|
|
114
|
+
return node.altText ? { url: node.url, altText: node.altText } : { url: node.url };
|
|
115
|
+
}
|
|
116
|
+
function normalizeMenuItem(n) {
|
|
117
|
+
return {
|
|
118
|
+
id: n.id,
|
|
119
|
+
title: n.title,
|
|
120
|
+
url: n.url ?? null,
|
|
121
|
+
type: n.type,
|
|
122
|
+
resourceId: n.resourceId ?? null,
|
|
123
|
+
items: (n.items ?? []).map(normalizeMenuItem),
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
function normalizeMenu(n) {
|
|
127
|
+
if (!n)
|
|
128
|
+
return null;
|
|
129
|
+
return { id: n.id, handle: n.handle, title: n.title, items: (n.items ?? []).map(normalizeMenuItem) };
|
|
130
|
+
}
|
|
131
|
+
function normalizeArticle(n, blogHandle) {
|
|
132
|
+
return {
|
|
133
|
+
id: n.id,
|
|
134
|
+
handle: n.handle,
|
|
135
|
+
title: n.title,
|
|
136
|
+
excerpt: n.excerpt ?? null,
|
|
137
|
+
contentHtml: n.contentHtml ?? '',
|
|
138
|
+
image: img(n.image),
|
|
139
|
+
author: n.author?.name ?? null,
|
|
140
|
+
...(n.publishedAt ? { publishedAt: n.publishedAt } : {}),
|
|
141
|
+
tags: n.tags ?? [],
|
|
142
|
+
...(blogHandle ? { blogHandle } : {}),
|
|
143
|
+
...(n.seo ? { seo: { title: n.seo.title ?? null, description: n.seo.description ?? null, keywords: n.seo.keywords ?? [] } } : {}),
|
|
144
|
+
...(n.templateSuffix ? { templateSuffix: n.templateSuffix } : {}),
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
function normalizeMetaobject(n) {
|
|
148
|
+
const fields = (n.fields ?? []).map((f) => ({
|
|
149
|
+
key: f.key,
|
|
150
|
+
value: f.value ?? null,
|
|
151
|
+
type: f.type,
|
|
152
|
+
}));
|
|
153
|
+
const values = {};
|
|
154
|
+
for (const f of fields)
|
|
155
|
+
values[f.key] = f.value;
|
|
156
|
+
return { id: n.id, handle: n.handle, type: n.type, fields, values };
|
|
157
|
+
}
|
|
158
|
+
function normalizeAddress(n) {
|
|
159
|
+
return {
|
|
160
|
+
id: n.id,
|
|
161
|
+
firstName: n.firstName ?? null,
|
|
162
|
+
lastName: n.lastName ?? null,
|
|
163
|
+
company: n.company ?? null,
|
|
164
|
+
address1: n.address1 ?? null,
|
|
165
|
+
address2: n.address2 ?? null,
|
|
166
|
+
city: n.city ?? null,
|
|
167
|
+
province: n.province ?? null,
|
|
168
|
+
country: n.country ?? null,
|
|
169
|
+
zip: n.zip ?? null,
|
|
170
|
+
phone: n.phone ?? null,
|
|
171
|
+
formatted: n.formatted ?? [],
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
function normalizeCustomer(n) {
|
|
175
|
+
return {
|
|
176
|
+
id: n.id,
|
|
177
|
+
email: n.email ?? null,
|
|
178
|
+
firstName: n.firstName ?? null,
|
|
179
|
+
lastName: n.lastName ?? null,
|
|
180
|
+
displayName: n.displayName ?? '',
|
|
181
|
+
phone: n.phone ?? null,
|
|
182
|
+
acceptsMarketing: Boolean(n.acceptsMarketing),
|
|
183
|
+
numberOfOrders: Number(n.numberOfOrders ?? 0),
|
|
184
|
+
tags: n.tags ?? [],
|
|
185
|
+
defaultAddress: n.defaultAddress ? normalizeAddress(n.defaultAddress) : null,
|
|
186
|
+
addresses: (n.addresses?.nodes ?? []).map(normalizeAddress),
|
|
187
|
+
...(n.storeCreditAccounts?.length
|
|
188
|
+
? { storeCreditAccounts: n.storeCreditAccounts.map((a) => ({
|
|
189
|
+
id: a.id,
|
|
190
|
+
balance: a.balance ?? ZERO,
|
|
191
|
+
})) }
|
|
192
|
+
: {}),
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
function normalizeOrder(n) {
|
|
196
|
+
return {
|
|
197
|
+
id: n.id,
|
|
198
|
+
name: n.name,
|
|
199
|
+
orderNumber: Number(n.orderNumber ?? 0),
|
|
200
|
+
processedAt: n.processedAt,
|
|
201
|
+
financialStatus: n.financialStatus ?? null,
|
|
202
|
+
fulfillmentStatus: n.fulfillmentStatus ?? 'UNFULFILLED',
|
|
203
|
+
totalPrice: n.currentTotalPrice ?? n.totalPrice ?? ZERO,
|
|
204
|
+
statusUrl: n.statusUrl ?? '',
|
|
205
|
+
email: n.email ?? null,
|
|
206
|
+
phone: n.phone ?? null,
|
|
207
|
+
...(typeof n.cancelled === 'boolean' ? { cancelled: n.cancelled } : {}),
|
|
208
|
+
cancelReason: n.cancelReason ?? null,
|
|
209
|
+
...(n.subtotalPrice ? { subtotalPrice: n.subtotalPrice } : {}),
|
|
210
|
+
...(n.totalTax ? { totalTax: n.totalTax } : {}),
|
|
211
|
+
...(n.totalShippingPrice ? { totalShippingPrice: n.totalShippingPrice } : {}),
|
|
212
|
+
...(n.shippingAddress ? { shippingAddress: normalizeAddress(n.shippingAddress) } : {}),
|
|
213
|
+
...(n.billingAddress ? { billingAddress: normalizeAddress(n.billingAddress) } : {}),
|
|
214
|
+
...(n.fulfillments?.length
|
|
215
|
+
? { fulfillments: n.fulfillments.map((f) => ({
|
|
216
|
+
trackingCompany: f.trackingCompany ?? null,
|
|
217
|
+
trackingInfo: (f.trackingInfo ?? []).map((t) => ({ number: t.number ?? null, url: t.url ?? null })),
|
|
218
|
+
})) }
|
|
219
|
+
: {}),
|
|
220
|
+
...(n.transactions?.length
|
|
221
|
+
? { transactions: n.transactions.map((t) => ({
|
|
222
|
+
id: t.id,
|
|
223
|
+
kind: t.kind,
|
|
224
|
+
status: t.status,
|
|
225
|
+
amount: t.amount ?? ZERO,
|
|
226
|
+
gateway: t.gateway ?? null,
|
|
227
|
+
paymentMethod: t.paymentMethod ?? null,
|
|
228
|
+
processedAt: t.processedAt,
|
|
229
|
+
})) }
|
|
230
|
+
: {}),
|
|
231
|
+
...(n.giftCards?.length
|
|
232
|
+
? { giftCards: n.giftCards.map((g) => ({
|
|
233
|
+
id: g.id,
|
|
234
|
+
lastCharacters: g.lastCharacters ?? '',
|
|
235
|
+
balance: g.balance ?? ZERO,
|
|
236
|
+
initialValue: g.initialValue ?? ZERO,
|
|
237
|
+
enabled: Boolean(g.enabled),
|
|
238
|
+
expiresOn: g.expiresOn ?? null,
|
|
239
|
+
})) }
|
|
240
|
+
: {}),
|
|
241
|
+
lineItems: (n.lineItems?.nodes ?? []).map((li) => ({
|
|
242
|
+
title: li.title,
|
|
243
|
+
variantTitle: li.variantTitle ?? null,
|
|
244
|
+
quantity: Number(li.quantity ?? 0),
|
|
245
|
+
image: img(li.variant?.image),
|
|
246
|
+
totalPrice: li.discountedTotalPrice ?? ZERO,
|
|
247
|
+
})),
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
function normalizeShopPolicy(n) {
|
|
251
|
+
if (!n)
|
|
252
|
+
return null;
|
|
253
|
+
return { handle: n.handle, title: n.title, url: n.url, ...(n.body ? { body: n.body } : {}) };
|
|
254
|
+
}
|
|
255
|
+
function normalizeShop(n) {
|
|
256
|
+
if (!n)
|
|
257
|
+
return null;
|
|
258
|
+
return {
|
|
259
|
+
name: n.name,
|
|
260
|
+
description: n.description ?? null,
|
|
261
|
+
email: n.email ?? null,
|
|
262
|
+
phone: n.phone ?? null,
|
|
263
|
+
brand: n.brand
|
|
264
|
+
? {
|
|
265
|
+
logo: img(n.brand.logo),
|
|
266
|
+
squareLogo: img(n.brand.squareLogo),
|
|
267
|
+
coverImage: img(n.brand.coverImage),
|
|
268
|
+
slogan: n.brand.slogan ?? null,
|
|
269
|
+
shortDescription: n.brand.shortDescription ?? null,
|
|
270
|
+
// `fonts` is non-null in the SDL but a snapshot rebuilt from an older
|
|
271
|
+
// bootstrap won't carry it — normalise to [] so a theme can map over
|
|
272
|
+
// it without guarding.
|
|
273
|
+
fonts: Array.isArray(n.brand.fonts)
|
|
274
|
+
? n.brand.fonts.filter((f) => typeof f === 'string' && f.trim() !== '')
|
|
275
|
+
: [],
|
|
276
|
+
// Colour groups arrive as arrays and can be empty; keep the shape
|
|
277
|
+
// stable so a theme can index `primary[0]` without guarding twice.
|
|
278
|
+
colors: n.brand.colors
|
|
279
|
+
? {
|
|
280
|
+
primary: n.brand.colors.primary ?? [],
|
|
281
|
+
secondary: n.brand.colors.secondary ?? [],
|
|
282
|
+
}
|
|
283
|
+
: null,
|
|
284
|
+
}
|
|
285
|
+
: null,
|
|
286
|
+
policies: {
|
|
287
|
+
privacy: normalizeShopPolicy(n.privacyPolicy),
|
|
288
|
+
refund: normalizeShopPolicy(n.refundPolicy),
|
|
289
|
+
termsOfService: normalizeShopPolicy(n.termsOfService),
|
|
290
|
+
shipping: normalizeShopPolicy(n.shippingPolicy),
|
|
291
|
+
subscription: normalizeShopPolicy(n.subscriptionPolicy),
|
|
292
|
+
},
|
|
293
|
+
cookieBanner: n.cookieBanner
|
|
294
|
+
? {
|
|
295
|
+
enabled: Boolean(n.cookieBanner.enabled),
|
|
296
|
+
mode: n.cookieBanner.mode ?? null,
|
|
297
|
+
dataSharingTitle: n.cookieBanner.dataSharingTitle ?? null,
|
|
298
|
+
dataSharingVisible: n.cookieBanner.dataSharingVisible !== false,
|
|
299
|
+
title: n.cookieBanner.title ?? null,
|
|
300
|
+
body: n.cookieBanner.body ?? null,
|
|
301
|
+
acceptLabel: n.cookieBanner.acceptLabel ?? null,
|
|
302
|
+
declineLabel: n.cookieBanner.declineLabel ?? null,
|
|
303
|
+
manageLabel: n.cookieBanner.manageLabel ?? null,
|
|
304
|
+
position: n.cookieBanner.position ?? null,
|
|
305
|
+
colorTheme: n.cookieBanner.colorTheme ?? null,
|
|
306
|
+
}
|
|
307
|
+
: null,
|
|
308
|
+
primaryDomain: n.primaryDomain
|
|
309
|
+
? {
|
|
310
|
+
host: n.primaryDomain.host,
|
|
311
|
+
url: n.primaryDomain.url,
|
|
312
|
+
sslEnabled: Boolean(n.primaryDomain.sslEnabled),
|
|
313
|
+
}
|
|
314
|
+
: null,
|
|
315
|
+
paymentSettings: n.paymentSettings
|
|
316
|
+
? {
|
|
317
|
+
acceptedCardBrands: n.paymentSettings.acceptedCardBrands ?? [],
|
|
318
|
+
supportedDigitalWallets: n.paymentSettings.supportedDigitalWallets ?? [],
|
|
319
|
+
currencyCode: n.paymentSettings.currencyCode ?? null,
|
|
320
|
+
}
|
|
321
|
+
: null,
|
|
322
|
+
};
|
|
323
|
+
}
|
|
324
|
+
/** UserError[] → message strings. */
|
|
325
|
+
function errs(arr) {
|
|
326
|
+
return (arr ?? []).map((e) => e?.message).filter((m) => Boolean(m));
|
|
327
|
+
}
|
|
328
|
+
// ─── Bootstrap fields (merged into data.tsx's boot query) ─────────
|
|
329
|
+
/** GraphQL selection for shop + the two standard menus, spliced into the
|
|
330
|
+
* homepage bootstrap so the layout (nav + footer + policies) renders sync. */
|
|
331
|
+
export const BOOTSTRAP_SHOP_MENU = /* GraphQL */ `
|
|
332
|
+
shop {
|
|
333
|
+
name
|
|
334
|
+
description
|
|
335
|
+
email
|
|
336
|
+
phone
|
|
337
|
+
# Ask for the whole brand, not a third of it. Settings → Brand persists a
|
|
338
|
+
# logo, a square logo and brand colours; the SDL has carried them all along
|
|
339
|
+
# and this selection took only logo/slogan/shortDescription, so a merchant
|
|
340
|
+
# could upload a logo and set colours and their storefront never changed.
|
|
341
|
+
# Nullable throughout (BrandColorGroup.background/foreground are String),
|
|
342
|
+
# so an unconfigured store returns nulls rather than failing the query.
|
|
343
|
+
brand {
|
|
344
|
+
logo { url altText width height }
|
|
345
|
+
squareLogo { url altText width height }
|
|
346
|
+
coverImage { url altText width height }
|
|
347
|
+
slogan
|
|
348
|
+
shortDescription
|
|
349
|
+
colors {
|
|
350
|
+
primary { background foreground }
|
|
351
|
+
secondary { background foreground }
|
|
352
|
+
}
|
|
353
|
+
# Settings → Brand typography. Selected here because a theme cannot fetch
|
|
354
|
+
# it separately without a second bootstrap round-trip, and without it a
|
|
355
|
+
# merchant who picks brand fonts sees no change on their storefront.
|
|
356
|
+
fonts
|
|
357
|
+
}
|
|
358
|
+
privacyPolicy { handle title url }
|
|
359
|
+
refundPolicy { handle title url }
|
|
360
|
+
termsOfService { handle title url }
|
|
361
|
+
shippingPolicy { handle title url }
|
|
362
|
+
subscriptionPolicy { handle title url }
|
|
363
|
+
cookieBanner { enabled mode dataSharingTitle dataSharingVisible title body acceptLabel declineLabel manageLabel position colorTheme }
|
|
364
|
+
primaryDomain { host url sslEnabled }
|
|
365
|
+
paymentSettings { acceptedCardBrands supportedDigitalWallets currencyCode }
|
|
366
|
+
}
|
|
367
|
+
mainMenu: menu(handle: "main-menu") { ...MenuFields }
|
|
368
|
+
footerMenu: menu(handle: "footer") { ...MenuFields }
|
|
369
|
+
`;
|
|
370
|
+
/** Fragment defs the bootstrap query must include when it uses BOOTSTRAP_SHOP_MENU. */
|
|
371
|
+
export const BOOTSTRAP_SHOP_MENU_FRAGMENTS = MENU_FIELDS;
|
|
372
|
+
/** Pull shop + the prefetched menus out of a bootstrap response. */
|
|
373
|
+
export function readBootstrapShopMenu(boot) {
|
|
374
|
+
const menus = new Map();
|
|
375
|
+
const main = normalizeMenu(boot?.mainMenu);
|
|
376
|
+
if (main)
|
|
377
|
+
menus.set('main-menu', main);
|
|
378
|
+
const footer = normalizeMenu(boot?.footerMenu);
|
|
379
|
+
if (footer)
|
|
380
|
+
menus.set('footer', footer);
|
|
381
|
+
// A policy route also asked for the bodies (aliased `policyBodies`): fold them in.
|
|
382
|
+
const bodies = boot?.policyBodies;
|
|
383
|
+
const shopNode = bodies && boot?.shop
|
|
384
|
+
? Object.fromEntries(Object.entries(boot.shop).map(([k, v]) => [k, v && typeof v === 'object' && bodies[k]?.body ? { ...v, body: bodies[k].body } : v]))
|
|
385
|
+
: boot?.shop;
|
|
386
|
+
return { shop: normalizeShop(shopNode), menus };
|
|
387
|
+
}
|
|
388
|
+
// ───────────────────────── Factory ───────────────────────────────
|
|
389
|
+
/**
|
|
390
|
+
* Build the live storefront extension methods on top of a `graphql()` requester.
|
|
391
|
+
* `menuCache` is shared with the boot-prefetched menus so `fetchMenu()` warms
|
|
392
|
+
* the same map that the sync `menu()` reads.
|
|
393
|
+
*/
|
|
394
|
+
export function createStorefrontMethods(graphql, helpers, menuCache) {
|
|
395
|
+
const { normalizeProduct } = helpers;
|
|
396
|
+
const fetchMenu = async (handle) => {
|
|
397
|
+
const cached = menuCache.get(handle);
|
|
398
|
+
if (cached)
|
|
399
|
+
return cached;
|
|
400
|
+
const res = await graphql(`${MENU_FIELDS}\n query Menu($handle: String!) { menu(handle: $handle) { ...MenuFields } }`, { handle });
|
|
401
|
+
const menu = normalizeMenu(res?.menu);
|
|
402
|
+
if (menu)
|
|
403
|
+
menuCache.set(handle, menu);
|
|
404
|
+
return menu;
|
|
405
|
+
};
|
|
406
|
+
// store-api's search types every node as Product (pages and articles too), so
|
|
407
|
+
// asking it for articles/pages makes Product.availableForSale fail and nulls
|
|
408
|
+
// the WHOLE result — a query matching one page returned nothing at all. Until
|
|
409
|
+
// it is fixed: products from search (types: [PRODUCT], which it answers
|
|
410
|
+
// correctly); articles and pages from their own lists, matched here, since
|
|
411
|
+
// their `query:` argument is ignored too.
|
|
412
|
+
const search = async (query, opts) => {
|
|
413
|
+
const types = opts?.types ?? null;
|
|
414
|
+
const want = (t) => !types || types.includes(t);
|
|
415
|
+
const res = await graphql(`${PRODUCT_CARD}${ARTICLE_FIELDS}
|
|
416
|
+
query Search($query: String!, $first: Int!, $after: String, $withArticles: Boolean!, $withPages: Boolean!) {
|
|
417
|
+
search(query: $query, first: $first, after: $after, types: [PRODUCT]) {
|
|
418
|
+
totalCount
|
|
419
|
+
productFilters { id label type values { id label count input } }
|
|
420
|
+
pageInfo { hasNextPage hasPreviousPage startCursor endCursor }
|
|
421
|
+
nodes { __typename ... on Product { ...ProductCard } }
|
|
422
|
+
}
|
|
423
|
+
articles(first: 100) @include(if: $withArticles) { nodes { ...ArticleFields blog { handle } } }
|
|
424
|
+
pages(first: 100) @include(if: $withPages) { nodes { handle title body } }
|
|
425
|
+
}`, { query, first: opts?.first ?? 24, after: opts?.after ?? null, withArticles: want('ARTICLE') && !opts?.after, withPages: want('PAGE') && !opts?.after });
|
|
426
|
+
const s = res?.search ?? {};
|
|
427
|
+
const needle = query.trim().toLocaleLowerCase();
|
|
428
|
+
const hit = (...texts) => needle !== '' && texts.some((t) => typeof t === 'string' && t.replace(/<[^>]*>/g, ' ').toLocaleLowerCase().includes(needle));
|
|
429
|
+
const articles = (res?.articles?.nodes ?? []).filter((a) => hit(a.title, a.excerpt, a.contentHtml, ...(a.tags ?? [])));
|
|
430
|
+
const pages = (res?.pages?.nodes ?? []).filter((p) => hit(p.title, p.body));
|
|
431
|
+
const products = want('PRODUCT') ? (s.nodes ?? []).filter((n) => n.__typename === 'Product').map(normalizeProduct) : [];
|
|
432
|
+
return {
|
|
433
|
+
totalCount: (want('PRODUCT') ? Number(s.totalCount ?? 0) : 0) + articles.length + pages.length,
|
|
434
|
+
products,
|
|
435
|
+
pages: pages.map((n) => ({ handle: n.handle, title: n.title })),
|
|
436
|
+
articles: articles.map((n) => normalizeArticle(n, n.blog?.handle)),
|
|
437
|
+
filters: normalizeFilters(s.productFilters),
|
|
438
|
+
pageInfo: normalizePageInfo(s.pageInfo),
|
|
439
|
+
};
|
|
440
|
+
};
|
|
441
|
+
const collectionProducts = async (handle, opts) => {
|
|
442
|
+
const res = await graphql(`${PRODUCT_CARD}
|
|
443
|
+
query CollectionProducts(
|
|
444
|
+
$handle: String!, $first: Int!, $after: String,
|
|
445
|
+
$sortKey: ProductCollectionSortKey, $reverse: Boolean, $filters: [ProductFilterInput!]
|
|
446
|
+
) {
|
|
447
|
+
collection(handle: $handle) {
|
|
448
|
+
products(first: $first, after: $after, sortKey: $sortKey, reverse: $reverse, filters: $filters) {
|
|
449
|
+
nodes { ...ProductCard }
|
|
450
|
+
filters { id label type presentation values { id label count input } }
|
|
451
|
+
pageInfo { hasNextPage hasPreviousPage startCursor endCursor }
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
}`, {
|
|
455
|
+
handle,
|
|
456
|
+
first: opts?.first ?? 24,
|
|
457
|
+
after: opts?.after ?? null,
|
|
458
|
+
sortKey: opts?.sortKey ?? null,
|
|
459
|
+
reverse: opts?.reverse ?? null,
|
|
460
|
+
filters: opts?.filters ?? null,
|
|
461
|
+
});
|
|
462
|
+
const c = res?.collection?.products ?? {};
|
|
463
|
+
return {
|
|
464
|
+
products: (c.nodes ?? []).map(normalizeProduct),
|
|
465
|
+
filters: normalizeFilters(c.filters),
|
|
466
|
+
pageInfo: normalizePageInfo(c.pageInfo),
|
|
467
|
+
...(res && res.collection === null ? { missing: true } : {}),
|
|
468
|
+
};
|
|
469
|
+
};
|
|
470
|
+
const predictiveSearch = async (query, opts) => {
|
|
471
|
+
const res = await graphql(`${PRODUCT_CARD}${ARTICLE_FIELDS}
|
|
472
|
+
query Predictive($query: String!, $limit: Int!) {
|
|
473
|
+
predictiveSearch(query: $query, limit: $limit) {
|
|
474
|
+
queries { text }
|
|
475
|
+
products { ...ProductCard }
|
|
476
|
+
collections { handle title image { url altText width height } }
|
|
477
|
+
pages { handle title }
|
|
478
|
+
articles { ...ArticleFields }
|
|
479
|
+
}
|
|
480
|
+
}`, { query, limit: opts?.limit ?? 4 });
|
|
481
|
+
const ps = res?.predictiveSearch ?? {};
|
|
482
|
+
return {
|
|
483
|
+
queries: (ps.queries ?? []).map((q) => q.text),
|
|
484
|
+
products: (ps.products ?? []).map(normalizeProduct),
|
|
485
|
+
collections: (ps.collections ?? []).map((c) => ({
|
|
486
|
+
handle: c.handle,
|
|
487
|
+
title: c.title,
|
|
488
|
+
image: img(c.image),
|
|
489
|
+
})),
|
|
490
|
+
pages: (ps.pages ?? []).map((p) => ({ handle: p.handle, title: p.title })),
|
|
491
|
+
articles: (ps.articles ?? []).map((a) => normalizeArticle(a)),
|
|
492
|
+
};
|
|
493
|
+
};
|
|
494
|
+
const productRecommendations = async (productId) => {
|
|
495
|
+
const res = await graphql(`${PRODUCT_CARD}
|
|
496
|
+
query Recs($productId: ID!) {
|
|
497
|
+
productRecommendations(productId: $productId) { ...ProductCard }
|
|
498
|
+
}`, { productId });
|
|
499
|
+
return (res?.productRecommendations ?? []).map(normalizeProduct);
|
|
500
|
+
};
|
|
501
|
+
const blogByHandle = async (handle, opts) => {
|
|
502
|
+
const res = await graphql(`${ARTICLE_FIELDS}
|
|
503
|
+
query Blog($handle: String!, $first: Int!) {
|
|
504
|
+
blog(handle: $handle) {
|
|
505
|
+
id handle title
|
|
506
|
+
seo { title description keywords }
|
|
507
|
+
templateSuffix
|
|
508
|
+
articles(first: $first) { nodes { ...ArticleFields } }
|
|
509
|
+
}
|
|
510
|
+
}`, { handle, first: opts?.articles ?? 20 });
|
|
511
|
+
const b = res?.blog;
|
|
512
|
+
if (!b)
|
|
513
|
+
return null;
|
|
514
|
+
return {
|
|
515
|
+
id: b.id,
|
|
516
|
+
handle: b.handle,
|
|
517
|
+
title: b.title,
|
|
518
|
+
articles: (b.articles?.nodes ?? []).map((a) => normalizeArticle(a, b.handle)),
|
|
519
|
+
...(b.seo ? { seo: { title: b.seo.title ?? null, description: b.seo.description ?? null, keywords: b.seo.keywords ?? [] } } : {}),
|
|
520
|
+
...(b.templateSuffix ? { templateSuffix: b.templateSuffix } : {}),
|
|
521
|
+
};
|
|
522
|
+
};
|
|
523
|
+
const articleByHandle = async (blogHandle, articleHandle) => {
|
|
524
|
+
// Through the blog, not the top-level `article(handle:)`: store-api's
|
|
525
|
+
// resolver passes that ArticleHandleInput object to a string handle
|
|
526
|
+
// lookup and answers internal_error for every article.
|
|
527
|
+
const res = await graphql(`${ARTICLE_FIELDS}
|
|
528
|
+
query Article($blog: String!, $article: String!) {
|
|
529
|
+
blog(handle: $blog) { articleByHandle(handle: $article) { ...ArticleFields } }
|
|
530
|
+
}`, { blog: blogHandle, article: articleHandle });
|
|
531
|
+
const article = res?.blog?.articleByHandle;
|
|
532
|
+
return article ? normalizeArticle(article, blogHandle) : null;
|
|
533
|
+
};
|
|
534
|
+
const metaobject = async (type, handle) => {
|
|
535
|
+
const res = await graphql(`query Metaobject($handle: MetaobjectHandleInput!) {
|
|
536
|
+
metaobject(handle: $handle) { id handle type fields { key value type } }
|
|
537
|
+
}`, { handle: { type, handle } });
|
|
538
|
+
return res?.metaobject ? normalizeMetaobject(res.metaobject) : null;
|
|
539
|
+
};
|
|
540
|
+
const metaobjects = async (type, opts) => {
|
|
541
|
+
const res = await graphql(`query Metaobjects($type: String!, $first: Int!) {
|
|
542
|
+
metaobjects(type: $type, first: $first) {
|
|
543
|
+
nodes { id handle type fields { key value type } }
|
|
544
|
+
}
|
|
545
|
+
}`, { type, first: opts?.first ?? 50 });
|
|
546
|
+
return (res?.metaobjects?.nodes ?? []).map(normalizeMetaobject);
|
|
547
|
+
};
|
|
548
|
+
// ─── Customer account flow ──────────────────────────────────────
|
|
549
|
+
const customer = {
|
|
550
|
+
async login(email, password) {
|
|
551
|
+
const res = await graphql(`mutation Login($input: CustomerAccessTokenCreateInput!) {
|
|
552
|
+
customerAccessTokenCreate(input: $input) {
|
|
553
|
+
customerAccessToken { accessToken expiresAt }
|
|
554
|
+
customerUserErrors { message }
|
|
555
|
+
}
|
|
556
|
+
}`, { input: { email, password } });
|
|
557
|
+
const p = res?.customerAccessTokenCreate;
|
|
558
|
+
return {
|
|
559
|
+
token: p?.customerAccessToken?.accessToken ?? null,
|
|
560
|
+
expiresAt: p?.customerAccessToken?.expiresAt ?? null,
|
|
561
|
+
errors: errs(p?.customerUserErrors),
|
|
562
|
+
};
|
|
563
|
+
},
|
|
564
|
+
async logout(token) {
|
|
565
|
+
await graphql(`mutation Logout($t: String!) {
|
|
566
|
+
customerAccessTokenDelete(customerAccessToken: $t) { deletedAccessToken userErrors { message } }
|
|
567
|
+
}`, { t: token });
|
|
568
|
+
},
|
|
569
|
+
async renew(token) {
|
|
570
|
+
const res = await graphql(`mutation Renew($t: String!) {
|
|
571
|
+
customerAccessTokenRenew(customerAccessToken: $t) {
|
|
572
|
+
customerAccessToken { accessToken expiresAt }
|
|
573
|
+
userErrors { message }
|
|
574
|
+
}
|
|
575
|
+
}`, { t: token });
|
|
576
|
+
const p = res?.customerAccessTokenRenew;
|
|
577
|
+
return {
|
|
578
|
+
token: p?.customerAccessToken?.accessToken ?? null,
|
|
579
|
+
expiresAt: p?.customerAccessToken?.expiresAt ?? null,
|
|
580
|
+
errors: errs(p?.userErrors),
|
|
581
|
+
};
|
|
582
|
+
},
|
|
583
|
+
async register(input) {
|
|
584
|
+
const res = await graphql(`${CUSTOMER_FIELDS}
|
|
585
|
+
mutation Register($input: CustomerCreateInput!) {
|
|
586
|
+
customerCreate(input: $input) {
|
|
587
|
+
customer { ...CustomerFields }
|
|
588
|
+
customerUserErrors { message }
|
|
589
|
+
}
|
|
590
|
+
}`, { input });
|
|
591
|
+
const p = res?.customerCreate;
|
|
592
|
+
const errors = errs(p?.customerUserErrors);
|
|
593
|
+
return {
|
|
594
|
+
ok: Boolean(p?.customer) && errors.length === 0,
|
|
595
|
+
errors,
|
|
596
|
+
...(p?.customer ? { data: normalizeCustomer(p.customer) } : {}),
|
|
597
|
+
};
|
|
598
|
+
},
|
|
599
|
+
async get(token) {
|
|
600
|
+
const res = await graphql(`${CUSTOMER_FIELDS}
|
|
601
|
+
query Me($t: String) { customer(customerAccessToken: $t) { ...CustomerFields } }`, { t: token });
|
|
602
|
+
return res?.customer ? normalizeCustomer(res.customer) : null;
|
|
603
|
+
},
|
|
604
|
+
async paymentMethods(token) {
|
|
605
|
+
const res = await graphql(`query PaymentMethods($t: String) {
|
|
606
|
+
customer(customerAccessToken: $t) {
|
|
607
|
+
paymentMethods {
|
|
608
|
+
id isDefault isActive type
|
|
609
|
+
card { brand lastDigits expiryMonth expiryYear name }
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
}`, { t: token });
|
|
613
|
+
return (res?.customer?.paymentMethods ?? []).map((m) => ({
|
|
614
|
+
id: m.id,
|
|
615
|
+
isDefault: Boolean(m.isDefault),
|
|
616
|
+
isActive: Boolean(m.isActive),
|
|
617
|
+
type: m.type,
|
|
618
|
+
...(m.card ? { card: {
|
|
619
|
+
brand: m.card.brand ?? null,
|
|
620
|
+
lastDigits: m.card.lastDigits ?? null,
|
|
621
|
+
expiryMonth: m.card.expiryMonth ?? null,
|
|
622
|
+
expiryYear: m.card.expiryYear ?? null,
|
|
623
|
+
name: m.card.name ?? null,
|
|
624
|
+
} } : {}),
|
|
625
|
+
}));
|
|
626
|
+
},
|
|
627
|
+
async orders(token, opts) {
|
|
628
|
+
const res = await graphql(`${ORDER_FIELDS}
|
|
629
|
+
query Orders($t: String, $first: Int!) {
|
|
630
|
+
customer(customerAccessToken: $t) {
|
|
631
|
+
orders(first: $first, sortKey: PROCESSED_AT, reverse: true) { nodes { ...OrderFields } }
|
|
632
|
+
}
|
|
633
|
+
}`, { t: token, first: opts?.first ?? 20 });
|
|
634
|
+
return (res?.customer?.orders?.nodes ?? []).map(normalizeOrder);
|
|
635
|
+
},
|
|
636
|
+
async orderByLookup(orderNumber, email) {
|
|
637
|
+
const res = await graphql(`${ORDER_FIELDS}
|
|
638
|
+
query Lookup($n: String!, $e: String!) {
|
|
639
|
+
orderByLookup(orderNumber: $n, email: $e) { ...OrderFields }
|
|
640
|
+
}`, { n: orderNumber, e: email });
|
|
641
|
+
return res?.orderByLookup ? normalizeOrder(res.orderByLookup) : null;
|
|
642
|
+
},
|
|
643
|
+
async createAddress(token, address) {
|
|
644
|
+
const res = await graphql(`mutation AddrCreate($t: String!, $a: MailingAddressInput!) {
|
|
645
|
+
customerAddressCreate(customerAccessToken: $t, address: $a) {
|
|
646
|
+
customerAddress { id }
|
|
647
|
+
customerUserErrors { message }
|
|
648
|
+
}
|
|
649
|
+
}`, { t: token, a: address });
|
|
650
|
+
const p = res?.customerAddressCreate;
|
|
651
|
+
const errors = errs(p?.customerUserErrors);
|
|
652
|
+
return {
|
|
653
|
+
ok: Boolean(p?.customerAddress?.id) && errors.length === 0,
|
|
654
|
+
errors,
|
|
655
|
+
...(p?.customerAddress?.id ? { data: { id: p.customerAddress.id } } : {}),
|
|
656
|
+
};
|
|
657
|
+
},
|
|
658
|
+
async updateAddress(token, id, address) {
|
|
659
|
+
const res = await graphql(`mutation AddrUpdate($t: String!, $id: ID!, $a: MailingAddressInput!) {
|
|
660
|
+
customerAddressUpdate(customerAccessToken: $t, id: $id, address: $a) {
|
|
661
|
+
customerAddress { id }
|
|
662
|
+
customerUserErrors { message }
|
|
663
|
+
}
|
|
664
|
+
}`, { t: token, id, a: address });
|
|
665
|
+
const errors = errs(res?.customerAddressUpdate?.customerUserErrors);
|
|
666
|
+
return { ok: errors.length === 0, errors };
|
|
667
|
+
},
|
|
668
|
+
async deleteAddress(token, id) {
|
|
669
|
+
const res = await graphql(`mutation AddrDelete($t: String!, $id: ID!) {
|
|
670
|
+
customerAddressDelete(customerAccessToken: $t, id: $id) {
|
|
671
|
+
deletedCustomerAddressId
|
|
672
|
+
customerUserErrors { message }
|
|
673
|
+
}
|
|
674
|
+
}`, { t: token, id });
|
|
675
|
+
const errors = errs(res?.customerAddressDelete?.customerUserErrors);
|
|
676
|
+
return { ok: errors.length === 0, errors };
|
|
677
|
+
},
|
|
678
|
+
async setDefaultAddress(token, addressId) {
|
|
679
|
+
const res = await graphql(`mutation AddrDefault($t: String!, $id: ID!) {
|
|
680
|
+
customerDefaultAddressUpdate(customerAccessToken: $t, addressId: $id) {
|
|
681
|
+
customer { id }
|
|
682
|
+
customerUserErrors { message }
|
|
683
|
+
}
|
|
684
|
+
}`, { t: token, id: addressId });
|
|
685
|
+
const errors = errs(res?.customerDefaultAddressUpdate?.customerUserErrors);
|
|
686
|
+
return { ok: errors.length === 0, errors };
|
|
687
|
+
},
|
|
688
|
+
};
|
|
689
|
+
return {
|
|
690
|
+
fetchMenu,
|
|
691
|
+
search,
|
|
692
|
+
collectionProducts,
|
|
693
|
+
// No server render seeds this client's listings.
|
|
694
|
+
listingSeed: () => null,
|
|
695
|
+
predictiveSearch,
|
|
696
|
+
productRecommendations,
|
|
697
|
+
blogByHandle,
|
|
698
|
+
articleByHandle,
|
|
699
|
+
metaobject,
|
|
700
|
+
metaobjects,
|
|
701
|
+
customer,
|
|
702
|
+
};
|
|
703
|
+
}
|
|
704
|
+
// ───────────────────────── Token storage helper ──────────────────
|
|
705
|
+
const CUSTOMER_TOKEN_KEY = 'tq-customer-token';
|
|
706
|
+
/** Tiny localStorage helper for the customer access token (SSR-safe no-ops). */
|
|
707
|
+
export const customerTokenStore = {
|
|
708
|
+
get() {
|
|
709
|
+
if (typeof localStorage === 'undefined')
|
|
710
|
+
return null;
|
|
711
|
+
return localStorage.getItem(CUSTOMER_TOKEN_KEY);
|
|
712
|
+
},
|
|
713
|
+
set(token) {
|
|
714
|
+
if (typeof localStorage === 'undefined')
|
|
715
|
+
return;
|
|
716
|
+
localStorage.setItem(CUSTOMER_TOKEN_KEY, token);
|
|
717
|
+
},
|
|
718
|
+
clear() {
|
|
719
|
+
if (typeof localStorage === 'undefined')
|
|
720
|
+
return;
|
|
721
|
+
localStorage.removeItem(CUSTOMER_TOKEN_KEY);
|
|
722
|
+
},
|
|
723
|
+
};
|