@shopware/composables 0.0.0-canary-20250116155626

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.
Files changed (137) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +172 -0
  3. package/composables/index.ts +1 -0
  4. package/dist/index.d.mts +2239 -0
  5. package/dist/index.d.ts +2239 -0
  6. package/dist/index.mjs +2686 -0
  7. package/nuxt.config.ts +14 -0
  8. package/package.json +83 -0
  9. package/src/_test/index.ts +1 -0
  10. package/src/_test/useSetup.ts +43 -0
  11. package/src/cms/README.md +9 -0
  12. package/src/cms/cmsAssociations.ts +28 -0
  13. package/src/cms/index.ts +2 -0
  14. package/src/cms/useCmsElementConfig.test.ts +145 -0
  15. package/src/cms/useCmsElementConfig.ts +29 -0
  16. package/src/cms/useCmsElementImage.test.ts +179 -0
  17. package/src/cms/useCmsElementImage.ts +103 -0
  18. package/src/cms/useCmsElementProductBox.test.ts +15 -0
  19. package/src/cms/useCmsElementProductBox.ts +25 -0
  20. package/src/createShopwareContext.test.ts +23 -0
  21. package/src/createShopwareContext.ts +43 -0
  22. package/src/devtools/plugin.ts +201 -0
  23. package/src/helpers/ContextError.spec.ts +20 -0
  24. package/src/helpers/ContextError.ts +6 -0
  25. package/src/index.test.ts +54 -0
  26. package/src/index.ts +111 -0
  27. package/src/mocks/Cart.ts +774 -0
  28. package/src/mocks/CmsPage.ts +544 -0
  29. package/src/mocks/CmsProductBox.ts +377 -0
  30. package/src/mocks/Configurator.ts +123 -0
  31. package/src/mocks/Country.ts +184 -0
  32. package/src/mocks/CrossSellingResponse.ts +552 -0
  33. package/src/mocks/LandingPage.ts +62 -0
  34. package/src/mocks/Menu.ts +598 -0
  35. package/src/mocks/Order.ts +1151 -0
  36. package/src/mocks/Product.ts +473 -0
  37. package/src/mocks/ProductTierPrices.ts +771 -0
  38. package/src/mocks/Salutations.ts +51 -0
  39. package/src/mocks/Search.ts +518 -0
  40. package/src/types/cmsBlockTypes.ts +80 -0
  41. package/src/types/cmsElementTypes.ts +381 -0
  42. package/src/types/cmsSectionTypes.ts +15 -0
  43. package/src/types/index.ts +52 -0
  44. package/src/useAddToCart/useAddToCart.test.ts +39 -0
  45. package/src/useAddToCart/useAddToCart.ts +90 -0
  46. package/src/useAddress/useAddress.test.ts +134 -0
  47. package/src/useAddress/useAddress.ts +202 -0
  48. package/src/useB2bQuoteManagement/useB2bQuoteManagement.test.ts +137 -0
  49. package/src/useB2bQuoteManagement/useB2bQuoteManagement.ts +225 -0
  50. package/src/useBreadcrumbs/useBreadcrumbs.test.ts +71 -0
  51. package/src/useBreadcrumbs/useBreadcrumbs.ts +89 -0
  52. package/src/useCart/useCart.test.ts +251 -0
  53. package/src/useCart/useCart.ts +314 -0
  54. package/src/useCartErrorParamsResolver/useCartErrorParamsResolver.test.ts +122 -0
  55. package/src/useCartErrorParamsResolver/useCartErrorParamsResolver.ts +80 -0
  56. package/src/useCartItem/useCartItem.test.ts +168 -0
  57. package/src/useCartItem/useCartItem.ts +157 -0
  58. package/src/useCartNotification/useCartNotification.test.ts +129 -0
  59. package/src/useCartNotification/useCartNotification.ts +63 -0
  60. package/src/useCategory/useCategory.test.ts +14 -0
  61. package/src/useCategory/useCategory.ts +32 -0
  62. package/src/useCategorySearch/useCategorySearch.test.ts +98 -0
  63. package/src/useCategorySearch/useCategorySearch.ts +81 -0
  64. package/src/useCheckout/useCheckout.test.ts +104 -0
  65. package/src/useCheckout/useCheckout.ts +163 -0
  66. package/src/useCmsBlock/useCmsBlock.test.ts +25 -0
  67. package/src/useCmsBlock/useCmsBlock.ts +36 -0
  68. package/src/useCmsMeta/useCmsMeta.test.ts +17 -0
  69. package/src/useCmsMeta/useCmsMeta.ts +46 -0
  70. package/src/useCmsSection/useCmsSection.test.ts +18 -0
  71. package/src/useCmsSection/useCmsSection.ts +39 -0
  72. package/src/useCmsTranslations/useCmsTranslations.test.ts +10 -0
  73. package/src/useCmsTranslations/useCmsTranslations.ts +9 -0
  74. package/src/useContext/useContext.ts +36 -0
  75. package/src/useCountries/useCountries.test.ts +64 -0
  76. package/src/useCountries/useCountries.ts +64 -0
  77. package/src/useCustomerOrders/useCustomerOrders.test.ts +47 -0
  78. package/src/useCustomerOrders/useCustomerOrders.ts +89 -0
  79. package/src/useCustomerPassword/useCustomerPassword.test.ts +164 -0
  80. package/src/useCustomerPassword/useCustomerPassword.ts +57 -0
  81. package/src/useDefaultOrderAssociations/useDefaultOrderAssociations.test.ts +41 -0
  82. package/src/useDefaultOrderAssociations/useDefaultOrderAssociations.ts +45 -0
  83. package/src/useInternationalization/useInternationalization.test.ts +155 -0
  84. package/src/useInternationalization/useInternationalization.ts +159 -0
  85. package/src/useLandingSearch/useLandingSearch.test.ts +30 -0
  86. package/src/useLandingSearch/useLandingSearch.ts +58 -0
  87. package/src/useListing/useListing.test.ts +249 -0
  88. package/src/useListing/useListing.ts +592 -0
  89. package/src/useLocalWishlist/useLocalWishlist.test.ts +54 -0
  90. package/src/useLocalWishlist/useLocalWishlist.ts +95 -0
  91. package/src/useNavigation/useNavigation.test.ts +68 -0
  92. package/src/useNavigation/useNavigation.ts +83 -0
  93. package/src/useNavigationContext/useNavigationContext.test.ts +48 -0
  94. package/src/useNavigationContext/useNavigationContext.ts +39 -0
  95. package/src/useNavigationSearch/useNavigationSearch.test.ts +110 -0
  96. package/src/useNavigationSearch/useNavigationSearch.ts +60 -0
  97. package/src/useNewsletter/useNewsletter.test.ts +55 -0
  98. package/src/useNewsletter/useNewsletter.ts +109 -0
  99. package/src/useNotifications/useNotifications.test.ts +56 -0
  100. package/src/useNotifications/useNotifications.ts +119 -0
  101. package/src/useOrderDetails/useOrderDetails.test.ts +224 -0
  102. package/src/useOrderDetails/useOrderDetails.ts +329 -0
  103. package/src/useOrderPayment/useOrderPayment.test.ts +80 -0
  104. package/src/useOrderPayment/useOrderPayment.ts +119 -0
  105. package/src/usePrice/usePrice.test.ts +97 -0
  106. package/src/usePrice/usePrice.ts +106 -0
  107. package/src/useProduct/useProduct.test.ts +36 -0
  108. package/src/useProduct/useProduct.ts +56 -0
  109. package/src/useProductAssociations/useProductAssociations.spec.ts +105 -0
  110. package/src/useProductAssociations/useProductAssociations.ts +107 -0
  111. package/src/useProductConfigurator/useProductConfigurator.spec.ts +77 -0
  112. package/src/useProductConfigurator/useProductConfigurator.ts +134 -0
  113. package/src/useProductPrice/useProductPrice.test.ts +126 -0
  114. package/src/useProductPrice/useProductPrice.ts +131 -0
  115. package/src/useProductReviews/useProductReviews.test.ts +45 -0
  116. package/src/useProductReviews/useProductReviews.ts +80 -0
  117. package/src/useProductSearch/useProductSearch.test.ts +37 -0
  118. package/src/useProductSearch/useProductSearch.ts +58 -0
  119. package/src/useProductSearchSuggest/useProductSearchSuggest.test.ts +19 -0
  120. package/src/useProductSearchSuggest/useProductSearchSuggest.ts +67 -0
  121. package/src/useProductWishlist/useProductWishlist.test.ts +63 -0
  122. package/src/useProductWishlist/useProductWishlist.ts +74 -0
  123. package/src/useSalutations/useSalutations.spec.ts +37 -0
  124. package/src/useSalutations/useSalutations.ts +55 -0
  125. package/src/useSessionContext/useSessionContext.test.ts +221 -0
  126. package/src/useSessionContext/useSessionContext.ts +281 -0
  127. package/src/useShopwareContext/useShopwareContext.test.ts +8 -0
  128. package/src/useShopwareContext/useShopwareContext.ts +31 -0
  129. package/src/useSyncWishlist/useSyncWishlist.test.ts +117 -0
  130. package/src/useSyncWishlist/useSyncWishlist.ts +117 -0
  131. package/src/useUrlResolver/useUrlResolver.test.ts +25 -0
  132. package/src/useUrlResolver/useUrlResolver.ts +31 -0
  133. package/src/useUser/useUser.test.ts +290 -0
  134. package/src/useUser/useUser.ts +321 -0
  135. package/src/useWishlist/useWishlist.test.ts +178 -0
  136. package/src/useWishlist/useWishlist.ts +128 -0
  137. package/types/api-types.ts +11 -0
