@shopware/composables 1.11.1 → 1.12.1

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 (100) hide show
  1. package/README.md +18 -13
  2. package/dist/index.d.mts +35 -5
  3. package/dist/index.d.ts +35 -5
  4. package/dist/index.mjs +167 -100
  5. package/nuxt.config.ts +10 -0
  6. package/package.json +14 -15
  7. package/src/cms/useCmsElementConfig.test.ts +1 -0
  8. package/src/cms/useCmsElementConfig.ts +1 -0
  9. package/src/cms/useCmsElementImage.test.ts +1 -0
  10. package/src/cms/useCmsElementImage.ts +2 -0
  11. package/src/cms/useCmsElementProductBox.test.ts +1 -0
  12. package/src/cms/useCmsElementProductBox.ts +2 -0
  13. package/src/createShopwareContext.test.ts +1 -0
  14. package/src/createShopwareContext.ts +8 -0
  15. package/src/devtools/plugin.ts +13 -13
  16. package/src/helpers/ContextError.spec.ts +1 -0
  17. package/src/index.test.ts +2 -0
  18. package/src/index.ts +1 -0
  19. package/src/types/cmsElementTypes.ts +1 -0
  20. package/src/useAddToCart/useAddToCart.test.ts +1 -0
  21. package/src/useAddToCart/useAddToCart.ts +1 -0
  22. package/src/useAddress/useAddress.test.ts +2 -0
  23. package/src/useAddress/useAddress.ts +1 -0
  24. package/src/useB2bQuoteManagement/useB2bQuoteManagement.test.ts +1 -0
  25. package/src/useBreadcrumbs/useBreadcrumbs.test.ts +2 -0
  26. package/src/useBreadcrumbs/useBreadcrumbs.ts +2 -1
  27. package/src/useCart/useCart.test.ts +19 -0
  28. package/src/useCart/useCart.ts +6 -3
  29. package/src/useCartErrorParamsResolver/useCartErrorParamsResolver.test.ts +1 -0
  30. package/src/useCartItem/useCartItem.test.ts +15 -0
  31. package/src/useCartItem/useCartItem.ts +15 -7
  32. package/src/useCartNotification/useCartNotification.test.ts +2 -0
  33. package/src/useCategory/useCategory.test.ts +2 -0
  34. package/src/useCategory/useCategory.ts +2 -0
  35. package/src/useCategorySearch/useCategorySearch.test.ts +104 -2
  36. package/src/useCategorySearch/useCategorySearch.ts +45 -23
  37. package/src/useCheckout/useCheckout.test.ts +1 -0
  38. package/src/useCheckout/useCheckout.ts +1 -0
  39. package/src/useCmsBlock/useCmsBlock.test.ts +2 -0
  40. package/src/useCmsMeta/useCmsMeta.test.ts +2 -0
  41. package/src/useCmsMeta/useCmsMeta.ts +1 -0
  42. package/src/useCmsSection/useCmsSection.test.ts +2 -0
  43. package/src/useCmsTranslations/useCmsTranslations.test.ts +2 -0
  44. package/src/useCountries/useCountries.test.ts +96 -6
  45. package/src/useCountries/useCountries.ts +19 -8
  46. package/src/useCustomerOrders/useCustomerOrders.test.ts +1 -0
  47. package/src/useCustomerOrders/useCustomerOrders.ts +1 -0
  48. package/src/useCustomerPassword/useCustomerPassword.test.ts +1 -0
  49. package/src/useDefaultOrderAssociations/useDefaultOrderAssociations.test.ts +1 -0
  50. package/src/useInternationalization/useInternationalization.test.ts +14 -1
  51. package/src/useInternationalization/useInternationalization.ts +5 -3
  52. package/src/useLandingSearch/useLandingSearch.test.ts +1 -0
  53. package/src/useLandingSearch/useLandingSearch.ts +1 -0
  54. package/src/useListing/useListing.test.ts +2 -0
  55. package/src/useListing/useListing.ts +3 -2
  56. package/src/useLocalWishlist/useLocalWishlist.test.ts +1 -0
  57. package/src/useNavigation/useNavigation.test.ts +24 -1
  58. package/src/useNavigation/useNavigation.ts +36 -14
  59. package/src/useNavigationContext/useNavigationContext.test.ts +1 -0
  60. package/src/useNavigationContext/useNavigationContext.ts +1 -0
  61. package/src/useNavigationSearch/useNavigationSearch.test.ts +30 -0
  62. package/src/useNavigationSearch/useNavigationSearch.ts +19 -12
  63. package/src/useNewsletter/useNewsletter.test.ts +1 -0
  64. package/src/useNewsletter/useNewsletter.ts +1 -0
  65. package/src/useNotifications/useNotifications.test.ts +1 -0
  66. package/src/useOrderDetails/useOrderDetails.test.ts +1 -0
  67. package/src/useOrderDetails/useOrderDetails.ts +1 -0
  68. package/src/useOrderPayment/useOrderPayment.test.ts +2 -0
  69. package/src/useOrderPayment/useOrderPayment.ts +1 -0
  70. package/src/usePrice/usePrice.test.ts +2 -0
  71. package/src/usePrice/usePrice.ts +1 -0
  72. package/src/useProduct/useProduct.test.ts +2 -0
  73. package/src/useProduct/useProduct.ts +2 -0
  74. package/src/useProductAssociations/useProductAssociations.spec.ts +48 -0
  75. package/src/useProductAssociations/useProductAssociations.ts +7 -3
  76. package/src/useProductConfigurator/useProductConfigurator.spec.ts +24 -0
  77. package/src/useProductConfigurator/useProductConfigurator.ts +20 -13
  78. package/src/useProductPrice/useProductPrice.test.ts +2 -0
  79. package/src/useProductPrice/useProductPrice.ts +1 -0
  80. package/src/useProductReviews/useProductReviews.test.ts +42 -2
  81. package/src/useProductReviews/useProductReviews.ts +18 -8
  82. package/src/useProductSearch/useProductSearch.test.ts +24 -1
  83. package/src/useProductSearch/useProductSearch.ts +24 -11
  84. package/src/useProductSearchSuggest/useProductSearchSuggest.test.ts +1 -0
  85. package/src/useProductSearchSuggest/useProductSearchSuggest.ts +1 -0
  86. package/src/useProductWishlist/useProductWishlist.test.ts +2 -0
  87. package/src/useProductWishlist/useProductWishlist.ts +1 -0
  88. package/src/useSalutations/useSalutations.spec.ts +15 -0
  89. package/src/useSalutations/useSalutations.ts +5 -2
  90. package/src/useSessionContext/useSessionContext.test.ts +84 -6
  91. package/src/useSessionContext/useSessionContext.ts +17 -2
  92. package/src/useShopwareContext/useShopwareContext.test.ts +1 -0
  93. package/src/useShopwareContext/useShopwareContext.ts +13 -0
  94. package/src/useSyncWishlist/useSyncWishlist.test.ts +1 -0
  95. package/src/useSyncWishlist/useSyncWishlist.ts +1 -0
  96. package/src/useUrlResolver/useUrlResolver.test.ts +1 -0
  97. package/src/useUser/useUser.test.ts +55 -2
  98. package/src/useUser/useUser.ts +34 -25
  99. package/src/useWishlist/useWishlist.test.ts +2 -0
  100. package/src/useWishlist/useWishlist.ts +1 -0
