@decocms/apps-vtex 7.2.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/package.json +67 -0
- package/src/README.md +6 -0
- package/src/__tests__/client-segment-cookie.test.ts +255 -0
- package/src/__tests__/client-set-cookie-forward.test.ts +257 -0
- package/src/actions/address.ts +250 -0
- package/src/actions/analytics/sendEvent.ts +86 -0
- package/src/actions/auth.ts +333 -0
- package/src/actions/checkout.ts +522 -0
- package/src/actions/index.ts +11 -0
- package/src/actions/masterData.ts +168 -0
- package/src/actions/misc.ts +188 -0
- package/src/actions/newsletter.ts +105 -0
- package/src/actions/orders.ts +34 -0
- package/src/actions/profile.ts +201 -0
- package/src/actions/session.ts +85 -0
- package/src/actions/trigger.ts +43 -0
- package/src/actions/wishlist.ts +114 -0
- package/src/client.ts +667 -0
- package/src/commerceLoaders.ts +261 -0
- package/src/hooks/__tests__/createUseCart.test.ts +184 -0
- package/src/hooks/__tests__/createUseUser.test.ts +48 -0
- package/src/hooks/__tests__/createUseWishlist.test.ts +87 -0
- package/src/hooks/createUseCart.ts +360 -0
- package/src/hooks/createUseUser.ts +153 -0
- package/src/hooks/createUseWishlist.ts +242 -0
- package/src/hooks/index.ts +19 -0
- package/src/hooks/useAutocomplete.ts +83 -0
- package/src/hooks/useCart.ts +243 -0
- package/src/hooks/useUser.ts +78 -0
- package/src/hooks/useWishlist.ts +119 -0
- package/src/index.ts +17 -0
- package/src/invoke.ts +181 -0
- package/src/loaders/ProductDetailsPage.ts +1 -0
- package/src/loaders/ProductList.ts +1 -0
- package/src/loaders/ProductListingPage.ts +1 -0
- package/src/loaders/address.ts +115 -0
- package/src/loaders/autocomplete.ts +58 -0
- package/src/loaders/brands.ts +44 -0
- package/src/loaders/cart.ts +52 -0
- package/src/loaders/catalog.ts +163 -0
- package/src/loaders/collections.ts +55 -0
- package/src/loaders/index.ts +19 -0
- package/src/loaders/intelligentSearch/__tests__/productListingPage.test.ts +20 -0
- package/src/loaders/intelligentSearch/productDetailsPage.ts +95 -0
- package/src/loaders/intelligentSearch/productList.ts +154 -0
- package/src/loaders/intelligentSearch/productListingPage.ts +506 -0
- package/src/loaders/intelligentSearch/suggestions.ts +46 -0
- package/src/loaders/legacy/productDetailsPage.ts +1 -0
- package/src/loaders/legacy/productList.ts +1 -0
- package/src/loaders/legacy/relatedProductsLoader.ts +81 -0
- package/src/loaders/legacy.ts +635 -0
- package/src/loaders/logistics.ts +111 -0
- package/src/loaders/minicart.ts +78 -0
- package/src/loaders/navbar.ts +26 -0
- package/src/loaders/orders.ts +92 -0
- package/src/loaders/pageType.ts +68 -0
- package/src/loaders/payment.ts +97 -0
- package/src/loaders/productListFull.ts +159 -0
- package/src/loaders/profile.ts +138 -0
- package/src/loaders/promotion.ts +30 -0
- package/src/loaders/search.ts +124 -0
- package/src/loaders/session.ts +83 -0
- package/src/loaders/user.ts +87 -0
- package/src/loaders/wishlist.ts +89 -0
- package/src/loaders/wishlistProducts.ts +69 -0
- package/src/loaders/workflow/products.ts +64 -0
- package/src/loaders/workflow.ts +316 -0
- package/src/logo.png +0 -0
- package/src/middleware.ts +240 -0
- package/src/mod.ts +152 -0
- package/src/registry.ts +9 -0
- package/src/types.ts +248 -0
- package/src/utils/__tests__/cookieSanitizer.test.ts +162 -0
- package/src/utils/__tests__/fetch.test.ts +80 -0
- package/src/utils/__tests__/fetchCache.test.ts +112 -0
- package/src/utils/__tests__/instrumentedFetch.test.ts +158 -0
- package/src/utils/__tests__/intelligentSearch.test.ts +88 -0
- package/src/utils/__tests__/minicart.test.ts +184 -0
- package/src/utils/__tests__/operationRouter.test.ts +227 -0
- package/src/utils/__tests__/resourceRange.test.ts +32 -0
- package/src/utils/__tests__/sitemap.test.ts +185 -0
- package/src/utils/__tests__/slugify.test.ts +31 -0
- package/src/utils/__tests__/transform.test.ts +698 -0
- package/src/utils/accountLoaders.ts +203 -0
- package/src/utils/authHelpers.ts +85 -0
- package/src/utils/batch.ts +18 -0
- package/src/utils/cookieSanitizer.ts +173 -0
- package/src/utils/cookies.ts +167 -0
- package/src/utils/enrichment.ts +560 -0
- package/src/utils/fetch.ts +107 -0
- package/src/utils/fetchCache.ts +222 -0
- package/src/utils/index.ts +19 -0
- package/src/utils/instrumentedFetch.ts +78 -0
- package/src/utils/intelligentSearch.ts +96 -0
- package/src/utils/legacy.ts +139 -0
- package/src/utils/minicart.ts +139 -0
- package/src/utils/operationRouter.ts +139 -0
- package/src/utils/pickAndOmit.ts +25 -0
- package/src/utils/proxy.ts +435 -0
- package/src/utils/resourceRange.ts +10 -0
- package/src/utils/segment.ts +152 -0
- package/src/utils/similars.ts +37 -0
- package/src/utils/sitemap.ts +282 -0
- package/src/utils/slugCache.ts +28 -0
- package/src/utils/slugify.ts +13 -0
- package/src/utils/transform.ts +1509 -0
- package/src/utils/types.ts +1884 -0
- package/src/utils/vtexId.ts +138 -0
- package/tsconfig.json +7 -0
|
@@ -0,0 +1,506 @@
|
|
|
1
|
+
import {
|
|
2
|
+
filtersFromPageTypes,
|
|
3
|
+
getVtexConfig,
|
|
4
|
+
intelligentSearch,
|
|
5
|
+
type PageType,
|
|
6
|
+
pageTypesFromPath,
|
|
7
|
+
toFacetPath,
|
|
8
|
+
} from "../../client";
|
|
9
|
+
import { pickSku, toProduct } from "../../utils/transform";
|
|
10
|
+
import type { Product as ProductVTEX, Sort } from "../../utils/types";
|
|
11
|
+
|
|
12
|
+
export interface SelectedFacet {
|
|
13
|
+
key: string;
|
|
14
|
+
value: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Friendly fuzzy labels for CMS UIs. Translate to the raw IS API value via
|
|
19
|
+
* {@link mapLabelledFuzzyToFuzzy} before passing into a loader's `fuzzy` field.
|
|
20
|
+
*/
|
|
21
|
+
export type LabelledFuzzy = "automatic" | "disabled" | "enabled";
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Translate a friendly fuzzy label to the value the VTEX Intelligent Search
|
|
25
|
+
* API expects. Returns `undefined` when the label is omitted so callers can
|
|
26
|
+
* skip the param entirely.
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* intelligentSearch({ fuzzy: mapLabelledFuzzyToFuzzy(props.fuzzy) })
|
|
30
|
+
*/
|
|
31
|
+
export const mapLabelledFuzzyToFuzzy = (label?: LabelledFuzzy): "0" | "1" | "auto" | undefined => {
|
|
32
|
+
switch (label) {
|
|
33
|
+
case "automatic":
|
|
34
|
+
return "auto";
|
|
35
|
+
case "enabled":
|
|
36
|
+
return "1";
|
|
37
|
+
case "disabled":
|
|
38
|
+
return "0";
|
|
39
|
+
default:
|
|
40
|
+
return undefined;
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
export interface PLPProps {
|
|
45
|
+
/**
|
|
46
|
+
* @title Query
|
|
47
|
+
* @description Overrides the search term used to fetch the listing.
|
|
48
|
+
*/
|
|
49
|
+
query?: string;
|
|
50
|
+
/**
|
|
51
|
+
* @title Items per page
|
|
52
|
+
* @description Number of products per page to display.
|
|
53
|
+
*/
|
|
54
|
+
count?: number;
|
|
55
|
+
/**
|
|
56
|
+
* @title Sorting
|
|
57
|
+
* @description Order in which products are returned.
|
|
58
|
+
*/
|
|
59
|
+
sort?: Sort;
|
|
60
|
+
/**
|
|
61
|
+
* @title Fuzzy
|
|
62
|
+
* @description Controls Intelligent Search typo tolerance.
|
|
63
|
+
*/
|
|
64
|
+
fuzzy?: LabelledFuzzy;
|
|
65
|
+
/**
|
|
66
|
+
* @title Page offset
|
|
67
|
+
* @description Starting page (0-indexed) for the listing query.
|
|
68
|
+
*/
|
|
69
|
+
page?: number;
|
|
70
|
+
/**
|
|
71
|
+
* @title Selected Facets
|
|
72
|
+
* @description Override selected facets from the URL (e.g. force a collection).
|
|
73
|
+
*/
|
|
74
|
+
selectedFacets?: SelectedFacet[];
|
|
75
|
+
/**
|
|
76
|
+
* @title Hide Unavailable Items
|
|
77
|
+
* @description Do not return out-of-stock items.
|
|
78
|
+
*/
|
|
79
|
+
hideUnavailableItems?: boolean;
|
|
80
|
+
/** Injected by CMS resolve — the matched page path (e.g. "/pisos/piso-vinilico-clicado") */
|
|
81
|
+
__pagePath?: string;
|
|
82
|
+
/** Injected by CMS resolve — the full request URL (e.g. "https://site.com/s?q=telha&sort=price:asc") */
|
|
83
|
+
__pageUrl?: string;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// -- Types matching VTEX IS API responses --
|
|
87
|
+
|
|
88
|
+
interface ISPaginationItem {
|
|
89
|
+
index: number;
|
|
90
|
+
proxyUrl?: string;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
interface ISPagination {
|
|
94
|
+
count: number;
|
|
95
|
+
current: ISPaginationItem;
|
|
96
|
+
before: ISPaginationItem[];
|
|
97
|
+
after: ISPaginationItem[];
|
|
98
|
+
perPage: number;
|
|
99
|
+
next: ISPaginationItem;
|
|
100
|
+
previous: ISPaginationItem;
|
|
101
|
+
first: ISPaginationItem;
|
|
102
|
+
last: ISPaginationItem;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
interface ISProductSearchResult {
|
|
106
|
+
products: any[];
|
|
107
|
+
recordsFiltered: number;
|
|
108
|
+
pagination: ISPagination;
|
|
109
|
+
correction?: { misspelled?: boolean };
|
|
110
|
+
operator?: string;
|
|
111
|
+
redirect?: string;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
interface ISFacetValueBoolean {
|
|
115
|
+
quantity: number;
|
|
116
|
+
name: string;
|
|
117
|
+
value: string;
|
|
118
|
+
selected: boolean;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
interface ISFacetValueRange {
|
|
122
|
+
quantity: number;
|
|
123
|
+
name: string;
|
|
124
|
+
selected: boolean;
|
|
125
|
+
range: { from: number; to: number };
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
interface ISFacet {
|
|
129
|
+
key: string;
|
|
130
|
+
name: string;
|
|
131
|
+
type: "TEXT" | "PRICERANGE";
|
|
132
|
+
hidden: boolean;
|
|
133
|
+
quantity: number;
|
|
134
|
+
values: Array<ISFacetValueBoolean | ISFacetValueRange>;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
interface ISFacetsResult {
|
|
138
|
+
facets: ISFacet[];
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// Valid page types for filtering (matching original getValidTypesFromPageTypes)
|
|
142
|
+
const VALID_PAGE_TYPES = new Set([
|
|
143
|
+
"Brand",
|
|
144
|
+
"Category",
|
|
145
|
+
"Department",
|
|
146
|
+
"SubCategory",
|
|
147
|
+
"Collection",
|
|
148
|
+
"Cluster",
|
|
149
|
+
"Search",
|
|
150
|
+
"FullText",
|
|
151
|
+
"Product",
|
|
152
|
+
]);
|
|
153
|
+
|
|
154
|
+
function getValidPageTypes(pageTypes: PageType[]): PageType[] {
|
|
155
|
+
return pageTypes.filter((pt) => VALID_PAGE_TYPES.has(pt.pageType));
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// -- Filter transformation (mirrors original toFilter + facetToToggle) --
|
|
159
|
+
|
|
160
|
+
function formatRange(from: number, to: number): string {
|
|
161
|
+
return `${from}:${to}`;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function isRangeValue(val: any): val is ISFacetValueRange {
|
|
165
|
+
return Boolean(val.range);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function filtersToSearchParams(
|
|
169
|
+
facets: SelectedFacet[],
|
|
170
|
+
paramsToPersist?: URLSearchParams,
|
|
171
|
+
): URLSearchParams {
|
|
172
|
+
const searchParams = new URLSearchParams(paramsToPersist);
|
|
173
|
+
for (const { key, value } of facets) {
|
|
174
|
+
searchParams.append(`filter.${key}`, value);
|
|
175
|
+
}
|
|
176
|
+
return searchParams;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function facetToToggle(
|
|
180
|
+
selectedFacets: SelectedFacet[],
|
|
181
|
+
key: string,
|
|
182
|
+
paramsToPersist?: URLSearchParams,
|
|
183
|
+
basePath = "",
|
|
184
|
+
) {
|
|
185
|
+
return (item: ISFacetValueBoolean | ISFacetValueRange) => {
|
|
186
|
+
const { quantity, selected } = item;
|
|
187
|
+
const isRange = isRangeValue(item);
|
|
188
|
+
const value = isRange
|
|
189
|
+
? formatRange(item.range.from, item.range.to)
|
|
190
|
+
: (item as ISFacetValueBoolean).value;
|
|
191
|
+
const label = isRange ? value : (item as ISFacetValueBoolean).name;
|
|
192
|
+
const facet = { key, value };
|
|
193
|
+
|
|
194
|
+
const filters = selected
|
|
195
|
+
? selectedFacets.filter((f) => f.key !== key || f.value !== value)
|
|
196
|
+
: [...selectedFacets, facet];
|
|
197
|
+
|
|
198
|
+
return {
|
|
199
|
+
value,
|
|
200
|
+
quantity,
|
|
201
|
+
selected,
|
|
202
|
+
url: `${basePath}?${filtersToSearchParams(filters, paramsToPersist)}`,
|
|
203
|
+
label,
|
|
204
|
+
};
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
function toFilter(
|
|
209
|
+
selectedFacets: SelectedFacet[],
|
|
210
|
+
paramsToPersist?: URLSearchParams,
|
|
211
|
+
basePath = "",
|
|
212
|
+
) {
|
|
213
|
+
return (facet: ISFacet) => ({
|
|
214
|
+
"@type": "FilterToggle" as const,
|
|
215
|
+
key: facet.key,
|
|
216
|
+
label: facet.name,
|
|
217
|
+
quantity: facet.quantity,
|
|
218
|
+
values: facet.values.map(facetToToggle(selectedFacets, facet.key, paramsToPersist, basePath)),
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
// -- Breadcrumb from page types (mirrors original pageTypesToBreadcrumbList) --
|
|
223
|
+
|
|
224
|
+
function pageTypesToBreadcrumb(pageTypes: PageType[]) {
|
|
225
|
+
const filtered = pageTypes.filter(
|
|
226
|
+
(pt) =>
|
|
227
|
+
pt.pageType === "Category" || pt.pageType === "Department" || pt.pageType === "SubCategory",
|
|
228
|
+
);
|
|
229
|
+
return filtered.map((page, index) => {
|
|
230
|
+
const position = index + 1;
|
|
231
|
+
const slugParts = filtered.slice(0, position).map((x) => {
|
|
232
|
+
const urlPath = x.url ? new URL(`http://${x.url}`).pathname : "";
|
|
233
|
+
const segments = urlPath.split("/").filter(Boolean);
|
|
234
|
+
return segments[segments.length - 1]?.toLowerCase() ?? "";
|
|
235
|
+
});
|
|
236
|
+
return {
|
|
237
|
+
"@type": "ListItem" as const,
|
|
238
|
+
name: page.name,
|
|
239
|
+
item: `/${slugParts.join("/")}`,
|
|
240
|
+
position,
|
|
241
|
+
};
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
// -- SEO from page types (mirrors original pageTypesToSeo) --
|
|
246
|
+
|
|
247
|
+
function pageTypesToSeo(pageTypes: PageType[]) {
|
|
248
|
+
const current = pageTypes[pageTypes.length - 1];
|
|
249
|
+
if (!current) return undefined;
|
|
250
|
+
return {
|
|
251
|
+
title: current.title || current.name || "",
|
|
252
|
+
description: current.metaTagDescription || "",
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
// -- Build IS query params (mirrors original withDefaultParams) --
|
|
257
|
+
|
|
258
|
+
function buildISParams(opts: {
|
|
259
|
+
query: string;
|
|
260
|
+
page: number;
|
|
261
|
+
count: number;
|
|
262
|
+
sort: string;
|
|
263
|
+
fuzzy?: string;
|
|
264
|
+
locale: string;
|
|
265
|
+
hideUnavailableItems: boolean;
|
|
266
|
+
}): Record<string, string> {
|
|
267
|
+
const params: Record<string, string> = {
|
|
268
|
+
page: String(opts.page + 1), // IS API is 1-indexed
|
|
269
|
+
count: String(opts.count),
|
|
270
|
+
query: opts.query,
|
|
271
|
+
sort: opts.sort,
|
|
272
|
+
locale: opts.locale,
|
|
273
|
+
hideUnavailableItems: String(opts.hideUnavailableItems),
|
|
274
|
+
};
|
|
275
|
+
if (opts.fuzzy) params.fuzzy = opts.fuzzy;
|
|
276
|
+
return params;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
const INVALID_PLP_PREFIXES = [
|
|
280
|
+
"/image/",
|
|
281
|
+
"/.well-known/",
|
|
282
|
+
"/assets/",
|
|
283
|
+
"/favicon",
|
|
284
|
+
"/_serverFn/",
|
|
285
|
+
"/_build/",
|
|
286
|
+
"/node_modules/",
|
|
287
|
+
];
|
|
288
|
+
|
|
289
|
+
function isValidPLPPath(path: string): boolean {
|
|
290
|
+
const lower = path.toLowerCase();
|
|
291
|
+
if (INVALID_PLP_PREFIXES.some((p) => lower.startsWith(p))) return false;
|
|
292
|
+
const ext = lower.split("/").pop()?.split(".")?.pop();
|
|
293
|
+
if (
|
|
294
|
+
ext &&
|
|
295
|
+
[
|
|
296
|
+
"png",
|
|
297
|
+
"jpg",
|
|
298
|
+
"jpeg",
|
|
299
|
+
"gif",
|
|
300
|
+
"svg",
|
|
301
|
+
"webp",
|
|
302
|
+
"ico",
|
|
303
|
+
"css",
|
|
304
|
+
"js",
|
|
305
|
+
"woff",
|
|
306
|
+
"woff2",
|
|
307
|
+
"ttf",
|
|
308
|
+
].includes(ext)
|
|
309
|
+
) {
|
|
310
|
+
return false;
|
|
311
|
+
}
|
|
312
|
+
return true;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* Mirrors the original deco-cx/apps PLP loader:
|
|
317
|
+
*
|
|
318
|
+
* 1. Resolve facets from CMS props or Page Type API
|
|
319
|
+
* 2. Call product_search AND facets APIs in parallel (same params)
|
|
320
|
+
* 3. Transform products to schema.org format
|
|
321
|
+
* 4. Transform facets to FilterToggle format
|
|
322
|
+
* 5. Build pagination from IS response
|
|
323
|
+
*/
|
|
324
|
+
export default async function vtexProductListingPage(props: PLPProps): Promise<any | null> {
|
|
325
|
+
const pageUrl = props.__pageUrl ? new URL(props.__pageUrl, "https://localhost") : null;
|
|
326
|
+
|
|
327
|
+
const query = props.query ?? pageUrl?.searchParams.get("q") ?? "";
|
|
328
|
+
const countFromUrl = pageUrl?.searchParams.get("PS");
|
|
329
|
+
const rawCount = Number(countFromUrl ?? props.count ?? 12);
|
|
330
|
+
const count = Number.isFinite(rawCount) && rawCount > 0 ? rawCount : 12;
|
|
331
|
+
const sort = props.sort || pageUrl?.searchParams.get("sort") || "";
|
|
332
|
+
// props.fuzzy is a friendly LabelledFuzzy ("automatic"|…) — translate it to the
|
|
333
|
+
// raw IS API value. The URL param is already a raw value, so it passes through.
|
|
334
|
+
const fuzzy =
|
|
335
|
+
mapLabelledFuzzyToFuzzy(props.fuzzy) ?? pageUrl?.searchParams.get("fuzzy") ?? undefined;
|
|
336
|
+
const pageFromUrl = pageUrl?.searchParams.get("page");
|
|
337
|
+
const rawPage = props.page ?? (pageFromUrl ? Number(pageFromUrl) - 1 : 0);
|
|
338
|
+
const page = Number.isFinite(rawPage) && rawPage >= 0 ? Math.floor(rawPage) : 0;
|
|
339
|
+
|
|
340
|
+
const { selectedFacets: cmsSelectedFacets, hideUnavailableItems = false, __pagePath } = props;
|
|
341
|
+
|
|
342
|
+
try {
|
|
343
|
+
// 1. Resolve selected facets (CMS + URL filter.* params, matching original)
|
|
344
|
+
let facets: SelectedFacet[] =
|
|
345
|
+
cmsSelectedFacets && cmsSelectedFacets.length > 0 ? [...cmsSelectedFacets] : [];
|
|
346
|
+
|
|
347
|
+
// Extract filter.* params from URL (e.g. filter.category-1=telhas)
|
|
348
|
+
if (pageUrl) {
|
|
349
|
+
for (const [name, value] of pageUrl.searchParams.entries()) {
|
|
350
|
+
const dotIndex = name.indexOf(".");
|
|
351
|
+
if (dotIndex > 0 && name.slice(0, dotIndex) === "filter") {
|
|
352
|
+
const key = name.slice(dotIndex + 1);
|
|
353
|
+
if (key && !facets.some((f) => f.key === key && f.value === value)) {
|
|
354
|
+
facets.push({ key, value });
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
// Handle VTEX `map` query param (e.g. /1368?map=productClusterIds).
|
|
361
|
+
// The `map` param tells IS how to interpret each path segment as a facet type.
|
|
362
|
+
// Segments and map values are positionally matched (comma-separated).
|
|
363
|
+
if (facets.length === 0 && pageUrl && __pagePath) {
|
|
364
|
+
const mapParam = pageUrl.searchParams.get("map");
|
|
365
|
+
if (mapParam) {
|
|
366
|
+
const segments = __pagePath.split("/").filter(Boolean);
|
|
367
|
+
const mapValues = mapParam.split(",");
|
|
368
|
+
for (let i = 0; i < Math.min(segments.length, mapValues.length); i++) {
|
|
369
|
+
const key = mapValues[i].trim();
|
|
370
|
+
const value = decodeURIComponent(segments[i]);
|
|
371
|
+
if (key && value) {
|
|
372
|
+
facets.push({ key, value });
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
let pageTypes: PageType[] = [];
|
|
379
|
+
|
|
380
|
+
if (
|
|
381
|
+
facets.length === 0 &&
|
|
382
|
+
!query &&
|
|
383
|
+
__pagePath &&
|
|
384
|
+
__pagePath !== "/" &&
|
|
385
|
+
__pagePath !== "/*" &&
|
|
386
|
+
isValidPLPPath(__pagePath)
|
|
387
|
+
) {
|
|
388
|
+
const allPageTypes = await pageTypesFromPath(__pagePath);
|
|
389
|
+
pageTypes = getValidPageTypes(allPageTypes);
|
|
390
|
+
facets = filtersFromPageTypes(pageTypes);
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
if (!facets.length && !query) {
|
|
394
|
+
return null;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
const facetPath = toFacetPath(facets);
|
|
398
|
+
const config = getVtexConfig();
|
|
399
|
+
const locale = config.locale ?? "pt-BR";
|
|
400
|
+
|
|
401
|
+
const params = buildISParams({
|
|
402
|
+
query,
|
|
403
|
+
page,
|
|
404
|
+
count,
|
|
405
|
+
sort,
|
|
406
|
+
fuzzy,
|
|
407
|
+
locale,
|
|
408
|
+
hideUnavailableItems,
|
|
409
|
+
});
|
|
410
|
+
|
|
411
|
+
const productEndpoint = facetPath ? `/product_search/${facetPath}` : "/product_search/";
|
|
412
|
+
|
|
413
|
+
const facetsEndpoint = facetPath ? `/facets/${facetPath}` : "/facets/";
|
|
414
|
+
|
|
415
|
+
// 2. Parallel calls — exactly like the original
|
|
416
|
+
const [productsResult, facetsResult] = await Promise.all([
|
|
417
|
+
intelligentSearch<ISProductSearchResult>(productEndpoint, params),
|
|
418
|
+
intelligentSearch<ISFacetsResult>(facetsEndpoint, params),
|
|
419
|
+
]);
|
|
420
|
+
|
|
421
|
+
const { products: vtexProducts, pagination, recordsFiltered } = productsResult;
|
|
422
|
+
|
|
423
|
+
// 3. Transform products using shared transform pipeline (same as deco-cx/apps)
|
|
424
|
+
const baseUrl = config.publicUrl
|
|
425
|
+
? `https://${config.publicUrl}`
|
|
426
|
+
: `https://${config.account}.vtexcommercestable.com.br`;
|
|
427
|
+
|
|
428
|
+
const schemaProducts = (vtexProducts as ProductVTEX[]).map((p) => {
|
|
429
|
+
const sku = pickSku(p);
|
|
430
|
+
return toProduct(p, sku, 0, { baseUrl, priceCurrency: "BRL" });
|
|
431
|
+
});
|
|
432
|
+
|
|
433
|
+
// Persist URL params (q, sort, filter.*) across filter toggles and pagination links
|
|
434
|
+
const paramsToPersist = new URLSearchParams();
|
|
435
|
+
if (pageUrl) {
|
|
436
|
+
for (const [k, v] of pageUrl.searchParams.entries()) {
|
|
437
|
+
if (k !== "page" && k !== "PS" && !k.startsWith("filter.")) {
|
|
438
|
+
paramsToPersist.append(k, v);
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
} else {
|
|
442
|
+
if (query) paramsToPersist.set("q", query);
|
|
443
|
+
if (sort) paramsToPersist.set("sort", sort);
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
// 4. Transform facets to filters (matching original toFilter)
|
|
447
|
+
const visibleFacets = facetsResult.facets.filter((f) => !f.hidden);
|
|
448
|
+
const basePath = __pagePath && __pagePath !== "/" ? __pagePath : "";
|
|
449
|
+
const filters = visibleFacets.map(toFilter(facets, paramsToPersist, basePath));
|
|
450
|
+
|
|
451
|
+
// 5. Build pagination (matching original logic)
|
|
452
|
+
const currentPageoffset = 1;
|
|
453
|
+
const hasNextPage = Boolean(pagination.next?.proxyUrl);
|
|
454
|
+
const hasPreviousPage = page > 0;
|
|
455
|
+
|
|
456
|
+
const nextPageParams = new URLSearchParams(paramsToPersist);
|
|
457
|
+
const prevPageParams = new URLSearchParams(paramsToPersist);
|
|
458
|
+
|
|
459
|
+
// Re-add active filter.* params so pagination links preserve selected filters
|
|
460
|
+
for (const { key, value } of facets) {
|
|
461
|
+
nextPageParams.append(`filter.${key}`, value);
|
|
462
|
+
prevPageParams.append(`filter.${key}`, value);
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
if (hasNextPage) {
|
|
466
|
+
nextPageParams.set("page", String(page + currentPageoffset + 1));
|
|
467
|
+
}
|
|
468
|
+
if (hasPreviousPage) {
|
|
469
|
+
prevPageParams.set("page", String(page + currentPageoffset - 1));
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
const breadcrumbItems = pageTypesToBreadcrumb(pageTypes);
|
|
473
|
+
|
|
474
|
+
return {
|
|
475
|
+
"@type": "ProductListingPage",
|
|
476
|
+
breadcrumb: {
|
|
477
|
+
"@type": "BreadcrumbList",
|
|
478
|
+
itemListElement: breadcrumbItems,
|
|
479
|
+
numberOfItems: breadcrumbItems.length,
|
|
480
|
+
},
|
|
481
|
+
filters,
|
|
482
|
+
products: schemaProducts,
|
|
483
|
+
pageInfo: {
|
|
484
|
+
nextPage: hasNextPage ? `${basePath}?${nextPageParams}` : undefined,
|
|
485
|
+
previousPage: hasPreviousPage ? `${basePath}?${prevPageParams}` : undefined,
|
|
486
|
+
currentPage: page + currentPageoffset,
|
|
487
|
+
records: recordsFiltered,
|
|
488
|
+
recordPerPage: pagination.perPage,
|
|
489
|
+
},
|
|
490
|
+
sortOptions: [
|
|
491
|
+
{ value: "", label: "relevance:desc" },
|
|
492
|
+
{ value: "price:desc", label: "price:desc" },
|
|
493
|
+
{ value: "price:asc", label: "price:asc" },
|
|
494
|
+
{ value: "orders:desc", label: "orders:desc" },
|
|
495
|
+
{ value: "name:desc", label: "name:desc" },
|
|
496
|
+
{ value: "name:asc", label: "name:asc" },
|
|
497
|
+
{ value: "release:desc", label: "release:desc" },
|
|
498
|
+
{ value: "discount:desc", label: "discount:desc" },
|
|
499
|
+
],
|
|
500
|
+
seo: pageTypesToSeo(pageTypes),
|
|
501
|
+
};
|
|
502
|
+
} catch (error) {
|
|
503
|
+
console.error("[VTEX] PLP error:", error);
|
|
504
|
+
return null;
|
|
505
|
+
}
|
|
506
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* IS autocomplete suggestions loader.
|
|
3
|
+
* Maps VTEX IS response to commerce Suggestion type.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import type { Product, Suggestion } from "@decocms/apps-commerce/types";
|
|
7
|
+
import { getVtexConfig, intelligentSearch } from "../../client";
|
|
8
|
+
import { pickSku, toProduct } from "../../utils/transform";
|
|
9
|
+
import type { Product as ProductVTEX } from "../../utils/types";
|
|
10
|
+
|
|
11
|
+
export interface SuggestionsProps {
|
|
12
|
+
query?: string;
|
|
13
|
+
count?: number;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export default async function vtexSuggestions(props: SuggestionsProps): Promise<Suggestion | null> {
|
|
17
|
+
const query = props.query || "";
|
|
18
|
+
if (!query.trim()) return { searches: [], products: [] };
|
|
19
|
+
|
|
20
|
+
try {
|
|
21
|
+
const data = await intelligentSearch<{
|
|
22
|
+
searches: Array<{ term: string; count: number }>;
|
|
23
|
+
products: ProductVTEX[];
|
|
24
|
+
}>("/autocomplete_suggestions/", { query });
|
|
25
|
+
|
|
26
|
+
const searches = (data.searches ?? []).map((s) => ({
|
|
27
|
+
term: s.term,
|
|
28
|
+
hits: s.count || 0,
|
|
29
|
+
}));
|
|
30
|
+
|
|
31
|
+
const config = getVtexConfig();
|
|
32
|
+
const baseUrl = config.publicUrl
|
|
33
|
+
? `https://${config.publicUrl}`
|
|
34
|
+
: `https://${config.account}.vtexcommercestable.${config.domain ?? "com.br"}`;
|
|
35
|
+
|
|
36
|
+
const products: Product[] = (data.products ?? []).slice(0, props.count ?? 4).map((p) => {
|
|
37
|
+
const sku = pickSku(p);
|
|
38
|
+
return toProduct(p, sku, 0, { baseUrl, priceCurrency: "BRL" });
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
return { searches, products };
|
|
42
|
+
} catch (error) {
|
|
43
|
+
console.error("[VTEX] Suggestions error:", error);
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default, type PDPProps } from "../intelligentSearch/productDetailsPage";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default, type ProductListProps } from "../intelligentSearch/productList";
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Related/cross-selling products loader using Legacy Catalog API + shared transform.
|
|
3
|
+
* Maps VTEX catalog response to schema.org Product[] following deco-cx/apps pattern.
|
|
4
|
+
*
|
|
5
|
+
* Includes in-flight dedup for slug→productId resolution so multiple sections
|
|
6
|
+
* on the same page (similars, suggestions, whoboughtalsobought, etc.) share a
|
|
7
|
+
* single search/{slug}/p call instead of each doing their own.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import type { Product } from "@decocms/apps-commerce/types";
|
|
11
|
+
import { getVtexConfig, vtexCachedFetch } from "../../client";
|
|
12
|
+
import { resolveProductIdBySlug } from "../../utils/slugCache";
|
|
13
|
+
import { pickSku, toProduct } from "../../utils/transform";
|
|
14
|
+
import type { LegacyProduct } from "../../utils/types";
|
|
15
|
+
|
|
16
|
+
export type CrossSellingType =
|
|
17
|
+
| "similars"
|
|
18
|
+
| "suggestions"
|
|
19
|
+
| "accessories"
|
|
20
|
+
| "whosawalsosaw"
|
|
21
|
+
| "whosawalsobought"
|
|
22
|
+
| "whoboughtalsobought"
|
|
23
|
+
| "showtogether";
|
|
24
|
+
|
|
25
|
+
export interface RelatedProductsProps {
|
|
26
|
+
slug?: string;
|
|
27
|
+
productId?: string;
|
|
28
|
+
crossSelling?: CrossSellingType;
|
|
29
|
+
count?: number;
|
|
30
|
+
hideUnavailableItems?: boolean;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function fetchCrossSelling(
|
|
34
|
+
type: CrossSellingType,
|
|
35
|
+
productId: string,
|
|
36
|
+
): Promise<LegacyProduct[] | null> {
|
|
37
|
+
return vtexCachedFetch<LegacyProduct[]>(
|
|
38
|
+
`/api/catalog_system/pub/products/crossselling/${type}/${productId}`,
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export default async function vtexRelatedProducts(
|
|
43
|
+
props: RelatedProductsProps,
|
|
44
|
+
): Promise<Product[] | null> {
|
|
45
|
+
const { slug, crossSelling = "similars", count = 8 } = props;
|
|
46
|
+
|
|
47
|
+
let productId = props.productId;
|
|
48
|
+
|
|
49
|
+
if (!productId) {
|
|
50
|
+
if (!slug) return null;
|
|
51
|
+
const linkText = slug.replace(/\/p$/, "").replace(/^\//, "");
|
|
52
|
+
productId = (await resolveProductIdBySlug(linkText)) ?? undefined;
|
|
53
|
+
if (!productId) return null;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
try {
|
|
57
|
+
const related = await fetchCrossSelling(crossSelling, productId);
|
|
58
|
+
if (!related?.length) return [];
|
|
59
|
+
|
|
60
|
+
const config = getVtexConfig();
|
|
61
|
+
const baseUrl = config.publicUrl
|
|
62
|
+
? `https://${config.publicUrl}`
|
|
63
|
+
: `https://${config.account}.vtexcommercestable.${config.domain ?? "com.br"}`;
|
|
64
|
+
|
|
65
|
+
let result = related.slice(0, count).map((p) => {
|
|
66
|
+
const sku = pickSku(p);
|
|
67
|
+
return toProduct(p, sku, 0, { baseUrl, priceCurrency: "BRL" });
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
if (props.hideUnavailableItems) {
|
|
71
|
+
result = result.filter((p) =>
|
|
72
|
+
p.offers?.offers?.some((o) => o.availability === "https://schema.org/InStock"),
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return result;
|
|
77
|
+
} catch (error) {
|
|
78
|
+
console.error("[VTEX] Related products error:", error);
|
|
79
|
+
return null;
|
|
80
|
+
}
|
|
81
|
+
}
|