@@ -0,0 +1,381 @@
1
+ import type { CSSProperties } from "vue";
2
+ import type { Schemas } from "#shopware";
3
+
4
+ export type SourceType = "static" | "mapped";
5
+
6
+ export type Position = "left" | "center" | "right";
7
+
8
+ export type DisplayMode =
9
+ | Exclude<CSSProperties["objectFit"], undefined>
10
+ | "standard";
11
+
12
+ export type BoxLayout = "standard" | "image" | "minimal";
13
+
14
+ export type VerticalAlign = "flex-start" | "center" | "flex-end" | "";
15
+
16
+ export type ElementConfig<VALUE_TYPE> = {
17
+ source: SourceType;
18
+ value: VALUE_TYPE;
19
+ };
20
+
21
+ type TextElementConfig = {
22
+ content: ElementConfig<string>;
23
+ verticalAlign: ElementConfig<VerticalAlign>;
24
+ };
25
+
26
+ type ElementFieldConfig = {
27
+ name: string;
28
+ source: SourceType;
29
+ value: string | null;
30
+ apiAlias: string;
31
+ };
32
+
33
+ // Text
34
+ export type CmsElementText = Schemas["CmsSlot"] & {
35
+ type: "text" | typeof String;
36
+ slot: typeof String;
37
+ config: TextElementConfig;
38
+ fieldConfig: ElementFieldConfig[];
39
+ data: {
40
+ content: string;
41
+ apiAlias: "cms_text";
42
+ };
43
+ translated: {
44
+ config: TextElementConfig;
45
+ };
46
+ };
47
+
48
+ // Image
49
+
50
+ type ImageElementConfig = {
51
+ url: ElementConfig<string>;
52
+ media: ElementConfig<string>;
53
+ newTab: ElementConfig<boolean>;
54
+ product: ElementConfig<string>;
55
+ boxLayout: ElementConfig<BoxLayout>;
56
+ displayMode: ElementConfig<DisplayMode>;
57
+ minHeight: ElementConfig<string | number>;
58
+ verticalAlign: ElementConfig<VerticalAlign>;
59
+ horizontalAlign: ElementConfig<VerticalAlign>;
60
+ };
61
+
62
+ export type CmsElementImage = Schemas["CmsSlot"] & {
63
+ type: "image";
64
+ config: ImageElementConfig;
65
+ data: {
66
+ mediaId: string;
67
+ url: string;
68
+ newTab: boolean;
69
+ apiAlias: "cms_image";
70
+ media: Schemas["Media"];
71
+ };
72
+ };
73
+
74
+ // Image Slider
75
+ export type SliderElementConfig = {
76
+ minWidth?: ElementConfig<string>;
77
+ minHeight: ElementConfig<string | number>;
78
+ verticalAlign?: ElementConfig<VerticalAlign>;
79
+ displayMode?: ElementConfig<"standard" | "cover" | "contain">;
80
+ navigationDots?: ElementConfig<"outside" | "inside" | "">;
81
+ navigationArrows?: ElementConfig<"outside" | "inside" | "">;
82
+ };
83
+
84
+ type ImageSliderElementConfig = ImageElementConfig &
85
+ SliderElementConfig & {
86
+ sliderItems: ElementConfig<
87
+ Array<{
88
+ url: null | string;
89
+ newTab: boolean;
90
+ mediaId: string;
91
+ mediaUrl: string;
92
+ }>
93
+ >;
94
+ };
95
+
96
+ export type CmsElementImageSlider = Schemas["CmsSlot"] & {
97
+ type: "image-slider";
98
+ config: ImageSliderElementConfig;
99
+ data: {
100
+ apiAlias: "cms_image_slider";
101
+ navigation: unknown;
102
+ sliderItems: {
103
+ url: string;
104
+ newTab: boolean;
105
+ media: Schemas["Media"];
106
+ mediaId: string;
107
+ apiAlias: "cms_image_slider_item";
108
+ }[];
109
+ };
110
+ };
111
+
112
+ // Image Gallery
113
+
114
+ type ImageGalleryElementConfig = ImageSliderElementConfig & {
115
+ galleryPosition: ElementConfig<Position>;
116
+ magnifierOverGallery: ElementConfig<boolean>;
117
+ keepAspectRatioOnZoom: ElementConfig<boolean>;
118
+ fullScreen: ElementConfig<boolean>;
119
+ zoom: ElementConfig<boolean>;
120
+ };
121
+
122
+ export type CmsElementImageGallery = Schemas["CmsSlot"] & {
123
+ type: "image-gallery";
124
+ config: ImageGalleryElementConfig;
125
+ data: {
126
+ // actually a CmsElementImageSlider data, consider unify this
127
+ apiAlias: "cms_image_slider";
128
+ navigation: unknown;
129
+ sliderItems: Array<
130
+ | {
131
+ url: null | string;
132
+ newTab: boolean;
133
+ media: Schemas["Media"];
134
+ apiAlias: "cms_image_slider_item";
135
+ }
136
+ | Schemas["ProductMedia"]
137
+ >;
138
+ };
139
+ };
140
+
141
+ // YouTube video
142
+ type YouTubeVideoElementConfig = {
143
+ end: ElementConfig<string>;
144
+ url: ElementConfig<string>;
145
+ loop: ElementConfig<boolean>;
146
+ start: ElementConfig<string>;
147
+ videoID: ElementConfig<string>;
148
+ autoPlay: ElementConfig<boolean>;
149
+ displayMode: ElementConfig<DisplayMode>;
150
+ previewMedia: ElementConfig<string>;
151
+ showControls: ElementConfig<boolean>;
152
+ needsConfirmation: ElementConfig<boolean>;
153
+ advancePrivacyMode: ElementConfig<boolean>;
154
+ };
155
+ export type CmsElementYoutubeVideo = Schemas["CmsSlot"] & {
156
+ type: "youtube-video";
157
+ config: YouTubeVideoElementConfig;
158
+ data: {
159
+ mediaId: string | null;
160
+ url: null | string;
161
+ newTab: null | boolean;
162
+ media: null | Schemas["Media"];
163
+ apiAlias: "cms_image";
164
+ };
165
+ };
166
+
167
+ // Vimeo video
168
+ type VimeoVideoElementConfig = {
169
+ loop: ElementConfig<boolean>;
170
+ color: ElementConfig<string>;
171
+ title: ElementConfig<boolean>;
172
+ mute: ElementConfig<boolean>;
173
+ byLine: ElementConfig<boolean>;
174
+ videoID: ElementConfig<string>;
175
+ autoplay: ElementConfig<boolean>;
176
+ controls: ElementConfig<boolean>;
177
+ portrait: ElementConfig<boolean>;
178
+ doNotTrack: ElementConfig<boolean>;
179
+ previewMedia: ElementConfig<string>;
180
+ needsConfirmation: ElementConfig<boolean>;
181
+ };
182
+
183
+ export type CmsElementVimeoVideo = Schemas["CmsSlot"] & {
184
+ type: "vimeo-video";
185
+ config: VimeoVideoElementConfig;
186
+ data: {
187
+ mediaId: string | null; // actually a CmsElementImage data, consider unify this
188
+ url: null | string;
189
+ newTab: null | boolean;
190
+ media: null | Schemas["Media"];
191
+ apiAlias: "cms_image";
192
+ };
193
+ };
194
+
195
+ // Product Box
196
+
197
+ type ProductBoxElementConfig = {
198
+ boxLayout: ElementConfig<BoxLayout>;
199
+ product: ElementConfig<string>;
200
+ };
201
+
202
+ export type CmsElementProductBox = Schemas["CmsSlot"] & {
203
+ type: "product-box";
204
+ config: ProductBoxElementConfig;
205
+ data: {
206
+ productId: string;
207
+ product: Schemas["Product"];
208
+ apiAlias: "cms_product_box";
209
+ };
210
+ };
211
+
212
+ // Product Slider
213
+
214
+ type ProductSliderElementConfig = {
215
+ title: ElementConfig<string>;
216
+ border: ElementConfig<boolean>;
217
+ rotate: ElementConfig<boolean>;
218
+ products: ElementConfig<string[]>;
219
+ boxLayout: ElementConfig<BoxLayout>;
220
+ elMinWidth: ElementConfig<string>;
221
+ navigation: ElementConfig<boolean>;
222
+ displayMode: ElementConfig<DisplayMode>;
223
+ verticalAlign: ElementConfig<VerticalAlign>;
224
+ productStream: ElementConfig<string>;
225
+ productStreamSorting: ElementConfig<string>;
226
+ };
227
+
228
+ export type CmsElementProductSlider = Schemas["CmsSlot"] & {
229
+ type: "product-slider";
230
+ config: ProductSliderElementConfig;
231
+ data: {
232
+ apiAlias: "cms_product_slider";
233
+ products: Schemas["Product"][];
234
+ };
235
+ };
236
+
237
+ // Sidebar Filter (Sidebar filter)
238
+
239
+ type CmsSidebarFilterElementConfig = {
240
+ boxLayout: ElementConfig<BoxLayout>;
241
+ content: ElementConfig<string>;
242
+ displayMode: ElementConfig<DisplayMode>;
243
+ media: ElementConfig<string>;
244
+ minHeight: ElementConfig<string | number>;
245
+ newTab: ElementConfig<boolean>;
246
+ url: ElementConfig<string>;
247
+ verticalAlign: ElementConfig<VerticalAlign>;
248
+ };
249
+
250
+ export type CmsElementSidebarFilter = Schemas["CmsSlot"] & {
251
+ type: "sidebar-filter";
252
+ config: CmsSidebarFilterElementConfig;
253
+ };
254
+
255
+ // Product Listing
256
+
257
+ type CmsProductListingElementConfig = {
258
+ filters: ElementConfig<string>;
259
+ boxLayout: ElementConfig<BoxLayout>;
260
+ showSorting: ElementConfig<boolean>;
261
+ defaultSorting: ElementConfig<string>;
262
+ useCustomSorting: ElementConfig<boolean>;
263
+ availableSortings: ElementConfig<string[]>;
264
+ propertyWhitelist: ElementConfig<string[]>;
265
+ };
266
+
267
+ export type CmsElementProductListing = Schemas["CmsSlot"] & {
268
+ type: "product-listing";
269
+ config: CmsProductListingElementConfig;
270
+ data: {
271
+ apiAlias: "cms_product_listing";
272
+ listing: Schemas["ProductListingResult"];
273
+ };
274
+ };
275
+
276
+ // Category Navigation
277
+
278
+ type CategoryNavigationElementConfig = unknown;
279
+
280
+ export type CmsElementCategoryNavigation = Schemas["CmsSlot"] & {
281
+ type: "category-navigation";
282
+ config: CategoryNavigationElementConfig;
283
+ };
284
+
285
+ type ProductDescriptionReviewsElementConfig = {
286
+ product: ElementConfig<string>;
287
+ alignment: ElementConfig<VerticalAlign>;
288
+ };
289
+
290
+ export type CmsElementProductDescriptionReviews = Schemas["CmsSlot"] & {
291
+ type: "product-description-reviews";
292
+ config: ProductDescriptionReviewsElementConfig;
293
+ data: {
294
+ productId: null | string;
295
+ ratingSuccess: boolean;
296
+ product?: Schemas["Product"];
297
+ reviews: {
298
+ elements: Schemas["ProductReview"][];
299
+ };
300
+ apiAlias: "cms_product_description_reviews";
301
+ };
302
+ };
303
+
304
+ // Buy Box
305
+
306
+ type BuyBoxElementConfig = ProductDescriptionReviewsElementConfig;
307
+ // buy box has the same interface in data as product description reviews! unify later
308
+ export type CmsElementBuyBox = Schemas["CmsSlot"] & {
309
+ type: "buy-box";
310
+ config: BuyBoxElementConfig;
311
+ data: {
312
+ configuratorSettings: Schemas["PropertyGroup"][] | null;
313
+ productId: null | string;
314
+ ratingSuccess: boolean;
315
+ product?: Schemas["Product"];
316
+ reviews: Schemas["ProductReview"][];
317
+ apiAlias: "cms_product_description_reviews";
318
+ };
319
+ };
320
+
321
+ // Cross Selling
322
+
323
+ type CrossSellingElementConfig = {
324
+ product: ElementConfig<string>;
325
+ alignment: ElementConfig<VerticalAlign>;
326
+ boxLayout: ElementConfig<BoxLayout>;
327
+ elMinWidth: ElementConfig<string>;
328
+ displayMode: ElementConfig<DisplayMode>;
329
+ };
330
+
331
+ export type CmsElementCrossSelling = Schemas["CmsSlot"] & {
332
+ type: "cross-selling";
333
+ config: CrossSellingElementConfig;
334
+ data: {
335
+ apiAlias: "cms_cross_selling";
336
+ crossSellings: Schemas["CrossSellingElement"][];
337
+ };
338
+ };
339
+
340
+ // Form (newsletter and contact form types)
341
+
342
+ type FormElementConfig = {
343
+ type: ElementConfig<"contact" | "newsletter">;
344
+ title: ElementConfig<string>;
345
+ mailReceiver: ElementConfig<string[]>;
346
+ confirmationText: ElementConfig<string>;
347
+ defaultMailReceiver: ElementConfig<boolean>;
348
+ };
349
+
350
+ export type CmsElementForm = Schemas["CmsSlot"] & {
351
+ type: "form";
352
+ config: FormElementConfig;
353
+ data: Schemas["Salutation"][];
354
+ };
355
+
356
+ // Product Name
357
+ export type CmsElementProductName = Schemas["CmsSlot"] & {
358
+ type: "product-name";
359
+ config: TextElementConfig;
360
+ fieldConfig: ElementFieldConfig[];
361
+ data: {
362
+ content: string;
363
+ apiAlias: "cms_text";
364
+ };
365
+ translated: {
366
+ config: TextElementConfig;
367
+ };
368
+ };
369
+
370
+ // Manufacturer Logo
371
+ export type CmsElementManufacturerLogo = Schemas["CmsSlot"] & {
372
+ type: "manufacturer-logo";
373
+ config: ImageElementConfig;
374
+ data: {
375
+ mediaId: string;
376
+ url: string;
377
+ newTab: boolean;
378
+ apiAlias: "cms_manufacturer_logo";
379
+ media: Schemas["Media"];
380
+ };
381
+ };
@@ -0,0 +1,15 @@
1
+ import type { Schemas } from "#shopware";
2
+
3
+ export type CmsSectionBlock<
4
+ TYPE extends Schemas["CmsSection"]["type"],
5
+ SECTION_POSITIONS,
6
+ > = Schemas["CmsSection"] & {
7
+ type: TYPE;
8
+ blocks: Array<Schemas["CmsBlock"] & { sectionPosition: SECTION_POSITIONS }>;
9
+ };
10
+
11
+ // Section Default can have only one container for blocks: main (full page)
12
+ export type CmsSectionDefault = CmsSectionBlock<"default", "main">;
13
+
14
+ // Section Sidebar can have blocks in two different containers: sidebar (left), and main (right)
15
+ export type CmsSectionSidebar = CmsSectionBlock<"sidebar", "sidebar" | "main">;
@@ -0,0 +1,52 @@
1
+ // import type {
2
+ // ProductResponse as LegacyProductResponse,
3
+ // LandingPage,
4
+ // Category,
5
+ // CmsPage,
6
+ // } from "@shopware-pwa/types";
7
+
8
+ export * from "./cmsBlockTypes";
9
+ export * from "./cmsElementTypes";
10
+ export * from "./cmsSectionTypes";
11
+
12
+ /**
13
+ * API responses' types
14
+ * A combination of legacy response types and new response types
15
+ * @TODO: to be unified in the final types package
16
+ */
17
+ // export type CategoryResponse = Category;
18
+ // export type ProductResponse = LegacyProductResponse;
19
+ // export type LandingPageResponse = LandingPage;
20
+ /**
21
+ * Types for internal usage.
22
+ *
23
+ * @TODO: rethink the format of the types
24
+ */
25
+
26
+ // export const PRODUCT_ROUTE_NAME = "frontend.detail.page";
27
+ // export const CATEGORY_ROUTE_NAME = "frontend.navigation.page";
28
+ // export const LANDING_PAGE_ROUTE_NAME = "frontend.landing.page";
29
+
30
+ // export type ResourceType =
31
+ // | typeof PRODUCT_ROUTE_NAME
32
+ // | typeof CATEGORY_ROUTE_NAME
33
+ // | typeof LANDING_PAGE_ROUTE_NAME;
34
+
35
+ // type CategoryCmsResult = {
36
+ // category: CategoryResponse;
37
+ // };
38
+
39
+ // type ProductCmsResult = ProductResponse;
40
+
41
+ // type LandingPageCmsResult = {
42
+ // landingPage: LandingPage;
43
+ // };
44
+
45
+ // export type SearchCmsResult = (
46
+ // | CategoryCmsResult
47
+ // | ProductCmsResult
48
+ // | LandingPageCmsResult
49
+ // ) & {
50
+ // cmsPage: null | CmsPage;
51
+ // resourceType: ResourceType;
52
+ // };
@@ -0,0 +1,39 @@
1
+ import { describe, expect, it, vi } from "vitest";
2
+ import { ref } from "vue";
3
+ import { useSetup } from "../_test";
4
+ import ProductMocked from "../mocks/Product";
5
+ import { useAddToCart } from "./useAddToCart";
6
+
7
+ describe("useB2bQuoteManagement", () => {
8
+ vi.mock("../useCart/useCart.ts", () => ({
9
+ useCart() {
10
+ return {
11
+ addProduct: () => ({}),
12
+ cartItems: {
13
+ value: [
14
+ {
15
+ referencedId: ProductMocked.id,
16
+ good: true,
17
+ quantity: 1,
18
+ },
19
+ ],
20
+ },
21
+ };
22
+ },
23
+ }));
24
+
25
+ it("add product to cart", async () => {
26
+ const { vm } = await useSetup(() => useAddToCart(ref(ProductMocked)));
27
+
28
+ await vm.addToCart();
29
+ expect(vm.quantity).toBe(1);
30
+ expect(vm.count).toBe(1);
31
+ expect(vm.isInCart).toBe(true);
32
+ });
33
+
34
+ it("add product to cart - empty cart", async () => {
35
+ const { vm } = await useSetup(() => useAddToCart(ref()));
36
+ expect(vm.count).toBe(0);
37
+ await expect(vm.addToCart()).rejects.toThrow();
38
+ });
39
+ });
@@ -0,0 +1,90 @@
1
+ import { computed, ref, unref } from "vue";
2
+ import type { ComputedRef, Ref } from "vue";
3
+ import { useCart } from "#imports";
4
+ import type { Schemas } from "#shopware";
5
+ export type UseAddToCartReturn = {
6
+ /**
7
+ * Add to cart method
8
+ * @type {function}
9
+ */
10
+ addToCart(): Promise<Schemas["Cart"]>;
11
+ /**
12
+ * If you want to add more that 1 product set quantity before invoking `addToCart`
13
+ */
14
+ quantity: Ref<number>;
15
+ /**
16
+ * Returns product count in stock
17
+ */
18
+ getStock: ComputedRef<number | undefined>;
19
+ /**
20
+ * Returns product count in available stock
21
+ */
22
+ getAvailableStock: ComputedRef<number | undefined>;
23
+ /**
24
+ * Flag if product is already in cart
25
+ */
26
+ isInCart: ComputedRef<boolean>;
27
+ /**
28
+ * count of the product quantity already in the cart
29
+ */
30
+ count: ComputedRef<number>;
31
+ };
32
+
33
+ /**
34
+ * Composable to manage adding product to cart
35
+ *
36
+ * With this composable you can:
37
+ * - Add product to cart
38
+ * - Get product quantity
39
+ * - Get product stock
40
+ * - Get product available stock
41
+ * - Check if product is in cart
42
+ * - Get product count in cart
43
+ *
44
+ * @public
45
+ * @category Cart & Checkout
46
+ */
47
+ export function useAddToCart(
48
+ product: Ref<Schemas["Product"] | undefined>,
49
+ ): UseAddToCartReturn {
50
+ const _product = computed(() => unref(product));
51
+
52
+ const { addProduct, cartItems } = useCart();
53
+ const quantity: Ref<number> = ref(1);
54
+
55
+ async function addToCart(): Promise<Schemas["Cart"]> {
56
+ if (!_product.value?.id) throw new Error("Product id is required");
57
+
58
+ const addToCartResponse = await addProduct({
59
+ id: _product.value?.id,
60
+ quantity: quantity.value,
61
+ });
62
+ quantity.value = 1;
63
+ return addToCartResponse;
64
+ }
65
+
66
+ const getStock = computed(() => _product.value?.stock);
67
+
68
+ const getAvailableStock = computed(() => _product.value?.availableStock);
69
+
70
+ const isInCart = computed(() =>
71
+ cartItems.value.some(
72
+ (item: Schemas["LineItem"]) => item.referencedId === _product.value?.id,
73
+ ),
74
+ );
75
+
76
+ return {
77
+ addToCart,
78
+ quantity,
79
+ getStock,
80
+ getAvailableStock,
81
+ isInCart,
82
+ count: computed(
83
+ () =>
84
+ cartItems.value.find(
85
+ (item: Schemas["LineItem"]) =>
86
+ item.referencedId === _product.value?.id,
87
+ )?.quantity || 0,
88
+ ),
89
+ };
90
+ }
@@ -0,0 +1,134 @@
1
+ import { ApiClientError } from "@shopware/api-client";
2
+ import type { FetchResponse } from "ofetch";
3
+ import { describe, expect, it, vi } from "vitest";
4
+ import type { Schemas } from "#shopware";
5
+ import { useSetup } from "../_test";
6
+ import { useAddress } from "./useAddress";
7
+
8
+ const MOCKED_ADDRESS = {
9
+ countryId: "6777d83705454d078fc9a7419296c7dc",
10
+ countryStateId: "",
11
+ salutationId: "d5e543063dd642b48ef94b02d68e5785",
12
+ firstName: "Test",
13
+ lastName: "Test",
14
+ zipcode: "123-1234",
15
+ city: "city test",
16
+ street: "test street address",
17
+ };
18
+
19
+ describe("useAddress", () => {
20
+ it("load customer address", async () => {
21
+ const { vm, injections } = await useSetup(useAddress);
22
+ injections.apiClient.invoke.mockResolvedValue({ data: {} });
23
+ await vm.loadCustomerAddresses();
24
+
25
+ expect(injections.apiClient.invoke).toHaveBeenCalledWith(
26
+ expect.stringContaining("listAddress"),
27
+ expect.objectContaining({}),
28
+ );
29
+ });
30
+
31
+ it("load customer address - error", async () => {
32
+ const { vm } = await useSetup(useAddress, {
33
+ apiClient: {
34
+ invoke: vi.fn().mockImplementation(() => {
35
+ throw new ApiClientError({
36
+ status: 403,
37
+ _data: {
38
+ errors: [
39
+ {
40
+ code: "TEST",
41
+ status: 403,
42
+ detail: "Forbidden",
43
+ },
44
+ ],
45
+ },
46
+ } as unknown as FetchResponse<{ errors: Array<{ title: string }> }>);
47
+ }),
48
+ },
49
+ });
50
+
51
+ await expect(vm.loadCustomerAddresses()).rejects.toThrowError();
52
+ });
53
+
54
+ it("create address", async () => {
55
+ const { vm, injections } = await useSetup(useAddress);
56
+ injections.apiClient.invoke.mockResolvedValue({ data: {} });
57
+ // Check types
58
+ await vm.createCustomerAddress(
59
+ MOCKED_ADDRESS as Schemas["CustomerAddress"],
60
+ );
61
+
62
+ expect(injections.apiClient.invoke).toHaveBeenCalledWith(
63
+ expect.stringContaining("createCustomerAddress"),
64
+ expect.objectContaining({ body: MOCKED_ADDRESS }),
65
+ );
66
+ });
67
+
68
+ it("update address", async () => {
69
+ const { vm, injections } = await useSetup(useAddress);
70
+ injections.apiClient.invoke.mockResolvedValue({ data: {} });
71
+ await vm.updateCustomerAddress(
72
+ MOCKED_ADDRESS as Schemas["CustomerAddress"],
73
+ );
74
+
75
+ expect(injections.apiClient.invoke).toHaveBeenCalledWith(
76
+ expect.stringContaining("updateCustomerAddress"),
77
+ expect.objectContaining({ body: MOCKED_ADDRESS }),
78
+ );
79
+ });
80
+
81
+ it("delete address", async () => {
82
+ const { vm, injections } = await useSetup(useAddress);
83
+ injections.apiClient.invoke.mockResolvedValue({ data: {} });
84
+ await vm.deleteCustomerAddress("address-id");
85
+
86
+ expect(injections.apiClient.invoke).toHaveBeenCalledWith(
87
+ expect.stringContaining("deleteCustomerAddress"),
88
+ expect.objectContaining({ pathParams: { addressId: "address-id" } }),
89
+ );
90
+ });
91
+
92
+ it("set default billing address", async () => {
93
+ const { vm, injections } = await useSetup(useAddress);
94
+ injections.apiClient.invoke.mockResolvedValue({ data: {} });
95
+ await vm.setDefaultCustomerBillingAddress("address-id");
96
+
97
+ expect(injections.apiClient.invoke).toHaveBeenCalledWith(
98
+ expect.stringContaining("defaultBillingAddress"),
99
+ expect.objectContaining({ pathParams: { addressId: "address-id" } }),
100
+ );
101
+ });
102
+
103
+ it("set default shipping address", async () => {
104
+ const { vm, injections } = await useSetup(useAddress);
105
+ injections.apiClient.invoke.mockResolvedValue({ data: {} });
106
+ await vm.setDefaultCustomerShippingAddress("address-id");
107
+
108
+ expect(injections.apiClient.invoke).toHaveBeenCalledWith(
109
+ expect.stringContaining("defaultShippingAddress"),
110
+ expect.objectContaining({ pathParams: { addressId: "address-id" } }),
111
+ );
112
+ });
113
+
114
+ it("build error message builder", async () => {
115
+ const { vm } = await useSetup(useAddress);
116
+ const message = vm.errorMessageBuilder({
117
+ code: "TEST",
118
+ });
119
+
120
+ expect(message).toBe(null);
121
+
122
+ const messageBlank = vm.errorMessageBuilder({
123
+ code: "VIOLATION::IS_BLANK_ERROR",
124
+ source: {
125
+ pointer: "/data/attributes/firstName",
126
+ },
127
+ detail: "This value should not be blank.",
128
+ });
129
+
130
+ expect(messageBlank).toBe(
131
+ "data/attributes/firstName - This value should not be blank.",
132
+ );
133
+ });
134
+ });