@shopware/composables 1.11.1 → 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.
Files changed (99) hide show
  1. package/README.md +12 -11
  2. package/dist/index.d.mts +26 -1
  3. package/dist/index.d.ts +26 -1
  4. package/dist/index.mjs +125 -75
  5. package/package.json +11 -12
  6. package/src/cms/useCmsElementConfig.test.ts +1 -0
  7. package/src/cms/useCmsElementConfig.ts +1 -0
  8. package/src/cms/useCmsElementImage.test.ts +1 -0
  9. package/src/cms/useCmsElementImage.ts +2 -0
  10. package/src/cms/useCmsElementProductBox.test.ts +1 -0
  11. package/src/cms/useCmsElementProductBox.ts +2 -0
  12. package/src/createShopwareContext.test.ts +1 -0
  13. package/src/createShopwareContext.ts +8 -0
  14. package/src/devtools/plugin.ts +13 -13
  15. package/src/helpers/ContextError.spec.ts +1 -0
  16. package/src/index.test.ts +2 -0
  17. package/src/index.ts +1 -0
  18. package/src/types/cmsElementTypes.ts +1 -0
  19. package/src/useAddToCart/useAddToCart.test.ts +1 -0
  20. package/src/useAddToCart/useAddToCart.ts +1 -0
  21. package/src/useAddress/useAddress.test.ts +2 -0
  22. package/src/useAddress/useAddress.ts +1 -0
  23. package/src/useB2bQuoteManagement/useB2bQuoteManagement.test.ts +1 -0
  24. package/src/useBreadcrumbs/useBreadcrumbs.test.ts +2 -0
  25. package/src/useBreadcrumbs/useBreadcrumbs.ts +1 -0
  26. package/src/useCart/useCart.test.ts +2 -0
  27. package/src/useCart/useCart.ts +1 -0
  28. package/src/useCartErrorParamsResolver/useCartErrorParamsResolver.test.ts +1 -0
  29. package/src/useCartItem/useCartItem.test.ts +2 -0
  30. package/src/useCartItem/useCartItem.ts +1 -0
  31. package/src/useCartNotification/useCartNotification.test.ts +2 -0
  32. package/src/useCategory/useCategory.test.ts +2 -0
  33. package/src/useCategory/useCategory.ts +2 -0
  34. package/src/useCategorySearch/useCategorySearch.test.ts +29 -0
  35. package/src/useCategorySearch/useCategorySearch.ts +15 -7
  36. package/src/useCheckout/useCheckout.test.ts +1 -0
  37. package/src/useCheckout/useCheckout.ts +1 -0
  38. package/src/useCmsBlock/useCmsBlock.test.ts +2 -0
  39. package/src/useCmsMeta/useCmsMeta.test.ts +2 -0
  40. package/src/useCmsMeta/useCmsMeta.ts +1 -0
  41. package/src/useCmsSection/useCmsSection.test.ts +2 -0
  42. package/src/useCmsTranslations/useCmsTranslations.test.ts +2 -0
  43. package/src/useCountries/useCountries.test.ts +19 -0
  44. package/src/useCountries/useCountries.ts +14 -7
  45. package/src/useCustomerOrders/useCustomerOrders.test.ts +1 -0
  46. package/src/useCustomerOrders/useCustomerOrders.ts +1 -0
  47. package/src/useCustomerPassword/useCustomerPassword.test.ts +1 -0
  48. package/src/useDefaultOrderAssociations/useDefaultOrderAssociations.test.ts +1 -0
  49. package/src/useInternationalization/useInternationalization.test.ts +14 -1
  50. package/src/useInternationalization/useInternationalization.ts +5 -3
  51. package/src/useLandingSearch/useLandingSearch.test.ts +1 -0
  52. package/src/useLandingSearch/useLandingSearch.ts +1 -0
  53. package/src/useListing/useListing.test.ts +2 -0
  54. package/src/useListing/useListing.ts +3 -2
  55. package/src/useLocalWishlist/useLocalWishlist.test.ts +1 -0
  56. package/src/useNavigation/useNavigation.test.ts +24 -1
  57. package/src/useNavigation/useNavigation.ts +36 -14
  58. package/src/useNavigationContext/useNavigationContext.test.ts +1 -0
  59. package/src/useNavigationContext/useNavigationContext.ts +1 -0
  60. package/src/useNavigationSearch/useNavigationSearch.test.ts +30 -0
  61. package/src/useNavigationSearch/useNavigationSearch.ts +19 -12
  62. package/src/useNewsletter/useNewsletter.test.ts +1 -0
  63. package/src/useNewsletter/useNewsletter.ts +1 -0
  64. package/src/useNotifications/useNotifications.test.ts +1 -0
  65. package/src/useOrderDetails/useOrderDetails.test.ts +1 -0
  66. package/src/useOrderDetails/useOrderDetails.ts +1 -0
  67. package/src/useOrderPayment/useOrderPayment.test.ts +2 -0
  68. package/src/useOrderPayment/useOrderPayment.ts +1 -0
  69. package/src/usePrice/usePrice.test.ts +2 -0
  70. package/src/usePrice/usePrice.ts +1 -0
  71. package/src/useProduct/useProduct.test.ts +2 -0
  72. package/src/useProduct/useProduct.ts +2 -0
  73. package/src/useProductAssociations/useProductAssociations.spec.ts +48 -0
  74. package/src/useProductAssociations/useProductAssociations.ts +7 -3
  75. package/src/useProductConfigurator/useProductConfigurator.spec.ts +24 -0
  76. package/src/useProductConfigurator/useProductConfigurator.ts +20 -13
  77. package/src/useProductPrice/useProductPrice.test.ts +2 -0
  78. package/src/useProductPrice/useProductPrice.ts +1 -0
  79. package/src/useProductReviews/useProductReviews.test.ts +1 -0
  80. package/src/useProductReviews/useProductReviews.ts +1 -0
  81. package/src/useProductSearch/useProductSearch.test.ts +24 -1
  82. package/src/useProductSearch/useProductSearch.ts +24 -11
  83. package/src/useProductSearchSuggest/useProductSearchSuggest.test.ts +1 -0
  84. package/src/useProductSearchSuggest/useProductSearchSuggest.ts +1 -0
  85. package/src/useProductWishlist/useProductWishlist.test.ts +2 -0
  86. package/src/useProductWishlist/useProductWishlist.ts +1 -0
  87. package/src/useSalutations/useSalutations.spec.ts +15 -0
  88. package/src/useSalutations/useSalutations.ts +5 -2
  89. package/src/useSessionContext/useSessionContext.test.ts +18 -0
  90. package/src/useSessionContext/useSessionContext.ts +10 -0
  91. package/src/useShopwareContext/useShopwareContext.test.ts +1 -0
  92. package/src/useShopwareContext/useShopwareContext.ts +13 -0
  93. package/src/useSyncWishlist/useSyncWishlist.test.ts +1 -0
  94. package/src/useSyncWishlist/useSyncWishlist.ts +1 -0
  95. package/src/useUrlResolver/useUrlResolver.test.ts +1 -0
  96. package/src/useUser/useUser.test.ts +35 -2
  97. package/src/useUser/useUser.ts +34 -25
  98. package/src/useWishlist/useWishlist.test.ts +2 -0
  99. package/src/useWishlist/useWishlist.ts +1 -0