@@ -21,6 +21,10 @@ export type UseSessionContextReturn = {
21
21
  sessionContext: ComputedRef<Schemas["SalesChannelContext"] | undefined>;
22
22
  /**
23
23
  * Fetches the session context and assigns the result to the `sessionContext` property
24
+ *
25
+ * Throws when the context cannot be loaded. Callers that depend on a verified
26
+ * context switch, such as logout or checkout flows, should block progression
27
+ * when this fails.
24
28
  */
25
29
  refreshSessionContext(): Promise<void>;
26
30
  /**
@@ -103,6 +107,12 @@ export type UseSessionContextReturn = {
103
107
  * current language code
104
108
  */
105
109
  currentLanguageId: ComputedRef<string | undefined>;
110
+ /**
111
+ * Locale code of the active language (e.g. `"en-GB"`), read from the
112
+ * context's `languageInfo`. Available from the session context alone — no
113
+ * separate language list request is required to detect the current locale.
114
+ */
115
+ currentLocaleCode: ComputedRef<string | undefined>;
106
116
  /**
107
117
  * current context's customer object
108
118
  */
@@ -131,6 +141,7 @@ export function useSessionContext(
131
141
  _sessionContext.value = data;
132
142
  } catch (e) {
133
143
  console.error("[UseSessionContext][refreshSessionContext]", e);
144
+ throw e;
134
145
  }
135
146
  };
136
147
 
@@ -221,7 +232,7 @@ export function useSessionContext(
221
232
  shippingAddressId: address.id,
222
233
  },
223
234
  });
224
- refreshSessionContext();
235
+ await refreshSessionContext();
225
236
  };
226
237
 
227
238
  // TODO: replace the source from defaultBillingAddress by new value once NEXT-28627 is solved
@@ -241,7 +252,7 @@ export function useSessionContext(
241
252
  billingAddressId: address.id,
242
253
  },
243
254
  });
244
- refreshSessionContext();
255
+ await refreshSessionContext();
245
256
  };
246
257
 
