@revenexx/cover 0.13.0 → 0.15.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/app/api/product.ts +1 -0
- package/app/components/layout/header/LocaleSwitcher.vue +96 -12
- package/app/components/ui/forms/SearchInput.vue +205 -8
- package/app/composables/useProductCard.ts +14 -0
- package/app/composables/useProductListing.ts +27 -1
- package/app/composables/useProductLocales.ts +56 -0
- package/app/composables/useSearchEvents.ts +67 -0
- package/app/interfaces/market.ts +7 -0
- package/app/layouts/default.vue +10 -1
- package/app/plugins/search-events.client.ts +41 -0
- package/app/shared/locale.ts +133 -0
- package/app/shared/productLocales.ts +109 -0
- package/app/shared/searchAttribution.ts +91 -0
- package/app/shared/themeEvents.ts +1 -1
- package/i18n/locales/de/common.json +2 -1
- package/i18n/locales/de/search.json +7 -0
- package/i18n/locales/en/common.json +2 -1
- package/i18n/locales/en/search.json +7 -0
- package/package.json +1 -1
- package/server/api/brands.get.ts +3 -2
- package/server/api/categories/[slug]/subcategories.get.ts +9 -6
- package/server/api/product/[id]/locales.get.ts +57 -0
- package/server/api/products/index.get.ts +6 -1
- package/server/api/search/events.post.ts +65 -0
- package/server/api/typesense/search.post.ts +12 -1
- package/server/api/typesense/suggest.post.ts +43 -22
- package/server/interfaces/brand.ts +3 -2
- package/server/interfaces/product.ts +13 -0
- package/server/services/ApiBrandService.ts +8 -8
- package/server/services/ApiMarketService.ts +9 -2
- package/server/services/ApiProductService.ts +5 -1
- package/server/utils/liveCatalog.ts +69 -0
- package/server/utils/locale.ts +58 -4
- package/server/utils/searchClient.ts +27 -4
- package/server/utils/searchEvents.ts +46 -0
- package/server/utils/searchMultiSearchFallback.ts +2 -0
- package/server/utils/searchSuggest.ts +207 -0
- package/server/utils/suggestTiles.ts +141 -0
|
@@ -55,6 +55,12 @@ interface SearchAnswer {
|
|
|
55
55
|
* or an http(s) URL). Only the search page follows it.
|
|
56
56
|
*/
|
|
57
57
|
redirect?: string;
|
|
58
|
+
/** The search's id, for the click events its results send (PE-867); live mode only. */
|
|
59
|
+
queryId?: string;
|
|
60
|
+
/** The locale whose index answered (live only; see SearchClientResult.locale). */
|
|
61
|
+
locale?: string;
|
|
62
|
+
/** True when the index of another language answered (see SearchClientResult.localeFallback). */
|
|
63
|
+
localeFallback?: boolean;
|
|
58
64
|
}
|
|
59
65
|
|
|
60
66
|
export default defineEventHandler(async (event): Promise<SearchAnswer | null> => {
|
|
@@ -78,6 +84,9 @@ export default defineEventHandler(async (event): Promise<SearchAnswer | null> =>
|
|
|
78
84
|
// storefront's filter names and maps the facets back.
|
|
79
85
|
if (resolveProductServiceKey(event) === "api") {
|
|
80
86
|
const locale = resolveLocale(event);
|
|
87
|
+
// The market's locale for this page (`de-CH`, `fr-FR`): the search
|
|
88
|
+
// service routes it to that language's index (PE-866).
|
|
89
|
+
const searchLocale = await resolveMarketLocale(event);
|
|
81
90
|
try {
|
|
82
91
|
const request = await buildLiveListingFilters(filters, priceRange, locale);
|
|
83
92
|
const view = request.pinnedCategoryCode ? "category" : "search";
|
|
@@ -89,7 +98,7 @@ export default defineEventHandler(async (event): Promise<SearchAnswer | null> =>
|
|
|
89
98
|
sort,
|
|
90
99
|
page,
|
|
91
100
|
perPage,
|
|
92
|
-
locale,
|
|
101
|
+
locale: searchLocale,
|
|
93
102
|
});
|
|
94
103
|
|
|
95
104
|
// Catalog shell only — offers (price + availability) hydrate
|
|
@@ -108,6 +117,8 @@ export default defineEventHandler(async (event): Promise<SearchAnswer | null> =>
|
|
|
108
117
|
perPage,
|
|
109
118
|
priceFilter: liveListingPriceFilter(result, request.priceRequested),
|
|
110
119
|
...(redirect ? { redirect } : {}),
|
|
120
|
+
...(result.queryId ? { queryId: result.queryId } : {}),
|
|
121
|
+
...(result.locale ? { locale: result.locale, localeFallback: result.localeFallback } : {}),
|
|
111
122
|
};
|
|
112
123
|
}
|
|
113
124
|
catch (err) {
|
|
@@ -1,39 +1,58 @@
|
|
|
1
|
+
import type { CategoryTile, ManufacturerTile, PageTile, ProductSuggestion } from "../../utils/suggestTiles";
|
|
2
|
+
import {
|
|
3
|
+
categoryTiles,
|
|
4
|
+
manufacturerTiles,
|
|
5
|
+
pageTiles,
|
|
6
|
+
productSuggestion,
|
|
7
|
+
} from "../../utils/suggestTiles";
|
|
8
|
+
|
|
1
9
|
const SUGGESTION_COUNT = 6;
|
|
10
|
+
const TILE_COUNT = 4;
|
|
11
|
+
const PAGE_COUNT = 3;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* The quick search answer: product suggestions plus category, manufacturer and
|
|
15
|
+
* page tiles (PE-867), the `queryId` the dropdown sends back with its click
|
|
16
|
+
* events, and where curation sends the query on Enter, if anywhere.
|
|
17
|
+
*/
|
|
18
|
+
export interface SuggestAnswer {
|
|
19
|
+
suggestions: ProductSuggestion[];
|
|
20
|
+
categories: CategoryTile[];
|
|
21
|
+
manufacturers: ManufacturerTile[];
|
|
22
|
+
pages: PageTile[];
|
|
23
|
+
queryId: string;
|
|
24
|
+
redirect?: string;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const EMPTY: SuggestAnswer = { suggestions: [], categories: [], manufacturers: [], pages: [], queryId: "" };
|
|
2
28
|
|
|
3
|
-
export default defineEventHandler(async (event) => {
|
|
29
|
+
export default defineEventHandler(async (event): Promise<SuggestAnswer> => {
|
|
4
30
|
const { q } = await readBody<{ q: string }>(event);
|
|
5
31
|
|
|
6
32
|
if (!q?.trim()) {
|
|
7
|
-
return {
|
|
33
|
+
return { ...EMPTY };
|
|
8
34
|
}
|
|
9
35
|
|
|
10
|
-
// Live mode
|
|
11
|
-
//
|
|
36
|
+
// Live mode: one request to the search service's suggest endpoint, every
|
|
37
|
+
// collection with its own `suggest` preset (searchSuggest.ts); the local
|
|
38
|
+
// docker index stays the mock implementation, products only.
|
|
12
39
|
if (resolveProductServiceKey(event) === "api") {
|
|
13
|
-
const locale =
|
|
40
|
+
const locale = await resolveMarketLocale(event);
|
|
14
41
|
try {
|
|
15
42
|
// Twice the suggestions: on the deprecated multi_search fallback a
|
|
16
43
|
// product still indexed under both `de` and `de-DE` comes back twice
|
|
17
44
|
// and is folded into one, which would leave the list short.
|
|
18
|
-
const result = await
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
perPage: SUGGESTION_COUNT * 2,
|
|
22
|
-
locale,
|
|
23
|
-
});
|
|
24
|
-
const docs = result.docs.slice(0, SUGGESTION_COUNT);
|
|
45
|
+
const result = await searchSuggestClient.suggest({ q, locale, perPage: SUGGESTION_COUNT * 2 });
|
|
46
|
+
const tree = result.categories.length ? await getLiveCategories(locale) : [];
|
|
47
|
+
const redirect = searchRedirect(result);
|
|
25
48
|
|
|
26
49
|
return {
|
|
27
|
-
suggestions:
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
categorySlug: doc.categories?.[0] ?? "",
|
|
34
|
-
subcategorySlug: undefined,
|
|
35
|
-
};
|
|
36
|
-
}),
|
|
50
|
+
suggestions: result.products.slice(0, SUGGESTION_COUNT).map(productSuggestion),
|
|
51
|
+
categories: categoryTiles(result.categories, tree, TILE_COUNT),
|
|
52
|
+
manufacturers: manufacturerTiles(result.manufacturers, TILE_COUNT),
|
|
53
|
+
pages: pageTiles(result.pages, PAGE_COUNT),
|
|
54
|
+
queryId: result.queryId,
|
|
55
|
+
...(redirect ? { redirect } : {}),
|
|
37
56
|
};
|
|
38
57
|
}
|
|
39
58
|
catch (err) {
|
|
@@ -54,10 +73,12 @@ export default defineEventHandler(async (event) => {
|
|
|
54
73
|
});
|
|
55
74
|
|
|
56
75
|
return {
|
|
76
|
+
...EMPTY,
|
|
57
77
|
suggestions: (results.hits ?? []).map((hit) => {
|
|
58
78
|
const doc = hit.document as Record<string, unknown>;
|
|
59
79
|
return {
|
|
60
80
|
id: String(doc.id),
|
|
81
|
+
docId: String(doc.id),
|
|
61
82
|
name: doc.name as string,
|
|
62
83
|
sku: doc.sku as string,
|
|
63
84
|
category: doc.category as string,
|
|
@@ -7,11 +7,12 @@ import type { Locale } from "../utils/locale";
|
|
|
7
7
|
* Register the active implementation via app.config → brandService.
|
|
8
8
|
*/
|
|
9
9
|
export interface IBrandService {
|
|
10
|
-
|
|
10
|
+
/** `searchLocale`: the market locale to search in, defaults to `locale` (PE-866). */
|
|
11
|
+
listBrands(limit: number, locale: Locale, searchLocale?: string): Promise<ShopBrand[]>;
|
|
11
12
|
/**
|
|
12
13
|
* The complete manufacturer list without logos (`logo: null`) — for pickers
|
|
13
14
|
* that must offer every brand, not the top N a grid shows. Cheaper than
|
|
14
15
|
* `listBrands`: no per-brand logo lookups.
|
|
15
16
|
*/
|
|
16
|
-
listBrandNames(locale: Locale): Promise<ShopBrand[]>;
|
|
17
|
+
listBrandNames(locale: Locale, searchLocale?: string): Promise<ShopBrand[]>;
|
|
17
18
|
}
|
|
@@ -9,6 +9,11 @@ export interface ProductListFilter {
|
|
|
9
9
|
subcategory?: string;
|
|
10
10
|
/** Request locale — live implementations serve locale-scoped documents. */
|
|
11
11
|
locale?: Locale;
|
|
12
|
+
/**
|
|
13
|
+
* The market locale to search in (`de-CH`, `fr-FR`); defaults to `locale`.
|
|
14
|
+
* The search service routes it to that language's index (PE-866).
|
|
15
|
+
*/
|
|
16
|
+
searchLocale?: string;
|
|
12
17
|
}
|
|
13
18
|
|
|
14
19
|
/**
|
|
@@ -32,4 +37,12 @@ export interface IProductService {
|
|
|
32
37
|
* renders no associations section.
|
|
33
38
|
*/
|
|
34
39
|
getProductAssociations(id: string, locale?: Locale): Promise<ProductAssociations | null>;
|
|
40
|
+
/**
|
|
41
|
+
* The storefront languages (of `languages`) this product has a page in. A
|
|
42
|
+
* catalogue that hides untranslated products (`search_untranslated=hide`)
|
|
43
|
+
* has none in a language the product is not translated into. Optional: a
|
|
44
|
+
* service without it has the product in every language, which is what the
|
|
45
|
+
* storefront assumes when it is missing.
|
|
46
|
+
*/
|
|
47
|
+
getProductLanguages?(id: string, languages: readonly string[]): Promise<string[]>;
|
|
35
48
|
}
|
|
@@ -26,14 +26,14 @@ const cache = new Map<string, { brands: ShopBrand[]; loadedAt: number }>();
|
|
|
26
26
|
* for the same brand.
|
|
27
27
|
*/
|
|
28
28
|
export class ApiBrandService implements IBrandService {
|
|
29
|
-
async listBrands(limit: number, locale: Locale): Promise<ShopBrand[]> {
|
|
30
|
-
const cacheKey = `${revenexxTenantKey()}:${
|
|
29
|
+
async listBrands(limit: number, locale: Locale, searchLocale: string = locale): Promise<ShopBrand[]> {
|
|
30
|
+
const cacheKey = `${revenexxTenantKey()}:${searchLocale}:${limit}`;
|
|
31
31
|
const cached = cache.get(cacheKey);
|
|
32
32
|
if (cached && Date.now() - cached.loadedAt < BRAND_CACHE_TTL_MS) {
|
|
33
33
|
return cached.brands;
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
-
const counts = await this.facetManufacturers(
|
|
36
|
+
const counts = await this.facetManufacturers(searchLocale);
|
|
37
37
|
const top = counts.filter(entry => entry.value.trim().length > 0).slice(0, limit);
|
|
38
38
|
|
|
39
39
|
if (top.length === 0) {
|
|
@@ -53,7 +53,7 @@ export class ApiBrandService implements IBrandService {
|
|
|
53
53
|
view: "brand",
|
|
54
54
|
filters: { manufacturer: [entry.value] },
|
|
55
55
|
perPage: 1,
|
|
56
|
-
locale,
|
|
56
|
+
locale: searchLocale,
|
|
57
57
|
}))));
|
|
58
58
|
}
|
|
59
59
|
|
|
@@ -70,15 +70,15 @@ export class ApiBrandService implements IBrandService {
|
|
|
70
70
|
return brands;
|
|
71
71
|
}
|
|
72
72
|
|
|
73
|
-
async listBrandNames(locale: Locale): Promise<ShopBrand[]> {
|
|
73
|
+
async listBrandNames(locale: Locale, searchLocale: string = locale): Promise<ShopBrand[]> {
|
|
74
74
|
// "names" cannot collide with a numeric limit key.
|
|
75
|
-
const cacheKey = `${revenexxTenantKey()}:${
|
|
75
|
+
const cacheKey = `${revenexxTenantKey()}:${searchLocale}:names`;
|
|
76
76
|
const cached = cache.get(cacheKey);
|
|
77
77
|
if (cached && Date.now() - cached.loadedAt < BRAND_CACHE_TTL_MS) {
|
|
78
78
|
return cached.brands;
|
|
79
79
|
}
|
|
80
80
|
|
|
81
|
-
const counts = await this.facetManufacturers(
|
|
81
|
+
const counts = await this.facetManufacturers(searchLocale);
|
|
82
82
|
const brands = counts
|
|
83
83
|
.filter(entry => entry.value.trim().length > 0)
|
|
84
84
|
.map((entry): ShopBrand => ({ name: entry.value, logo: null, productCount: entry.count }));
|
|
@@ -88,7 +88,7 @@ export class ApiBrandService implements IBrandService {
|
|
|
88
88
|
}
|
|
89
89
|
|
|
90
90
|
/** Round trip 1 of listBrands: names + counts, count-descending. */
|
|
91
|
-
private async facetManufacturers(locale:
|
|
91
|
+
private async facetManufacturers(locale: string): Promise<{ value: string; count: number }[]> {
|
|
92
92
|
const result = await searchClient.search({ view: "brand", locale });
|
|
93
93
|
|
|
94
94
|
return result.facets.find(facet => facet.field === "manufacturer")?.counts ?? [];
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { ShopMarket, ShopMarketCurrency, ShopMarketLocale } from "../../app/interfaces/market";
|
|
2
|
+
import { offeredLocales } from "../../app/shared/locale";
|
|
2
3
|
import type { IMarketService } from "../interfaces/market";
|
|
3
4
|
|
|
4
5
|
interface ApiMarketRow {
|
|
@@ -17,6 +18,8 @@ interface ApiLocaleRow {
|
|
|
17
18
|
country: string;
|
|
18
19
|
is_default: boolean;
|
|
19
20
|
position: number;
|
|
21
|
+
/** `preparing` | `active`; absent on markets builds before PE-866 (= active). */
|
|
22
|
+
status?: string | null;
|
|
20
23
|
}
|
|
21
24
|
|
|
22
25
|
interface ApiCurrencyRow {
|
|
@@ -72,12 +75,16 @@ export class ApiMarketService implements IMarketService {
|
|
|
72
75
|
name: market.name,
|
|
73
76
|
currency: market.currency,
|
|
74
77
|
isDefault: market.is_default,
|
|
75
|
-
|
|
78
|
+
// Default first, a `preparing` locale left out: the shop never
|
|
79
|
+
// offers a language whose catalogue and search are still being
|
|
80
|
+
// built (PE-866).
|
|
81
|
+
locales: offeredLocales(locales.map((locale): ShopMarketLocale => ({
|
|
76
82
|
code: locale.code,
|
|
77
83
|
language: locale.language,
|
|
78
84
|
country: locale.country,
|
|
79
85
|
isDefault: locale.is_default,
|
|
80
|
-
|
|
86
|
+
status: locale.status === "preparing" ? "preparing" : "active",
|
|
87
|
+
}))),
|
|
81
88
|
currencies: currencies.map((currency): ShopMarketCurrency => ({
|
|
82
89
|
code: currency.code,
|
|
83
90
|
isDefault: currency.is_default,
|
|
@@ -32,7 +32,7 @@ export class ApiProductService implements IProductService {
|
|
|
32
32
|
view: "listing",
|
|
33
33
|
...(categories.length ? { filters: { categories } } : {}),
|
|
34
34
|
perPage: LIST_PAGE_SIZE,
|
|
35
|
-
locale,
|
|
35
|
+
locale: filter?.searchLocale ?? locale,
|
|
36
36
|
});
|
|
37
37
|
|
|
38
38
|
return result.docs.map(doc => mapLiveDocToProduct(doc));
|
|
@@ -51,6 +51,10 @@ export class ApiProductService implements IProductService {
|
|
|
51
51
|
return doc ? mapLiveDocToDetail(doc, locale) : null;
|
|
52
52
|
}
|
|
53
53
|
|
|
54
|
+
async getProductLanguages(id: string, languages: readonly string[]): Promise<string[]> {
|
|
55
|
+
return getLiveProductLanguages(id, languages);
|
|
56
|
+
}
|
|
57
|
+
|
|
54
58
|
/**
|
|
55
59
|
* Model siblings from the products app's REST rows — the search index has
|
|
56
60
|
* no parent link, so this is the one read that bypasses it.
|
|
@@ -14,6 +14,7 @@ import type {
|
|
|
14
14
|
ProductVariants,
|
|
15
15
|
} from "../../app/interfaces/product-variants";
|
|
16
16
|
import { pickLocalizedLabel, searchLocaleKeys } from "../../app/shared/locale";
|
|
17
|
+
import { languagesWithDocuments } from "../../app/shared/productLocales";
|
|
17
18
|
import type { Locale } from "./locale";
|
|
18
19
|
|
|
19
20
|
/**
|
|
@@ -73,6 +74,8 @@ export interface LiveSearchParams {
|
|
|
73
74
|
perPage?: number;
|
|
74
75
|
/** Facet values per field. Typesense returns only 10 when unset. */
|
|
75
76
|
maxFacetValues?: number;
|
|
77
|
+
/** Only these document fields in the hits (`include_fields`). */
|
|
78
|
+
includeFields?: string;
|
|
76
79
|
}
|
|
77
80
|
|
|
78
81
|
/** Where the platform's CDN is mounted, on every storefront domain. */
|
|
@@ -208,6 +211,7 @@ export async function searchLiveProductsMulti(
|
|
|
208
211
|
...(p.facetBy ? { facet_by: p.facetBy } : {}),
|
|
209
212
|
...(p.maxFacetValues ? { max_facet_values: p.maxFacetValues } : {}),
|
|
210
213
|
...(p.sortBy ? { sort_by: p.sortBy } : {}),
|
|
214
|
+
...(p.includeFields ? { include_fields: p.includeFields } : {}),
|
|
211
215
|
page: p.page ?? 1,
|
|
212
216
|
per_page: p.perPage ?? 24,
|
|
213
217
|
})),
|
|
@@ -393,6 +397,71 @@ export async function getLiveProductDoc(entityId: string, locale: Locale): Promi
|
|
|
393
397
|
return preferLocaleDocs(docs, locale)[0] ?? null;
|
|
394
398
|
}
|
|
395
399
|
|
|
400
|
+
/**
|
|
401
|
+
* `id:=[…]` over every document id one product may carry in any of these
|
|
402
|
+
* locales, each id once: `42` in `de`, `en` → `42:de-DE`, `42:de`, `42:en-US`,
|
|
403
|
+
* `42:en`.
|
|
404
|
+
*/
|
|
405
|
+
export function liveDocIdClauseForLocales(entityId: string, locales: readonly string[]): string {
|
|
406
|
+
const keys = [...new Set(locales.flatMap(locale => searchLocaleKeys(locale)))];
|
|
407
|
+
return `id:=[${keys.map(key => tsFilterValue(`${entityId}:${key}`)).join(",")}]`;
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
const PRODUCT_LANGUAGES_TTL_MS = 5 * 60 * 1000;
|
|
411
|
+
const PRODUCT_LANGUAGES_MAX_ENTRIES = 5000;
|
|
412
|
+
|
|
413
|
+
// Partitioned by tenant like the category cache. Translations change rarely
|
|
414
|
+
// and every product page asks (switcher and hreflang), so a short TTL keeps
|
|
415
|
+
// it to one search per product and tenant every few minutes.
|
|
416
|
+
const productLanguagesCache = new Map<string, { languages: string[]; loadedAt: number }>();
|
|
417
|
+
|
|
418
|
+
/** Clears the per-product language cache — tests only. */
|
|
419
|
+
export function resetProductLanguagesCache(): void {
|
|
420
|
+
productLanguagesCache.clear();
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
/**
|
|
424
|
+
* The storefront languages (of `languages`) this product has a page in.
|
|
425
|
+
*
|
|
426
|
+
* ONE request across every language: a lookup by document id over all their
|
|
427
|
+
* keys. It goes through multi_search, which does not pin a locale (the query
|
|
428
|
+
* endpoint does, via X-Revenexx-Locale, and so could only answer for one
|
|
429
|
+
* language per request). Only `id` and `locale` come back.
|
|
430
|
+
*/
|
|
431
|
+
export async function getLiveProductLanguages(entityId: string, languages: readonly string[]): Promise<string[]> {
|
|
432
|
+
if (!entityId || !languages.length) {
|
|
433
|
+
return [];
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
const cacheKey = `${revenexxTenantKey()}|${entityId}|${languages.join(",")}`;
|
|
437
|
+
const cached = productLanguagesCache.get(cacheKey);
|
|
438
|
+
if (cached && Date.now() - cached.loadedAt < PRODUCT_LANGUAGES_TTL_MS) {
|
|
439
|
+
return cached.languages;
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
const keyCount = new Set(languages.flatMap(language => searchLocaleKeys(language))).size;
|
|
443
|
+
const result = await searchLiveProducts({
|
|
444
|
+
q: "*",
|
|
445
|
+
queryBy: "name",
|
|
446
|
+
filterBy: liveDocIdClauseForLocales(entityId, languages),
|
|
447
|
+
includeFields: "id,locale",
|
|
448
|
+
perPage: keyCount,
|
|
449
|
+
});
|
|
450
|
+
const docLocales = (result.hits ?? []).map(({ document }) =>
|
|
451
|
+
document.locale || document.id.slice(document.id.lastIndexOf(":") + 1));
|
|
452
|
+
const available = languagesWithDocuments(docLocales, languages);
|
|
453
|
+
|
|
454
|
+
if (productLanguagesCache.size >= PRODUCT_LANGUAGES_MAX_ENTRIES) {
|
|
455
|
+
// Oldest first (insertion order) — a bound, not an LRU.
|
|
456
|
+
const oldest = productLanguagesCache.keys().next().value;
|
|
457
|
+
if (oldest !== undefined) {
|
|
458
|
+
productLanguagesCache.delete(oldest);
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
productLanguagesCache.set(cacheKey, { languages: available, loadedAt: Date.now() });
|
|
462
|
+
return available;
|
|
463
|
+
}
|
|
464
|
+
|
|
396
465
|
// ---------------------------------------------------------------------------
|
|
397
466
|
// Labels — every localized name the catalogue carries (categories, attributes,
|
|
398
467
|
// select options, association types) arrives as the same `labels` map, so it is
|
package/server/utils/locale.ts
CHANGED
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
import { getCookie, getHeader, type H3Event } from "h3";
|
|
2
2
|
|
|
3
|
+
import { pickMarketLocale } from "../../app/shared/locale";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Cookie the language choice persists the chosen MARKET locale in (`de-CH`,
|
|
7
|
+
* `fr-FR`). The URL prefix only says which UI strings render; this says
|
|
8
|
+
* which catalogue language the buyer asked for, which differs when the
|
|
9
|
+
* market offers a language the storefront has no UI strings for (PE-866).
|
|
10
|
+
*/
|
|
11
|
+
export const CONTENT_LOCALE_COOKIE_NAME = "cover-content-locale";
|
|
12
|
+
|
|
3
13
|
// Update when adding/removing locale subdirectories in i18n/locales/
|
|
4
14
|
const SUPPORTED = new Set(["de", "en"] as const);
|
|
5
15
|
export type Locale = "de" | "en";
|
|
@@ -34,10 +44,45 @@ export function resolveLocale(event: H3Event): Locale {
|
|
|
34
44
|
return fromAccept;
|
|
35
45
|
}
|
|
36
46
|
|
|
47
|
+
return defaultUiLanguage();
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function defaultUiLanguage(): Locale {
|
|
37
51
|
const configDefault = useRuntimeConfig().public.defaultLocale;
|
|
38
52
|
return (SUPPORTED.has(configDefault as Locale) ? configDefault : "de") as Locale;
|
|
39
53
|
}
|
|
40
54
|
|
|
55
|
+
/**
|
|
56
|
+
* The market locale this request reads the catalogue in — what search sends
|
|
57
|
+
* as `X-Revenexx-Locale` and what a contact is stored under.
|
|
58
|
+
*
|
|
59
|
+
* The market is the source of truth for which locales a store offers (its
|
|
60
|
+
* `preparing` locales excluded): the buyer's explicit choice when it fits the
|
|
61
|
+
* page language, else the market's locale for the page language. Without
|
|
62
|
+
* markets, or when they cannot be read, the page language's full code — what
|
|
63
|
+
* the storefront asked before markets carried locales.
|
|
64
|
+
*/
|
|
65
|
+
export async function resolveMarketLocale(event: H3Event): Promise<string> {
|
|
66
|
+
const language = resolveLocale(event);
|
|
67
|
+
let locales: { code: string; language?: string; isDefault?: boolean; status?: string }[] = [];
|
|
68
|
+
try {
|
|
69
|
+
const markets = await getMarketService(event).listMarkets();
|
|
70
|
+
const selected = resolveSelectedMarket(event);
|
|
71
|
+
const market = markets.find(m => m.code === selected) ?? markets.find(m => m.isDefault) ?? markets[0];
|
|
72
|
+
locales = market?.locales ?? [];
|
|
73
|
+
}
|
|
74
|
+
catch {
|
|
75
|
+
locales = [];
|
|
76
|
+
}
|
|
77
|
+
return pickMarketLocale({
|
|
78
|
+
locales,
|
|
79
|
+
chosen: getCookie(event, CONTENT_LOCALE_COOKIE_NAME),
|
|
80
|
+
uiLanguage: language,
|
|
81
|
+
uiLanguages: [...SUPPORTED],
|
|
82
|
+
defaultUiLanguage: defaultUiLanguage(),
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
|
|
41
86
|
/**
|
|
42
87
|
* The locale a CONTACT is stored under, which is not the locale the UI
|
|
43
88
|
* speaks. The storefront works in a language ("de"); the customers app keeps
|
|
@@ -46,6 +91,7 @@ export function resolveLocale(event: H3Event): Locale {
|
|
|
46
91
|
* language reads to the buyer as a registration that silently breaks.
|
|
47
92
|
*
|
|
48
93
|
* The market is the source of truth for which locales a store has. Falls back
|
|
94
|
+
* to the market's default locale when it has none for the page language, and
|
|
49
95
|
* to the bare language when markets cannot be read: a registration that
|
|
50
96
|
* reaches the app and may be refused beats one that never leaves.
|
|
51
97
|
*/
|
|
@@ -56,10 +102,18 @@ export async function resolveStoreLocale(event: H3Event): Promise<string> {
|
|
|
56
102
|
const selected = resolveSelectedMarket(event);
|
|
57
103
|
const market = markets.find(m => m.code === selected) ?? markets.find(m => m.isDefault) ?? markets[0];
|
|
58
104
|
const locales = market?.locales ?? [];
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
105
|
+
if (!locales.length) {
|
|
106
|
+
return language;
|
|
107
|
+
}
|
|
108
|
+
const picked = pickMarketLocale({
|
|
109
|
+
locales,
|
|
110
|
+
chosen: getCookie(event, CONTENT_LOCALE_COOKIE_NAME),
|
|
111
|
+
uiLanguage: language,
|
|
112
|
+
uiLanguages: [...SUPPORTED],
|
|
113
|
+
defaultUiLanguage: defaultUiLanguage(),
|
|
114
|
+
});
|
|
115
|
+
const known = locales.some(l => l.code === picked);
|
|
116
|
+
return known ? picked : (locales.find(l => l.isDefault) ?? locales[0])?.code ?? language;
|
|
63
117
|
}
|
|
64
118
|
catch {
|
|
65
119
|
return language;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { searchLocaleKeys } from "../../app/shared/locale";
|
|
1
|
+
import { localeLanguage, searchLocaleKeys } from "../../app/shared/locale";
|
|
2
2
|
import type { LiveProductDoc } from "./liveCatalog";
|
|
3
3
|
import { preferLocaleDocs } from "./liveCatalog";
|
|
4
4
|
import { searchViaMultiSearch } from "./searchMultiSearchFallback";
|
|
@@ -78,8 +78,18 @@ export interface SearchClientResult {
|
|
|
78
78
|
found: number;
|
|
79
79
|
page: number;
|
|
80
80
|
perPage: number;
|
|
81
|
-
/**
|
|
81
|
+
/**
|
|
82
|
+
* The locale whose index answered, "" when none. With one index per
|
|
83
|
+
* language (PE-865) the service answers a language it has no index for
|
|
84
|
+
* from the tenant's default language and says so here (it also sends
|
|
85
|
+
* `X-Revenexx-Locale-Served`, which the SDK call does not surface).
|
|
86
|
+
*/
|
|
82
87
|
locale: string;
|
|
88
|
+
/**
|
|
89
|
+
* True when another language answered than the one asked for — a page can
|
|
90
|
+
* say "shown in German" instead of passing the results off as French.
|
|
91
|
+
*/
|
|
92
|
+
localeFallback: boolean;
|
|
83
93
|
/** One document per product, the best-matching locale winning. */
|
|
84
94
|
docs: LiveProductDoc[];
|
|
85
95
|
facets: SearchFacetResult[];
|
|
@@ -194,7 +204,7 @@ function safeRedirectUrl(raw: unknown): string | null {
|
|
|
194
204
|
return /^https?:\/\//i.test(url) ? url : null;
|
|
195
205
|
}
|
|
196
206
|
|
|
197
|
-
function
|
|
207
|
+
export function normalizeSearchAction(raw: unknown): SearchAction | null {
|
|
198
208
|
if (!raw || typeof raw !== "object") {
|
|
199
209
|
return null;
|
|
200
210
|
}
|
|
@@ -212,6 +222,18 @@ function normalizeAction(raw: unknown): SearchAction | null {
|
|
|
212
222
|
return null;
|
|
213
223
|
}
|
|
214
224
|
|
|
225
|
+
/**
|
|
226
|
+
* Whether the served locale is another language than the requested one.
|
|
227
|
+
* A region answering for another region of the same language (`de` for
|
|
228
|
+
* `de-CH`) is not a fallback: the text is the buyer's language.
|
|
229
|
+
*/
|
|
230
|
+
export function isLocaleFallback(requested: string | undefined, served: string | undefined): boolean {
|
|
231
|
+
if (!requested || !served) {
|
|
232
|
+
return false;
|
|
233
|
+
}
|
|
234
|
+
return localeLanguage(requested) !== localeLanguage(served);
|
|
235
|
+
}
|
|
236
|
+
|
|
215
237
|
/** The query endpoint's answer in the shape every caller reads. */
|
|
216
238
|
export function mapSearchQueryResponse(
|
|
217
239
|
response: SearchQueryResponse,
|
|
@@ -230,6 +252,7 @@ export function mapSearchQueryResponse(
|
|
|
230
252
|
page: response.page ?? Math.max(1, params.page ?? 1),
|
|
231
253
|
perPage: response.per_page ?? params.perPage ?? hits.length,
|
|
232
254
|
locale: response.locale ?? "",
|
|
255
|
+
localeFallback: isLocaleFallback(params.locale, response.locale),
|
|
233
256
|
docs,
|
|
234
257
|
facets: (response.facets ?? []).map(facet => ({
|
|
235
258
|
field: facet.field,
|
|
@@ -237,7 +260,7 @@ export function mapSearchQueryResponse(
|
|
|
237
260
|
...(facet.stats ? { stats: facet.stats } : {}),
|
|
238
261
|
})),
|
|
239
262
|
actions: (response.actions ?? [])
|
|
240
|
-
.map(
|
|
263
|
+
.map(normalizeSearchAction)
|
|
241
264
|
.filter((action): action is SearchAction => action !== null),
|
|
242
265
|
ignoredFilters: response.ignored_filters ?? [],
|
|
243
266
|
ignoredSort: response.ignored_sort ?? null,
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The body POST /v1/search/events gets from the storefront's BFF (PE-867):
|
|
3
|
+
* the storefront's camelCase event, checked, in the search service's shape.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
const TYPES = new Set(["click", "add_to_cart", "purchase"]);
|
|
7
|
+
const COLLECTION_RE = /^[a-z][a-z0-9_-]{0,62}$/;
|
|
8
|
+
const DOC_ID_RE = /^[\w.:-]{1,128}$/;
|
|
9
|
+
const QUERY_ID_RE = /^[\w-]{1,40}$/;
|
|
10
|
+
|
|
11
|
+
export interface SearchEventInput {
|
|
12
|
+
type?: string;
|
|
13
|
+
queryId?: string;
|
|
14
|
+
collection?: string;
|
|
15
|
+
docId?: string;
|
|
16
|
+
position?: number;
|
|
17
|
+
q?: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** The body the search service gets, or null when the storefront sent nonsense. */
|
|
21
|
+
export function searchEventBody(input: SearchEventInput, userToken: string): Record<string, unknown> | null {
|
|
22
|
+
if (!input.type || !TYPES.has(input.type) || !input.collection || !COLLECTION_RE.test(input.collection)) {
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
if (input.queryId !== undefined && !QUERY_ID_RE.test(input.queryId)) {
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
if (input.docId !== undefined && !DOC_ID_RE.test(input.docId)) {
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
if (input.type === "click" && !input.queryId) {
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
const position = typeof input.position === "number" && Number.isInteger(input.position) && input.position >= 1 && input.position <= 1000
|
|
35
|
+
? input.position
|
|
36
|
+
: undefined;
|
|
37
|
+
return {
|
|
38
|
+
type: input.type,
|
|
39
|
+
collection: input.collection,
|
|
40
|
+
...(input.queryId ? { query_id: input.queryId } : {}),
|
|
41
|
+
...(input.docId ? { doc_id: input.docId } : {}),
|
|
42
|
+
...(position ? { position } : {}),
|
|
43
|
+
...(typeof input.q === "string" && input.q.trim() ? { q: input.q.trim().slice(0, 200) } : {}),
|
|
44
|
+
user_token: userToken,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
@@ -203,6 +203,8 @@ export async function searchViaMultiSearch(params: SearchClientParams): Promise<
|
|
|
203
203
|
page: response.page ?? page,
|
|
204
204
|
perPage,
|
|
205
205
|
locale,
|
|
206
|
+
// multi_search filters the mixed index by locale, it never substitutes one.
|
|
207
|
+
localeFallback: false,
|
|
206
208
|
docs,
|
|
207
209
|
facets: (response.facet_counts ?? []).map(fc => ({
|
|
208
210
|
field: fc.field_name,
|