@shopbite-de/storefront 2.6.0 → 2.7.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 +13 -0
- package/.github/workflows/ci.yaml +2 -0
- package/CLAUDE.md +54 -30
- package/GLOSSARY.md +327 -0
- package/README.md +5 -6
- package/app/app.vue +50 -40
- package/app/components/Address/Card.vue +2 -2
- package/app/components/Address/Detail.vue +17 -45
- package/app/components/Address/Form.vue +38 -26
- package/app/components/BranchSwitch.vue +40 -0
- package/app/components/Cart/Bar.vue +9 -9
- package/app/components/Cart/Item.vue +27 -36
- package/app/components/Cart/QuickView.vue +17 -16
- package/app/components/Cart/Upsell.vue +19 -27
- package/app/components/Category/Breadcrumb.vue +3 -8
- package/app/components/Category/FilterGroup.vue +6 -6
- package/app/components/Category/Listing.vue +44 -66
- package/app/components/Checkout/DeliveryTimeSelect.vue +21 -48
- package/app/components/Checkout/OrderMessage.vue +29 -0
- package/app/components/Checkout/OrdererAddress.vue +40 -0
- package/app/components/Checkout/PaymentAndDelivery.vue +40 -35
- package/app/components/Checkout/Summary.vue +89 -74
- package/app/components/Checkout/VoucherInput.vue +8 -8
- package/app/components/Contact/Form.vue +97 -13
- package/app/components/Footer.vue +2 -2
- package/app/components/Header.vue +9 -9
- package/app/components/Home/Categories.vue +13 -17
- package/app/components/Home/Facts.vue +6 -4
- package/app/components/Home/Hero.vue +1 -1
- package/app/components/Home/Highlights.vue +8 -8
- package/app/components/Home/Restaurant.vue +2 -2
- package/app/components/Menu/BonCard.vue +21 -37
- package/app/components/Menu/CartPanel.vue +2 -2
- package/app/components/Menu/CategoryIndex.vue +8 -12
- package/app/components/Menu/OrderMode.vue +2 -2
- package/app/components/Menu/SectionHeader.vue +6 -10
- package/app/components/Navigation/MobileTop.vue +12 -20
- package/app/components/Order/Confirmation.vue +10 -10
- package/app/components/Order/Detail.vue +47 -49
- package/app/components/Product/Configurator.vue +46 -30
- package/app/components/Product/CrossSelling.vue +56 -71
- package/app/components/Product/DeselectIngredient.vue +10 -11
- package/app/components/Product/Detail.vue +62 -55
- package/app/components/Product/FoodLabels.vue +4 -9
- package/app/components/Product/QuickView.vue +22 -30
- package/app/components/User/Detail.vue +20 -43
- package/app/components/User/LoginForm.vue +11 -17
- package/app/components/User/RegistrationForm.vue +3 -3
- package/app/components/Wishlist.vue +99 -106
- package/app/components/WishlistSaveButton.vue +17 -9
- package/app/composables/useAccountAddresses.ts +89 -0
- package/app/composables/useAccountOrders.ts +35 -0
- package/app/composables/useAddToCart.ts +63 -110
- package/app/composables/useCartData.ts +55 -0
- package/app/composables/useCartMutations.ts +93 -85
- package/app/composables/useCartUpsell.ts +27 -71
- package/app/composables/useCategoryListing.ts +36 -59
- package/app/composables/useCategorySeo.ts +11 -16
- package/app/composables/useCheckoutMethodGuard.ts +22 -46
- package/app/composables/useCheckoutMethods.ts +89 -0
- package/app/composables/useCheckoutOrderMessage.ts +19 -0
- package/app/composables/useCheckoutOrderer.ts +69 -0
- package/app/composables/useCommercePrice.ts +46 -9
- package/app/composables/useContactForm.ts +25 -32
- package/app/composables/useCustomer.ts +169 -0
- package/app/composables/useDeliveryTime.ts +112 -122
- package/app/composables/useHolidays.ts +12 -62
- package/app/composables/useHomeMenuCategories.ts +5 -5
- package/app/composables/useListingSearch.ts +9 -8
- package/app/composables/useLiveShopData.ts +15 -9
- package/app/composables/useMenuRootId.ts +3 -6
- package/app/composables/useMenuSection.ts +14 -0
- package/app/composables/useMenuSectionSchema.ts +23 -30
- package/app/composables/useNavigation.ts +24 -29
- package/app/composables/useOpeningHours.ts +21 -0
- package/app/composables/useOpeningHoursData.ts +9 -9
- package/app/composables/useOrder.ts +37 -0
- package/app/composables/useOrderMode.ts +18 -16
- package/app/composables/usePlaceOrder.ts +37 -0
- package/app/composables/useProductCartQuantity.ts +6 -6
- package/app/composables/useProductDetail.ts +28 -53
- package/app/composables/useProductEvents.ts +6 -5
- package/app/composables/useProductQuickView.ts +35 -68
- package/app/composables/useRegistrationForm.ts +85 -34
- package/app/composables/useRestaurantSchema.ts +7 -7
- package/app/composables/useSearchTracking.ts +6 -6
- package/app/composables/useSectionRoute.ts +48 -0
- package/app/composables/useServiceSchedule.ts +23 -0
- package/app/composables/useShippingLeadTime.ts +8 -6
- package/app/composables/useShippingMethodChoice.ts +13 -23
- package/app/composables/useShippingMethods.ts +31 -0
- package/app/composables/useShopConfig.ts +60 -0
- package/app/composables/useShopData.ts +20 -0
- package/app/composables/useSortingSelection.ts +5 -8
- package/app/composables/useStoreStatus.ts +13 -37
- package/app/composables/useTopSellers.ts +3 -3
- package/app/composables/useTrackEvent.ts +29 -24
- package/app/composables/useVoucherCode.ts +47 -12
- package/app/composables/useWishlistEntries.ts +233 -177
- package/app/error.vue +3 -2
- package/app/layouts/account.vue +4 -3
- package/app/pages/[...all].vue +6 -16
- package/app/pages/account/recover/password/index.vue +6 -14
- package/app/pages/anmelden.vue +1 -1
- package/app/pages/bestellung/[id]/erfolg.vue +3 -3
- package/app/pages/bestellung/[id]/fehler.vue +18 -29
- package/app/pages/bestellung/kasse.vue +24 -7
- package/app/pages/konto/adressen.vue +95 -40
- package/app/pages/konto/bestellungen.vue +10 -13
- package/app/pages/konto/index.vue +12 -2
- package/app/pages/konto/profil.vue +241 -42
- package/app/pages/passwort-vergessen.vue +5 -10
- package/app/pages/registrierung/bestaetigen.vue +9 -23
- package/app/pages/registrierung/index.vue +19 -11
- package/app/pages/suche.vue +29 -29
- package/app/plugins/shopware-layer-offline.ts +38 -0
- package/app/plugins/visitor-has-session.server.ts +19 -0
- package/app/utils/accountMessages.ts +74 -0
- package/app/utils/accountReads.ts +34 -0
- package/app/utils/bffWrite.ts +89 -0
- package/app/utils/dish.ts +121 -0
- package/app/utils/leadTime.ts +25 -29
- package/app/utils/media.ts +11 -17
- package/app/utils/menuGroups.ts +22 -35
- package/app/utils/money.ts +41 -0
- package/app/utils/openingHours.ts +30 -30
- package/app/utils/orderStatus.ts +17 -0
- package/app/utils/productUrl.ts +14 -31
- package/app/utils/restaurantTime.ts +326 -0
- package/app/utils/schema.ts +39 -42
- package/app/utils/wishlist.ts +139 -0
- package/docs/adr/0001-backend-agnostic-storefront.md +409 -0
- package/eslint.config.mjs +42 -2
- package/nuxt.config.ts +28 -0
- package/package.json +1 -1
- package/server/adapters/memory/cart.ts +370 -0
- package/server/adapters/memory/catalog.ts +367 -0
- package/server/adapters/memory/contact.ts +89 -0
- package/server/adapters/memory/customer.ts +458 -0
- package/server/adapters/memory/demo-menu.json +1190 -0
- package/server/adapters/memory/demoImages.ts +13 -0
- package/server/adapters/memory/demoMenu.ts +234 -0
- package/server/adapters/memory/demoNavigation.ts +71 -0
- package/server/adapters/memory/index.ts +122 -0
- package/server/adapters/memory/navigation.ts +168 -0
- package/server/adapters/memory/ordering.ts +379 -0
- package/server/adapters/memory/seed.ts +136 -0
- package/server/adapters/memory/wishlist.ts +166 -0
- package/server/adapters/notImplemented.ts +67 -0
- package/server/adapters/registry.ts +32 -0
- package/server/adapters/runtimeConfig.ts +14 -0
- package/server/adapters/shopware/cart.ts +792 -0
- package/server/adapters/shopware/catalog.ts +850 -0
- package/server/adapters/shopware/checkout.ts +138 -0
- package/server/adapters/shopware/contact.ts +255 -0
- package/server/adapters/shopware/customer.ts +460 -0
- package/server/adapters/shopware/index.ts +165 -0
- package/server/adapters/shopware/menuRoot.ts +24 -0
- package/server/adapters/shopware/navigation.ts +426 -0
- package/server/adapters/shopware/ordering.ts +1185 -0
- package/server/adapters/shopware/request.ts +67 -0
- package/server/adapters/shopware/shop.ts +249 -0
- package/server/adapters/shopware/storeApi.ts +62 -0
- package/server/adapters/shopware/wishlist.ts +473 -0
- package/server/api/__sitemap__/urls.get.ts +10 -38
- package/server/api/account/addresses/[addressId]/default.post.ts +13 -0
- package/server/api/account/addresses/[addressId].delete.ts +14 -0
- package/server/api/account/addresses/[addressId].patch.ts +15 -0
- package/server/api/account/addresses/index.get.ts +6 -0
- package/server/api/account/addresses/index.post.ts +9 -0
- package/server/api/account/email.post.ts +10 -0
- package/server/api/account/index.delete.ts +11 -0
- package/server/api/account/index.get.ts +9 -0
- package/server/api/account/login.post.ts +14 -0
- package/server/api/account/logout.post.ts +11 -0
- package/server/api/account/orders.get.ts +13 -0
- package/server/api/account/password-reset/confirm.post.ts +13 -0
- package/server/api/account/password-reset.post.ts +16 -0
- package/server/api/account/password.post.ts +18 -0
- package/server/api/account/profile.patch.ts +6 -0
- package/server/api/account/register/confirm.post.ts +13 -0
- package/server/api/account/register.post.ts +15 -0
- package/server/api/breadcrumb/[sectionId].get.ts +33 -0
- package/server/api/cart/index.get.ts +4 -0
- package/server/api/cart/lines/[lineId].delete.ts +6 -0
- package/server/api/cart/lines/[lineId].patch.ts +10 -0
- package/server/api/cart/lines.post.ts +10 -0
- package/server/api/cart/vouchers/[code].delete.ts +9 -0
- package/server/api/cart/vouchers.post.ts +12 -0
- package/server/api/checkout/guest.post.ts +13 -0
- package/server/api/checkout/orderer/addresses/[addressId].patch.ts +16 -0
- package/server/api/checkout/orderer.get.ts +10 -0
- package/server/api/checkout/orders.post.ts +60 -0
- package/server/api/checkout/payment-methods.get.ts +9 -0
- package/server/api/checkout/selection.get.ts +10 -0
- package/server/api/checkout/selection.post.ts +14 -0
- package/server/api/checkout/shipping-methods.get.ts +9 -0
- package/server/api/contact/form.get.ts +13 -0
- package/server/api/contact/index.post.ts +24 -0
- package/server/api/listing/[sectionId].get.ts +33 -0
- package/server/api/menu/dishes/[dishId]/variant.get.ts +29 -0
- package/server/api/menu/dishes/[dishId].get.ts +19 -0
- package/server/api/menu/dishes/index.get.ts +26 -0
- package/server/api/menu/media/[name].get.ts +18 -0
- package/server/api/menu/root.get.ts +12 -0
- package/server/api/menu/sections/[sectionId].get.ts +18 -0
- package/server/api/menu/upsell.get.ts +12 -0
- package/server/api/navigation/[kind].get.ts +75 -0
- package/server/api/orders/[orderId]/payment.post.ts +24 -0
- package/server/api/orders/[orderId].get.ts +13 -0
- package/server/api/payment-methods.get.ts +11 -15
- package/server/api/resolve-path.get.ts +20 -0
- package/server/api/search.get.ts +16 -80
- package/server/api/session.post.ts +18 -0
- package/server/api/shop/branches.get.ts +9 -0
- package/server/api/shop/config.get.ts +9 -0
- package/server/api/shop/holidays.get.ts +9 -0
- package/server/api/shop/opening-hours.get.ts +9 -0
- package/server/api/top-sellers.get.ts +14 -50
- package/server/api/wishlist/entries/[entryId].delete.ts +8 -0
- package/server/api/wishlist/entries.post.ts +13 -0
- package/server/api/wishlist/index.get.ts +13 -0
- package/server/api/wishlist/merge.post.ts +12 -0
- package/server/plugins/visitor-session.ts +35 -0
- package/server/ports/CartPort.ts +48 -0
- package/server/ports/CatalogPort.ts +66 -0
- package/server/ports/CheckoutPort.ts +128 -0
- package/server/ports/ContactPort.ts +38 -0
- package/server/ports/CustomerPort.ts +154 -0
- package/server/ports/NavigationPort.ts +58 -0
- package/server/ports/OrderPort.ts +31 -0
- package/server/ports/ShopPort.ts +29 -0
- package/server/ports/WishlistPort.ts +47 -0
- package/server/ports/index.ts +42 -0
- package/server/ports/session.ts +22 -0
- package/server/routes/llms.txt.ts +28 -154
- package/server/session/forwardedFor.ts +24 -0
- package/server/session/sameOrigin.ts +47 -0
- package/server/session/seal.ts +147 -0
- package/server/session/visitorSession.ts +288 -0
- package/server/utils/accountRoutes.ts +173 -0
- package/server/utils/backend.ts +21 -0
- package/server/utils/backoff.ts +18 -5
- package/server/utils/cacheKeys.ts +11 -0
- package/server/utils/cachePurge.ts +47 -27
- package/server/utils/cartRoutes.ts +124 -0
- package/server/utils/catalogData.ts +24 -0
- package/server/utils/checkoutRoutes.ts +326 -0
- package/server/utils/contactRoutes.ts +86 -0
- package/server/utils/domainHttp.ts +73 -0
- package/server/utils/listingParams.ts +49 -20
- package/server/utils/llmsTxt.ts +64 -19
- package/server/utils/navigationData.ts +14 -0
- package/server/utils/orderPlacement.ts +322 -0
- package/server/utils/outageBreaker.ts +126 -0
- package/server/utils/pathRoutes.ts +160 -0
- package/server/utils/requestBody.ts +43 -0
- package/server/utils/shopData.ts +132 -0
- package/server/utils/sitemap.ts +14 -36
- package/server/utils/storeApi.ts +71 -23
- package/server/utils/visitorSession.ts +209 -0
- package/server/utils/wishlistRoutes.ts +55 -0
- package/shared/domain/customer/index.ts +2 -0
- package/shared/domain/customer/types.ts +88 -0
- package/shared/domain/discoverability/index.ts +4 -0
- package/shared/domain/discoverability/paths.ts +93 -0
- package/shared/domain/discoverability/route.ts +32 -0
- package/shared/domain/discoverability/types.ts +75 -0
- package/shared/domain/index.ts +11 -0
- package/shared/domain/kernel/calendar.ts +88 -0
- package/shared/domain/kernel/errors.ts +100 -0
- package/shared/domain/kernel/freeze.ts +13 -0
- package/shared/domain/kernel/ids.ts +6 -0
- package/shared/domain/kernel/index.ts +8 -0
- package/shared/domain/kernel/media.ts +17 -0
- package/shared/domain/kernel/money.ts +104 -0
- package/shared/domain/kernel/timeOfDay.ts +39 -0
- package/shared/domain/menu/dishConfiguration.ts +72 -0
- package/shared/domain/menu/index.ts +3 -0
- package/shared/domain/menu/types.ts +185 -0
- package/shared/domain/ordering/index.ts +3 -0
- package/shared/domain/ordering/time.ts +114 -0
- package/shared/domain/ordering/types.ts +195 -0
- package/shared/domain/restaurant/holiday.ts +38 -0
- package/shared/domain/restaurant/index.ts +4 -0
- package/shared/domain/restaurant/openingInterval.ts +97 -0
- package/shared/domain/restaurant/types.ts +75 -0
- package/test/a11y/presets.test.ts +49 -31
- package/test/contract/cart.contract.ts +592 -0
- package/test/contract/cart.spec.ts +5 -0
- package/test/contract/cartFixtures.ts +573 -0
- package/test/contract/catalog.contract.ts +714 -0
- package/test/contract/catalog.spec.ts +5 -0
- package/test/contract/catalogFixtures.ts +572 -0
- package/test/contract/checkout.contract.ts +123 -0
- package/test/contract/checkout.spec.ts +5 -0
- package/test/contract/contact.contract.ts +149 -0
- package/test/contract/contact.spec.ts +4 -0
- package/test/contract/contactFixtures.ts +148 -0
- package/test/contract/customer.contract.ts +831 -0
- package/test/contract/customer.spec.ts +4 -0
- package/test/contract/customerFixtures.ts +468 -0
- package/test/contract/fixtures.ts +260 -0
- package/test/contract/harness.ts +45 -0
- package/test/contract/navigation.contract.ts +336 -0
- package/test/contract/navigation.spec.ts +5 -0
- package/test/contract/navigationFixtures.ts +311 -0
- package/test/contract/ordering.contract.ts +655 -0
- package/test/contract/ordering.spec.ts +4 -0
- package/test/contract/orderingFixtures.ts +498 -0
- package/test/contract/shop.contract.ts +164 -0
- package/test/contract/shop.spec.ts +5 -0
- package/test/contract/wishlist.contract.ts +375 -0
- package/test/contract/wishlist.spec.ts +4 -0
- package/test/contract/wishlistFixtures.ts +199 -0
- package/test/nuxt/AccountPagesPreset.test.ts +320 -83
- package/test/nuxt/AuthPagesPreset.test.ts +17 -24
- package/test/nuxt/CartBar.test.ts +12 -8
- package/test/nuxt/CartCheckoutPreset.test.ts +36 -33
- package/test/nuxt/CartItem.test.ts +39 -20
- package/test/nuxt/CartQuickView.test.ts +106 -0
- package/test/nuxt/CartUpsell.test.ts +37 -39
- package/test/nuxt/CategoryBreadcrumb.test.ts +26 -0
- package/test/nuxt/CheckoutFormsPreset.test.ts +92 -13
- package/test/nuxt/CheckoutOnePage.test.ts +28 -21
- package/test/nuxt/CheckoutPageMessage.test.ts +31 -0
- package/test/nuxt/CheckoutSummary.test.ts +163 -48
- package/test/nuxt/CheckoutVoucherInput.test.ts +77 -80
- package/test/nuxt/ContactFormPreset.test.ts +97 -36
- package/test/nuxt/DeliveryTimeSelect.test.ts +72 -45
- package/test/nuxt/FooterStoreStatus.test.ts +2 -2
- package/test/nuxt/HeaderFooterPreset.test.ts +11 -19
- package/test/nuxt/HomeCategories.test.ts +47 -0
- package/test/nuxt/HomeLeadTimes.test.ts +3 -3
- package/test/nuxt/MenuBonCard.test.ts +45 -32
- package/test/nuxt/NavigationMobileTop.test.ts +30 -28
- package/test/nuxt/OrderPagesPreset.test.ts +83 -52
- package/test/nuxt/PaymentAndDelivery.test.ts +25 -26
- package/test/nuxt/ProductConfigurator.test.ts +81 -0
- package/test/nuxt/ProductCrossSelling.test.ts +23 -20
- package/test/nuxt/ProductDeselectIngredient.test.ts +6 -9
- package/test/nuxt/ProductFoodLabels.test.ts +7 -24
- package/test/nuxt/ProductSheetPreset.test.ts +24 -24
- package/test/nuxt/RegisterConfirmPage.test.ts +26 -45
- package/test/nuxt/RegistrationForm.test.ts +57 -53
- package/test/nuxt/RegistrationPage.test.ts +37 -0
- package/test/nuxt/WishlistPage.test.ts +119 -43
- package/test/nuxt/WishlistSaveButton.test.ts +39 -12
- package/test/nuxt/accountSwitch.test.ts +106 -0
- package/test/nuxt/bffWrite.test.ts +41 -0
- package/test/nuxt/liveShopData.test.ts +36 -68
- package/test/nuxt/shopwareLayerOffline.test.ts +32 -0
- package/test/nuxt/useAddToCart.test.ts +142 -115
- package/test/nuxt/useCartMutations.test.ts +165 -113
- package/test/nuxt/useCartUpsell.test.ts +36 -63
- package/test/nuxt/useCategoryListing.test.ts +75 -118
- package/test/nuxt/useCheckoutMethodGuard.test.ts +54 -105
- package/test/nuxt/useCustomer.test.ts +127 -0
- package/test/nuxt/useDeliveryTime.test.ts +169 -157
- package/test/nuxt/useNavigation.test.ts +81 -0
- package/test/nuxt/useOpeningHoursData.test.ts +18 -18
- package/test/nuxt/useOrder.test.ts +38 -0
- package/test/nuxt/usePlaceOrder.test.ts +88 -0
- package/test/nuxt/useSearchTracking.test.ts +2 -2
- package/test/nuxt/useSectionRoute.test.ts +86 -0
- package/test/nuxt/useShippingLeadTime.test.ts +17 -17
- package/test/nuxt/useShippingMethodChoice.test.ts +22 -22
- package/test/nuxt/useShippingMethods.test.ts +71 -0
- package/test/nuxt/useShopConfig.test.ts +64 -0
- package/test/nuxt/useStoreStatus.test.ts +34 -50
- package/test/nuxt/useTopSellers.test.ts +6 -5
- package/test/nuxt/useTrackEvent.test.ts +68 -6
- package/test/nuxt/useWishlistEntries.test.ts +392 -128
- package/test/support/cart.ts +43 -0
- package/test/support/dishes.ts +116 -0
- package/test/support/navigation.ts +17 -0
- package/test/unit/accountMessages.spec.ts +53 -0
- package/test/unit/accountRoutes.spec.ts +637 -0
- package/test/unit/adapters/memoryCustomer.spec.ts +67 -0
- package/test/unit/adapters/memoryDemoMenu.spec.ts +174 -0
- package/test/unit/adapters/registry.spec.ts +100 -0
- package/test/unit/adapters/shopwareCart.spec.ts +636 -0
- package/test/unit/adapters/shopwareCatalog.spec.ts +486 -0
- package/test/unit/adapters/shopwareCheckout.spec.ts +197 -0
- package/test/unit/adapters/shopwareContact.spec.ts +296 -0
- package/test/unit/adapters/shopwareCustomer.spec.ts +256 -0
- package/test/unit/adapters/shopwareNavigation.spec.ts +594 -0
- package/test/unit/adapters/shopwareOrdering.spec.ts +620 -0
- package/test/unit/adapters/shopwareShop.spec.ts +259 -0
- package/test/unit/adapters/shopwareWishlist.spec.ts +306 -0
- package/test/unit/adapters/useBackend.spec.ts +41 -0
- package/test/unit/backoff.spec.ts +47 -0
- package/test/unit/boundedCache.spec.ts +59 -59
- package/test/unit/cacheKeys.spec.ts +44 -0
- package/test/unit/cachePurge.spec.ts +44 -19
- package/test/unit/cartRoutes.spec.ts +567 -0
- package/test/unit/catalogRoutes.spec.ts +287 -0
- package/test/unit/checkoutRoutes.spec.ts +545 -0
- package/test/unit/dish.spec.ts +128 -0
- package/test/unit/domain/discoverability.spec.ts +154 -0
- package/test/unit/domain/dishConfiguration.spec.ts +85 -0
- package/test/unit/domain/money.spec.ts +102 -0
- package/test/unit/domain/time.spec.ts +336 -0
- package/test/unit/domainHttp.spec.ts +74 -0
- package/test/unit/forwardedFor.spec.ts +24 -0
- package/test/unit/layoutCacheRoutes.spec.ts +47 -471
- package/test/unit/leadTime.spec.ts +28 -23
- package/test/unit/llmsTxt.spec.ts +120 -9
- package/test/unit/media.spec.ts +25 -26
- package/test/unit/menuGroups.spec.ts +31 -21
- package/test/unit/navigationRoutes.spec.ts +648 -0
- package/test/unit/nitroStubs.ts +6 -1
- package/test/unit/openingHours.spec.ts +57 -34
- package/test/unit/orderPlacement.spec.ts +362 -0
- package/test/unit/outageBreaker.spec.ts +213 -0
- package/test/unit/productUrl.spec.ts +10 -28
- package/test/unit/requestBody.spec.ts +56 -0
- package/test/unit/restaurantTime.spec.ts +295 -0
- package/test/unit/schema.spec.ts +58 -26
- package/test/unit/session/sameOrigin.spec.ts +76 -0
- package/test/unit/session/seal.spec.ts +128 -0
- package/test/unit/session/visitorSession.spec.ts +439 -0
- package/test/unit/shopRoutes.spec.ts +257 -0
- package/test/unit/sitemap.spec.ts +11 -47
- package/test/unit/storeApiForVisitor.spec.ts +109 -0
- package/test/unit/useCategorySeo.spec.ts +57 -31
- package/test/unit/wishlistContactRoutes.spec.ts +607 -0
- package/vitest.config.ts +5 -1
- package/app/components/SalesChannelSwitch.vue +0 -73
- package/app/composables/useBusinessHours.ts +0 -192
- package/app/composables/useCategoryById.ts +0 -10
- package/app/composables/useNavigationSearch.ts +0 -45
- package/app/composables/useProductConfigurator.ts +0 -70
- package/app/composables/useProductCrossSelling.ts +0 -42
- package/app/composables/useProductVariants.ts +0 -61
- package/app/composables/useProductVariantsZwei.ts +0 -62
- package/app/composables/useSeoUrlRoute.ts +0 -124
- package/app/composables/useShopBiteConfig.ts +0 -69
- package/app/composables/useSyncWishlist.ts +0 -119
- package/app/types/Association.d.ts +0 -14
- package/app/utils/product.ts +0 -139
- package/app/utils/seoPath.ts +0 -68
- package/app/utils/time.ts +0 -20
- package/server/api/breadcrumb/[categoryId].get.ts +0 -31
- package/server/api/category/[categoryId].get.ts +0 -37
- package/server/api/listing/[categoryId].get.ts +0 -102
- package/server/api/navigation/[type].get.ts +0 -97
- package/server/api/product/[productId]/cross-selling.get.ts +0 -26
- package/server/api/product/[productId].get.ts +0 -76
- package/server/api/product/variant.get.ts +0 -98
- package/server/api/seo-url/index.get.ts +0 -17
- package/server/api/seo-url/known.get.ts +0 -33
- package/server/api/shopbite/business-hours.get.ts +0 -22
- package/server/api/shopbite/config.get.ts +0 -6
- package/server/api/shopbite/holidays.get.ts +0 -20
- package/server/utils/menuRoot.ts +0 -31
- package/server/utils/seoPaths.ts +0 -130
- package/server/utils/seoUrlCache.ts +0 -98
- package/server/utils/shopBiteConfig.ts +0 -30
- package/test/nuxt/useBusinessHours.test.ts +0 -106
- package/test/nuxt/useProductConfigurator.test.ts +0 -87
- package/test/nuxt/useProductVariants.test.ts +0 -95
- package/test/nuxt/useProductVariantsZwei.test.ts +0 -52
- package/test/nuxt/useSeoUrlRoute.test.ts +0 -176
- package/test/nuxt/useShopBiteConfig.test.ts +0 -131
- package/test/nuxt/useSyncWishlist.test.ts +0 -85
- package/test/unit/product.spec.ts +0 -160
- package/test/unit/seoPath.spec.ts +0 -161
- package/test/unit/seoPaths.spec.ts +0 -117
|
@@ -0,0 +1,322 @@
|
|
|
1
|
+
import { bypassOutageBreaker } from "./outageBreaker";
|
|
2
|
+
import {
|
|
3
|
+
DomainError,
|
|
4
|
+
isDomainError,
|
|
5
|
+
type Order,
|
|
6
|
+
type PlacedOrder,
|
|
7
|
+
type ServiceSchedule,
|
|
8
|
+
type ShippingMethod,
|
|
9
|
+
type WishedTime,
|
|
10
|
+
} from "../../shared/domain";
|
|
11
|
+
import {
|
|
12
|
+
restaurantNow,
|
|
13
|
+
shiftDate,
|
|
14
|
+
WISHED_TIME_STEP,
|
|
15
|
+
wishedTimeWindow,
|
|
16
|
+
} from "../../app/utils/restaurantTime";
|
|
17
|
+
import { shippingLeadTime } from "../../app/utils/leadTime";
|
|
18
|
+
import type { Backend, BackendSession } from "../ports";
|
|
19
|
+
import type { PlaceOrderInput } from "./checkoutRoutes";
|
|
20
|
+
|
|
21
|
+
/** What the storefront checks before it asks the backend for an order. */
|
|
22
|
+
export type PlacementGates = Readonly<{
|
|
23
|
+
/** The restaurant's switch, as the backend has it now. */
|
|
24
|
+
acceptsOrders: () => Promise<boolean>;
|
|
25
|
+
/** Time zone, opening hours and holidays of the ordering window. */
|
|
26
|
+
schedule: () => Promise<ServiceSchedule>;
|
|
27
|
+
shippingMethods: () => Promise<readonly ShippingMethod[]>;
|
|
28
|
+
}>;
|
|
29
|
+
|
|
30
|
+
const outside = () =>
|
|
31
|
+
new DomainError(
|
|
32
|
+
"outside_ordering_window",
|
|
33
|
+
"The wished time is outside the ordering window.",
|
|
34
|
+
);
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* The wished time lies in the ordering window the checkout offers now
|
|
38
|
+
* (`wishedTimeWindow`: today's spans, from now plus the lead time to the
|
|
39
|
+
* span's end, never on a holiday), on the grid. One grid step of grace
|
|
40
|
+
* before the earliest time: the browser's clock moves on once a minute.
|
|
41
|
+
*/
|
|
42
|
+
export function checkWishedTime(
|
|
43
|
+
wished: WishedTime,
|
|
44
|
+
now: Date,
|
|
45
|
+
schedule: ServiceSchedule,
|
|
46
|
+
leadMinutes: number | null,
|
|
47
|
+
): void {
|
|
48
|
+
const window = wishedTimeWindow(now, schedule, leadMinutes);
|
|
49
|
+
if (window.kind !== "open") throw outside();
|
|
50
|
+
const today = restaurantNow(now, schedule.timeZone).date;
|
|
51
|
+
const offset =
|
|
52
|
+
wished.date === today
|
|
53
|
+
? 0
|
|
54
|
+
: wished.date === shiftDate(today, 1)
|
|
55
|
+
? 1440
|
|
56
|
+
: null;
|
|
57
|
+
if (offset === null) throw outside();
|
|
58
|
+
const minutes = offset + wished.time.minutes;
|
|
59
|
+
if (minutes < window.earliest - WISHED_TIME_STEP || minutes > window.latest) {
|
|
60
|
+
throw outside();
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
type Attempt = { at: number; placed?: PlacedOrder };
|
|
65
|
+
|
|
66
|
+
/** An order request that got no answer: the order may still land. */
|
|
67
|
+
type Unanswered = { at: number };
|
|
68
|
+
|
|
69
|
+
const statusUnknown = () =>
|
|
70
|
+
new DomainError(
|
|
71
|
+
"order_status_unknown",
|
|
72
|
+
"The order got no answer and cannot be found (yet).",
|
|
73
|
+
);
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Places a visitor's order at most once (ADR 0001, step 5). The browser
|
|
77
|
+
* sends one attempt id per press of the order button and the same id
|
|
78
|
+
* again when it retries (outdated session).
|
|
79
|
+
*
|
|
80
|
+
* - An attempt this process placed already answers its order again
|
|
81
|
+
* (double click, a retry after the answer was lost on the way).
|
|
82
|
+
* - The adapter sends the order request once and waits long for it
|
|
83
|
+
* (Shopware: 60 s), since the backend may still commit an order the
|
|
84
|
+
* storefront stopped waiting for. When the answer does not come, the
|
|
85
|
+
* attempt and the session are marked unanswered and nothing is ever
|
|
86
|
+
* placed for them again: this request, a retry and a new press look for
|
|
87
|
+
* an order the session placed since (polling for a while) and answer it,
|
|
88
|
+
* or `order_status_unknown` (the guest is told to check the e-mail or
|
|
89
|
+
* call before ordering again). The mark ends after `unansweredTtlMs`.
|
|
90
|
+
* - A new attempt with an empty cart is `order_already_placed` with the
|
|
91
|
+
* session's order of the last 15 minutes (another tab, a reload), never
|
|
92
|
+
* a new success; without one it is `cart_empty`.
|
|
93
|
+
* - A placed order whose payment cannot be started is still answered,
|
|
94
|
+
* `paymentFailed`, so the guest goes to the payment retry.
|
|
95
|
+
*
|
|
96
|
+
* Runs inside the visitor's exclusive section (one write at a time per
|
|
97
|
+
* process). Attempts and marks are per process: an unanswered order seen
|
|
98
|
+
* by another instance is unknown there (documented limit).
|
|
99
|
+
*/
|
|
100
|
+
export function createOrderPlacement(
|
|
101
|
+
options: {
|
|
102
|
+
/** The clock of the ordering window (tests). */
|
|
103
|
+
now?: () => Date;
|
|
104
|
+
/** Attempts remembered (oldest first out) and for how long. */
|
|
105
|
+
maxAttempts?: number;
|
|
106
|
+
attemptTtlMs?: number;
|
|
107
|
+
/** How long an unanswered order blocks the session's placements. */
|
|
108
|
+
unansweredTtlMs?: number;
|
|
109
|
+
/** Pauses between the looks for an unanswered order (about 30 s). */
|
|
110
|
+
pollDelaysMs?: readonly number[];
|
|
111
|
+
/** How far the backend's clock may be behind ours. */
|
|
112
|
+
clockSkewMs?: number;
|
|
113
|
+
/** How old an order may be to name it for an empty cart. */
|
|
114
|
+
recentMs?: number;
|
|
115
|
+
} = {},
|
|
116
|
+
) {
|
|
117
|
+
const now = options.now ?? (() => new Date());
|
|
118
|
+
const maxAttempts = options.maxAttempts ?? 10_000;
|
|
119
|
+
const ttlMs = options.attemptTtlMs ?? 30 * 60_000;
|
|
120
|
+
const unansweredTtlMs = options.unansweredTtlMs ?? 15 * 60_000;
|
|
121
|
+
const pollDelaysMs = options.pollDelaysMs ?? [
|
|
122
|
+
0, 3000, 5000, 7000, 7000, 8000,
|
|
123
|
+
];
|
|
124
|
+
const skewMs = options.clockSkewMs ?? 2 * 60_000;
|
|
125
|
+
const recentMs = options.recentMs ?? 15 * 60_000;
|
|
126
|
+
const attempts = new Map<string, Attempt>();
|
|
127
|
+
const unanswered = new Map<string, Unanswered>();
|
|
128
|
+
|
|
129
|
+
function bounded<T>(map: Map<string, T>, key: string, value: T) {
|
|
130
|
+
map.delete(key);
|
|
131
|
+
map.set(key, value);
|
|
132
|
+
while (map.size > maxAttempts) map.delete(map.keys().next().value!);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function known(key: string): Attempt | undefined {
|
|
136
|
+
const attempt = attempts.get(key);
|
|
137
|
+
if (attempt && Date.now() - attempt.at > ttlMs) {
|
|
138
|
+
attempts.delete(key);
|
|
139
|
+
return undefined;
|
|
140
|
+
}
|
|
141
|
+
return attempt;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function pending(sessionKey: string): Unanswered | undefined {
|
|
145
|
+
const mark = unanswered.get(sessionKey);
|
|
146
|
+
if (mark && Date.now() - mark.at > unansweredTtlMs) {
|
|
147
|
+
unanswered.delete(sessionKey);
|
|
148
|
+
return undefined;
|
|
149
|
+
}
|
|
150
|
+
return mark;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
async function withPayment(
|
|
154
|
+
backend: Backend,
|
|
155
|
+
session: BackendSession,
|
|
156
|
+
order: Order,
|
|
157
|
+
urls: (orderId: string) => { success: string; failure: string },
|
|
158
|
+
): Promise<PlacedOrder> {
|
|
159
|
+
try {
|
|
160
|
+
const { redirectUrl } = await backend.checkout.startPayment(
|
|
161
|
+
session,
|
|
162
|
+
order.id,
|
|
163
|
+
urls(order.id),
|
|
164
|
+
);
|
|
165
|
+
return { order, redirectUrl, paymentFailed: false };
|
|
166
|
+
} catch (error) {
|
|
167
|
+
console.error("[checkout] the payment could not be started", error);
|
|
168
|
+
return { order, redirectUrl: null, paymentFailed: true };
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** The session's order placed since `since`, looking for a while. */
|
|
173
|
+
async function findLanded(
|
|
174
|
+
backend: Backend,
|
|
175
|
+
session: BackendSession,
|
|
176
|
+
since: number,
|
|
177
|
+
): Promise<Order | null> {
|
|
178
|
+
for (const delay of pollDelaysMs) {
|
|
179
|
+
if (delay) await new Promise((resolve) => setTimeout(resolve, delay));
|
|
180
|
+
try {
|
|
181
|
+
// past an open outage breaker: the order may have landed
|
|
182
|
+
const { orders } = await bypassOutageBreaker(() =>
|
|
183
|
+
backend.orders.listOrders(session, 1, 1),
|
|
184
|
+
);
|
|
185
|
+
const newest = orders[0];
|
|
186
|
+
if (newest && Date.parse(newest.placedAt) >= since - skewMs) {
|
|
187
|
+
return newest;
|
|
188
|
+
}
|
|
189
|
+
} catch (error) {
|
|
190
|
+
// the backend may still be slow; look again
|
|
191
|
+
if (!isDomainError(error, "backend_unavailable")) {
|
|
192
|
+
if (!isDomainError(error, "unauthenticated")) throw error;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
return null;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/** The session's newest order if it was placed in the last minutes. */
|
|
200
|
+
async function recentOrder(
|
|
201
|
+
backend: Backend,
|
|
202
|
+
session: BackendSession,
|
|
203
|
+
): Promise<Order | null> {
|
|
204
|
+
try {
|
|
205
|
+
const { orders } = await bypassOutageBreaker(() =>
|
|
206
|
+
backend.orders.listOrders(session, 1, 1),
|
|
207
|
+
);
|
|
208
|
+
const newest = orders[0];
|
|
209
|
+
return newest &&
|
|
210
|
+
Date.parse(newest.placedAt) >= Date.now() - recentMs - skewMs
|
|
211
|
+
? newest
|
|
212
|
+
: null;
|
|
213
|
+
} catch (error) {
|
|
214
|
+
if (isDomainError(error, "unauthenticated")) return null;
|
|
215
|
+
throw error;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
const keysOf = (args: PlaceArgs) => {
|
|
220
|
+
const sessionKey = `${args.backend.name}\u0000${args.visitorKey}`;
|
|
221
|
+
return {
|
|
222
|
+
sessionKey,
|
|
223
|
+
attemptKey: `${sessionKey}\u0000${args.input.attemptId}`,
|
|
224
|
+
};
|
|
225
|
+
};
|
|
226
|
+
|
|
227
|
+
/** The order of an unanswered placement, or `order_status_unknown`. */
|
|
228
|
+
async function resolveUnanswered(
|
|
229
|
+
args: PlaceArgs,
|
|
230
|
+
mark: Unanswered,
|
|
231
|
+
): Promise<PlacedOrder> {
|
|
232
|
+
const { backend, session, urls } = args;
|
|
233
|
+
const { sessionKey, attemptKey } = keysOf(args);
|
|
234
|
+
const order = await findLanded(backend, session, mark.at);
|
|
235
|
+
if (!order) throw statusUnknown();
|
|
236
|
+
unanswered.delete(sessionKey);
|
|
237
|
+
const placed =
|
|
238
|
+
order.paymentStatus === "open"
|
|
239
|
+
? await withPayment(backend, session, order, urls)
|
|
240
|
+
: { order, redirectUrl: null, paymentFailed: false };
|
|
241
|
+
bounded(attempts, attemptKey, { at: mark.at, placed });
|
|
242
|
+
return placed;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
async function place(args: PlaceArgs): Promise<PlacedOrder> {
|
|
246
|
+
const { backend, session, input, gates, urls } = args;
|
|
247
|
+
const { sessionKey, attemptKey } = keysOf(args);
|
|
248
|
+
const attempt = known(attemptKey);
|
|
249
|
+
if (attempt?.placed) return attempt.placed;
|
|
250
|
+
|
|
251
|
+
// an order request of this session got no answer: never place again
|
|
252
|
+
const mark = pending(sessionKey);
|
|
253
|
+
if (mark) return resolveUnanswered(args, mark);
|
|
254
|
+
|
|
255
|
+
const cart = await backend.cart.getCart(session);
|
|
256
|
+
if (!cart.lines.length) {
|
|
257
|
+
// another tab or a reload of a press that placed the order: name
|
|
258
|
+
// it, but never as a new order (no payment, no tracking)
|
|
259
|
+
const recent = await recentOrder(backend, session);
|
|
260
|
+
if (recent) {
|
|
261
|
+
throw new DomainError(
|
|
262
|
+
"order_already_placed",
|
|
263
|
+
"This session placed an order moments ago.",
|
|
264
|
+
{ orderId: recent.id },
|
|
265
|
+
);
|
|
266
|
+
}
|
|
267
|
+
throw new DomainError("cart_empty", "The cart holds nothing to order.");
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
if (!(await gates.acceptsOrders())) {
|
|
271
|
+
throw new DomainError(
|
|
272
|
+
"ordering_closed",
|
|
273
|
+
"The restaurant does not take orders now.",
|
|
274
|
+
);
|
|
275
|
+
}
|
|
276
|
+
const [schedule, methods] = await Promise.all([
|
|
277
|
+
gates.schedule(),
|
|
278
|
+
gates.shippingMethods(),
|
|
279
|
+
]);
|
|
280
|
+
const lead = shippingLeadTime(methods, cart.shippingMethodId);
|
|
281
|
+
checkWishedTime(input.wishedTime, now(), schedule, lead.minutes);
|
|
282
|
+
|
|
283
|
+
const at = Date.now();
|
|
284
|
+
let order: Order;
|
|
285
|
+
try {
|
|
286
|
+
// a slow order (a lock in Shopware) never opens the outage breaker
|
|
287
|
+
order = await bypassOutageBreaker(() =>
|
|
288
|
+
backend.checkout.placeOrder(session, {
|
|
289
|
+
wishedTime: input.wishedTime,
|
|
290
|
+
fulfillment: lead.fulfillment,
|
|
291
|
+
note: input.note,
|
|
292
|
+
}),
|
|
293
|
+
);
|
|
294
|
+
} catch (error) {
|
|
295
|
+
if (!isDomainError(error, "backend_unavailable")) throw error;
|
|
296
|
+
// the request went out and got no answer: the order may exist
|
|
297
|
+
const sent: Unanswered = { at };
|
|
298
|
+
bounded(unanswered, sessionKey, sent);
|
|
299
|
+
return resolveUnanswered(args, sent);
|
|
300
|
+
}
|
|
301
|
+
const placed = await withPayment(backend, session, order, urls);
|
|
302
|
+
bounded(attempts, attemptKey, { at, placed });
|
|
303
|
+
return placed;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
return {
|
|
307
|
+
place,
|
|
308
|
+
/** The answer of an attempt this process placed already, if any. */
|
|
309
|
+
known: (args: PlaceArgs): PlacedOrder | undefined =>
|
|
310
|
+
known(keysOf(args).attemptKey)?.placed,
|
|
311
|
+
};
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
type PlaceArgs = {
|
|
315
|
+
backend: Backend;
|
|
316
|
+
session: BackendSession;
|
|
317
|
+
/** The visitor's session id: attempts of two visitors never mix. */
|
|
318
|
+
visitorKey: string;
|
|
319
|
+
input: PlaceOrderInput;
|
|
320
|
+
gates: PlacementGates;
|
|
321
|
+
urls: (orderId: string) => { success: string; failure: string };
|
|
322
|
+
};
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
* The Store API outage breaker (ADR 0001, step 6 review): when Shopware
|
|
5
|
+
* stops answering, a rendered page waits for about one deadline instead of
|
|
6
|
+
* one per read. It opens only after two requests without an answer
|
|
7
|
+
* (deadline, no connection) within `WINDOW_MS`, with no answer between,
|
|
8
|
+
* and then fails requests at once with 503 for `OUTAGE_OPEN_MS`, letting
|
|
9
|
+
* one probe through `OUTAGE_PROBE_MS` after the last one ended; the first
|
|
10
|
+
* answer (also a refusal or a 5xx) closes it. Other errors (an unreadable answer,
|
|
11
|
+
* a programming error) never count. The order placement and its follow-up
|
|
12
|
+
* look-ups run in `bypassOutageBreaker`: a slow order never takes the shop
|
|
13
|
+
* down, and a look-up for an order that may have landed always asks.
|
|
14
|
+
*/
|
|
15
|
+
export const OUTAGE_WINDOW_MS = 15_000;
|
|
16
|
+
export const OUTAGE_OPEN_MS = 30_000;
|
|
17
|
+
/**
|
|
18
|
+
* While open, one request asks Shopware (a probe) this many ms after the
|
|
19
|
+
* last probe ended, never two at once: a page in an outage pays at most
|
|
20
|
+
* one deadline, and the shop is back soon after Shopware.
|
|
21
|
+
*/
|
|
22
|
+
export const OUTAGE_PROBE_MS = 10_000;
|
|
23
|
+
|
|
24
|
+
let failures: number[] = [];
|
|
25
|
+
let openUntil = 0;
|
|
26
|
+
let lastProbe = 0;
|
|
27
|
+
let probing = false;
|
|
28
|
+
const bypass = new AsyncLocalStorage<boolean>();
|
|
29
|
+
|
|
30
|
+
/** Runs `task` with requests that neither wait for nor open the breaker. */
|
|
31
|
+
export function bypassOutageBreaker<T>(task: () => Promise<T>): Promise<T> {
|
|
32
|
+
return bypass.run(true, task);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export const isOutageBreakerBypassed = (): boolean =>
|
|
36
|
+
bypass.getStore() === true;
|
|
37
|
+
|
|
38
|
+
const NO_ANSWER_CODES = new Set([
|
|
39
|
+
"ECONNREFUSED",
|
|
40
|
+
"ECONNRESET",
|
|
41
|
+
"ENOTFOUND",
|
|
42
|
+
"EAI_AGAIN",
|
|
43
|
+
"ETIMEDOUT",
|
|
44
|
+
"EHOSTUNREACH",
|
|
45
|
+
"ENETUNREACH",
|
|
46
|
+
"UND_ERR_CONNECT_TIMEOUT",
|
|
47
|
+
"UND_ERR_HEADERS_TIMEOUT",
|
|
48
|
+
"UND_ERR_BODY_TIMEOUT",
|
|
49
|
+
"UND_ERR_SOCKET",
|
|
50
|
+
]);
|
|
51
|
+
|
|
52
|
+
type Failure = {
|
|
53
|
+
name?: string;
|
|
54
|
+
code?: string;
|
|
55
|
+
statusCode?: number;
|
|
56
|
+
status?: number;
|
|
57
|
+
response?: { status?: number };
|
|
58
|
+
cause?: unknown;
|
|
59
|
+
} | null;
|
|
60
|
+
|
|
61
|
+
const statusOf = (error: Failure) =>
|
|
62
|
+
error?.statusCode ?? error?.status ?? error?.response?.status;
|
|
63
|
+
|
|
64
|
+
/** A deadline or a connection that failed: Shopware gave no answer. */
|
|
65
|
+
export function isMissingAnswer(error: unknown): boolean {
|
|
66
|
+
if (statusOf(error as Failure) !== undefined) return false;
|
|
67
|
+
for (let value = error as Failure, depth = 0; value && depth < 5; depth++) {
|
|
68
|
+
if (value.name === "TimeoutError" || value.name === "AbortError")
|
|
69
|
+
return true;
|
|
70
|
+
if (value.code && NO_ANSWER_CODES.has(value.code)) return true;
|
|
71
|
+
value = (value.cause ?? null) as Failure;
|
|
72
|
+
}
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** A Store API request behind the breaker. */
|
|
77
|
+
export async function guardedRequest<T>(request: () => Promise<T>): Promise<T> {
|
|
78
|
+
const bypassed = isOutageBreakerBypassed();
|
|
79
|
+
const now = Date.now();
|
|
80
|
+
const open = now < openUntil;
|
|
81
|
+
// while open, one request every few seconds is let through: its answer
|
|
82
|
+
// closes the breaker as soon as Shopware is back
|
|
83
|
+
const probe =
|
|
84
|
+
!bypassed && open && !probing && now - lastProbe >= OUTAGE_PROBE_MS;
|
|
85
|
+
if (probe) probing = true;
|
|
86
|
+
if (!bypassed && open && !probe) {
|
|
87
|
+
throw Object.assign(new Error("The Store API is down."), {
|
|
88
|
+
name: "StoreApiDown",
|
|
89
|
+
statusCode: 503,
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
try {
|
|
93
|
+
const answer = await request();
|
|
94
|
+
if (!bypassed) close();
|
|
95
|
+
return answer;
|
|
96
|
+
} catch (error) {
|
|
97
|
+
if (!bypassed) {
|
|
98
|
+
// a probe without an answer: still down, open for another window
|
|
99
|
+
if (probe && isMissingAnswer(error)) {
|
|
100
|
+
openUntil = Date.now() + OUTAGE_OPEN_MS;
|
|
101
|
+
} else if (isMissingAnswer(error)) noteMissingAnswer();
|
|
102
|
+
else if (statusOf(error as Failure) !== undefined) close();
|
|
103
|
+
}
|
|
104
|
+
throw error;
|
|
105
|
+
} finally {
|
|
106
|
+
if (probe) {
|
|
107
|
+
probing = false;
|
|
108
|
+
lastProbe = Date.now();
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function close() {
|
|
114
|
+
failures = [];
|
|
115
|
+
openUntil = 0;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function noteMissingAnswer() {
|
|
119
|
+
const now = Date.now();
|
|
120
|
+
failures = [...failures.filter((at) => now - at <= OUTAGE_WINDOW_MS), now];
|
|
121
|
+
if (failures.length >= 2) {
|
|
122
|
+
openUntil = now + OUTAGE_OPEN_MS;
|
|
123
|
+
lastProbe = now;
|
|
124
|
+
failures = [];
|
|
125
|
+
}
|
|
126
|
+
}
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import {
|
|
3
|
+
isHostilePath,
|
|
4
|
+
isSafeRedirectTarget,
|
|
5
|
+
mayBeKnownPath,
|
|
6
|
+
pathKey,
|
|
7
|
+
routeTo,
|
|
8
|
+
toggleTrailingSlash,
|
|
9
|
+
type Id,
|
|
10
|
+
type ResolvedRoute,
|
|
11
|
+
} from "../../shared/domain";
|
|
12
|
+
import { createBackoff, createResilientLoader } from "./backoff";
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Path resolution behind the BFF (ADR 0001, step 3): `/api/resolve-path`
|
|
16
|
+
* answers what a storefront path stands for (`NavigationPort.resolvePath`)
|
|
17
|
+
* and shields the backend from vulnerability scanners:
|
|
18
|
+
*
|
|
19
|
+
* 1. Hostile paths (`isHostilePath`) are answered null without any request.
|
|
20
|
+
* 2. Paths no section ever had are answered null from the cached list of
|
|
21
|
+
* known paths (`known-paths`), without a lookup. A failed load lets
|
|
22
|
+
* every path through for the backoff; a list too long to filter by
|
|
23
|
+
* lets every path through for the TTL.
|
|
24
|
+
* 3. Lookups are cached (`path-route`) by the path in lower case, so one
|
|
25
|
+
* entry serves every casing; the answer for the visited spelling is
|
|
26
|
+
* built from the stored section and canonical path (`routeTo`). Only
|
|
27
|
+
* paths that spell the canonical path up to case (or with the trailing
|
|
28
|
+
* slash toggled) and paths under the backend's unlisted prefixes
|
|
29
|
+
* (technical URLs, one per entity) are stored: misses, former paths and
|
|
30
|
+
* loose matches of a collation (accents, trailing spaces) never create
|
|
31
|
+
* an entry, so the cache does not grow per spelling.
|
|
32
|
+
*
|
|
33
|
+
* `swr: false`: a renamed section neither keeps its old route nor 404s at
|
|
34
|
+
* its new path beyond the TTL (`cacheTtl.seoUrl`).
|
|
35
|
+
*/
|
|
36
|
+
const { cacheTtl } = useRuntimeConfig().public.shopBite;
|
|
37
|
+
|
|
38
|
+
type Known = { keys: Set<string>; unlistedPrefixes: readonly string[] };
|
|
39
|
+
|
|
40
|
+
const knownPaths = defineCachedFunction(
|
|
41
|
+
async (): Promise<{
|
|
42
|
+
known: { keys: string[]; unlistedPrefixes: string[] } | null;
|
|
43
|
+
loadedAt: number;
|
|
44
|
+
}> => {
|
|
45
|
+
const known = await useBackend().navigation.listKnownPaths();
|
|
46
|
+
return {
|
|
47
|
+
known: known && {
|
|
48
|
+
keys: [...known.current, ...known.former].map(pathKey),
|
|
49
|
+
unlistedPrefixes: [...known.unlistedPrefixes],
|
|
50
|
+
},
|
|
51
|
+
loadedAt: Date.now(),
|
|
52
|
+
};
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
maxAge: cacheTtl.seoUrl,
|
|
56
|
+
// a new section must not 404 beyond the TTL
|
|
57
|
+
swr: false,
|
|
58
|
+
name: "known-paths",
|
|
59
|
+
getKey: () => "known-paths",
|
|
60
|
+
},
|
|
61
|
+
);
|
|
62
|
+
|
|
63
|
+
/** After a failed load, let every path through for the backoff. */
|
|
64
|
+
const knownBackoff = createBackoff();
|
|
65
|
+
let memo: { loadedAt: number; known: Known | null } | null = null;
|
|
66
|
+
|
|
67
|
+
/** The known paths, null when they cannot filter (too many, or failed). */
|
|
68
|
+
async function currentKnownPaths(): Promise<Known | null> {
|
|
69
|
+
if (knownBackoff.active()) return null;
|
|
70
|
+
try {
|
|
71
|
+
const { known, loadedAt } = await knownPaths();
|
|
72
|
+
if (memo?.loadedAt !== loadedAt) {
|
|
73
|
+
memo = {
|
|
74
|
+
loadedAt,
|
|
75
|
+
known: known && {
|
|
76
|
+
keys: new Set(known.keys),
|
|
77
|
+
unlistedPrefixes: known.unlistedPrefixes,
|
|
78
|
+
},
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
return memo.known;
|
|
82
|
+
} catch (error) {
|
|
83
|
+
knownBackoff.trip();
|
|
84
|
+
console.error("Failed to load the known paths", error);
|
|
85
|
+
return null;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
type Target = { sectionId: Id; canonicalPath: string };
|
|
90
|
+
|
|
91
|
+
const isUnlisted = (path: string, known: Known | null) =>
|
|
92
|
+
Boolean(known?.unlistedPrefixes.some((prefix) => path.startsWith(prefix)));
|
|
93
|
+
|
|
94
|
+
/** Whether an answer may be stored: bounded by the sections, not spellings. */
|
|
95
|
+
function storable(path: string, canonicalPath: string, unlisted: boolean) {
|
|
96
|
+
if (unlisted) return true;
|
|
97
|
+
const canonical = canonicalPath.toLowerCase();
|
|
98
|
+
return (
|
|
99
|
+
path.toLowerCase() === canonical ||
|
|
100
|
+
toggleTrailingSlash(path).toLowerCase() === canonical
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// backs off during an outage; nothing is remembered (the cache keeps the
|
|
105
|
+
// storable answers)
|
|
106
|
+
const lookupLoader = createResilientLoader<ResolvedRoute | null>(() => false);
|
|
107
|
+
|
|
108
|
+
const cachedTarget = defineCachedFunction(
|
|
109
|
+
async (
|
|
110
|
+
path: string,
|
|
111
|
+
unlisted: boolean,
|
|
112
|
+
): Promise<{ target: Target | null; storable: boolean }> => {
|
|
113
|
+
const route = await lookupLoader(path, () =>
|
|
114
|
+
useBackend().navigation.resolvePath(path),
|
|
115
|
+
);
|
|
116
|
+
if (!route) return { target: null, storable: false };
|
|
117
|
+
const canonicalPath =
|
|
118
|
+
route.kind === "redirect" ? route.to : route.canonicalPath;
|
|
119
|
+
// never a redirect to another host (`//host/`), whatever the backend says
|
|
120
|
+
if (!isSafeRedirectTarget(canonicalPath)) {
|
|
121
|
+
console.warn("Left out a route that is no storefront path", {
|
|
122
|
+
path,
|
|
123
|
+
canonicalPath,
|
|
124
|
+
});
|
|
125
|
+
return { target: null, storable: false };
|
|
126
|
+
}
|
|
127
|
+
return {
|
|
128
|
+
target: { sectionId: route.sectionId, canonicalPath },
|
|
129
|
+
storable: storable(path, canonicalPath, unlisted),
|
|
130
|
+
};
|
|
131
|
+
},
|
|
132
|
+
{
|
|
133
|
+
maxAge: cacheTtl.seoUrl,
|
|
134
|
+
// a renamed section must not keep its old route beyond the TTL
|
|
135
|
+
swr: false,
|
|
136
|
+
name: "path-route",
|
|
137
|
+
// unlisted prefixes are case-sensitive: `/Navigation/<id>` (a path)
|
|
138
|
+
// stays apart from the technical `/navigation/<id>`
|
|
139
|
+
getKey: (path: string, unlisted: boolean) =>
|
|
140
|
+
createHash("sha1")
|
|
141
|
+
.update(`${unlisted ? "u" : "p"}:${path.toLowerCase()}`)
|
|
142
|
+
.digest("hex"),
|
|
143
|
+
validate: (entry) =>
|
|
144
|
+
Boolean((entry.value as { storable?: boolean } | undefined)?.storable),
|
|
145
|
+
},
|
|
146
|
+
);
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* What a storefront path stands for, for `/api/resolve-path`. Null: no
|
|
150
|
+
* section has it; a 503 while the backend is paused after a failure.
|
|
151
|
+
*/
|
|
152
|
+
export async function resolveRoute(
|
|
153
|
+
path: string,
|
|
154
|
+
): Promise<ResolvedRoute | null> {
|
|
155
|
+
if (isHostilePath(path)) return null;
|
|
156
|
+
const known = await currentKnownPaths();
|
|
157
|
+
if (!mayBeKnownPath(path, known)) return null;
|
|
158
|
+
const { target } = await cachedTarget(path, isUnlisted(path, known));
|
|
159
|
+
return target ? routeTo(path, target.sectionId, target.canonicalPath) : null;
|
|
160
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { H3Event } from "h3";
|
|
2
|
+
|
|
3
|
+
/** Bodies of the storefront's small forms (wishlist, contact) stay below. */
|
|
4
|
+
export const SMALL_BODY_BYTES = 64 * 1024;
|
|
5
|
+
|
|
6
|
+
const tooLarge = () =>
|
|
7
|
+
createError({
|
|
8
|
+
statusCode: 413,
|
|
9
|
+
statusMessage: "Payload Too Large",
|
|
10
|
+
data: { code: "payload_too_large" },
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* A JSON body of at most `maxBytes`, refused before it is buffered (the
|
|
15
|
+
* declared length) or while it streams in (413 `payload_too_large`). A
|
|
16
|
+
* body that is no JSON is answered as no body (the route's checks refuse
|
|
17
|
+
* it). Without a stream (tests) the parsed body is taken as it is.
|
|
18
|
+
*/
|
|
19
|
+
export async function readSmallBody(
|
|
20
|
+
event: H3Event,
|
|
21
|
+
maxBytes: number = SMALL_BODY_BYTES,
|
|
22
|
+
): Promise<unknown> {
|
|
23
|
+
const declared = Number(getRequestHeader(event, "content-length") ?? 0);
|
|
24
|
+
if (declared > maxBytes) throw tooLarge();
|
|
25
|
+
const request = event.node?.req as
|
|
26
|
+
(AsyncIterable<Uint8Array> & object) | undefined;
|
|
27
|
+
if (!request || typeof request[Symbol.asyncIterator] !== "function") {
|
|
28
|
+
return readBody(event);
|
|
29
|
+
}
|
|
30
|
+
const chunks: Uint8Array[] = [];
|
|
31
|
+
let size = 0;
|
|
32
|
+
for await (const chunk of request) {
|
|
33
|
+
size += chunk.length;
|
|
34
|
+
if (size > maxBytes) throw tooLarge();
|
|
35
|
+
chunks.push(chunk);
|
|
36
|
+
}
|
|
37
|
+
if (!size) return undefined;
|
|
38
|
+
try {
|
|
39
|
+
return JSON.parse(Buffer.concat(chunks).toString("utf8"));
|
|
40
|
+
} catch {
|
|
41
|
+
return undefined;
|
|
42
|
+
}
|
|
43
|
+
}
|