247
258
  const setContext = (context: Schemas["SalesChannelContext"]) => {
@@ -262,6 +273,9 @@ export function useSessionContext(
262
273
  const languageIdChain = computed(
263
274
  () => sessionContext.value?.context?.languageIdChain?.[0] || "",
264
275
  );
276
+ const currentLocaleCode = computed(
277
+ () => sessionContext.value?.languageInfo?.localeCode,
278
+ );
265
279
  const taxState = computed(() => sessionContext.value?.context?.taxState);
266
280
  const userFromContext = computed(() => sessionContext.value?.customer);
267
281
 
@@ -287,6 +301,7 @@ export function useSessionContext(
287
301
  languageIdChain,
288
302
  salesChannelLanguageId: languageId,
289
303
  currentLanguageId: languageIdChain,
304
+ currentLocaleCode,
290
305
  setCountry,
291
306
  setContext,
292
307
  };
@@ -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
 
@@ -54,6 +56,8 @@ const REGISTRATION_DATA: Omit<
54
56
 
55
57
  describe("useUser", () => {
56
58
  afterEach(() => {
59
+ vi.clearAllMocks();
60
+ refreshSessionContextSpy.mockReset();
57
61
  userFromContextRef.value = undefined;
58
62
  });
59
63
 
@@ -149,9 +153,27 @@ describe("useUser", () => {
149
153
  expect(injections.apiClient.invoke).toHaveBeenCalledWith(
150
154
  expect.stringContaining("logoutCustomer"),
151
155
  );
156
+ expect(refreshSessionContextSpy).toHaveBeenCalled();
157
+ expect(refreshCartSpy).toHaveBeenCalled();
152
158
  expect(result).toEqual(logoutResponse.data);
153
159
  });
154
160
 
161
+ it("logout rejects when the session context cannot be verified", async () => {
162
+ const { vm, injections } = useSetup(() => useUser());
163
+ const error = new Error("context refresh failed");
164
+ injections.apiClient.invoke.mockResolvedValue({
165
+ data: { redirectUrl: "/" },
166
+ });
167
+ refreshSessionContextSpy.mockRejectedValue(error);
168
+
169
+ await expect(vm.logout()).rejects.toThrow(error);
170
+
171
+ expect(injections.apiClient.invoke).toHaveBeenCalledWith(
172
+ expect.stringContaining("logoutCustomer"),
173
+ );
174
+ expect(refreshCartSpy).not.toHaveBeenCalled();
175
+ });
176
+
155
177
  it("refreshUser", async () => {
156
178
  const { vm, injections } = useSetup(() => useUser());
157
179
  const customerData = { id: "cust-1", email: "test@test.com" };
@@ -187,7 +209,7 @@ describe("useUser", () => {
187
209
  await vm.loadCountry(countryId);
188
210
 
189
211
  expect(injections.apiClient.invoke).toHaveBeenCalledWith(
190
- expect.stringContaining("readCountry"),
212
+ expect.stringContaining("readCountry post"),
191
213
  expect.objectContaining({
192
214
  body: {
193
215
  filter: [
@@ -341,7 +363,7 @@ describe("useUser", () => {
341
363
  const result = await vm.loadSalutation("test");
342
364
 
343
365
  expect(injections.apiClient.invoke).toHaveBeenCalledWith(
344
- expect.stringContaining("readSalutation"),
366
+ expect.stringContaining("readSalutation post"),
345
367
  expect.objectContaining({
346
368
  body: {
347
369
  filter: [
@@ -358,6 +380,37 @@ describe("useUser", () => {
358
380
  expect(result).toEqual(salutationData);
359
381
  });
360
382
 
383
+ it("loadCountry / loadSalutation use GET variants when cacheableReads is enabled", async () => {
384
+ const { vm, injections } = useSetup(() => useUser(), {
385
+ shopware: { cacheableReads: true },
386
+ });
387
+ injections.apiClient.invoke.mockResolvedValue({ data: {} });
388
+
389
+ await vm.loadCountry("country-id");
390
+ expect(injections.apiClient.invoke).toHaveBeenCalledWith(
391
+ expect.stringContaining("readCountryGet get"),
392
+ expect.objectContaining({
393
+ query: {
394
+ _criteria: encodeForQuery({
395
+ filter: [{ field: "id", type: "equals", value: "country-id" }],
396
+ }),
397
+ },
398
+ }),
399
+ );
400
+
401
+ await vm.loadSalutation("salutation-id");
402
+ expect(injections.apiClient.invoke).toHaveBeenCalledWith(
403
+ expect.stringContaining("readSalutationGet get"),
404
+ expect.objectContaining({
405
+ query: {
406
+ _criteria: encodeForQuery({
407
+ filter: [{ field: "id", type: "equals", value: "salutation-id" }],
408
+ }),
409
+ },
410
+ }),
411
+ );
412
+ });
413
+
361
414
  it("loadSalutation with empty elements sets salutation to null", async () => {
362
415
  const { vm, injections } = useSetup(() => useUser());
363
416
  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