@@ -1,5 +1,6 @@
1
1
  import { computed } from "vue";
2
2
  import type { ComputedRef } from "vue";
3
+
3
4
  import { useLocalWishlist, useSyncWishlist, useUser } from "#imports";
4
5
 
5
6
  export type UseProductWishlistReturn = {
@@ -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 = await apiClient.invoke("readSalutation post /salutation");
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,4 +1,5 @@
1
1
  import { describe, expect, it } from "vitest";
2
+
2
3
  import { useShopwareContext } from "./useShopwareContext";
3
4
 
4
5
  describe("useShopwareContext", () => {
@@ -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,7 @@
1
1
  import { ApiClientError } from "@shopware/api-client";
2
2
  import type { FetchResponse } from "ofetch";
3
3
  import { beforeEach, describe, expect, it, vi } from "vitest";
4
+
4
5
  import { useSetup } from "../_test";
5
6
  import { useSyncWishlist } from "./useSyncWishlist";
6
7
 
@@ -1,5 +1,6 @@
1
1
  import { computed, ref } from "vue";
2
2
  import type { ComputedRef, Ref } from "vue";
3
+
3
4
  import { useShopwareContext } from "#imports";
4
5
  import type { Schemas } from "#shopware";
5
6
 
@@ -1,4 +1,5 @@
1
1
  import { describe, expect, it } from "vitest";
2
+
2
3
  import { useSetup } from "../_test";
3
4
  import { useUrlResolver } from "./useUrlResolver";
4
5
 
@@ -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({
@@ -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 countries = await apiClient.invoke("readCountry post /country", {
228
- body: {
229
- filter: [
230
- {
231
- field: "id",
232
- type: "equals",
233
- value: countryId,
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 salutations = await apiClient.invoke(
247
- "readSalutation post /salutation",
248
- {
249
- body: {
250
- filter: [
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) =>
@@ -4,6 +4,7 @@ import { defu } from "defu";
4
4
  */
5
5
  import { computed, ref } from "vue";
6
6
  import type { ComputedRef } from "vue";
7
+
7
8
  import { useLocalWishlist, useSyncWishlist, useUser } from "#imports";
8
9
  import type { Schemas } from "#shopware";
9
10