@shop-on/theme-server 0.9.0 → 0.11.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/dist/account-state.js +1 -2
- package/dist/account.d.ts +1 -3
- package/dist/account.js +1 -3
- package/dist/activity.d.ts +1 -3
- package/dist/activity.js +1 -2
- package/dist/analytics-proxy.d.ts +8 -14
- package/dist/analytics-proxy.js +111 -28
- package/dist/analytics-script-proxy.d.ts +1 -5
- package/dist/analytics-script-proxy.js +4 -14
- package/dist/api-contract/src/addresses.js +32 -0
- package/dist/api-contract/src/areas.js +49 -0
- package/dist/api-contract/src/custom-data.d.ts +31 -0
- package/dist/api-contract/src/custom-data.js +303 -0
- package/dist/api-contract/src/ids.js +110 -0
- package/dist/api-contract/src/image.js +65 -0
- package/dist/api-contract/src/limits.js +44 -0
- package/dist/api-contract/src/media.js +13 -0
- package/dist/api-contract/src/pickup.js +11 -0
- package/dist/api-contract/src/recommendations.js +4 -0
- package/dist/api-contract/src/shops.js +27 -0
- package/dist/api-contract/src/storefront/analytics.d.ts +100 -0
- package/dist/api-contract/src/storefront/analytics.js +138 -0
- package/dist/api-contract/src/storefront/areas.d.ts +2 -0
- package/dist/api-contract/src/storefront/cart.d.ts +132 -0
- package/dist/api-contract/src/storefront/cart.js +248 -0
- package/dist/api-contract/src/storefront/catalog.d.ts +3 -0
- package/dist/api-contract/src/storefront/checkout-analytics.d.ts +2 -0
- package/dist/api-contract/src/storefront/checkout.d.ts +2 -0
- package/dist/api-contract/src/storefront/checkout.js +624 -0
- package/dist/api-contract/src/storefront/client.d.ts +3 -0
- package/dist/api-contract/src/storefront/client.js +6 -0
- package/dist/api-contract/src/storefront/content.d.ts +3 -0
- package/dist/api-contract/src/storefront/custom-data.d.ts +2 -0
- package/dist/api-contract/src/storefront/custom-data.js +27 -0
- package/dist/api-contract/src/storefront/customer.d.ts +2 -0
- package/dist/api-contract/src/storefront/customer.js +261 -0
- package/dist/api-contract/src/storefront/downloads.d.ts +2 -0
- package/dist/api-contract/src/storefront/draft-order-invoice.d.ts +2 -0
- package/dist/api-contract/src/storefront/forms.d.ts +2 -0
- package/dist/api-contract/src/storefront/forms.js +82 -0
- package/dist/api-contract/src/storefront/index.d.ts +20956 -0
- package/dist/api-contract/src/storefront/orders.d.ts +2 -0
- package/dist/api-contract/src/storefront/policies.d.ts +2 -0
- package/dist/api-contract/src/storefront/procedure.d.ts +2 -0
- package/dist/api-contract/src/storefront/procedure.js +29 -0
- package/dist/api-contract/src/storefront/reviews.d.ts +20 -0
- package/dist/api-contract/src/storefront/search.d.ts +3 -0
- package/dist/api-contract/src/storefront/seo.d.ts +2 -0
- package/dist/api-contract/src/storefront/seo.js +132 -0
- package/dist/api-contract/src/storefront/shop.d.ts +2 -0
- package/dist/api-contract/src/storefront/shop.js +101 -0
- package/dist/api-contract/src/taxonomy.js +5 -0
- package/dist/api-contract/src/text.js +15 -0
- package/dist/api-contract/src/theme-documents.js +13 -0
- package/dist/api-contract/src/theme-translations.d.ts +14 -0
- package/dist/api-contract/src/theme-translations.js +30 -0
- package/dist/api-contract/src/video.js +19 -0
- package/dist/availability.d.ts +6 -3
- package/dist/availability.js +11 -5
- package/dist/blog.d.ts +53 -59
- package/dist/blog.js +197 -497
- package/dist/cache.d.ts +5 -6
- package/dist/cache.js +1 -2
- package/dist/canonical-host.d.ts +4 -6
- package/dist/canonical-host.js +5 -6
- package/dist/card.js +159 -0
- package/dist/cart-customer.d.ts +1 -3
- package/dist/cart-customer.js +64 -105
- package/dist/cart-mutations.d.ts +43 -77
- package/dist/cart-mutations.js +106 -232
- package/dist/cart-recommendations.d.ts +1 -3
- package/dist/cart-recommendations.js +1 -2
- package/dist/cart-session.d.ts +1 -3
- package/dist/cart-session.js +1 -2
- package/dist/cart.d.ts +63 -224
- package/dist/cart.js +70 -197
- package/dist/checkout.d.ts +13 -17
- package/dist/checkout.js +75 -101
- package/dist/coalesce.js +57 -0
- package/dist/collection.d.ts +89 -298
- package/dist/collection.js +210 -656
- package/dist/contact.d.ts +20 -10
- package/dist/contact.js +36 -30
- package/dist/content-version.d.ts +37 -21
- package/dist/content-version.js +64 -42
- package/dist/contract-kit/src/client.d.ts +12 -0
- package/dist/contract-kit/src/client.js +156 -0
- package/dist/contract-kit/src/errors.d.ts +1 -0
- package/dist/contract-kit/src/errors.js +81 -0
- package/dist/contract-kit/src/ids.d.ts +1 -0
- package/dist/contract-kit/src/ids.js +118 -0
- package/dist/contract-kit/src/index.d.ts +7 -0
- package/dist/contract-kit/src/money.d.ts +1 -0
- package/dist/contract-kit/src/money.js +10 -0
- package/dist/contract-kit/src/pagination.d.ts +1 -0
- package/dist/contract-kit/src/procedure.d.ts +69 -0
- package/dist/contract-kit/src/procedure.js +16 -0
- package/dist/contract-kit/src/realtime.d.ts +1 -0
- package/dist/cookie-secret.js +1 -2
- package/dist/csrf.js +1 -2
- package/dist/customer-handoff.d.ts +1 -3
- package/dist/customer-handoff.js +10 -25
- package/dist/customer-session.d.ts +1 -3
- package/dist/customer-session.js +1 -2
- package/dist/customer.d.ts +2 -3
- package/dist/customer.js +27 -44
- package/dist/files.d.ts +49 -10
- package/dist/files.js +106 -89
- package/dist/fonts.d.ts +3 -5
- package/dist/fonts.js +16 -35
- package/dist/html.d.ts +1 -3
- package/dist/html.js +1 -2
- package/dist/liquid.d.ts +4 -10
- package/dist/liquid.js +25 -32
- package/dist/listing.js +79 -0
- package/dist/marketing-cookies.d.ts +1 -3
- package/dist/marketing-cookies.js +1 -2
- package/dist/menu.d.ts +46 -155
- package/dist/menu.js +51 -61
- package/dist/metafields.d.ts +9 -5
- package/dist/metafields.js +10 -38
- package/dist/metaobject.d.ts +10 -5
- package/dist/metaobject.js +43 -66
- package/dist/money.d.ts +7 -0
- package/dist/money.js +18 -0
- package/dist/newsletter.d.ts +19 -10
- package/dist/newsletter.js +31 -25
- package/dist/page.d.ts +13 -17
- package/dist/page.js +49 -105
- package/dist/password-session.d.ts +1 -3
- package/dist/password-session.js +1 -2
- package/dist/password.d.ts +25 -18
- package/dist/password.js +41 -59
- package/dist/policy.d.ts +7 -7
- package/dist/policy.js +19 -55
- package/dist/product.d.ts +42 -67
- package/dist/product.js +144 -644
- package/dist/read-ahead.d.ts +18 -0
- package/dist/read-ahead.js +32 -0
- package/dist/recommendations.d.ts +6 -12
- package/dist/recommendations.js +36 -60
- package/dist/review.d.ts +134 -94
- package/dist/review.js +257 -469
- package/dist/search.d.ts +59 -64
- package/dist/search.js +170 -469
- package/dist/seo-context.d.ts +1 -3
- package/dist/seo-context.js +4 -5
- package/dist/seo-documents.d.ts +50 -0
- package/dist/seo-documents.js +149 -0
- package/dist/shop-seo.d.ts +3 -5
- package/dist/shop-seo.js +7 -26
- package/dist/shop.d.ts +38 -42
- package/dist/shop.js +37 -51
- package/dist/store-availability.d.ts +12 -14
- package/dist/store-availability.js +18 -56
- package/dist/storefront.js +199 -0
- package/dist/structured-data.d.ts +3 -28
- package/dist/structured-data.js +19 -49
- package/dist/trace.js +1 -2
- package/dist/translations.d.ts +26 -31
- package/dist/translations.js +55 -28
- package/package.json +16 -14
- package/dist/graphql-env.d.ts +0 -22778
- package/dist/graphql.d.ts +0 -64
- package/dist/graphql.js +0 -344
- package/dist/schema.storefront.graphql +0 -10483
- package/dist/seo-proxy.d.ts +0 -11
- package/dist/seo-proxy.js +0 -26
package/dist/search.js
CHANGED
|
@@ -1,523 +1,224 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
$first: Int!
|
|
11
|
-
$mediaFirst: Int!
|
|
12
|
-
$includeOptions: Boolean!
|
|
13
|
-
$includeVariants: Boolean = false
|
|
14
|
-
$withMetafields: Boolean = false
|
|
15
|
-
$metafieldFirst: Int = 0
|
|
16
|
-
$metafieldKeys: [MetafieldKeyInput!] = []
|
|
17
|
-
$after: String
|
|
18
|
-
$filter: CollectionProductFilterInput
|
|
19
|
-
$sort: ProductSearchSortEnum
|
|
20
|
-
) {
|
|
21
|
-
productSearch(query: $query, first: $first, after: $after, filter: $filter, sort: $sort) {
|
|
22
|
-
totalCount
|
|
23
|
-
pageInfo {
|
|
24
|
-
endCursor
|
|
25
|
-
hasNextPage
|
|
26
|
-
}
|
|
27
|
-
nodes {
|
|
28
|
-
...CollectionProductNode
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
productSearchFacets(query: $query, filter: $filter) {
|
|
32
|
-
namespace
|
|
33
|
-
key
|
|
34
|
-
label
|
|
35
|
-
values {
|
|
36
|
-
value
|
|
37
|
-
label
|
|
38
|
-
count
|
|
39
|
-
selected
|
|
40
|
-
swatch {
|
|
41
|
-
hex
|
|
42
|
-
image {
|
|
43
|
-
__typename
|
|
44
|
-
... on ShopMediaImage {
|
|
45
|
-
url(transform: { maxWidth: 128, maxHeight: 128 })
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
# Present only on the price facet — min/max bounds for the slider.
|
|
51
|
-
range {
|
|
52
|
-
min
|
|
53
|
-
max
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
# Unfiltered min/max default-variant price across the query matches, for
|
|
57
|
-
# seeding the price-filter slider's bounds (no $filter, so the slider
|
|
58
|
-
# endpoints stay stable as the buyer toggles filters).
|
|
59
|
-
productSearchPriceRange(query: $query) {
|
|
60
|
-
min
|
|
61
|
-
max
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
`, [CollectionProductNodeFragment]);
|
|
65
|
-
function toMoney(m) {
|
|
66
|
-
return {
|
|
67
|
-
amount: Number(m.amount),
|
|
68
|
-
currencyCode: m.currencyCode
|
|
69
|
-
};
|
|
1
|
+
import { cardShape, toCard } from "./card.js";
|
|
2
|
+
import { imageFileInfo } from "./files.js";
|
|
3
|
+
import { listingCard, listingInput, pageInput, toCollectionListItem, toFacets, toPriceBounds } from "./listing.js";
|
|
4
|
+
import { toMoney } from "./money.js";
|
|
5
|
+
import { orFallback, storefront } from "./storefront.js";
|
|
6
|
+
// src/search.ts
|
|
7
|
+
/** A query as the api reads it; empty when there is nothing to search for. */
|
|
8
|
+
function searchQuery(query) {
|
|
9
|
+
return query.trim().slice(0, 200).trim();
|
|
70
10
|
}
|
|
71
|
-
/**
|
|
72
|
-
*
|
|
73
|
-
|
|
11
|
+
/** `RELEVANCE` is the api's `featured` on a search: the merchant's pins for
|
|
12
|
+
* the query first, then the best match. */
|
|
13
|
+
const SEARCH_SORT = {
|
|
14
|
+
RELEVANCE: "featured",
|
|
15
|
+
CREATED_DESC: "newest",
|
|
16
|
+
TITLE_ASC: "title_asc",
|
|
17
|
+
TITLE_DESC: "title_desc",
|
|
18
|
+
PRICE_ASC: "price_asc",
|
|
19
|
+
PRICE_DESC: "price_desc",
|
|
20
|
+
BEST_SELLING: "best_selling"
|
|
21
|
+
};
|
|
22
|
+
const SEARCH_PAGE_SIZE = 24;
|
|
23
|
+
/** Server-driven /search page: one page of the matches with their facets.
|
|
24
|
+
* `null` for a blank query (the route renders the empty prompt) and on a
|
|
25
|
+
* failed read. Uncached because query/filter/sort/page permutations would
|
|
26
|
+
* blow the TTL cache. */
|
|
74
27
|
const fetchProductSearchPage = async (data) => {
|
|
75
|
-
const query = data.query
|
|
28
|
+
const query = searchQuery(data.query);
|
|
76
29
|
if (!query) return null;
|
|
77
|
-
const
|
|
78
|
-
const first = data.first ?? SEARCH_PAGE_SIZE;
|
|
79
|
-
const mediaFirst = data.mediaFirst ?? 5;
|
|
80
|
-
const includeOptions = data.includeOptions ?? true;
|
|
30
|
+
const input = listingInput(data.filter, data.page ?? 1, data.first ?? SEARCH_PAGE_SIZE);
|
|
81
31
|
try {
|
|
82
|
-
const
|
|
32
|
+
const listing = await storefront(data.locale).search.products({
|
|
83
33
|
query,
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
}, data.locale ? { acceptLanguage: data.locale } : void 0);
|
|
93
|
-
const conn = res.productSearch;
|
|
94
|
-
const products = conn.nodes.map(toCollectionProduct);
|
|
95
|
-
const facets = res.productSearchFacets.map((f) => ({
|
|
96
|
-
namespace: f.namespace,
|
|
97
|
-
key: f.key,
|
|
98
|
-
label: f.label,
|
|
99
|
-
values: f.values.map((v) => ({
|
|
100
|
-
value: v.value,
|
|
101
|
-
label: v.label,
|
|
102
|
-
count: v.count,
|
|
103
|
-
selected: v.selected,
|
|
104
|
-
swatch: v.swatch ? {
|
|
105
|
-
hex: v.swatch.hex ?? null,
|
|
106
|
-
imageUrl: v.swatch.image?.__typename === "ShopMediaImage" ? v.swatch.image.url : null
|
|
107
|
-
} : null
|
|
108
|
-
})),
|
|
109
|
-
range: f.range ? toPriceBounds(f.range) : null
|
|
110
|
-
}));
|
|
34
|
+
...input,
|
|
35
|
+
sort: SEARCH_SORT[data.sort ?? "RELEVANCE"],
|
|
36
|
+
card: listingCard({
|
|
37
|
+
mediaFirst: data.mediaFirst ?? 5,
|
|
38
|
+
includeOptions: data.includeOptions ?? true,
|
|
39
|
+
metafieldKeys: data.metafieldKeys
|
|
40
|
+
})
|
|
41
|
+
});
|
|
111
42
|
return {
|
|
112
43
|
query,
|
|
113
|
-
total_count:
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
},
|
|
118
|
-
products,
|
|
119
|
-
facets,
|
|
120
|
-
price_bounds: toPriceBounds(res.productSearchPriceRange)
|
|
44
|
+
total_count: listing.total,
|
|
45
|
+
products: listing.items.map(toCard),
|
|
46
|
+
facets: toFacets(listing.facets, input.selections),
|
|
47
|
+
price_bounds: toPriceBounds(listing.priceRange)
|
|
121
48
|
};
|
|
122
49
|
} catch (err) {
|
|
123
50
|
console.warn("[fetchProductSearchPage] failed:", err);
|
|
124
51
|
return null;
|
|
125
52
|
}
|
|
126
53
|
};
|
|
127
|
-
const EXCERPT_LENGTH = 160;
|
|
128
|
-
const SearchCountsQuery = graphql(`
|
|
129
|
-
query SearchCounts($query: String!) {
|
|
130
|
-
productSearch(query: $query, first: 0) {
|
|
131
|
-
totalCount
|
|
132
|
-
}
|
|
133
|
-
collectionSearch(query: $query, first: 0) {
|
|
134
|
-
totalCount
|
|
135
|
-
}
|
|
136
|
-
contentSearch(query: $query, first: 0) {
|
|
137
|
-
totalCount
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
`);
|
|
141
|
-
const CollectionSearchQuery = graphql(`
|
|
142
|
-
query CollectionSearch($query: String!, $first: Int!, $after: String) {
|
|
143
|
-
collectionSearch(query: $query, first: $first, after: $after) {
|
|
144
|
-
totalCount
|
|
145
|
-
pageInfo {
|
|
146
|
-
hasNextPage
|
|
147
|
-
}
|
|
148
|
-
nodes {
|
|
149
|
-
handle
|
|
150
|
-
title
|
|
151
|
-
description
|
|
152
|
-
descriptionHtml
|
|
153
|
-
productsCount
|
|
154
|
-
featuredImage {
|
|
155
|
-
...CollectionImage
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
`, [CollectionImageFragment]);
|
|
161
|
-
const ContentSearchQuery = graphql(`
|
|
162
|
-
query ContentSearch(
|
|
163
|
-
$query: String!
|
|
164
|
-
$first: Int!
|
|
165
|
-
$after: String
|
|
166
|
-
$excerptLength: Int!
|
|
167
|
-
$withImage: Boolean!
|
|
168
|
-
$imageWidth: Int!
|
|
169
|
-
) {
|
|
170
|
-
contentSearch(query: $query, first: $first, after: $after) {
|
|
171
|
-
totalCount
|
|
172
|
-
pageInfo {
|
|
173
|
-
hasNextPage
|
|
174
|
-
}
|
|
175
|
-
nodes {
|
|
176
|
-
__typename
|
|
177
|
-
... on ShopPage {
|
|
178
|
-
handle
|
|
179
|
-
title
|
|
180
|
-
publishedAt
|
|
181
|
-
excerpt: body(truncateAt: $excerptLength)
|
|
182
|
-
}
|
|
183
|
-
... on ShopArticle {
|
|
184
|
-
handle
|
|
185
|
-
title
|
|
186
|
-
publishedAt
|
|
187
|
-
summary(truncateAt: $excerptLength)
|
|
188
|
-
bodyExcerpt: body(truncateAt: $excerptLength)
|
|
189
|
-
blog {
|
|
190
|
-
handle
|
|
191
|
-
title
|
|
192
|
-
}
|
|
193
|
-
image @include(if: $withImage) {
|
|
194
|
-
__typename
|
|
195
|
-
id
|
|
196
|
-
alt
|
|
197
|
-
... on ShopMediaImage {
|
|
198
|
-
url(transform: { maxWidth: $imageWidth })
|
|
199
|
-
url2x: url(transform: { maxWidth: $imageWidth, scale: 2 })
|
|
200
|
-
width
|
|
201
|
-
height
|
|
202
|
-
focalPoint {
|
|
203
|
-
x
|
|
204
|
-
y
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
`);
|
|
213
54
|
/** A result row's thumbnail width when none is asked for, and its clamp. */
|
|
214
55
|
const RESOURCE_IMAGE_WIDTH = 120;
|
|
215
|
-
|
|
216
|
-
|
|
56
|
+
const IMAGE_MAX_WIDTH = 1200;
|
|
57
|
+
function clampWidth(width, fallback) {
|
|
58
|
+
return Math.min(Math.max(Math.round(width ?? fallback), 1), IMAGE_MAX_WIDTH);
|
|
217
59
|
}
|
|
218
60
|
const emptyResourcePage = () => ({
|
|
219
61
|
total_count: 0,
|
|
220
62
|
has_next_page: false,
|
|
221
63
|
items: []
|
|
222
64
|
});
|
|
223
|
-
/**
|
|
224
|
-
|
|
225
|
-
|
|
65
|
+
/** A page of `items` at `offset` out of `total`. */
|
|
66
|
+
function resourcePage(items, total, offset) {
|
|
67
|
+
return {
|
|
68
|
+
total_count: total,
|
|
69
|
+
has_next_page: offset + items.length < total,
|
|
70
|
+
items
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
/** How many products, collections, and pages and articles a query matches.
|
|
74
|
+
* Null when the read fails — a theme then draws no tab row rather than a row
|
|
75
|
+
* of zeroes it can't stand behind. */
|
|
226
76
|
async function fetchSearchCounts(data) {
|
|
227
|
-
const query = data.query
|
|
77
|
+
const query = searchQuery(data.query);
|
|
228
78
|
if (!query) return {
|
|
229
79
|
products: 0,
|
|
230
80
|
collections: 0,
|
|
231
81
|
content: 0
|
|
232
82
|
};
|
|
233
|
-
|
|
234
|
-
const res = await request(SearchCountsQuery, { query }, data.locale ? { acceptLanguage: data.locale } : void 0);
|
|
235
|
-
return {
|
|
236
|
-
products: res.productSearch.totalCount,
|
|
237
|
-
collections: res.collectionSearch?.totalCount ?? 0,
|
|
238
|
-
content: res.contentSearch?.totalCount ?? 0
|
|
239
|
-
};
|
|
240
|
-
} catch (err) {
|
|
241
|
-
console.warn("[fetchSearchCounts] failed:", err);
|
|
242
|
-
return null;
|
|
243
|
-
}
|
|
83
|
+
return orFallback(storefront(data.locale).search.counts({ query }), null, "fetchSearchCounts");
|
|
244
84
|
}
|
|
245
85
|
/** One page of matching collections, in the shape the collections list page
|
|
246
86
|
* already renders (`CollectionListItem`), best match first. */
|
|
247
87
|
async function fetchCollectionSearchPage(data) {
|
|
248
|
-
const query = data.query
|
|
88
|
+
const query = searchQuery(data.query);
|
|
249
89
|
if (!query) return emptyResourcePage();
|
|
90
|
+
const page = pageInput(data.page ?? 1, data.first);
|
|
250
91
|
try {
|
|
251
|
-
const
|
|
92
|
+
const answer = await storefront(data.locale).search.collections({
|
|
252
93
|
query,
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
if (!conn) return emptyResourcePage();
|
|
257
|
-
return {
|
|
258
|
-
total_count: conn.totalCount,
|
|
259
|
-
has_next_page: conn.pageInfo.hasNextPage,
|
|
260
|
-
items: conn.nodes.map((c) => ({
|
|
261
|
-
handle: c.handle,
|
|
262
|
-
title: c.title,
|
|
263
|
-
description: c.description,
|
|
264
|
-
description_html: c.descriptionHtml,
|
|
265
|
-
image: c.featuredImage ? toFileInfo(c.featuredImage, { baseWidth: 640 }) : null,
|
|
266
|
-
products_count: c.productsCount
|
|
267
|
-
}))
|
|
268
|
-
};
|
|
94
|
+
...page
|
|
95
|
+
});
|
|
96
|
+
return resourcePage(answer.items.map(toCollectionListItem), answer.total, page.offset);
|
|
269
97
|
} catch (err) {
|
|
270
98
|
console.warn("[fetchCollectionSearchPage] failed:", err);
|
|
271
99
|
return emptyResourcePage();
|
|
272
100
|
}
|
|
273
101
|
}
|
|
274
102
|
/** One page of matching pages and articles as a single ranked list. An
|
|
275
|
-
* article's excerpt is its summary,
|
|
276
|
-
* `imageWidth` for a thumbnail on each article; without it none is
|
|
103
|
+
* article's excerpt is its summary, else its body's opening. Pass
|
|
104
|
+
* `imageWidth` for a thumbnail on each article; without it none is drawn. */
|
|
277
105
|
async function fetchContentSearchPage(data) {
|
|
278
|
-
const query = data.query
|
|
106
|
+
const query = searchQuery(data.query);
|
|
279
107
|
if (!query) return emptyResourcePage();
|
|
280
108
|
const withImage = data.imageWidth !== void 0;
|
|
281
|
-
const imageWidth =
|
|
109
|
+
const imageWidth = clampWidth(data.imageWidth, RESOURCE_IMAGE_WIDTH);
|
|
110
|
+
const page = pageInput(data.page ?? 1, data.first);
|
|
282
111
|
try {
|
|
283
|
-
const
|
|
112
|
+
const answer = await storefront(data.locale).search.content({
|
|
284
113
|
query,
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
...withImage && { image: node.image ? toFileInfo(node.image, { baseWidth: imageWidth }) : null }
|
|
304
|
-
} : {
|
|
305
|
-
kind: "page",
|
|
306
|
-
handle: node.handle,
|
|
307
|
-
title: node.title,
|
|
308
|
-
excerpt: node.excerpt,
|
|
309
|
-
publishedAt: node.publishedAt ?? null
|
|
310
|
-
})
|
|
311
|
-
};
|
|
114
|
+
...page
|
|
115
|
+
});
|
|
116
|
+
return resourcePage(answer.items.map((hit) => hit.kind === "article" ? {
|
|
117
|
+
kind: "article",
|
|
118
|
+
handle: hit.handle,
|
|
119
|
+
blogHandle: hit.blog.handle,
|
|
120
|
+
blogTitle: hit.blog.title,
|
|
121
|
+
title: hit.title,
|
|
122
|
+
excerpt: hit.excerpt,
|
|
123
|
+
publishedAt: hit.publishedAt,
|
|
124
|
+
...withImage && { image: hit.image ? imageFileInfo(hit.image, imageWidth) : null }
|
|
125
|
+
} : {
|
|
126
|
+
kind: "page",
|
|
127
|
+
handle: hit.handle,
|
|
128
|
+
title: hit.title,
|
|
129
|
+
excerpt: hit.excerpt,
|
|
130
|
+
publishedAt: hit.publishedAt
|
|
131
|
+
}), answer.total, page.offset);
|
|
312
132
|
} catch (err) {
|
|
313
133
|
console.warn("[fetchContentSearchPage] failed:", err);
|
|
314
134
|
return emptyResourcePage();
|
|
315
135
|
}
|
|
316
136
|
}
|
|
317
|
-
const RESOURCE_SUGGESTIONS = 3;
|
|
318
|
-
const ResourceSuggestionsQuery = graphql(`
|
|
319
|
-
query ResourceSuggestions($query: String!, $first: Int!) {
|
|
320
|
-
collectionSearch(query: $query, first: $first) {
|
|
321
|
-
nodes {
|
|
322
|
-
handle
|
|
323
|
-
title
|
|
324
|
-
}
|
|
325
|
-
}
|
|
326
|
-
contentSearch(query: $query, first: $first) {
|
|
327
|
-
nodes {
|
|
328
|
-
__typename
|
|
329
|
-
... on ShopPage {
|
|
330
|
-
handle
|
|
331
|
-
title
|
|
332
|
-
}
|
|
333
|
-
... on ShopArticle {
|
|
334
|
-
handle
|
|
335
|
-
title
|
|
336
|
-
blog {
|
|
337
|
-
handle
|
|
338
|
-
}
|
|
339
|
-
}
|
|
340
|
-
}
|
|
341
|
-
}
|
|
342
|
-
}
|
|
343
|
-
`);
|
|
344
|
-
/** Top matching collections, pages and articles for a typeahead — its own
|
|
345
|
-
* lean operation, issued in the same tick as `fetchProductSuggestions` so the
|
|
346
|
-
* pair rides one POST. Hit per keystroke, so a failure (or a tripped rate
|
|
347
|
-
* limit, which nulls a field) is simply two empty groups: the product rows
|
|
348
|
-
* must not go down with it. */
|
|
349
|
-
async function fetchResourceSuggestions(data) {
|
|
350
|
-
const query = data.query.trim();
|
|
351
|
-
if (query.length < 2) return {
|
|
352
|
-
collections: [],
|
|
353
|
-
content: []
|
|
354
|
-
};
|
|
355
|
-
try {
|
|
356
|
-
const res = await request(ResourceSuggestionsQuery, {
|
|
357
|
-
query,
|
|
358
|
-
first: RESOURCE_SUGGESTIONS
|
|
359
|
-
}, data.locale ? { acceptLanguage: data.locale } : void 0);
|
|
360
|
-
return {
|
|
361
|
-
collections: (res.collectionSearch?.nodes ?? []).map((c) => ({
|
|
362
|
-
handle: c.handle,
|
|
363
|
-
title: c.title
|
|
364
|
-
})),
|
|
365
|
-
content: (res.contentSearch?.nodes ?? []).map((node) => node.__typename === "ShopArticle" ? {
|
|
366
|
-
kind: "article",
|
|
367
|
-
handle: node.handle,
|
|
368
|
-
blogHandle: node.blog.handle,
|
|
369
|
-
title: node.title
|
|
370
|
-
} : {
|
|
371
|
-
kind: "page",
|
|
372
|
-
handle: node.handle,
|
|
373
|
-
title: node.title
|
|
374
|
-
})
|
|
375
|
-
};
|
|
376
|
-
} catch (err) {
|
|
377
|
-
console.warn("[fetchResourceSuggestions] failed:", err);
|
|
378
|
-
return {
|
|
379
|
-
collections: [],
|
|
380
|
-
content: []
|
|
381
|
-
};
|
|
382
|
-
}
|
|
383
|
-
}
|
|
384
137
|
const SUGGESTIONS_DEFAULT = 6;
|
|
385
|
-
const
|
|
386
|
-
const SUGGESTIONS_MIN_QUERY_LENGTH = 1;
|
|
138
|
+
const RESOURCE_SUGGESTIONS = 3;
|
|
387
139
|
const SUGGESTIONS_MEDIA_WIDTH = 80;
|
|
388
|
-
const SUGGESTIONS_MEDIA_MAX_WIDTH = 1200;
|
|
389
140
|
const TERM_SUGGESTIONS = 5;
|
|
390
|
-
const
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
let termsPromise;
|
|
473
|
-
try {
|
|
474
|
-
termsPromise = request(SearchTermSuggestionsQuery, {
|
|
475
|
-
query: q,
|
|
476
|
-
first: TERM_SUGGESTIONS
|
|
477
|
-
}, options).catch(() => ({ searchTermSuggestions: null }));
|
|
478
|
-
if (q.length < SUGGESTIONS_MIN_QUERY_LENGTH) return {
|
|
479
|
-
suggestions: [],
|
|
480
|
-
terms: (await termsPromise).searchTermSuggestions
|
|
481
|
-
};
|
|
482
|
-
const first = Math.min(Math.max(data.first ?? SUGGESTIONS_DEFAULT, 1), SUGGESTIONS_MAX);
|
|
483
|
-
const mediaWidth = Math.min(Math.max(Math.round(data.mediaWidth ?? SUGGESTIONS_MEDIA_WIDTH), 1), SUGGESTIONS_MEDIA_MAX_WIDTH);
|
|
484
|
-
const withHover = data.hover === true;
|
|
485
|
-
const withQuickAdd = data.quickAdd === true;
|
|
486
|
-
const productsPromise = request(ProductSuggestionsQuery, {
|
|
487
|
-
query: q,
|
|
488
|
-
first,
|
|
489
|
-
mediaWidth,
|
|
490
|
-
withHover,
|
|
491
|
-
withQuickAdd
|
|
492
|
-
}, options);
|
|
493
|
-
const [res, termsRes] = await Promise.all([productsPromise, termsPromise]);
|
|
494
|
-
return {
|
|
495
|
-
suggestions: res.productSuggestions.map((p) => {
|
|
496
|
-
const images = (p.media?.nodes ?? []).map((n) => toFileInfo(n, { baseWidth: mediaWidth })).filter((m) => m?.kind === "image");
|
|
497
|
-
return {
|
|
498
|
-
handle: p.handle,
|
|
499
|
-
title: p.title,
|
|
500
|
-
vendor: p.vendor ?? null,
|
|
501
|
-
featured_media: p.featuredMedia ? toFileInfo(p.featuredMedia) : null,
|
|
502
|
-
secondary_media: images[1] ?? null,
|
|
503
|
-
price: toMoney(p.defaultVariant.priceV2),
|
|
504
|
-
compare_at: p.defaultVariant.compareAtV2 ? toMoney(p.defaultVariant.compareAtV2) : null,
|
|
505
|
-
...withQuickAdd ? {
|
|
506
|
-
defaultVariantId: p.defaultVariant.id,
|
|
507
|
-
available: p.defaultVariant.availableForSale,
|
|
508
|
-
variantsCount: p.variantsCount
|
|
509
|
-
} : {}
|
|
510
|
-
};
|
|
511
|
-
}),
|
|
512
|
-
terms: termsRes.searchTermSuggestions
|
|
513
|
-
};
|
|
514
|
-
} catch (err) {
|
|
515
|
-
console.warn("[fetchProductSuggestions] failed:", err);
|
|
516
|
-
return {
|
|
517
|
-
suggestions: [],
|
|
518
|
-
terms: termsPromise ? (await termsPromise).searchTermSuggestions : null
|
|
519
|
-
};
|
|
520
|
-
}
|
|
141
|
+
const noRows = {
|
|
142
|
+
suggestions: [],
|
|
143
|
+
collections: [],
|
|
144
|
+
content: []
|
|
145
|
+
};
|
|
146
|
+
/**
|
|
147
|
+
* Storefront typeahead, hit per keystroke (debounced by the caller): one
|
|
148
|
+
* `search.suggest` for the rows of every kind and, beside it, one
|
|
149
|
+
* `search.terms` for the completions. Terms are read unless the theme says
|
|
150
|
+
* `terms: false` (one that draws them only for a blank query passes it on a
|
|
151
|
+
* matched one) — a blank query is the empty field's "most searched" list —
|
|
152
|
+
* and are the optional half: a terms failure (or `terms: false`) is
|
|
153
|
+
* `terms: null`, a rows failure no rows, neither takes the other down. The
|
|
154
|
+
* server is the trust boundary for every number.
|
|
155
|
+
*
|
|
156
|
+
* A plain function, not a `createServerFn`: a server fn declared in a package
|
|
157
|
+
* and reached only through a theme handler's dynamic import never makes
|
|
158
|
+
* Start's server-fn manifest, so calling it fails at runtime with "Server
|
|
159
|
+
* function info not found". A theme wraps this in its own server fn.
|
|
160
|
+
*/
|
|
161
|
+
async function fetchSearchSuggestions(data) {
|
|
162
|
+
const query = searchQuery(data.query);
|
|
163
|
+
const client = storefront(data.locale);
|
|
164
|
+
const terms = data.terms === false ? Promise.resolve(null) : orFallback(client.search.terms({
|
|
165
|
+
query,
|
|
166
|
+
limit: TERM_SUGGESTIONS
|
|
167
|
+
}).then((answer) => answer.terms), null, "fetchSearchSuggestions terms");
|
|
168
|
+
if (!query) return {
|
|
169
|
+
...noRows,
|
|
170
|
+
terms: await terms
|
|
171
|
+
};
|
|
172
|
+
const mediaWidth = clampWidth(data.mediaWidth, SUGGESTIONS_MEDIA_WIDTH);
|
|
173
|
+
const hover = data.hover === true;
|
|
174
|
+
const quickAdd = data.quickAdd === true;
|
|
175
|
+
const rows = orFallback(client.search.suggest({
|
|
176
|
+
query,
|
|
177
|
+
products: Math.min(Math.max(data.first ?? SUGGESTIONS_DEFAULT, 1), 10),
|
|
178
|
+
resources: data.resources ? RESOURCE_SUGGESTIONS : 0,
|
|
179
|
+
card: cardShape({ media: hover ? 2 : 1 })
|
|
180
|
+
}), null, "fetchSearchSuggestions");
|
|
181
|
+
const [answer, termList] = await Promise.all([rows, terms]);
|
|
182
|
+
if (!answer) return {
|
|
183
|
+
...noRows,
|
|
184
|
+
terms: termList
|
|
185
|
+
};
|
|
186
|
+
return {
|
|
187
|
+
suggestions: answer.products.map((p) => {
|
|
188
|
+
const lead = p.media[0];
|
|
189
|
+
const shown = lead?.image ?? lead?.video?.poster ?? null;
|
|
190
|
+
const second = p.media[1]?.image ?? null;
|
|
191
|
+
return {
|
|
192
|
+
handle: p.handle,
|
|
193
|
+
title: p.title,
|
|
194
|
+
vendor: p.vendor,
|
|
195
|
+
featured_media: shown ? imageFileInfo(shown, mediaWidth) : null,
|
|
196
|
+
...hover && { secondary_media: lead?.image && second ? imageFileInfo(second, mediaWidth) : null },
|
|
197
|
+
price: toMoney(p.defaultVariant.price),
|
|
198
|
+
compare_at: p.defaultVariant.compareAtPrice === null ? null : toMoney(p.defaultVariant.compareAtPrice),
|
|
199
|
+
...quickAdd && {
|
|
200
|
+
defaultVariantId: p.defaultVariant.id,
|
|
201
|
+
available: p.defaultVariant.available,
|
|
202
|
+
variantsCount: p.variantsCount
|
|
203
|
+
}
|
|
204
|
+
};
|
|
205
|
+
}),
|
|
206
|
+
collections: answer.collections.map((c) => ({
|
|
207
|
+
handle: c.handle,
|
|
208
|
+
title: c.title
|
|
209
|
+
})),
|
|
210
|
+
content: answer.content.map((hit) => hit.kind === "article" ? {
|
|
211
|
+
kind: "article",
|
|
212
|
+
handle: hit.handle,
|
|
213
|
+
blogHandle: hit.blog.handle,
|
|
214
|
+
blogTitle: hit.blog.title,
|
|
215
|
+
title: hit.title
|
|
216
|
+
} : {
|
|
217
|
+
kind: "page",
|
|
218
|
+
handle: hit.handle,
|
|
219
|
+
title: hit.title
|
|
220
|
+
}),
|
|
221
|
+
terms: termList
|
|
222
|
+
};
|
|
521
223
|
}
|
|
522
|
-
|
|
523
|
-
export { fetchCollectionSearchPage, fetchContentSearchPage, fetchProductSearchPage, fetchProductSuggestions, fetchResourceSuggestions, fetchSearchCounts };
|
|
224
|
+
export { fetchCollectionSearchPage, fetchContentSearchPage, fetchProductSearchPage, fetchSearchCounts, fetchSearchSuggestions };
|