@shopbite-de/storefront 2.1.0 → 2.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/.env.example +3 -1
- package/.github/workflows/build.yaml +1 -1
- package/.github/workflows/ci.yaml +5 -5
- package/CLAUDE.md +6 -3
- package/api-types/storeApiSchema.json +5 -0
- package/api-types/storeApiTypes.d.ts +2 -0
- package/app/components/Category/Listing.vue +53 -0
- package/app/components/Menu/BonCard.vue +13 -0
- package/app/components/Menu/FoodLabelLegend.vue +28 -0
- package/app/components/Navigation/MobileTop.vue +98 -9
- package/app/components/Product/FoodLabels.vue +39 -0
- package/app/components/Product/QuickView.vue +1 -0
- package/app/components/Sb/Sheet.vue +36 -18
- package/app/composables/useHomeMenuCategories.ts +6 -4
- package/app/composables/useMenuRootId.ts +27 -0
- package/app/composables/useNavigation.ts +2 -5
- package/app/composables/useProductQuickView.ts +4 -1
- package/app/composables/useTopSellers.ts +4 -1
- package/app/utils/menuGroups.ts +57 -0
- package/app/utils/menuRoot.ts +15 -0
- package/app/utils/product.ts +132 -0
- package/nuxt.config.ts +3 -0
- package/package.json +1 -1
- package/server/api/__sitemap__/urls.get.ts +1 -4
- package/server/api/_cache/purge.post.ts +31 -0
- package/server/api/listing/[categoryId].get.ts +13 -1
- package/server/api/product/[productId].get.ts +10 -1
- package/server/api/search.get.ts +11 -1
- package/server/routes/llms.txt.ts +3 -3
- package/server/utils/cachePurge.ts +62 -0
- package/server/utils/menuRoot.ts +25 -0
- package/test/nuxt/NavigationMobileTop.test.ts +90 -0
- package/test/nuxt/SbSheetHeader.test.ts +44 -0
- package/test/unit/cachePurge.spec.ts +65 -0
- package/test/unit/menuGroups.spec.ts +56 -0
- package/test/unit/menuRoot.spec.ts +21 -0
- package/test/unit/product.spec.ts +78 -0
- package/.gitignore +0 -45
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import type { Schemas } from "#shopware";
|
|
2
|
+
|
|
3
|
+
/** A sub-section of a menu section with its dishes (#487). */
|
|
4
|
+
export interface MenuGroup {
|
|
5
|
+
id: string;
|
|
6
|
+
name: string;
|
|
7
|
+
products: Schemas["Product"][];
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/** The category with this id anywhere in a navigation tree. */
|
|
11
|
+
export function findCategory(
|
|
12
|
+
tree: Schemas["Category"][] | undefined,
|
|
13
|
+
id: string,
|
|
14
|
+
): Schemas["Category"] | undefined {
|
|
15
|
+
for (const category of tree ?? []) {
|
|
16
|
+
if (category.id === id) return category;
|
|
17
|
+
const found = findCategory(category.children, id);
|
|
18
|
+
if (found) return found;
|
|
19
|
+
}
|
|
20
|
+
return undefined;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function idsBelow(category: Schemas["Category"]): string[] {
|
|
24
|
+
return [category.id, ...(category.children ?? []).flatMap(idsBelow)];
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Splits the dishes of a section into the ones directly in it and one group
|
|
29
|
+
* per sub-section, in the order of the navigation (= backoffice order).
|
|
30
|
+
* Deeper levels count to their sub-section; empty groups are left out, so a
|
|
31
|
+
* filter or search shows only what matches. Without sub-sections: null.
|
|
32
|
+
*/
|
|
33
|
+
export function groupBySubsections(
|
|
34
|
+
products: Schemas["Product"][],
|
|
35
|
+
subsections: Schemas["Category"][] | undefined,
|
|
36
|
+
): { direct: Schemas["Product"][]; groups: MenuGroup[] } | null {
|
|
37
|
+
if (!subsections?.length) return null;
|
|
38
|
+
const buckets = subsections.map((category) => ({
|
|
39
|
+
group: {
|
|
40
|
+
id: category.id,
|
|
41
|
+
name: category.translated?.name ?? category.name ?? "",
|
|
42
|
+
products: [] as Schemas["Product"][],
|
|
43
|
+
},
|
|
44
|
+
ids: new Set(idsBelow(category)),
|
|
45
|
+
}));
|
|
46
|
+
const direct: Schemas["Product"][] = [];
|
|
47
|
+
for (const product of products) {
|
|
48
|
+
const bucket = buckets.find((b) =>
|
|
49
|
+
(product.categoryIds ?? []).some((id) => b.ids.has(id)),
|
|
50
|
+
);
|
|
51
|
+
(bucket?.group.products ?? direct).push(product);
|
|
52
|
+
}
|
|
53
|
+
return {
|
|
54
|
+
direct,
|
|
55
|
+
groups: buckets.map((b) => b.group).filter((g) => g.products.length),
|
|
56
|
+
};
|
|
57
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The root category of the menu (#486): NUXT_PUBLIC_SHOP_BITE_MENU_CATEGORY_ID
|
|
3
|
+
* if set (existing installations), else `menuCategoryId` of the plugin
|
|
4
|
+
* configuration (`/store-api/shopbite/config`, shopware-plugin#35, which
|
|
5
|
+
* falls back to the sales channel's navigation category), else the
|
|
6
|
+
* navigation root.
|
|
7
|
+
*/
|
|
8
|
+
export const NAVIGATION_ROOT = "main-navigation";
|
|
9
|
+
|
|
10
|
+
export function pickMenuRoot(
|
|
11
|
+
configured: string | undefined,
|
|
12
|
+
fromPlugin: string | undefined | null,
|
|
13
|
+
): string {
|
|
14
|
+
return configured || fromPlugin || NAVIGATION_ROOT;
|
|
15
|
+
}
|
package/app/utils/product.ts
CHANGED
|
@@ -66,3 +66,135 @@ export function getDiets(
|
|
|
66
66
|
hasYesOption(sortedProperties, DIET_GROUPS[diet]),
|
|
67
67
|
);
|
|
68
68
|
}
|
|
69
|
+
|
|
70
|
+
// Mandatory food information (#485): property groups and custom fields of the
|
|
71
|
+
// ShopBite plugin (shopware-plugin#36), maintained in the backoffice.
|
|
72
|
+
const ALLERGEN_GROUP = "Allergene";
|
|
73
|
+
const ADDITIVE_GROUP = "Zusatzstoffe";
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Names of the allergen codes for the legend. The options name the cereal
|
|
77
|
+
* or nut (LMIV Annex II), the legend names the allergen.
|
|
78
|
+
*/
|
|
79
|
+
export const ALLERGEN_CODE_NAMES: Record<string, string> = {
|
|
80
|
+
A: "Glutenhaltiges Getreide",
|
|
81
|
+
B: "Krebstiere",
|
|
82
|
+
C: "Eier",
|
|
83
|
+
D: "Fisch",
|
|
84
|
+
E: "Erdnüsse",
|
|
85
|
+
F: "Soja",
|
|
86
|
+
G: "Milch",
|
|
87
|
+
H: "Schalenfrüchte",
|
|
88
|
+
L: "Sellerie",
|
|
89
|
+
M: "Senf",
|
|
90
|
+
N: "Sesam",
|
|
91
|
+
O: "Schwefeldioxid und Sulfite",
|
|
92
|
+
P: "Lupinen",
|
|
93
|
+
R: "Weichtiere",
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
export interface FoodLabel {
|
|
97
|
+
code: string;
|
|
98
|
+
name: string;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export interface FoodLabels {
|
|
102
|
+
allergens: FoodLabel[];
|
|
103
|
+
additives: FoodLabel[];
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function customFieldsOf(entity: {
|
|
107
|
+
customFields?: unknown;
|
|
108
|
+
translated?: unknown;
|
|
109
|
+
}): Record<string, unknown> {
|
|
110
|
+
const translated = (entity.translated as { customFields?: unknown })
|
|
111
|
+
?.customFields;
|
|
112
|
+
return ((translated ?? entity.customFields) as Record<string, unknown>) ?? {};
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function labelsOf(
|
|
116
|
+
sortedProperties: Schemas["PropertyGroup"][] | undefined,
|
|
117
|
+
groupName: string,
|
|
118
|
+
): FoodLabel[] {
|
|
119
|
+
const group = sortedProperties?.find(
|
|
120
|
+
(propertyGroup) => propertyGroup.translated.name === groupName,
|
|
121
|
+
);
|
|
122
|
+
return (group?.options ?? []).map((option) => ({
|
|
123
|
+
code: String(customFieldsOf(option).shopbite_code ?? ""),
|
|
124
|
+
name: option.translated?.name ?? option.name ?? "",
|
|
125
|
+
}));
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** Allergens and additives of a product, in the order of the catalogue. */
|
|
129
|
+
export function getFoodLabels(
|
|
130
|
+
sortedProperties: Schemas["PropertyGroup"][] | undefined,
|
|
131
|
+
): FoodLabels {
|
|
132
|
+
return {
|
|
133
|
+
allergens: labelsOf(sortedProperties, ALLERGEN_GROUP),
|
|
134
|
+
additives: labelsOf(sortedProperties, ADDITIVE_GROUP),
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Whether the restaurant confirmed the lists as complete. Without it an
|
|
140
|
+
* empty list means "not entered yet", never "free of allergens".
|
|
141
|
+
*/
|
|
142
|
+
export function allergensChecked(product: Schemas["Product"]): boolean {
|
|
143
|
+
return customFieldsOf(product).shopbite_allergens_checked === true;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** "A, G, 2": every code once, allergens first. */
|
|
147
|
+
export function foodLabelCodes(labels: FoodLabels): string[] {
|
|
148
|
+
return [
|
|
149
|
+
...new Set(
|
|
150
|
+
[...labels.allergens, ...labels.additives]
|
|
151
|
+
.map((label) => label.code)
|
|
152
|
+
.filter(Boolean),
|
|
153
|
+
),
|
|
154
|
+
];
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** Plain text for the product sheet and screen readers. */
|
|
158
|
+
export function foodLabelText(labels: FoodLabels): string {
|
|
159
|
+
const parts: string[] = [];
|
|
160
|
+
if (labels.allergens.length) {
|
|
161
|
+
parts.push(
|
|
162
|
+
`Enthält: ${labels.allergens.map((label) => label.name).join(", ")}.`,
|
|
163
|
+
);
|
|
164
|
+
}
|
|
165
|
+
if (labels.additives.length) {
|
|
166
|
+
parts.push(
|
|
167
|
+
`Zusatzstoffe: ${labels.additives.map((label) => label.name).join(", ")}.`,
|
|
168
|
+
);
|
|
169
|
+
}
|
|
170
|
+
return parts.join(" ");
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Legend of the codes used in a list of products: allergens by their
|
|
175
|
+
* allergen name, additives by their option name, sorted like a menu.
|
|
176
|
+
*/
|
|
177
|
+
export function foodLabelLegend(products: Schemas["Product"][]): FoodLabel[] {
|
|
178
|
+
const legend = new Map<string, string>();
|
|
179
|
+
for (const product of products) {
|
|
180
|
+
const labels = getFoodLabels(
|
|
181
|
+
product.sortedProperties as Schemas["PropertyGroup"][] | undefined,
|
|
182
|
+
);
|
|
183
|
+
for (const label of labels.allergens) {
|
|
184
|
+
if (label.code)
|
|
185
|
+
legend.set(label.code, ALLERGEN_CODE_NAMES[label.code] ?? label.name);
|
|
186
|
+
}
|
|
187
|
+
for (const label of labels.additives) {
|
|
188
|
+
if (label.code) legend.set(label.code, label.name);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
return [...legend]
|
|
192
|
+
.map(([code, name]) => ({ code, name }))
|
|
193
|
+
.sort((a, b) => {
|
|
194
|
+
const numeric = Number(a.code) - Number(b.code);
|
|
195
|
+
if (!Number.isNaN(numeric)) return numeric;
|
|
196
|
+
if (Number.isNaN(Number(a.code)) !== Number.isNaN(Number(b.code)))
|
|
197
|
+
return Number.isNaN(Number(a.code)) ? -1 : 1;
|
|
198
|
+
return a.code.localeCompare(b.code);
|
|
199
|
+
});
|
|
200
|
+
}
|
package/nuxt.config.ts
CHANGED
|
@@ -56,6 +56,9 @@ export default defineNuxtConfig({
|
|
|
56
56
|
adminEndpoint: "",
|
|
57
57
|
},
|
|
58
58
|
apiClientConfig: {},
|
|
59
|
+
// Secret of POST /api/_cache/purge (#484), the backoffice empties the
|
|
60
|
+
// Store API caches after menu changes. Empty = route off.
|
|
61
|
+
cachePurgeSecret: "",
|
|
59
62
|
geoapifyApiKey: "",
|
|
60
63
|
// AI crawlers named in robots.txt (#403, server/plugins/ai-crawlers.ts):
|
|
61
64
|
// search = AI search and assistant fetches, training = model training.
|
package/package.json
CHANGED
|
@@ -21,10 +21,7 @@ const NAVIGATION_CRITERIA = {
|
|
|
21
21
|
*/
|
|
22
22
|
export default defineSitemapEventHandler(async () => {
|
|
23
23
|
const { shopware, shopBite } = useRuntimeConfig().public;
|
|
24
|
-
const rootIds = new Set([
|
|
25
|
-
"main-navigation",
|
|
26
|
-
shopBite.menuCategoryId || "main-navigation",
|
|
27
|
-
]);
|
|
24
|
+
const rootIds = new Set(["main-navigation", await menuRootId()]);
|
|
28
25
|
|
|
29
26
|
const trees = await Promise.all(
|
|
30
27
|
[...rootIds].map((rootId) =>
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
|
|
3
|
+
const Id = z.string().regex(/^[0-9a-f]{32}$/i);
|
|
4
|
+
const Body = z
|
|
5
|
+
.object({
|
|
6
|
+
products: z.array(Id).max(1000).optional(),
|
|
7
|
+
categories: z.array(Id).max(1000).optional(),
|
|
8
|
+
})
|
|
9
|
+
.default({});
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Empties the Store API caches after the backoffice changed the menu (#484).
|
|
13
|
+
* Protected by NUXT_CACHE_PURGE_SECRET; without the variable the route is
|
|
14
|
+
* off. An empty body purges everything.
|
|
15
|
+
*/
|
|
16
|
+
export default defineEventHandler(async (event) => {
|
|
17
|
+
const secret = useRuntimeConfig(event).cachePurgeSecret;
|
|
18
|
+
if (!secret) throw createError({ statusCode: 404 });
|
|
19
|
+
if (!hasPurgeSecret(getHeader(event, "authorization"), secret)) {
|
|
20
|
+
throw createError({ statusCode: 401, statusMessage: "Unauthorized" });
|
|
21
|
+
}
|
|
22
|
+
const raw = await readBody(event).catch(() => undefined);
|
|
23
|
+
const parsed = Body.safeParse(raw ?? {});
|
|
24
|
+
if (!parsed.success)
|
|
25
|
+
throw createError({ statusCode: 400, statusMessage: "Invalid body" });
|
|
26
|
+
|
|
27
|
+
const storage = useStorage();
|
|
28
|
+
const keys = keysToPurge(await storage.getKeys("cache:nitro"), parsed.data);
|
|
29
|
+
await Promise.all(keys.map((key) => storage.removeItem(key)));
|
|
30
|
+
return { purged: keys.length };
|
|
31
|
+
});
|
|
@@ -15,7 +15,12 @@ const BASE_CRITERIA = {
|
|
|
15
15
|
// `properties` would repeat every option with its group. The quick view
|
|
16
16
|
// loads its own product (/api/product/[productId]).
|
|
17
17
|
"sortedProperties",
|
|
18
|
+
// Allergens checked (#485); includes filter `translated` as well,
|
|
19
|
+
// so custom fields only come back when listed here.
|
|
20
|
+
"customFields",
|
|
18
21
|
"cover",
|
|
22
|
+
// Groups the dishes of a section by sub-section (#487).
|
|
23
|
+
"categoryIds",
|
|
19
24
|
// "Ausverkauft" on the card (#325).
|
|
20
25
|
"available",
|
|
21
26
|
// Product deep link (#289); the request sends `sw-include-seo-urls`.
|
|
@@ -28,7 +33,14 @@ const BASE_CRITERIA = {
|
|
|
28
33
|
// The entity is `property_group`: under `property` the groups came back
|
|
29
34
|
// with every field, nested once more in each option.
|
|
30
35
|
property_group: ["id", "name", "translated", "displayType", "options"],
|
|
31
|
-
property_group_option: [
|
|
36
|
+
property_group_option: [
|
|
37
|
+
"id",
|
|
38
|
+
"name",
|
|
39
|
+
"translated",
|
|
40
|
+
"customFields",
|
|
41
|
+
"media",
|
|
42
|
+
"mediaId",
|
|
43
|
+
],
|
|
32
44
|
product_option: ["id", "groupId", "name", "translated", "group"],
|
|
33
45
|
...MEDIA_INCLUDES,
|
|
34
46
|
},
|
|
@@ -20,13 +20,22 @@ const criteria = {
|
|
|
20
20
|
"children",
|
|
21
21
|
"parentId",
|
|
22
22
|
"sortedProperties",
|
|
23
|
+
// Allergens checked (#485); includes filter `translated` as well,
|
|
24
|
+
// so custom fields only come back when listed here.
|
|
25
|
+
"customFields",
|
|
23
26
|
"cover",
|
|
24
27
|
],
|
|
25
28
|
// The entity is `property_group`: under `property` the groups came back
|
|
26
29
|
// with every field. Options keep their group for the configurator and
|
|
27
30
|
// the ingredient selection (`option.group.name`).
|
|
28
31
|
property_group: ["id", "name", "translated", "options"],
|
|
29
|
-
property_group_option: [
|
|
32
|
+
property_group_option: [
|
|
33
|
+
"id",
|
|
34
|
+
"name",
|
|
35
|
+
"translated",
|
|
36
|
+
"customFields",
|
|
37
|
+
"group",
|
|
38
|
+
],
|
|
30
39
|
product_configurator_setting: ["id", "optionId", "option", "productId"],
|
|
31
40
|
category: ["id", "name", "translated"],
|
|
32
41
|
...MEDIA_INCLUDES,
|
package/server/api/search.get.ts
CHANGED
|
@@ -15,6 +15,9 @@ const BASE_CRITERIA = {
|
|
|
15
15
|
// `properties` would repeat every option with its group. The quick view
|
|
16
16
|
// loads its own product (/api/product/[productId]).
|
|
17
17
|
"sortedProperties",
|
|
18
|
+
// Allergens checked (#485); includes filter `translated` as well,
|
|
19
|
+
// so custom fields only come back when listed here.
|
|
20
|
+
"customFields",
|
|
18
21
|
"cover",
|
|
19
22
|
// "Ausverkauft" on the card (#325).
|
|
20
23
|
"available",
|
|
@@ -22,7 +25,14 @@ const BASE_CRITERIA = {
|
|
|
22
25
|
// The entity is `property_group`: under `property` the groups came back
|
|
23
26
|
// with every field, nested once more in each option.
|
|
24
27
|
property_group: ["id", "name", "translated", "displayType", "options"],
|
|
25
|
-
property_group_option: [
|
|
28
|
+
property_group_option: [
|
|
29
|
+
"id",
|
|
30
|
+
"name",
|
|
31
|
+
"translated",
|
|
32
|
+
"customFields",
|
|
33
|
+
"media",
|
|
34
|
+
"mediaId",
|
|
35
|
+
],
|
|
26
36
|
},
|
|
27
37
|
associations: {
|
|
28
38
|
cover: {
|
|
@@ -52,10 +52,10 @@ const PRODUCT_CRITERIA = {
|
|
|
52
52
|
* its section out instead of failing the file.
|
|
53
53
|
*/
|
|
54
54
|
export default defineEventHandler(async (event) => {
|
|
55
|
-
const { shopware,
|
|
55
|
+
const { shopware, site } = useRuntimeConfig(event).public;
|
|
56
56
|
const siteUrl = getSiteConfig(event).url;
|
|
57
57
|
const headers = { "sw-access-key": shopware.accessToken };
|
|
58
|
-
const
|
|
58
|
+
const menuRoot = await menuRootId();
|
|
59
59
|
|
|
60
60
|
const storeApi = <T>(path: string, body?: Record<string, unknown>) =>
|
|
61
61
|
$fetch<T>(`${shopware.endpoint}${path}`, {
|
|
@@ -67,7 +67,7 @@ export default defineEventHandler(async (event) => {
|
|
|
67
67
|
const [navigation, products, hours, holidays, config, context, pages, home] =
|
|
68
68
|
await Promise.allSettled([
|
|
69
69
|
storeApi<Schemas["Category"][]>(
|
|
70
|
-
`/navigation/${
|
|
70
|
+
`/navigation/${menuRoot}/${menuRoot}`,
|
|
71
71
|
NAVIGATION_CRITERIA,
|
|
72
72
|
),
|
|
73
73
|
loadProducts((page) =>
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { timingSafeEqual } from "node:crypto";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Cache purge for the backoffice (#484): the cached Store API handlers keep
|
|
5
|
+
* products, listings and categories for a day (`cacheTtl`). Nitro stores a
|
|
6
|
+
* handler response under `cache:nitro:handlers:<name>:<escaped key>.json`;
|
|
7
|
+
* the escaped key keeps the hex ids, so an id finds its entries.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/** Entries of one product: the product, its variants and its extras. */
|
|
11
|
+
const PER_PRODUCT = ["product", "variant", "cross-selling"];
|
|
12
|
+
/** Lists that may show any product or category. */
|
|
13
|
+
const LISTS = ["listing", "search"];
|
|
14
|
+
const ALL = [...PER_PRODUCT, "category", ...LISTS];
|
|
15
|
+
const HANDLERS = "cache:nitro:handlers:";
|
|
16
|
+
/** Route rules with `swr` (llms.txt lists the menu). */
|
|
17
|
+
const ROUTES = "cache:nitro:routes:";
|
|
18
|
+
|
|
19
|
+
export interface PurgeTarget {
|
|
20
|
+
products?: string[];
|
|
21
|
+
categories?: string[];
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const handlerOf = (key: string) =>
|
|
25
|
+
key.slice(HANDLERS.length).split(":")[0] ?? "";
|
|
26
|
+
const compact = (id: string) => id.toLowerCase().replace(/[^0-9a-f]/g, "");
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* The cache keys to remove: without a target everything; with a target the
|
|
30
|
+
* entries of the named products and categories plus every list. An extras
|
|
31
|
+
* group change reaches many dishes, so cross-sellings always go completely.
|
|
32
|
+
*/
|
|
33
|
+
export function keysToPurge(
|
|
34
|
+
keys: string[],
|
|
35
|
+
target: PurgeTarget = {},
|
|
36
|
+
): string[] {
|
|
37
|
+
const products = (target.products ?? []).map(compact).filter(Boolean);
|
|
38
|
+
const categories = (target.categories ?? []).map(compact).filter(Boolean);
|
|
39
|
+
const everything = !products.length && !categories.length;
|
|
40
|
+
return keys.filter((key) => {
|
|
41
|
+
if (key.startsWith(ROUTES)) return true;
|
|
42
|
+
if (!key.startsWith(HANDLERS)) return false;
|
|
43
|
+
const handler = handlerOf(key);
|
|
44
|
+
if (!ALL.includes(handler)) return false;
|
|
45
|
+
if (everything || LISTS.includes(handler) || handler === "cross-selling")
|
|
46
|
+
return true;
|
|
47
|
+
if (handler === "category")
|
|
48
|
+
return categories.some((id) => key.includes(id));
|
|
49
|
+
return products.some((id) => key.includes(id));
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Constant-time check of the `Authorization: Bearer <secret>` header. */
|
|
54
|
+
export function hasPurgeSecret(
|
|
55
|
+
header: string | undefined,
|
|
56
|
+
secret: string,
|
|
57
|
+
): boolean {
|
|
58
|
+
if (!secret || !header?.startsWith("Bearer ")) return false;
|
|
59
|
+
const given = Buffer.from(header.slice("Bearer ".length));
|
|
60
|
+
const wanted = Buffer.from(secret);
|
|
61
|
+
return given.length === wanted.length && timingSafeEqual(given, wanted);
|
|
62
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { pickMenuRoot } from "../../app/utils/menuRoot";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Server side of the menu root (#486, see app/utils/menuRoot.ts) for the
|
|
5
|
+
* sitemap and llms.txt. The plugin value is kept for five minutes.
|
|
6
|
+
*/
|
|
7
|
+
const pluginMenuRoot = defineCachedFunction(
|
|
8
|
+
async (): Promise<string | null> => {
|
|
9
|
+
const { shopware } = useRuntimeConfig().public;
|
|
10
|
+
const config = await $fetch<{ menuCategoryId?: string }>(
|
|
11
|
+
`${shopware.endpoint}/shopbite/config`,
|
|
12
|
+
{ headers: { "sw-access-key": shopware.accessToken } },
|
|
13
|
+
).catch(() => null);
|
|
14
|
+
return config?.menuCategoryId ?? null;
|
|
15
|
+
},
|
|
16
|
+
{ maxAge: 300, name: "menu-root", getKey: () => "menu-root" },
|
|
17
|
+
);
|
|
18
|
+
|
|
19
|
+
export async function menuRootId(): Promise<string> {
|
|
20
|
+
const configured = useRuntimeConfig().public.shopBite.menuCategoryId as
|
|
21
|
+
string | undefined;
|
|
22
|
+
return configured
|
|
23
|
+
? configured
|
|
24
|
+
: pickMenuRoot(undefined, await pluginMenuRoot());
|
|
25
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { describe, it, expect, vi } from "vitest";
|
|
2
|
+
import { mountSuspended } from "@nuxt/test-utils/runtime";
|
|
3
|
+
import type { VueWrapper } from "@vue/test-utils";
|
|
4
|
+
import { ref } from "vue";
|
|
5
|
+
import MobileTop from "~/components/Navigation/MobileTop.vue";
|
|
6
|
+
|
|
7
|
+
// La Fattoria's menu: Fleischgerichte has sub-categories, Pizza has none.
|
|
8
|
+
vi.mock("~/composables/useNavigation", () => ({
|
|
9
|
+
useNavigation: () => ({
|
|
10
|
+
menuCardNavigation: ref([
|
|
11
|
+
{ id: "pizza", name: "Pizza", seoUrl: "/speisekarte/pizza/" },
|
|
12
|
+
{
|
|
13
|
+
id: "fleisch",
|
|
14
|
+
name: "Fleischgerichte",
|
|
15
|
+
seoUrl: "/speisekarte/fleischgerichte/",
|
|
16
|
+
children: [
|
|
17
|
+
{
|
|
18
|
+
id: "rind",
|
|
19
|
+
name: "Vom Rind",
|
|
20
|
+
seoUrl: "/speisekarte/fleischgerichte/vom-rind/",
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
id: "kalb",
|
|
24
|
+
name: "Vom Kalb",
|
|
25
|
+
seoUrl: "/speisekarte/fleischgerichte/vom-kalb/",
|
|
26
|
+
},
|
|
27
|
+
],
|
|
28
|
+
},
|
|
29
|
+
]),
|
|
30
|
+
}),
|
|
31
|
+
}));
|
|
32
|
+
|
|
33
|
+
type Row = {
|
|
34
|
+
label: string | undefined;
|
|
35
|
+
links: { text: string; current: string | undefined }[];
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
function chips(wrapper: VueWrapper): Row[] {
|
|
39
|
+
return wrapper.findAll("ul").map((list) => ({
|
|
40
|
+
label: list.attributes("aria-label"),
|
|
41
|
+
links: list.findAll("a").map((link) => ({
|
|
42
|
+
text: link.text(),
|
|
43
|
+
current: link.attributes("aria-current"),
|
|
44
|
+
})),
|
|
45
|
+
}));
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
describe("NavigationMobileTop", () => {
|
|
49
|
+
it("shows only the sections on a section without sub-categories", async () => {
|
|
50
|
+
const wrapper = await mountSuspended(MobileTop, {
|
|
51
|
+
route: "/speisekarte/pizza/",
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
const rows = chips(wrapper);
|
|
55
|
+
expect(rows).toHaveLength(1);
|
|
56
|
+
expect(rows[0]!.links).toEqual([
|
|
57
|
+
{ text: "Pizza", current: "page" },
|
|
58
|
+
{ text: "Fleischgerichte", current: undefined },
|
|
59
|
+
]);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it("lists the sub-categories of the open section in a second row", async () => {
|
|
63
|
+
const wrapper = await mountSuspended(MobileTop, {
|
|
64
|
+
route: "/speisekarte/fleischgerichte/",
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
const rows = chips(wrapper);
|
|
68
|
+
expect(rows[1]!.label).toBe("Unterkategorien von Fleischgerichte");
|
|
69
|
+
expect(rows[1]!.links).toEqual([
|
|
70
|
+
{ text: "Alle", current: "page" },
|
|
71
|
+
{ text: "Vom Rind", current: undefined },
|
|
72
|
+
{ text: "Vom Kalb", current: undefined },
|
|
73
|
+
]);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it("keeps the section and its row open on a sub-category", async () => {
|
|
77
|
+
const wrapper = await mountSuspended(MobileTop, {
|
|
78
|
+
route: "/speisekarte/fleischgerichte/vom-kalb/",
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
const rows = chips(wrapper);
|
|
82
|
+
const section = wrapper.find("[data-section]");
|
|
83
|
+
expect(section.text()).toBe("Fleischgerichte");
|
|
84
|
+
expect(section.classes()).toContain("bg-sb-ink");
|
|
85
|
+
expect(rows[1]!.links.find((link) => link.current)).toEqual({
|
|
86
|
+
text: "Vom Kalb",
|
|
87
|
+
current: "page",
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
});
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { mountSuspended } from "@nuxt/test-utils/runtime";
|
|
3
|
+
import { defineComponent, h } from "vue";
|
|
4
|
+
import { DialogTitle } from "reka-ui";
|
|
5
|
+
import SbSheet from "~/components/Sb/Sheet.vue";
|
|
6
|
+
|
|
7
|
+
// A custom header (product sheet) gets its own row below the actions, so
|
|
8
|
+
// a long dish name is not squeezed next to three 44 px buttons.
|
|
9
|
+
describe("SbSheet header", () => {
|
|
10
|
+
it("puts the actions and close into a toolbar above a custom header", async () => {
|
|
11
|
+
const Host = defineComponent(
|
|
12
|
+
() => () =>
|
|
13
|
+
h(
|
|
14
|
+
SbSheet,
|
|
15
|
+
{ open: true, title: "Hähnchenbrustfilet Paniert m. Kroketten" },
|
|
16
|
+
{
|
|
17
|
+
header: () =>
|
|
18
|
+
h(DialogTitle, { "data-test": "title" }, () => "Titel"),
|
|
19
|
+
actions: () => h("button", { "data-test": "save" }, "Merken"),
|
|
20
|
+
default: () => h("p", "Inhalt"),
|
|
21
|
+
},
|
|
22
|
+
),
|
|
23
|
+
);
|
|
24
|
+
await mountSuspended(Host);
|
|
25
|
+
|
|
26
|
+
const header = document.body.querySelector("header")!;
|
|
27
|
+
const [toolbar, content] = [...header.children];
|
|
28
|
+
expect(toolbar!.querySelector('[data-test="save"]')).not.toBeNull();
|
|
29
|
+
expect(toolbar!.querySelector('[aria-label="Schließen"]')).not.toBeNull();
|
|
30
|
+
expect(content!.querySelector('[data-test="title"]')).not.toBeNull();
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it("keeps title and close in one row without a custom header", async () => {
|
|
34
|
+
const Host = defineComponent(
|
|
35
|
+
() => () =>
|
|
36
|
+
h(SbSheet, { open: true, title: "Warenkorb" }, () => h("p", "Inhalt")),
|
|
37
|
+
);
|
|
38
|
+
await mountSuspended(Host);
|
|
39
|
+
|
|
40
|
+
const header = [...document.body.querySelectorAll("header")].at(-1)!;
|
|
41
|
+
expect(header.className).toContain("items-start");
|
|
42
|
+
expect(header.textContent).toContain("Warenkorb");
|
|
43
|
+
});
|
|
44
|
+
});
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { hasPurgeSecret, keysToPurge } from "../../server/utils/cachePurge";
|
|
3
|
+
|
|
4
|
+
const PIZZA = "018dadb399837af3aa669c8fff4f4d6c";
|
|
5
|
+
const MARGHERITA = "0197baf0a6a971eaada59ed5c84138de";
|
|
6
|
+
const SCHNITZEL = "018dbccef8257618b0b3a541f513aa9e";
|
|
7
|
+
// keys as Nitro stores them (escaped custom key, see server/utils/cachePurge.ts)
|
|
8
|
+
const KEYS = [
|
|
9
|
+
`cache:nitro:handlers:product:product${MARGHERITA}.json`,
|
|
10
|
+
`cache:nitro:handlers:product:product${SCHNITZEL}.json`,
|
|
11
|
+
`cache:nitro:handlers:variant:variant${MARGHERITA}a1b2.json`,
|
|
12
|
+
`cache:nitro:handlers:cross-selling:crossselling${SCHNITZEL}.json`,
|
|
13
|
+
`cache:nitro:handlers:category:category${PIZZA}.json`,
|
|
14
|
+
`cache:nitro:handlers:listing:${PIZZA}1.json`,
|
|
15
|
+
`cache:nitro:handlers:search:pizza1.json`,
|
|
16
|
+
`cache:nitro:handlers:sitemap:urls.json`,
|
|
17
|
+
`cache:nitro:routes:_:llmstxt.json`,
|
|
18
|
+
];
|
|
19
|
+
|
|
20
|
+
describe("keysToPurge", () => {
|
|
21
|
+
it("purges every Store API cache without a target", () => {
|
|
22
|
+
expect(keysToPurge(KEYS)).toEqual(
|
|
23
|
+
KEYS.filter((k) => !k.includes("sitemap")),
|
|
24
|
+
);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it("purges one product, its variants, all extras and all lists", () => {
|
|
28
|
+
expect(keysToPurge(KEYS, { products: [MARGHERITA] })).toEqual([
|
|
29
|
+
KEYS[0],
|
|
30
|
+
KEYS[2],
|
|
31
|
+
KEYS[3],
|
|
32
|
+
KEYS[5],
|
|
33
|
+
KEYS[6],
|
|
34
|
+
KEYS[8],
|
|
35
|
+
]);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("purges a category and the lists, not the products", () => {
|
|
39
|
+
expect(keysToPurge(KEYS, { categories: [PIZZA] })).toEqual([
|
|
40
|
+
KEYS[3],
|
|
41
|
+
KEYS[4],
|
|
42
|
+
KEYS[5],
|
|
43
|
+
KEYS[6],
|
|
44
|
+
KEYS[8],
|
|
45
|
+
]);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it("finds ids written with dashes or upper case", () => {
|
|
49
|
+
const dashed = `0197BAF0-A6A9-71EA-ADA5-9ED5C84138DE`;
|
|
50
|
+
expect(keysToPurge(KEYS, { products: [dashed] })).toContain(KEYS[0]);
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
describe("hasPurgeSecret", () => {
|
|
55
|
+
it("accepts only the exact bearer secret", () => {
|
|
56
|
+
expect(hasPurgeSecret("Bearer geheim", "geheim")).toBe(true);
|
|
57
|
+
expect(hasPurgeSecret("Bearer geheim2", "geheim")).toBe(false);
|
|
58
|
+
expect(hasPurgeSecret("geheim", "geheim")).toBe(false);
|
|
59
|
+
expect(hasPurgeSecret(undefined, "geheim")).toBe(false);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it("is off without a configured secret", () => {
|
|
63
|
+
expect(hasPurgeSecret("Bearer ", "")).toBe(false);
|
|
64
|
+
});
|
|
65
|
+
});
|