@shopware/composables 1.11.0 → 1.12.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/README.md +11 -8
- package/dist/index.d.mts +29 -3
- package/dist/index.d.ts +29 -3
- package/dist/index.mjs +138 -79
- package/package.json +16 -16
- package/src/cms/useCmsElementConfig.test.ts +1 -0
- package/src/cms/useCmsElementConfig.ts +1 -0
- package/src/cms/useCmsElementImage.test.ts +1 -0
- package/src/cms/useCmsElementImage.ts +2 -0
- package/src/cms/useCmsElementProductBox.test.ts +1 -0
- package/src/cms/useCmsElementProductBox.ts +2 -0
- package/src/createShopwareContext.test.ts +1 -0
- package/src/createShopwareContext.ts +8 -0
- package/src/devtools/plugin.ts +13 -13
- package/src/helpers/ContextError.spec.ts +1 -0
- package/src/index.test.ts +2 -0
- package/src/index.ts +1 -0
- package/src/types/cmsElementTypes.ts +1 -0
- package/src/useAddToCart/useAddToCart.test.ts +1 -0
- package/src/useAddToCart/useAddToCart.ts +1 -0
- package/src/useAddress/useAddress.test.ts +2 -0
- package/src/useAddress/useAddress.ts +1 -0
- package/src/useB2bQuoteManagement/useB2bQuoteManagement.test.ts +1 -0
- package/src/useBreadcrumbs/useBreadcrumbs.test.ts +2 -0
- package/src/useBreadcrumbs/useBreadcrumbs.ts +1 -0
- package/src/useCart/useCart.test.ts +2 -0
- package/src/useCart/useCart.ts +1 -0
- package/src/useCartErrorParamsResolver/useCartErrorParamsResolver.test.ts +1 -0
- package/src/useCartItem/useCartItem.test.ts +2 -0
- package/src/useCartItem/useCartItem.ts +1 -0
- package/src/useCartNotification/useCartNotification.test.ts +2 -0
- package/src/useCategory/useCategory.test.ts +2 -0
- package/src/useCategory/useCategory.ts +2 -0
- package/src/useCategorySearch/useCategorySearch.test.ts +29 -0
- package/src/useCategorySearch/useCategorySearch.ts +15 -7
- package/src/useCheckout/useCheckout.test.ts +1 -0
- package/src/useCheckout/useCheckout.ts +1 -0
- package/src/useCmsBlock/useCmsBlock.test.ts +2 -0
- package/src/useCmsMeta/useCmsMeta.test.ts +2 -0
- package/src/useCmsMeta/useCmsMeta.ts +1 -0
- package/src/useCmsSection/useCmsSection.test.ts +2 -0
- package/src/useCmsTranslations/useCmsTranslations.test.ts +2 -0
- package/src/useCountries/useCountries.test.ts +19 -0
- package/src/useCountries/useCountries.ts +14 -7
- package/src/useCustomerOrders/useCustomerOrders.test.ts +1 -0
- package/src/useCustomerOrders/useCustomerOrders.ts +1 -0
- package/src/useCustomerPassword/useCustomerPassword.test.ts +1 -0
- package/src/useDefaultOrderAssociations/useDefaultOrderAssociations.test.ts +1 -0
- package/src/useInternationalization/useInternationalization.test.ts +14 -1
- package/src/useInternationalization/useInternationalization.ts +5 -3
- package/src/useLandingSearch/useLandingSearch.test.ts +1 -0
- package/src/useLandingSearch/useLandingSearch.ts +1 -0
- package/src/useListing/useListing.test.ts +2 -0
- package/src/useListing/useListing.ts +3 -2
- package/src/useLocalWishlist/useLocalWishlist.test.ts +1 -0
- package/src/useNavigation/useNavigation.test.ts +24 -1
- package/src/useNavigation/useNavigation.ts +36 -14
- package/src/useNavigationContext/useNavigationContext.test.ts +1 -0
- package/src/useNavigationContext/useNavigationContext.ts +1 -0
- package/src/useNavigationSearch/useNavigationSearch.test.ts +125 -2
- package/src/useNavigationSearch/useNavigationSearch.ts +38 -21
- package/src/useNewsletter/useNewsletter.test.ts +5 -1
- package/src/useNewsletter/useNewsletter.ts +5 -1
- package/src/useNotifications/useNotifications.test.ts +1 -0
- package/src/useOrderDetails/useOrderDetails.test.ts +1 -0
- package/src/useOrderDetails/useOrderDetails.ts +1 -0
- package/src/useOrderPayment/useOrderPayment.test.ts +2 -0
- package/src/useOrderPayment/useOrderPayment.ts +1 -0
- package/src/usePrice/usePrice.test.ts +2 -0
- package/src/usePrice/usePrice.ts +1 -0
- package/src/useProduct/useProduct.test.ts +2 -0
- package/src/useProduct/useProduct.ts +2 -0
- package/src/useProductAssociations/useProductAssociations.spec.ts +48 -0
- package/src/useProductAssociations/useProductAssociations.ts +7 -3
- package/src/useProductConfigurator/useProductConfigurator.spec.ts +24 -0
- package/src/useProductConfigurator/useProductConfigurator.ts +20 -13
- package/src/useProductPrice/useProductPrice.test.ts +2 -0
- package/src/useProductPrice/useProductPrice.ts +1 -0
- package/src/useProductReviews/useProductReviews.test.ts +1 -0
- package/src/useProductReviews/useProductReviews.ts +1 -0
- package/src/useProductSearch/useProductSearch.test.ts +24 -1
- package/src/useProductSearch/useProductSearch.ts +24 -11
- package/src/useProductSearchSuggest/useProductSearchSuggest.test.ts +1 -0
- package/src/useProductSearchSuggest/useProductSearchSuggest.ts +1 -0
- package/src/useProductWishlist/useProductWishlist.test.ts +2 -0
- package/src/useProductWishlist/useProductWishlist.ts +1 -0
- package/src/useSalutations/useSalutations.spec.ts +15 -0
- package/src/useSalutations/useSalutations.ts +5 -2
- package/src/useSessionContext/useSessionContext.test.ts +18 -0
- package/src/useSessionContext/useSessionContext.ts +10 -0
- package/src/useShopwareContext/useShopwareContext.test.ts +1 -0
- package/src/useShopwareContext/useShopwareContext.ts +13 -0
- package/src/useSyncWishlist/useSyncWishlist.test.ts +1 -0
- package/src/useSyncWishlist/useSyncWishlist.ts +1 -0
- package/src/useUrlResolver/useUrlResolver.test.ts +1 -0
- package/src/useUser/useUser.test.ts +35 -2
- package/src/useUser/useUser.ts +34 -25
- package/src/useWishlist/useWishlist.test.ts +2 -0
- package/src/useWishlist/useWishlist.ts +1 -0
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { computed, ref } from "vue";
|
|
2
2
|
import type { ComputedRef } from "vue";
|
|
3
|
+
|
|
3
4
|
import { useShopwareContext } from "#imports";
|
|
4
5
|
import type { Schemas } from "#shopware";
|
|
5
6
|
|
|
@@ -31,14 +32,12 @@ export function useProductAssociations(
|
|
|
31
32
|
product: ComputedRef<Schemas["Product"]>,
|
|
32
33
|
options: {
|
|
33
34
|
associationContext: "cross-selling" | "reviews";
|
|
35
|
+
includeSeoUrls?: boolean;
|
|
34
36
|
},
|
|
35
37
|
): UseProductAssociationsReturn {
|
|
36
38
|
if (!product.value)
|
|
37
39
|
throw new Error("[useProductAssociations]: Product is not provided.");
|
|
38
40
|
|
|
39
|
-
// @ts-ignore: temporary until fixed or removed
|
|
40
|
-
const association = options.associationContext;
|
|
41
|
-
|
|
42
41
|
const { apiClient } = useShopwareContext();
|
|
43
42
|
const isLoading = ref(false);
|
|
44
43
|
const associations = ref<Schemas["CrossSellingElementCollection"]>([]);
|
|
@@ -84,6 +83,11 @@ export function useProductAssociations(
|
|
|
84
83
|
const response = await apiClient.invoke(
|
|
85
84
|
"readProductCrossSellings post /product/{productId}/cross-selling",
|
|
86
85
|
{
|
|
86
|
+
...(options.includeSeoUrls && {
|
|
87
|
+
headers: {
|
|
88
|
+
"sw-include-seo-urls": true,
|
|
89
|
+
},
|
|
90
|
+
}),
|
|
87
91
|
pathParams: { productId: product.value.id },
|
|
88
92
|
},
|
|
89
93
|
);
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { describe, expect, it, vi } from "vitest";
|
|
2
2
|
import { ref } from "vue";
|
|
3
|
+
|
|
3
4
|
import { useProduct, useProductConfigurator } from "#imports";
|
|
5
|
+
|
|
4
6
|
import { useSetup } from "../_test";
|
|
5
7
|
import mockedConfigurator from "../mocks/Configurator";
|
|
6
8
|
import mockedProduct from "../mocks/Product";
|
|
@@ -40,6 +42,28 @@ describe("useProductConfigurator", () => {
|
|
|
40
42
|
);
|
|
41
43
|
});
|
|
42
44
|
|
|
45
|
+
it("findVariantForSelectedOptions uses the cacheable GET variant when cacheableReads is enabled", async () => {
|
|
46
|
+
vi.mocked(useProduct).mockReturnValue({
|
|
47
|
+
configurator: ref(mockedConfigurator),
|
|
48
|
+
product: ref(mockedProduct),
|
|
49
|
+
} as unknown as ReturnType<typeof useProduct>);
|
|
50
|
+
const { vm, injections } = useSetup(useProductConfigurator, {
|
|
51
|
+
shopware: { cacheableReads: true },
|
|
52
|
+
} as Parameters<typeof useSetup>[1]);
|
|
53
|
+
injections.apiClient.invoke.mockResolvedValue({
|
|
54
|
+
data: { elements: [mockedProduct] },
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
await vm.findVariantForSelectedOptions({ test: "test" });
|
|
58
|
+
|
|
59
|
+
expect(injections.apiClient.invoke).toHaveBeenCalledWith(
|
|
60
|
+
"readProductGet get /product",
|
|
61
|
+
expect.objectContaining({
|
|
62
|
+
query: expect.objectContaining({ _criteria: expect.any(String) }),
|
|
63
|
+
}),
|
|
64
|
+
);
|
|
65
|
+
});
|
|
66
|
+
|
|
43
67
|
it("findVariantForSelectedOptions - error", async () => {
|
|
44
68
|
console.error = vi.fn();
|
|
45
69
|
const { vm, injections } = useSetup(useProductConfigurator);
|
|
@@ -1,6 +1,8 @@
|
|
|
1
|
+
import { encodeForQuery } from "@shopware/api-client/helpers";
|
|
1
2
|
import { getTranslatedProperty } from "@shopware/helpers";
|
|
2
3
|
import { computed, ref } from "vue";
|
|
3
4
|
import type { ComputedRef, Ref } from "vue";
|
|
5
|
+
|
|
4
6
|
import { useProduct, useShopwareContext } from "#imports";
|
|
5
7
|
import type { Schemas } from "#shopware";
|
|
6
8
|
|
|
@@ -40,7 +42,7 @@ export type UseProductConfiguratorReturn = {
|
|
|
40
42
|
* @category Product
|
|
41
43
|
*/
|
|
42
44
|
export function useProductConfigurator(): UseProductConfiguratorReturn {
|
|
43
|
-
const { apiClient } = useShopwareContext();
|
|
45
|
+
const { apiClient, cacheableReads } = useShopwareContext();
|
|
44
46
|
|
|
45
47
|
const { configurator, product } = useProduct();
|
|
46
48
|
|
|
@@ -91,19 +93,24 @@ export function useProductConfigurator(): UseProductConfiguratorReturn {
|
|
|
91
93
|
),
|
|
92
94
|
];
|
|
93
95
|
try {
|
|
94
|
-
const
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
seo_url: ["seoPathInfo"],
|
|
101
|
-
},
|
|
102
|
-
associations: {
|
|
103
|
-
seoUrls: {},
|
|
104
|
-
},
|
|
96
|
+
const criteria = {
|
|
97
|
+
filter,
|
|
98
|
+
limit: 1,
|
|
99
|
+
includes: {
|
|
100
|
+
product: ["id", "translated", "productNumber", "seoUrls"],
|
|
101
|
+
seo_url: ["seoPathInfo"],
|
|
105
102
|
},
|
|
106
|
-
|
|
103
|
+
associations: {
|
|
104
|
+
seoUrls: {},
|
|
105
|
+
},
|
|
106
|
+
};
|
|
107
|
+
const response = cacheableReads
|
|
108
|
+
? await apiClient.invoke("readProductGet get /product", {
|
|
109
|
+
query: { _criteria: encodeForQuery(criteria) },
|
|
110
|
+
})
|
|
111
|
+
: await apiClient.invoke("readProduct post /product", {
|
|
112
|
+
body: criteria,
|
|
113
|
+
});
|
|
107
114
|
return response.data.elements?.[0]; // return first matching product
|
|
108
115
|
} catch (e) {
|
|
109
116
|
console.error("SwProductDetails:findVariantForSelectedOptions", e);
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
2
|
import { ref } from "vue";
|
|
3
|
+
|
|
3
4
|
import type { Schemas } from "#shopware";
|
|
5
|
+
|
|
4
6
|
import { useSetup } from "../_test";
|
|
5
7
|
import mockedProduct from "../mocks/Product";
|
|
6
8
|
import productTierPrices from "../mocks/ProductTierPrices";
|
|
@@ -2,6 +2,7 @@ import { getProductTierPrices } from "@shopware/helpers";
|
|
|
2
2
|
import type { TierPrice } from "@shopware/helpers";
|
|
3
3
|
import { computed } from "vue";
|
|
4
4
|
import type { ComputedRef, Ref } from "vue";
|
|
5
|
+
|
|
5
6
|
import type { Schemas } from "#shopware";
|
|
6
7
|
|
|
7
8
|
export type UseProductPriceReturn = {
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
+
import { encodeForQuery } from "@shopware/api-client/helpers";
|
|
1
2
|
import { describe, expect, it } from "vitest";
|
|
3
|
+
|
|
2
4
|
import { useSetup } from "../_test";
|
|
3
5
|
import { cmsAssociations } from "../cms/cmsAssociations";
|
|
4
6
|
import { useProductSearch } from "./useProductSearch";
|
|
@@ -25,7 +27,7 @@ describe("useProductSearch", () => {
|
|
|
25
27
|
withCmsAssociations: true,
|
|
26
28
|
});
|
|
27
29
|
expect(injections.apiClient.invoke).toHaveBeenCalledWith(
|
|
28
|
-
expect.stringContaining("readProductDetail"),
|
|
30
|
+
expect.stringContaining("readProductDetail post"),
|
|
29
31
|
expect.objectContaining({
|
|
30
32
|
body: cmsAssociations,
|
|
31
33
|
pathParams: {
|
|
@@ -34,4 +36,25 @@ describe("useProductSearch", () => {
|
|
|
34
36
|
}),
|
|
35
37
|
);
|
|
36
38
|
});
|
|
39
|
+
|
|
40
|
+
it("uses the cacheable GET variant when cacheableReads is enabled", async () => {
|
|
41
|
+
const { vm, injections } = useSetup(() => useProductSearch(), {
|
|
42
|
+
shopware: { cacheableReads: true },
|
|
43
|
+
});
|
|
44
|
+
injections.apiClient.invoke.mockResolvedValue({ data: {} });
|
|
45
|
+
await vm.search("test", {
|
|
46
|
+
withCmsAssociations: true,
|
|
47
|
+
});
|
|
48
|
+
expect(injections.apiClient.invoke).toHaveBeenCalledWith(
|
|
49
|
+
expect.stringContaining("readProductDetailGet get"),
|
|
50
|
+
expect.objectContaining({
|
|
51
|
+
query: {
|
|
52
|
+
_criteria: encodeForQuery(cmsAssociations),
|
|
53
|
+
},
|
|
54
|
+
pathParams: {
|
|
55
|
+
productId: "test",
|
|
56
|
+
},
|
|
57
|
+
}),
|
|
58
|
+
);
|
|
59
|
+
});
|
|
37
60
|
});
|
|
@@ -1,6 +1,9 @@
|
|
|
1
|
+
import { encodeForQuery } from "@shopware/api-client/helpers";
|
|
1
2
|
import { defu } from "defu";
|
|
3
|
+
|
|
2
4
|
import { useShopwareContext } from "#imports";
|
|
3
5
|
import type { Schemas } from "#shopware";
|
|
6
|
+
|
|
4
7
|
import { cmsAssociations } from "../cms/cmsAssociations";
|
|
5
8
|
|
|
6
9
|
type UseProductSearchReturnOptions = {
|
|
@@ -28,7 +31,7 @@ export type UseProductSearchReturn = {
|
|
|
28
31
|
* @category Navigation & Routing
|
|
29
32
|
*/
|
|
30
33
|
export function useProductSearch(): UseProductSearchReturn {
|
|
31
|
-
const { apiClient } = useShopwareContext();
|
|
34
|
+
const { apiClient, cacheableReads } = useShopwareContext();
|
|
32
35
|
|
|
33
36
|
const search = async (
|
|
34
37
|
productId: string,
|
|
@@ -39,16 +42,26 @@ export function useProductSearch(): UseProductSearchReturn {
|
|
|
39
42
|
options?.criteria,
|
|
40
43
|
{ associations: options?.associations ?? {} },
|
|
41
44
|
);
|
|
42
|
-
const result =
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
45
|
+
const result = cacheableReads
|
|
46
|
+
? await apiClient.invoke(
|
|
47
|
+
"readProductDetailGet get /product/{productId}",
|
|
48
|
+
{
|
|
49
|
+
headers: {
|
|
50
|
+
"sw-include-seo-urls": true,
|
|
51
|
+
},
|
|
52
|
+
pathParams: { productId },
|
|
53
|
+
query: {
|
|
54
|
+
_criteria: encodeForQuery(associations),
|
|
55
|
+
},
|
|
56
|
+
},
|
|
57
|
+
)
|
|
58
|
+
: await apiClient.invoke("readProductDetail post /product/{productId}", {
|
|
59
|
+
headers: {
|
|
60
|
+
"sw-include-seo-urls": true,
|
|
61
|
+
},
|
|
62
|
+
pathParams: { productId },
|
|
63
|
+
body: associations,
|
|
64
|
+
});
|
|
52
65
|
return result.data;
|
|
53
66
|
};
|
|
54
67
|
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { flushPromises } from "@vue/test-utils";
|
|
2
2
|
import { describe, expect, it, vi } from "vitest";
|
|
3
3
|
import { ref } from "vue";
|
|
4
|
+
|
|
4
5
|
import { useSetup } from "../_test";
|
|
5
6
|
import Salutations from "../mocks/Salutations";
|
|
6
7
|
import { useSalutations } from "./useSalutations";
|
|
@@ -36,6 +37,20 @@ describe("useSalutations", () => {
|
|
|
36
37
|
expect(vm.getSalutations).toStrictEqual([]);
|
|
37
38
|
});
|
|
38
39
|
|
|
40
|
+
it("uses the cacheable GET variant when cacheableReads is enabled", async () => {
|
|
41
|
+
const { vm, injections } = useSetup(useSalutations, {
|
|
42
|
+
shopware: { cacheableReads: true },
|
|
43
|
+
apiClient: {
|
|
44
|
+
invoke: vi.fn().mockResolvedValue({ data: { elements: Salutations } }),
|
|
45
|
+
},
|
|
46
|
+
} as Parameters<typeof useSetup>[1]);
|
|
47
|
+
await vm.fetchSalutations();
|
|
48
|
+
|
|
49
|
+
expect(injections.apiClient.invoke).toHaveBeenCalledWith(
|
|
50
|
+
"readSalutationGet get /salutation",
|
|
51
|
+
);
|
|
52
|
+
});
|
|
53
|
+
|
|
39
54
|
it("should not fetch when swSalutations already populated", async () => {
|
|
40
55
|
const preloadedSalutations = ref(Salutations);
|
|
41
56
|
const { vm, injections } = useSetup(useSalutations, {
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { computed, inject, onMounted, provide, ref } from "vue";
|
|
2
2
|
import type { ComputedRef } from "vue";
|
|
3
|
+
|
|
3
4
|
import { useShopwareContext } from "#imports";
|
|
4
5
|
import type { Schemas, operations } from "#shopware";
|
|
5
6
|
|
|
@@ -22,7 +23,7 @@ export type UseSalutationsReturn = {
|
|
|
22
23
|
* @category Context & Language
|
|
23
24
|
*/
|
|
24
25
|
export function useSalutations(): UseSalutationsReturn {
|
|
25
|
-
const { apiClient } = useShopwareContext();
|
|
26
|
+
const { apiClient, cacheableReads } = useShopwareContext();
|
|
26
27
|
|
|
27
28
|
const _salutations = inject("swSalutations", ref());
|
|
28
29
|
provide("swSalutations", _salutations);
|
|
@@ -30,7 +31,9 @@ export function useSalutations(): UseSalutationsReturn {
|
|
|
30
31
|
const fetchSalutations = async (): Promise<
|
|
31
32
|
operations["readSalutation post /salutation"]["response"]
|
|
32
33
|
> => {
|
|
33
|
-
const result =
|
|
34
|
+
const result = cacheableReads
|
|
35
|
+
? await apiClient.invoke("readSalutationGet get /salutation")
|
|
36
|
+
: await apiClient.invoke("readSalutation post /salutation");
|
|
34
37
|
_salutations.value = result.data.elements;
|
|
35
38
|
return result.data;
|
|
36
39
|
};
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { describe, expect, it, vi } from "vitest";
|
|
2
|
+
|
|
2
3
|
import type { Schemas } from "#shopware";
|
|
4
|
+
|
|
3
5
|
import { useSetup } from "../_test";
|
|
4
6
|
import { useSessionContext } from "./useSessionContext";
|
|
5
7
|
|
|
@@ -67,6 +69,22 @@ describe("useSessionContext", () => {
|
|
|
67
69
|
expect(consoleErrorSpy).toHaveBeenCalledTimes(1);
|
|
68
70
|
});
|
|
69
71
|
|
|
72
|
+
it("currentLocaleCode reads languageInfo.localeCode from the context", async () => {
|
|
73
|
+
const { vm, injections } = useSetup(() => useSessionContext());
|
|
74
|
+
injections.apiClient.invoke.mockResolvedValue({
|
|
75
|
+
data: {
|
|
76
|
+
languageInfo: { name: "English", localeCode: "en-GB" },
|
|
77
|
+
},
|
|
78
|
+
});
|
|
79
|
+
await vm.refreshSessionContext();
|
|
80
|
+
expect(vm.currentLocaleCode).toBe("en-GB");
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it("currentLocaleCode is undefined when the context has no languageInfo", () => {
|
|
84
|
+
const { vm } = useSetup(() => useSessionContext());
|
|
85
|
+
expect(vm.currentLocaleCode).toBeUndefined();
|
|
86
|
+
});
|
|
87
|
+
|
|
70
88
|
it("setShippingMethod", async () => {
|
|
71
89
|
const { vm, injections } = useSetup(() => useSessionContext());
|
|
72
90
|
injections.apiClient.invoke.mockResolvedValue({ data: {} });
|
|
@@ -103,6 +103,12 @@ export type UseSessionContextReturn = {
|
|
|
103
103
|
* current language code
|
|
104
104
|
*/
|
|
105
105
|
currentLanguageId: ComputedRef<string | undefined>;
|
|
106
|
+
/**
|
|
107
|
+
* Locale code of the active language (e.g. `"en-GB"`), read from the
|
|
108
|
+
* context's `languageInfo`. Available from the session context alone — no
|
|
109
|
+
* separate language list request is required to detect the current locale.
|
|
110
|
+
*/
|
|
111
|
+
currentLocaleCode: ComputedRef<string | undefined>;
|
|
106
112
|
/**
|
|
107
113
|
* current context's customer object
|
|
108
114
|
*/
|
|
@@ -262,6 +268,9 @@ export function useSessionContext(
|
|
|
262
268
|
const languageIdChain = computed(
|
|
263
269
|
() => sessionContext.value?.context?.languageIdChain?.[0] || "",
|
|
264
270
|
);
|
|
271
|
+
const currentLocaleCode = computed(
|
|
272
|
+
() => sessionContext.value?.languageInfo?.localeCode,
|
|
273
|
+
);
|
|
265
274
|
const taxState = computed(() => sessionContext.value?.context?.taxState);
|
|
266
275
|
const userFromContext = computed(() => sessionContext.value?.customer);
|
|
267
276
|
|
|
@@ -287,6 +296,7 @@ export function useSessionContext(
|
|
|
287
296
|
languageIdChain,
|
|
288
297
|
salesChannelLanguageId: languageId,
|
|
289
298
|
currentLanguageId: languageIdChain,
|
|
299
|
+
currentLocaleCode,
|
|
290
300
|
setCountry,
|
|
291
301
|
setContext,
|
|
292
302
|
};
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { inject } from "vue";
|
|
2
|
+
|
|
2
3
|
import type { ApiClient } from "#shopware";
|
|
4
|
+
|
|
3
5
|
import ContextError from "../helpers/ContextError";
|
|
4
6
|
|
|
5
7
|
export type ShopwareContext = {
|
|
@@ -13,6 +15,16 @@ export type ShopwareContext = {
|
|
|
13
15
|
* If not provided, it will be "en-US"
|
|
14
16
|
*/
|
|
15
17
|
browserLocale: string;
|
|
18
|
+
/**
|
|
19
|
+
* When `true`, composables read data through the cacheable GET variants of
|
|
20
|
+
* the Store API (criteria compressed into the `_criteria` query param)
|
|
21
|
+
* instead of POST, so the responses can be cached by HTTP infrastructure
|
|
22
|
+
* (CDN, reverse proxy, browser). Requires a backend that supports the GET
|
|
23
|
+
* read routes.
|
|
24
|
+
*
|
|
25
|
+
* @default false
|
|
26
|
+
*/
|
|
27
|
+
cacheableReads: boolean;
|
|
16
28
|
};
|
|
17
29
|
|
|
18
30
|
/**
|
|
@@ -33,5 +45,6 @@ export function useShopwareContext(): ShopwareContext {
|
|
|
33
45
|
apiClient,
|
|
34
46
|
devStorefrontUrl: shopwareContext.devStorefrontUrl,
|
|
35
47
|
browserLocale: shopwareContext.browserLocale || "en-US",
|
|
48
|
+
cacheableReads: shopwareContext.cacheableReads ?? false,
|
|
36
49
|
};
|
|
37
50
|
}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import type { operations } from "@shopware/api-client/api-types";
|
|
2
|
+
import { encodeForQuery } from "@shopware/api-client/helpers";
|
|
2
3
|
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
3
4
|
import { ref } from "vue";
|
|
5
|
+
|
|
4
6
|
import { useSetup } from "../_test";
|
|
5
7
|
import { useUser } from "./useUser";
|
|
6
8
|
|
|
@@ -187,7 +189,7 @@ describe("useUser", () => {
|
|
|
187
189
|
await vm.loadCountry(countryId);
|
|
188
190
|
|
|
189
191
|
expect(injections.apiClient.invoke).toHaveBeenCalledWith(
|
|
190
|
-
expect.stringContaining("readCountry"),
|
|
192
|
+
expect.stringContaining("readCountry post"),
|
|
191
193
|
expect.objectContaining({
|
|
192
194
|
body: {
|
|
193
195
|
filter: [
|
|
@@ -341,7 +343,7 @@ describe("useUser", () => {
|
|
|
341
343
|
const result = await vm.loadSalutation("test");
|
|
342
344
|
|
|
343
345
|
expect(injections.apiClient.invoke).toHaveBeenCalledWith(
|
|
344
|
-
expect.stringContaining("readSalutation"),
|
|
346
|
+
expect.stringContaining("readSalutation post"),
|
|
345
347
|
expect.objectContaining({
|
|
346
348
|
body: {
|
|
347
349
|
filter: [
|
|
@@ -358,6 +360,37 @@ describe("useUser", () => {
|
|
|
358
360
|
expect(result).toEqual(salutationData);
|
|
359
361
|
});
|
|
360
362
|
|
|
363
|
+
it("loadCountry / loadSalutation use GET variants when cacheableReads is enabled", async () => {
|
|
364
|
+
const { vm, injections } = useSetup(() => useUser(), {
|
|
365
|
+
shopware: { cacheableReads: true },
|
|
366
|
+
});
|
|
367
|
+
injections.apiClient.invoke.mockResolvedValue({ data: {} });
|
|
368
|
+
|
|
369
|
+
await vm.loadCountry("country-id");
|
|
370
|
+
expect(injections.apiClient.invoke).toHaveBeenCalledWith(
|
|
371
|
+
expect.stringContaining("readCountryGet get"),
|
|
372
|
+
expect.objectContaining({
|
|
373
|
+
query: {
|
|
374
|
+
_criteria: encodeForQuery({
|
|
375
|
+
filter: [{ field: "id", type: "equals", value: "country-id" }],
|
|
376
|
+
}),
|
|
377
|
+
},
|
|
378
|
+
}),
|
|
379
|
+
);
|
|
380
|
+
|
|
381
|
+
await vm.loadSalutation("salutation-id");
|
|
382
|
+
expect(injections.apiClient.invoke).toHaveBeenCalledWith(
|
|
383
|
+
expect.stringContaining("readSalutationGet get"),
|
|
384
|
+
expect.objectContaining({
|
|
385
|
+
query: {
|
|
386
|
+
_criteria: encodeForQuery({
|
|
387
|
+
filter: [{ field: "id", type: "equals", value: "salutation-id" }],
|
|
388
|
+
}),
|
|
389
|
+
},
|
|
390
|
+
}),
|
|
391
|
+
);
|
|
392
|
+
});
|
|
393
|
+
|
|
361
394
|
it("loadSalutation with empty elements sets salutation to null", async () => {
|
|
362
395
|
const { vm, injections } = useSetup(() => useUser());
|
|
363
396
|
injections.apiClient.invoke.mockResolvedValue({
|
package/src/useUser/useUser.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
+
import { encodeForQuery } from "@shopware/api-client/helpers";
|
|
1
2
|
import { syncRefs } from "@vueuse/core";
|
|
2
3
|
import { computed, ref } from "vue";
|
|
3
4
|
import type { ComputedRef, Ref } from "vue";
|
|
5
|
+
|
|
4
6
|
import {
|
|
5
7
|
useCart,
|
|
6
8
|
useContext,
|
|
@@ -129,7 +131,7 @@ export type UseUserReturn = {
|
|
|
129
131
|
* @category Customer & Account
|
|
130
132
|
*/
|
|
131
133
|
export function useUser(): UseUserReturn {
|
|
132
|
-
const { apiClient } = useShopwareContext();
|
|
134
|
+
const { apiClient, cacheableReads } = useShopwareContext();
|
|
133
135
|
const { userFromContext, refreshSessionContext } = useSessionContext();
|
|
134
136
|
|
|
135
137
|
const _user = useContext<Schemas["Customer"] | undefined>("customer");
|
|
@@ -224,17 +226,22 @@ export function useUser(): UseUserReturn {
|
|
|
224
226
|
async function loadCountry(
|
|
225
227
|
countryId: string,
|
|
226
228
|
): Promise<operations["readCountry post /country"]["response"]> {
|
|
227
|
-
const
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
229
|
+
const criteria = {
|
|
230
|
+
filter: [
|
|
231
|
+
{
|
|
232
|
+
field: "id",
|
|
233
|
+
type: "equals" as const,
|
|
234
|
+
value: countryId,
|
|
235
|
+
},
|
|
236
|
+
],
|
|
237
|
+
};
|
|
238
|
+
const countries = cacheableReads
|
|
239
|
+
? await apiClient.invoke("readCountryGet get /country", {
|
|
240
|
+
query: { _criteria: encodeForQuery(criteria) },
|
|
241
|
+
})
|
|
242
|
+
: await apiClient.invoke("readCountry post /country", {
|
|
243
|
+
body: criteria,
|
|
244
|
+
});
|
|
238
245
|
|
|
239
246
|
country.value = countries.data.elements?.[0] ?? null;
|
|
240
247
|
return countries.data;
|
|
@@ -243,20 +250,22 @@ export function useUser(): UseUserReturn {
|
|
|
243
250
|
async function loadSalutation(
|
|
244
251
|
salutationId: string,
|
|
245
252
|
): Promise<operations["readSalutation post /salutation"]["response"]> {
|
|
246
|
-
const
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
field: "id",
|
|
253
|
-
type: "equals",
|
|
254
|
-
value: salutationId,
|
|
255
|
-
},
|
|
256
|
-
],
|
|
253
|
+
const criteria = {
|
|
254
|
+
filter: [
|
|
255
|
+
{
|
|
256
|
+
field: "id",
|
|
257
|
+
type: "equals" as const,
|
|
258
|
+
value: salutationId,
|
|
257
259
|
},
|
|
258
|
-
|
|
259
|
-
|
|
260
|
+
],
|
|
261
|
+
};
|
|
262
|
+
const salutations = cacheableReads
|
|
263
|
+
? await apiClient.invoke("readSalutationGet get /salutation", {
|
|
264
|
+
query: { _criteria: encodeForQuery(criteria) },
|
|
265
|
+
})
|
|
266
|
+
: await apiClient.invoke("readSalutation post /salutation", {
|
|
267
|
+
body: criteria,
|
|
268
|
+
});
|
|
260
269
|
salutation.value = salutations.data.elements?.[0] ?? null;
|
|
261
270
|
return salutations.data;
|
|
262
271
|
}
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
2
|
import { computed, ref } from "vue";
|
|
3
|
+
|
|
3
4
|
import { useSyncWishlist, useUser, useWishlist } from "#imports";
|
|
4
5
|
import type { Schemas } from "#shopware";
|
|
6
|
+
|
|
5
7
|
import { useSetup } from "../_test";
|
|
6
8
|
|
|
7
9
|
const getMockedUser = (isLoggedIn: boolean, isGuestSession: boolean) =>
|