@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,63 @@
1
+ import { useCart, useNotifications } from "#imports";
2
+ import type { Schemas } from "#shopware";
3
+
4
+ const successCodes = ["promotion-discount-added"];
5
+
6
+ export type useCartNotificationReturn = {
7
+ codeErrorsNotification(): void;
8
+ getErrorsCodes(): Schemas["CartError"][];
9
+ };
10
+
11
+ /**
12
+ * UI composable
13
+ *
14
+ * @category Cart & Checkout
15
+ * @returns
16
+ */
17
+ export function useCartNotification(): useCartNotificationReturn {
18
+ const { pushError, pushSuccess } = useNotifications();
19
+ const { consumeCartErrors } = useCart();
20
+
21
+ /**
22
+ * Get cart error and display
23
+ *
24
+ * @returns {void}
25
+ */
26
+ const codeErrorsNotification = () => {
27
+ const errors: Schemas["Cart"]["errors"] = consumeCartErrors();
28
+ if (!errors || Array.isArray(errors)) return;
29
+
30
+ for (const element of Object.keys(errors)) {
31
+ if (successCodes.includes(errors[element].messageKey)) {
32
+ pushSuccess(errors[element].message);
33
+ } else {
34
+ pushError(errors[element].message);
35
+ }
36
+ }
37
+ };
38
+
39
+ /**
40
+ * Get errors codes without displaying
41
+ *
42
+ * @returns CartError[] | undefined
43
+ */
44
+ const getErrorsCodes = () => {
45
+ const errors: Schemas["Cart"]["errors"] = consumeCartErrors();
46
+ if (!errors || Array.isArray(errors)) return [];
47
+
48
+ return Object.keys(errors).reduce(
49
+ (acc, element) => {
50
+ if (!successCodes.includes(errors[element].messageKey))
51
+ acc.push(errors[element] as Schemas["CartError"]);
52
+
53
+ return acc;
54
+ },
55
+ [] as Schemas["CartError"][],
56
+ );
57
+ };
58
+
59
+ return {
60
+ codeErrorsNotification,
61
+ getErrorsCodes,
62
+ };
63
+ }
@@ -0,0 +1,14 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import { ref } from "vue";
3
+ import type { Schemas } from "#shopware";
4
+ import { useSetup } from "../_test";
5
+ import { useCategory } from "./useCategory";
6
+
7
+ describe("useCategory", () => {
8
+ it("empty category init", () => {
9
+ const { vm } = useSetup(() =>
10
+ useCategory(ref({ id: "1" } as Schemas["Category"])),
11
+ );
12
+ expect(vm.category).toStrictEqual({ id: "1" });
13
+ });
14
+ });
@@ -0,0 +1,32 @@
1
+ import { computed } from "vue";
2
+ import type { ComputedRef, Ref } from "vue";
3
+ import { useContext } from "#imports";
4
+ import type { Schemas } from "#shopware";
5
+ import ContextError from "../helpers/ContextError";
6
+
7
+ export type UseCategoryReturn = {
8
+ /**
9
+ * Current category entity
10
+ */
11
+ category: ComputedRef<Schemas["Category"]>;
12
+ };
13
+
14
+ /**
15
+ * Composable to get the category from current CMS context
16
+ *
17
+ * @category Product
18
+ * @public
19
+ */
20
+ export function useCategory(
21
+ category?: Ref<Schemas["Category"]>,
22
+ ): UseCategoryReturn {
23
+ const _category = useContext("category", { context: category });
24
+ if (!_category.value) {
25
+ // TODO link docs with composables context usage
26
+ throw new ContextError("Category");
27
+ }
28
+
29
+ return {
30
+ category: computed(() => _category.value),
31
+ };
32
+ }
@@ -0,0 +1,98 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import { useSetup } from "../_test";
3
+ import { cmsAssociations } from "../cms/cmsAssociations";
4
+ import { useCategorySearch } from "./useCategorySearch";
5
+
6
+ describe("useCategorySearch", () => {
7
+ it("search method", () => {
8
+ const { vm, injections } = useSetup(useCategorySearch);
9
+ injections.apiClient.invoke.mockResolvedValue({
10
+ data: {},
11
+ });
12
+ vm.search("categoryId");
13
+
14
+ expect(injections.apiClient.invoke).toHaveBeenCalledWith(
15
+ expect.stringContaining("readCategory"),
16
+ expect.objectContaining({
17
+ pathParams: {
18
+ navigationId: "categoryId",
19
+ },
20
+ headers: {
21
+ "sw-include-seo-urls": true,
22
+ },
23
+ }),
24
+ );
25
+ });
26
+
27
+ it("search method - with cms associations ", () => {
28
+ const { vm, injections } = useSetup(useCategorySearch);
29
+ injections.apiClient.invoke.mockResolvedValue({
30
+ data: {},
31
+ });
32
+ vm.search("categoryId", {
33
+ withCmsAssociations: true,
34
+ });
35
+
36
+ expect(injections.apiClient.invoke).toHaveBeenCalledWith(
37
+ expect.stringContaining("readCategory"),
38
+ expect.objectContaining({
39
+ pathParams: {
40
+ navigationId: "categoryId",
41
+ },
42
+ headers: {
43
+ "sw-include-seo-urls": true,
44
+ },
45
+ body: {
46
+ associations: cmsAssociations,
47
+ },
48
+ }),
49
+ );
50
+ });
51
+
52
+ it("advanced search method", () => {
53
+ const { vm, injections } = useSetup(useCategorySearch);
54
+ injections.apiClient.invoke.mockResolvedValue({
55
+ data: {},
56
+ });
57
+
58
+ vm.advancedSearch({
59
+ query: {
60
+ limit: 10,
61
+ },
62
+ });
63
+
64
+ expect(injections.apiClient.invoke).toHaveBeenCalledWith(
65
+ expect.stringContaining("readCategoryList"),
66
+ expect.objectContaining({
67
+ body: {
68
+ associations: {},
69
+ limit: 10,
70
+ },
71
+ }),
72
+ );
73
+ });
74
+
75
+ it("advanced search method - with Cms Associations", () => {
76
+ const { vm, injections } = useSetup(useCategorySearch);
77
+ injections.apiClient.invoke.mockResolvedValue({
78
+ data: {},
79
+ });
80
+
81
+ vm.advancedSearch({
82
+ query: {
83
+ limit: 10,
84
+ },
85
+ withCmsAssociations: true,
86
+ });
87
+
88
+ expect(injections.apiClient.invoke).toHaveBeenCalledWith(
89
+ expect.stringContaining("readCategoryList"),
90
+ expect.objectContaining({
91
+ body: {
92
+ ...cmsAssociations,
93
+ limit: 10,
94
+ },
95
+ }),
96
+ );
97
+ });
98
+ });
@@ -0,0 +1,81 @@
1
+ import { useShopwareContext } from "#imports";
2
+ import type { Schemas } from "#shopware";
3
+ import { cmsAssociations } from "../cms/cmsAssociations";
4
+
5
+ export type UseCategorySearchReturn = {
6
+ /**
7
+ * Search for category by ID
8
+ * Accepts optional query params and associations
9
+ */
10
+ search(
11
+ categoryId: string,
12
+ options?: {
13
+ withCmsAssociations?: boolean;
14
+ query?: Schemas["Criteria"];
15
+ },
16
+ ): Promise<Schemas["Category"]>;
17
+
18
+ /**
19
+ * Search based on the query
20
+ */
21
+ advancedSearch(options: {
22
+ withCmsAssociations?: boolean;
23
+ query: Schemas["Criteria"];
24
+ }): Promise<Schemas["Category"][]>;
25
+ };
26
+
27
+ /**
28
+ * Composable for category search.
29
+ * @public
30
+ * @category Navigation & Routing
31
+ */
32
+ export function useCategorySearch(): UseCategorySearchReturn {
33
+ const { apiClient } = useShopwareContext();
34
+
35
+ async function search(
36
+ categoryId: string,
37
+ options?: {
38
+ withCmsAssociations?: boolean;
39
+ query?: Schemas["Criteria"];
40
+ },
41
+ ) {
42
+ const associations = options?.withCmsAssociations ? cmsAssociations : {};
43
+ const result = await apiClient.invoke(
44
+ "readCategory post /category/{navigationId}",
45
+ {
46
+ pathParams: {
47
+ navigationId: categoryId,
48
+ },
49
+ headers: {
50
+ "sw-include-seo-urls": true,
51
+ },
52
+ body: {
53
+ associations,
54
+ ...options?.query,
55
+ },
56
+ },
57
+ );
58
+ return result.data;
59
+ }
60
+
61
+ async function advancedSearch(options: {
62
+ withCmsAssociations?: boolean;
63
+ query: Schemas["Criteria"];
64
+ }) {
65
+ const associations = options?.withCmsAssociations
66
+ ? cmsAssociations.associations
67
+ : {};
68
+ const result = await apiClient.invoke("readCategoryList post /category", {
69
+ body: {
70
+ associations,
71
+ ...options?.query,
72
+ },
73
+ });
74
+ return result.data.elements ?? [];
75
+ }
76
+
77
+ return {
78
+ search,
79
+ advancedSearch,
80
+ };
81
+ }
@@ -0,0 +1,104 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import { useSetup } from "../_test";
3
+ import { useCheckout } from "./useCheckout";
4
+
5
+ describe("useCheckout", () => {
6
+ it("getShippingMethods - empty", async () => {
7
+ const { vm, injections } = useSetup(useCheckout);
8
+ injections.apiClient.invoke.mockResolvedValue({
9
+ data: { elements: undefined },
10
+ });
11
+
12
+ await vm.getShippingMethods();
13
+
14
+ expect(injections.apiClient.invoke).toHaveBeenCalledWith(
15
+ expect.stringContaining("readShippingMethod"),
16
+ expect.objectContaining({
17
+ body: {
18
+ associations: {
19
+ prices: {},
20
+ },
21
+ },
22
+ query: {
23
+ onlyAvailable: true,
24
+ },
25
+ }),
26
+ );
27
+ });
28
+
29
+ it("getShippingMethods", async () => {
30
+ const { vm, injections } = useSetup(useCheckout);
31
+ injections.apiClient.invoke.mockResolvedValue({
32
+ data: { elements: [{ position: 1 }, {}] },
33
+ });
34
+
35
+ await vm.getShippingMethods();
36
+
37
+ expect(injections.apiClient.invoke).toHaveBeenCalledWith(
38
+ expect.stringContaining("readShippingMethod"),
39
+ expect.objectContaining({
40
+ body: {
41
+ associations: {
42
+ prices: {},
43
+ },
44
+ },
45
+ query: {
46
+ onlyAvailable: true,
47
+ },
48
+ }),
49
+ );
50
+
51
+ const shippingMethods = await vm.getShippingMethods({ forceReload: true });
52
+ expect(shippingMethods.value).toEqual([{}, { position: 1 }]);
53
+
54
+ const shippingMethodsNoReload = await vm.getShippingMethods();
55
+ expect(shippingMethodsNoReload.value).toEqual([{}, { position: 1 }]);
56
+ });
57
+
58
+ it("getPaymentMethods", async () => {
59
+ const { vm, injections } = useSetup(useCheckout);
60
+ injections.apiClient.invoke.mockResolvedValue({
61
+ data: { elements: [{ id: "1" }] },
62
+ });
63
+
64
+ await vm.getPaymentMethods();
65
+
66
+ expect(injections.apiClient.invoke).toHaveBeenCalledWith(
67
+ expect.stringContaining("readPaymentMethod"),
68
+ expect.objectContaining({}),
69
+ );
70
+ await vm.getPaymentMethods({
71
+ forceReload: false,
72
+ });
73
+ expect(vm.paymentMethods).toEqual([{ id: "1" }]);
74
+ });
75
+
76
+ it("getPaymentMethods - empty", async () => {
77
+ const { vm, injections } = useSetup(useCheckout);
78
+ injections.apiClient.invoke.mockResolvedValue({
79
+ data: { elements: undefined },
80
+ });
81
+
82
+ await vm.getPaymentMethods();
83
+
84
+ expect(injections.apiClient.invoke).toHaveBeenCalledWith(
85
+ expect.stringContaining("readPaymentMethod"),
86
+ expect.objectContaining({}),
87
+ );
88
+ expect(vm.paymentMethods).toEqual([]);
89
+ });
90
+
91
+ it("createOrder", async () => {
92
+ const { vm, injections } = useSetup(useCheckout);
93
+ injections.apiClient.invoke.mockResolvedValue({
94
+ data: { elements: [] },
95
+ });
96
+
97
+ await vm.createOrder();
98
+
99
+ expect(injections.apiClient.invoke).toHaveBeenCalledWith(
100
+ expect.stringContaining("createOrder"),
101
+ expect.objectContaining({}),
102
+ );
103
+ });
104
+ });
@@ -0,0 +1,163 @@
1
+ import { defu } from "defu";
2
+ import { computed, inject, provide, ref } from "vue";
3
+ import type { ComputedRef } from "vue";
4
+ import { useSessionContext, useShopwareContext } from "#imports";
5
+ import type { Schemas, operations } from "#shopware";
6
+
7
+ export type UseCheckoutReturn = {
8
+ /**
9
+ * Fetches all available shipping methods
10
+ */
11
+ getShippingMethods(options?: {
12
+ forceReload: boolean;
13
+ }): Promise<ComputedRef<Schemas["ShippingMethod"][]>>;
14
+ /**
15
+ * List of available shipping methods
16
+ */
17
+ shippingMethods: ComputedRef<Schemas["ShippingMethod"][]>;
18
+ /**
19
+ * Fetches all available payment methods
20
+ */
21
+ getPaymentMethods(options?: {
22
+ forceReload: boolean;
23
+ }): Promise<ComputedRef<Schemas["PaymentMethod"][]>>;
24
+ /**
25
+ * List of available payment methods
26
+ */
27
+ paymentMethods: ComputedRef<Schemas["PaymentMethod"][]>;
28
+ /**
29
+ * Creates order based on the current cart
30
+ */
31
+ createOrder(
32
+ params?: operations["createOrder post /checkout/order"]["body"],
33
+ ): Promise<Schemas["Order"]>;
34
+ /**
35
+ * Shipping address for the current session
36
+ */
37
+ shippingAddress: ComputedRef<Schemas["CustomerAddress"] | undefined>;
38
+ /**
39
+ * Billing address for the current session
40
+ */
41
+ billingAddress: ComputedRef<Schemas["CustomerAddress"] | undefined>;
42
+ /**
43
+ * Selected shipping method for the current session
44
+ * Sugar for {@link useSessionContext.selectedShippingMethod}
45
+ */
46
+ selectedShippingMethod: ComputedRef<Schemas["ShippingMethod"] | null>;
47
+ /**
48
+ * Sets shipping method for the current session
49
+ * Sugar for {@link useSessionContext.setShippingMethod}
50
+ */
51
+ setShippingMethod(shippingMethod: { id: string }): Promise<void>;
52
+ /**
53
+ * Selected payment method for the current session
54
+ * Sugar for {@link useSessionContext.selectedPaymentMethod}
55
+ */
56
+ selectedPaymentMethod: ComputedRef<Schemas["PaymentMethod"] | null>;
57
+ /**
58
+ * Sets payment method for the current session
59
+ * Sugar for {@link useSessionContext.setPaymentMethod}
60
+ */
61
+ setPaymentMethod(paymentMethod: { id: string }): Promise<void>;
62
+ };
63
+
64
+ const shippingMethodsAssociations: Schemas["Criteria"] = {
65
+ associations: {
66
+ prices: {},
67
+ },
68
+ };
69
+
70
+ /**
71
+ * Composable to manage checkout process
72
+ * @public
73
+ * @category Cart & Checkout
74
+ */
75
+ export function useCheckout(): UseCheckoutReturn {
76
+ const { apiClient } = useShopwareContext();
77
+ const {
78
+ sessionContext,
79
+ selectedPaymentMethod,
80
+ selectedShippingMethod,
81
+ setShippingMethod,
82
+ setPaymentMethod,
83
+ } = useSessionContext();
84
+
85
+ const storeShippingMethods = inject("swShippingMethods", ref());
86
+ provide("swShippingMethods", storeShippingMethods);
87
+
88
+ const storePaymentMethods = inject("swPaymentMethods", ref());
89
+ provide("swPaymentMethods", storePaymentMethods);
90
+
91
+ const shippingMethods = computed(() => storeShippingMethods.value || []);
92
+ const paymentMethods = computed(() => storePaymentMethods.value || []);
93
+
94
+ async function getShippingMethods(
95
+ { forceReload } = { forceReload: false },
96
+ associations: Schemas["Criteria"] = {},
97
+ ) {
98
+ if (shippingMethods.value.length && !forceReload) return shippingMethods;
99
+ const mergedAssociations: Schemas["Criteria"] = defu(
100
+ shippingMethodsAssociations,
101
+ associations,
102
+ );
103
+ const response = await apiClient.invoke(
104
+ "readShippingMethod post /shipping-method",
105
+ {
106
+ body: {
107
+ ...mergedAssociations,
108
+ },
109
+ query: {
110
+ onlyAvailable: true,
111
+ },
112
+ },
113
+ );
114
+ storeShippingMethods.value =
115
+ response.data.elements?.sort(
116
+ (a: Schemas["ShippingMethod"], b: Schemas["ShippingMethod"]) =>
117
+ (a.position ?? 0) - (b.position ?? 0),
118
+ ) || [];
119
+ return shippingMethods;
120
+ }
121
+
122
+ async function getPaymentMethods({ forceReload } = { forceReload: false }) {
123
+ if (paymentMethods.value.length && !forceReload) return paymentMethods;
124
+ const response = await apiClient.invoke(
125
+ "readPaymentMethod post /payment-method",
126
+ {
127
+ body: { onlyAvailable: true },
128
+ },
129
+ );
130
+ storePaymentMethods.value = response.data.elements || [];
131
+ return paymentMethods;
132
+ }
133
+
134
+ async function createOrder(
135
+ params: operations["createOrder post /checkout/order"]["body"] = {},
136
+ ) {
137
+ const order = await apiClient.invoke("createOrder post /checkout/order", {
138
+ body: params,
139
+ });
140
+ return order.data;
141
+ }
142
+
143
+ const shippingAddress = computed(
144
+ () => sessionContext.value?.shippingLocation?.address,
145
+ );
146
+ const billingAddress = computed(
147
+ () => sessionContext.value?.customer?.activeBillingAddress,
148
+ );
149
+
150
+ return {
151
+ getPaymentMethods,
152
+ paymentMethods,
153
+ getShippingMethods,
154
+ shippingMethods,
155
+ createOrder,
156
+ shippingAddress,
157
+ billingAddress,
158
+ selectedShippingMethod,
159
+ setShippingMethod,
160
+ selectedPaymentMethod,
161
+ setPaymentMethod,
162
+ };
163
+ }
@@ -0,0 +1,25 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import type { Schemas } from "#shopware";
3
+ import { useSetup } from "../_test";
4
+ import { useCmsBlock } from "./useCmsBlock";
5
+
6
+ describe("composables - useCmsBlock", () => {
7
+ it("get block data", () => {
8
+ const mockData = {
9
+ apiAlias: "cms_block",
10
+ createdAt: "2020-08-06T06:26:23.880+00:00",
11
+ position: 0,
12
+ sectionId: "ri3n2ldk",
13
+ slots: [
14
+ {
15
+ slot: "left-bottom",
16
+ },
17
+ ],
18
+ type: "page",
19
+ };
20
+ const { vm } = useSetup(() => useCmsBlock(mockData as Schemas["CmsBlock"]));
21
+
22
+ expect(vm.block).toEqual(mockData);
23
+ expect(vm.getSlotContent("left-bottom")).toEqual(mockData.slots[0]);
24
+ });
25
+ });
@@ -0,0 +1,36 @@
1
+ import type { Schemas } from "#shopware";
2
+
3
+ type ArrayElement<ArrayType extends readonly unknown[]> =
4
+ ArrayType extends readonly (infer ElementType)[] ? ElementType : never;
5
+
6
+ export type UseCmsBlockReturn = {
7
+ /**
8
+ * Cms block content
9
+ */
10
+ block: Schemas["CmsBlock"];
11
+ /**
12
+ * Get slot content by slot name (identifier)
13
+ * @example getSlotContent("main")
14
+ */
15
+ getSlotContent(slotName: string): ArrayElement<Schemas["CmsBlock"]["slots"]>;
16
+ };
17
+
18
+ /**
19
+ * Composable to get cms block content
20
+ * @public
21
+ * @category CMS (Shopping Experiences)
22
+ */
23
+ export function useCmsBlock<BLOCK_TYPE extends Schemas["CmsBlock"]>(
24
+ content: BLOCK_TYPE,
25
+ ): UseCmsBlockReturn {
26
+ function getSlotContent(slotName: ArrayElement<BLOCK_TYPE["slots"]>["slot"]) {
27
+ return content.slots.find((slot) => slot.slot === slotName) as ArrayElement<
28
+ BLOCK_TYPE["slots"]
29
+ >;
30
+ }
31
+
32
+ return {
33
+ block: content,
34
+ getSlotContent,
35
+ };
36
+ }
@@ -0,0 +1,17 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import { useSetup } from "../_test";
3
+ import LandingPageMock from "../mocks/LandingPage";
4
+ import { useCmsMeta } from "./useCmsMeta";
5
+
6
+ describe("composables - useCmsMeta", () => {
7
+ it("should return title and meta tags", () => {
8
+ const { vm } = useSetup(() => useCmsMeta(LandingPageMock));
9
+
10
+ expect(vm.title).toEqual("Summer Giveaway");
11
+ expect(vm.meta).toEqual([
12
+ { name: "keywords", content: "Landing page keywords" },
13
+ { name: "description", content: "Landing page description" },
14
+ { name: "title", content: "Landing page title" },
15
+ ]);
16
+ });
17
+ });
@@ -0,0 +1,46 @@
1
+ import { getTranslatedProperty } from "@shopware-pwa/helpers-next";
2
+ import { computed } from "vue";
3
+ import type { ComputedRef } from "vue";
4
+ import type { Schemas } from "#shopware";
5
+
6
+ export type UseCmsMetaReturn = {
7
+ /**
8
+ * Meta title for current page/entity
9
+ */
10
+ title: ComputedRef<string>;
11
+ /**
12
+ * Meta tags for current page/entity
13
+ */
14
+ meta: ComputedRef<{ name: string; content: string }[]>;
15
+ };
16
+
17
+ /**
18
+ * TODO: remove parameter and use reactive state of cmsResponse provided by useCms composable
19
+ * @category CMS (Shopping Experiences)
20
+ */
21
+ export function useCmsMeta(
22
+ entity: Schemas["Category"] | Schemas["Product"] | Schemas["LandingPage"],
23
+ ): UseCmsMetaReturn {
24
+ const meta = computed(() => {
25
+ const entries = [];
26
+ const keywords = getTranslatedProperty(entity, "keywords");
27
+ const description = getTranslatedProperty(entity, "metaDescription");
28
+ const title = getTranslatedProperty(entity, "metaTitle");
29
+ if (keywords) {
30
+ entries.push({ name: "keywords", content: keywords });
31
+ }
32
+ if (description) {
33
+ entries.push({ name: "description", content: description });
34
+ }
35
+ if (title) {
36
+ entries.push({ name: "title", content: title });
37
+ }
38
+
39
+ return entries;
40
+ });
41
+
42
+ return {
43
+ title: computed(() => getTranslatedProperty(entity, "name")),
44
+ meta,
45
+ };
46
+ }