@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.
- package/LICENSE +21 -0
- package/README.md +172 -0
- package/composables/index.ts +1 -0
- package/dist/index.d.mts +2239 -0
- package/dist/index.d.ts +2239 -0
- package/dist/index.mjs +2686 -0
- package/nuxt.config.ts +14 -0
- package/package.json +83 -0
- package/src/_test/index.ts +1 -0
- package/src/_test/useSetup.ts +43 -0
- package/src/cms/README.md +9 -0
- package/src/cms/cmsAssociations.ts +28 -0
- package/src/cms/index.ts +2 -0
- package/src/cms/useCmsElementConfig.test.ts +145 -0
- package/src/cms/useCmsElementConfig.ts +29 -0
- package/src/cms/useCmsElementImage.test.ts +179 -0
- package/src/cms/useCmsElementImage.ts +103 -0
- package/src/cms/useCmsElementProductBox.test.ts +15 -0
- package/src/cms/useCmsElementProductBox.ts +25 -0
- package/src/createShopwareContext.test.ts +23 -0
- package/src/createShopwareContext.ts +43 -0
- package/src/devtools/plugin.ts +201 -0
- package/src/helpers/ContextError.spec.ts +20 -0
- package/src/helpers/ContextError.ts +6 -0
- package/src/index.test.ts +54 -0
- package/src/index.ts +111 -0
- package/src/mocks/Cart.ts +774 -0
- package/src/mocks/CmsPage.ts +544 -0
- package/src/mocks/CmsProductBox.ts +377 -0
- package/src/mocks/Configurator.ts +123 -0
- package/src/mocks/Country.ts +184 -0
- package/src/mocks/CrossSellingResponse.ts +552 -0
- package/src/mocks/LandingPage.ts +62 -0
- package/src/mocks/Menu.ts +598 -0
- package/src/mocks/Order.ts +1151 -0
- package/src/mocks/Product.ts +473 -0
- package/src/mocks/ProductTierPrices.ts +771 -0
- package/src/mocks/Salutations.ts +51 -0
- package/src/mocks/Search.ts +518 -0
- package/src/types/cmsBlockTypes.ts +80 -0
- package/src/types/cmsElementTypes.ts +381 -0
- package/src/types/cmsSectionTypes.ts +15 -0
- package/src/types/index.ts +52 -0
- package/src/useAddToCart/useAddToCart.test.ts +39 -0
- package/src/useAddToCart/useAddToCart.ts +90 -0
- package/src/useAddress/useAddress.test.ts +134 -0
- package/src/useAddress/useAddress.ts +202 -0
- package/src/useB2bQuoteManagement/useB2bQuoteManagement.test.ts +137 -0
- package/src/useB2bQuoteManagement/useB2bQuoteManagement.ts +225 -0
- package/src/useBreadcrumbs/useBreadcrumbs.test.ts +71 -0
- package/src/useBreadcrumbs/useBreadcrumbs.ts +89 -0
- package/src/useCart/useCart.test.ts +251 -0
- package/src/useCart/useCart.ts +314 -0
- package/src/useCartErrorParamsResolver/useCartErrorParamsResolver.test.ts +122 -0
- package/src/useCartErrorParamsResolver/useCartErrorParamsResolver.ts +80 -0
- package/src/useCartItem/useCartItem.test.ts +168 -0
- package/src/useCartItem/useCartItem.ts +157 -0
- package/src/useCartNotification/useCartNotification.test.ts +129 -0
- package/src/useCartNotification/useCartNotification.ts +63 -0
- package/src/useCategory/useCategory.test.ts +14 -0
- package/src/useCategory/useCategory.ts +32 -0
- package/src/useCategorySearch/useCategorySearch.test.ts +98 -0
- package/src/useCategorySearch/useCategorySearch.ts +81 -0
- package/src/useCheckout/useCheckout.test.ts +104 -0
- package/src/useCheckout/useCheckout.ts +163 -0
- package/src/useCmsBlock/useCmsBlock.test.ts +25 -0
- package/src/useCmsBlock/useCmsBlock.ts +36 -0
- package/src/useCmsMeta/useCmsMeta.test.ts +17 -0
- package/src/useCmsMeta/useCmsMeta.ts +46 -0
- package/src/useCmsSection/useCmsSection.test.ts +18 -0
- package/src/useCmsSection/useCmsSection.ts +39 -0
- package/src/useCmsTranslations/useCmsTranslations.test.ts +10 -0
- package/src/useCmsTranslations/useCmsTranslations.ts +9 -0
- package/src/useContext/useContext.ts +36 -0
- package/src/useCountries/useCountries.test.ts +64 -0
- package/src/useCountries/useCountries.ts +64 -0
- package/src/useCustomerOrders/useCustomerOrders.test.ts +47 -0
- package/src/useCustomerOrders/useCustomerOrders.ts +89 -0
- package/src/useCustomerPassword/useCustomerPassword.test.ts +164 -0
- package/src/useCustomerPassword/useCustomerPassword.ts +57 -0
- package/src/useDefaultOrderAssociations/useDefaultOrderAssociations.test.ts +41 -0
- package/src/useDefaultOrderAssociations/useDefaultOrderAssociations.ts +45 -0
- package/src/useInternationalization/useInternationalization.test.ts +155 -0
- package/src/useInternationalization/useInternationalization.ts +159 -0
- package/src/useLandingSearch/useLandingSearch.test.ts +30 -0
- package/src/useLandingSearch/useLandingSearch.ts +58 -0
- package/src/useListing/useListing.test.ts +249 -0
- package/src/useListing/useListing.ts +592 -0
- package/src/useLocalWishlist/useLocalWishlist.test.ts +54 -0
- package/src/useLocalWishlist/useLocalWishlist.ts +95 -0
- package/src/useNavigation/useNavigation.test.ts +68 -0
- package/src/useNavigation/useNavigation.ts +83 -0
- package/src/useNavigationContext/useNavigationContext.test.ts +48 -0
- package/src/useNavigationContext/useNavigationContext.ts +39 -0
- package/src/useNavigationSearch/useNavigationSearch.test.ts +110 -0
- package/src/useNavigationSearch/useNavigationSearch.ts +60 -0
- package/src/useNewsletter/useNewsletter.test.ts +55 -0
- package/src/useNewsletter/useNewsletter.ts +109 -0
- package/src/useNotifications/useNotifications.test.ts +56 -0
- package/src/useNotifications/useNotifications.ts +119 -0
- package/src/useOrderDetails/useOrderDetails.test.ts +224 -0
- package/src/useOrderDetails/useOrderDetails.ts +329 -0
- package/src/useOrderPayment/useOrderPayment.test.ts +80 -0
- package/src/useOrderPayment/useOrderPayment.ts +119 -0
- package/src/usePrice/usePrice.test.ts +97 -0
- package/src/usePrice/usePrice.ts +106 -0
- package/src/useProduct/useProduct.test.ts +36 -0
- package/src/useProduct/useProduct.ts +56 -0
- package/src/useProductAssociations/useProductAssociations.spec.ts +105 -0
- package/src/useProductAssociations/useProductAssociations.ts +107 -0
- package/src/useProductConfigurator/useProductConfigurator.spec.ts +77 -0
- package/src/useProductConfigurator/useProductConfigurator.ts +134 -0
- package/src/useProductPrice/useProductPrice.test.ts +126 -0
- package/src/useProductPrice/useProductPrice.ts +131 -0
- package/src/useProductReviews/useProductReviews.test.ts +45 -0
- package/src/useProductReviews/useProductReviews.ts +80 -0
- package/src/useProductSearch/useProductSearch.test.ts +37 -0
- package/src/useProductSearch/useProductSearch.ts +58 -0
- package/src/useProductSearchSuggest/useProductSearchSuggest.test.ts +19 -0
- package/src/useProductSearchSuggest/useProductSearchSuggest.ts +67 -0
- package/src/useProductWishlist/useProductWishlist.test.ts +63 -0
- package/src/useProductWishlist/useProductWishlist.ts +74 -0
- package/src/useSalutations/useSalutations.spec.ts +37 -0
- package/src/useSalutations/useSalutations.ts +55 -0
- package/src/useSessionContext/useSessionContext.test.ts +221 -0
- package/src/useSessionContext/useSessionContext.ts +281 -0
- package/src/useShopwareContext/useShopwareContext.test.ts +8 -0
- package/src/useShopwareContext/useShopwareContext.ts +31 -0
- package/src/useSyncWishlist/useSyncWishlist.test.ts +117 -0
- package/src/useSyncWishlist/useSyncWishlist.ts +117 -0
- package/src/useUrlResolver/useUrlResolver.test.ts +25 -0
- package/src/useUrlResolver/useUrlResolver.ts +31 -0
- package/src/useUser/useUser.test.ts +290 -0
- package/src/useUser/useUser.ts +321 -0
- package/src/useWishlist/useWishlist.test.ts +178 -0
- package/src/useWishlist/useWishlist.ts +128 -0
- package/types/api-types.ts +11 -0
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import type { Schemas } from "#shopware";
|
|
3
|
+
import { useSetup } from "../_test";
|
|
4
|
+
import CmsPage from "../mocks/CmsPage";
|
|
5
|
+
import { useCmsSection } from "./useCmsSection";
|
|
6
|
+
|
|
7
|
+
describe("useCmsSection", () => {
|
|
8
|
+
it("search", async () => {
|
|
9
|
+
const { vm, injections } = useSetup(() =>
|
|
10
|
+
useCmsSection(
|
|
11
|
+
CmsPage.cmsPage.sections[0] as unknown as Schemas["CmsSection"],
|
|
12
|
+
),
|
|
13
|
+
);
|
|
14
|
+
injections.apiClient.invoke.mockResolvedValue({ data: {} });
|
|
15
|
+
const block = await vm.getPositionContent("main");
|
|
16
|
+
expect(block).toEqual(CmsPage.cmsPage.sections[0].blocks);
|
|
17
|
+
});
|
|
18
|
+
});
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { Schemas } from "#shopware";
|
|
2
|
+
|
|
3
|
+
type ArrayElement<ArrayType extends readonly unknown[]> =
|
|
4
|
+
ArrayType extends readonly (infer ElementType)[] ? ElementType : never;
|
|
5
|
+
|
|
6
|
+
type UseCmsSectionType = {
|
|
7
|
+
/**
|
|
8
|
+
* CMS section
|
|
9
|
+
*/
|
|
10
|
+
section: Schemas["CmsSection"];
|
|
11
|
+
/**
|
|
12
|
+
* Position of the section
|
|
13
|
+
*/
|
|
14
|
+
getPositionContent(
|
|
15
|
+
position: ArrayElement<Schemas["CmsSection"]["blocks"]>["sectionPosition"],
|
|
16
|
+
): Array<Schemas["CmsBlock"]>;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Composable to get cms section content
|
|
21
|
+
* @public
|
|
22
|
+
* @category CMS (Shopping Experiences)
|
|
23
|
+
*/
|
|
24
|
+
export function useCmsSection<SECTION_TYPE extends Schemas["CmsSection"]>(
|
|
25
|
+
content: SECTION_TYPE,
|
|
26
|
+
): UseCmsSectionType {
|
|
27
|
+
function getPositionContent(
|
|
28
|
+
position: ArrayElement<SECTION_TYPE["blocks"]>["sectionPosition"],
|
|
29
|
+
) {
|
|
30
|
+
return content.blocks.filter(
|
|
31
|
+
(block) => block.sectionPosition === position,
|
|
32
|
+
) as Array<Schemas["CmsBlock"]>;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return {
|
|
36
|
+
section: content,
|
|
37
|
+
getPositionContent,
|
|
38
|
+
};
|
|
39
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { useCmsTranslations } from "#imports";
|
|
3
|
+
import { useSetup } from "../_test";
|
|
4
|
+
|
|
5
|
+
describe("useCmsTranslations", () => {
|
|
6
|
+
it("injection", async () => {
|
|
7
|
+
const { vm } = await useSetup(useCmsTranslations);
|
|
8
|
+
expect(vm).toEqual({});
|
|
9
|
+
});
|
|
10
|
+
});
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { injectLocal, provideLocal } from "@vueuse/core";
|
|
2
|
+
import { ref, unref } from "vue";
|
|
3
|
+
import type { Ref } from "vue";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Context helper composable to provide and inject data.
|
|
7
|
+
* It takes `injectionName` to inject the context. If no context is provided, it will create a new one and provide it.
|
|
8
|
+
* If `context` is provided in params, it will create new context, use param as value and provide it.
|
|
9
|
+
* If `replace` is provided, it will replace the existing context with the new value.
|
|
10
|
+
*
|
|
11
|
+
* @public
|
|
12
|
+
* @category Context & Language
|
|
13
|
+
*/
|
|
14
|
+
export function useContext<T>(
|
|
15
|
+
injectionName: string,
|
|
16
|
+
params?: {
|
|
17
|
+
context?: Ref<T> | T;
|
|
18
|
+
replace?: T;
|
|
19
|
+
},
|
|
20
|
+
) {
|
|
21
|
+
const isNewContext = !!params?.context;
|
|
22
|
+
|
|
23
|
+
const _context: Ref<T> = isNewContext
|
|
24
|
+
? (ref(unref(params?.context)) as Ref<T>)
|
|
25
|
+
: (injectLocal(injectionName, ref()) as Ref<T>);
|
|
26
|
+
provideLocal(injectionName, _context);
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Used for global context to replace it with new Value. Used mainly for session context
|
|
30
|
+
*/
|
|
31
|
+
if (params?.replace) {
|
|
32
|
+
_context.value = unref(params.replace);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return _context;
|
|
36
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from "vitest";
|
|
2
|
+
import { useSetup } from "../_test";
|
|
3
|
+
import CountryMock from "../mocks/Country";
|
|
4
|
+
import { useCountries } from "./useCountries";
|
|
5
|
+
|
|
6
|
+
describe("useCountries", () => {
|
|
7
|
+
it("useCountries flow", async () => {
|
|
8
|
+
const { vm, injections } = await useSetup(useCountries, {
|
|
9
|
+
apiClient: {
|
|
10
|
+
invoke: vi.fn().mockResolvedValue({
|
|
11
|
+
data: CountryMock,
|
|
12
|
+
}),
|
|
13
|
+
},
|
|
14
|
+
});
|
|
15
|
+
await vm.fetchCountries();
|
|
16
|
+
|
|
17
|
+
expect(vm.getStatesForCountry(CountryMock.elements[0].id)).toStrictEqual(
|
|
18
|
+
CountryMock.elements[0].states,
|
|
19
|
+
);
|
|
20
|
+
|
|
21
|
+
expect(injections.apiClient.invoke).toHaveBeenCalledWith(
|
|
22
|
+
expect.stringContaining("readCountry"),
|
|
23
|
+
expect.objectContaining({}),
|
|
24
|
+
);
|
|
25
|
+
|
|
26
|
+
expect(vm.getCountries).toStrictEqual(CountryMock.elements);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it("useCountries flow - no states", async () => {
|
|
30
|
+
const { vm } = await useSetup(useCountries, {
|
|
31
|
+
apiClient: {
|
|
32
|
+
invoke: vi.fn().mockResolvedValue({
|
|
33
|
+
data: {
|
|
34
|
+
elements: [
|
|
35
|
+
{
|
|
36
|
+
id: "test123",
|
|
37
|
+
},
|
|
38
|
+
],
|
|
39
|
+
},
|
|
40
|
+
}),
|
|
41
|
+
},
|
|
42
|
+
});
|
|
43
|
+
await vm.fetchCountries();
|
|
44
|
+
|
|
45
|
+
expect(vm.getStatesForCountry(CountryMock.elements[0].id)).toStrictEqual(
|
|
46
|
+
null,
|
|
47
|
+
);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it("useCountries flow - empty", async () => {
|
|
51
|
+
const { vm } = await useSetup(useCountries, {
|
|
52
|
+
apiClient: {
|
|
53
|
+
invoke: vi.fn().mockResolvedValue({
|
|
54
|
+
data: {
|
|
55
|
+
elements: null,
|
|
56
|
+
},
|
|
57
|
+
}),
|
|
58
|
+
},
|
|
59
|
+
});
|
|
60
|
+
await vm.fetchCountries();
|
|
61
|
+
|
|
62
|
+
expect(vm.getCountries).toStrictEqual([]);
|
|
63
|
+
});
|
|
64
|
+
});
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { computed, inject, onMounted, provide, ref } from "vue";
|
|
2
|
+
import type { ComputedRef } from "vue";
|
|
3
|
+
import { useShopwareContext } from "#imports";
|
|
4
|
+
import type { Schemas, operations } from "#shopware";
|
|
5
|
+
|
|
6
|
+
export type UseCountriesReturn = {
|
|
7
|
+
mountedCallback(): Promise<void>;
|
|
8
|
+
getCountries: ComputedRef<Schemas["Country"][]>;
|
|
9
|
+
fetchCountries(): Promise<
|
|
10
|
+
operations["readCountry post /country"]["response"]
|
|
11
|
+
>;
|
|
12
|
+
getStatesForCountry(countryId: string): Schemas["CountryState"][] | null;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Composable to manage countries
|
|
17
|
+
* @public
|
|
18
|
+
* @category Context & Language
|
|
19
|
+
*/
|
|
20
|
+
export function useCountries(): UseCountriesReturn {
|
|
21
|
+
const { apiClient } = useShopwareContext();
|
|
22
|
+
|
|
23
|
+
const _sharedCountries = inject("swCountries", ref());
|
|
24
|
+
provide("swCountries", _sharedCountries);
|
|
25
|
+
|
|
26
|
+
async function fetchCountries() {
|
|
27
|
+
const result = await apiClient.invoke("readCountry post /country", {
|
|
28
|
+
body: {
|
|
29
|
+
associations: {
|
|
30
|
+
states: {},
|
|
31
|
+
},
|
|
32
|
+
},
|
|
33
|
+
});
|
|
34
|
+
_sharedCountries.value = result.data.elements;
|
|
35
|
+
return result.data;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const getCountries = computed(() => {
|
|
39
|
+
return _sharedCountries.value ?? [];
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
const mountedCallback = async () => {
|
|
43
|
+
if (!_sharedCountries.value) {
|
|
44
|
+
await fetchCountries();
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
const getStatesForCountry = (countryId: string) => {
|
|
49
|
+
return (
|
|
50
|
+
getCountries.value.find((element: Schemas["Country"]) => {
|
|
51
|
+
return element.id === countryId;
|
|
52
|
+
})?.states || null
|
|
53
|
+
);
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
onMounted(mountedCallback);
|
|
57
|
+
|
|
58
|
+
return {
|
|
59
|
+
mountedCallback,
|
|
60
|
+
fetchCountries,
|
|
61
|
+
getStatesForCountry,
|
|
62
|
+
getCountries,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { useSetup } from "../_test";
|
|
3
|
+
import { useCustomerOrders } from "./useCustomerOrders";
|
|
4
|
+
|
|
5
|
+
describe("useCustomerOrders", () => {
|
|
6
|
+
it("should inovke load orders", async () => {
|
|
7
|
+
const { vm, injections } = useSetup(useCustomerOrders);
|
|
8
|
+
injections.apiClient.invoke.mockResolvedValue({
|
|
9
|
+
data: { orders: { elements: [] } },
|
|
10
|
+
});
|
|
11
|
+
vm.loadOrders();
|
|
12
|
+
expect(injections.apiClient.invoke).toHaveBeenCalledWith(
|
|
13
|
+
expect.stringContaining("readOrder"),
|
|
14
|
+
expect.objectContaining({}),
|
|
15
|
+
);
|
|
16
|
+
expect(vm.totalPages).toBe(0);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it("should invoke change page", async () => {
|
|
20
|
+
const { vm, injections } = useSetup(useCustomerOrders);
|
|
21
|
+
injections.apiClient.invoke.mockResolvedValue({
|
|
22
|
+
data: { orders: { elements: [] } },
|
|
23
|
+
});
|
|
24
|
+
vm.limit = 3;
|
|
25
|
+
vm.changeCurrentPage(2);
|
|
26
|
+
expect(injections.apiClient.invoke).toHaveBeenCalledWith(
|
|
27
|
+
expect.stringContaining("readOrder"),
|
|
28
|
+
expect.objectContaining({
|
|
29
|
+
body: {
|
|
30
|
+
limit: 3,
|
|
31
|
+
page: 2,
|
|
32
|
+
"total-count-mode": "exact",
|
|
33
|
+
},
|
|
34
|
+
}),
|
|
35
|
+
);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("should set current page after loading orders", async () => {
|
|
39
|
+
const { vm, injections } = useSetup(useCustomerOrders);
|
|
40
|
+
injections.apiClient.invoke.mockResolvedValue({
|
|
41
|
+
data: { orders: { elements: [], page: 23 } },
|
|
42
|
+
});
|
|
43
|
+
await vm.loadOrders();
|
|
44
|
+
|
|
45
|
+
expect(vm.currentPage).toBe(23);
|
|
46
|
+
});
|
|
47
|
+
});
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { computed, ref } from "vue";
|
|
2
|
+
import type { ComputedRef, Ref } from "vue";
|
|
3
|
+
import { useShopwareContext } from "#imports";
|
|
4
|
+
import type { Schemas, operations } from "#shopware";
|
|
5
|
+
|
|
6
|
+
export type UseCustomerOrdersReturn = {
|
|
7
|
+
/**
|
|
8
|
+
* All placed orders belonging to the logged-in customer
|
|
9
|
+
*/
|
|
10
|
+
orders: Ref<Schemas["Order"][]>;
|
|
11
|
+
/**
|
|
12
|
+
* Changes the current page of the orders list
|
|
13
|
+
*
|
|
14
|
+
* In order to change a page with additional parameters please use `loadOrders` method.
|
|
15
|
+
*/
|
|
16
|
+
changeCurrentPage(pageNumber: number | string): Promise<void>;
|
|
17
|
+
/**
|
|
18
|
+
* Fetches the orders list and assigns the result to the `orders` property
|
|
19
|
+
*/
|
|
20
|
+
loadOrders(
|
|
21
|
+
parameters?: operations["readOrder post /order"]["body"],
|
|
22
|
+
): Promise<void>;
|
|
23
|
+
/**
|
|
24
|
+
* Current page number
|
|
25
|
+
*/
|
|
26
|
+
currentPage: ComputedRef<number>;
|
|
27
|
+
/**
|
|
28
|
+
* total pages
|
|
29
|
+
*/
|
|
30
|
+
totalPages: ComputedRef<number>;
|
|
31
|
+
/**
|
|
32
|
+
* Limit of orders per page
|
|
33
|
+
*/
|
|
34
|
+
limit: Ref<number>;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Composable for fetching the orders list.
|
|
39
|
+
* @public
|
|
40
|
+
* @category Customer & Account
|
|
41
|
+
*/
|
|
42
|
+
export function useCustomerOrders(): UseCustomerOrdersReturn {
|
|
43
|
+
const { apiClient } = useShopwareContext();
|
|
44
|
+
|
|
45
|
+
const orders: Ref<Schemas["Order"][]> = ref([]);
|
|
46
|
+
|
|
47
|
+
const currentPaginationPage = ref<number>(1);
|
|
48
|
+
const limit = ref<number>(15);
|
|
49
|
+
|
|
50
|
+
const totalOrderItemsCount: Ref<number> = ref(0);
|
|
51
|
+
|
|
52
|
+
const currentParams = ref<Schemas["Criteria"]>({});
|
|
53
|
+
|
|
54
|
+
const loadOrders = async (
|
|
55
|
+
parameters: operations["readOrder post /order"]["body"] = {},
|
|
56
|
+
): Promise<void> => {
|
|
57
|
+
const params = {
|
|
58
|
+
...parameters,
|
|
59
|
+
limit: limit.value,
|
|
60
|
+
};
|
|
61
|
+
currentParams.value = params;
|
|
62
|
+
const fetchedOrders = await apiClient.invoke("readOrder post /order", {
|
|
63
|
+
body: { ...params, "total-count-mode": "exact" },
|
|
64
|
+
});
|
|
65
|
+
orders.value = fetchedOrders.data.orders.elements;
|
|
66
|
+
totalOrderItemsCount.value = fetchedOrders.data.orders.total ?? 0;
|
|
67
|
+
currentPaginationPage.value = fetchedOrders.data.orders.page ?? 1;
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
const changeCurrentPage = async (pageNumber: number) => {
|
|
71
|
+
await loadOrders({ ...currentParams.value, page: pageNumber });
|
|
72
|
+
currentPaginationPage.value = pageNumber;
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
const currentPage = computed(() => currentPaginationPage.value);
|
|
76
|
+
|
|
77
|
+
const totalPages = computed(() => {
|
|
78
|
+
return Math.ceil(totalOrderItemsCount.value / limit.value);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
return {
|
|
82
|
+
orders,
|
|
83
|
+
changeCurrentPage,
|
|
84
|
+
loadOrders,
|
|
85
|
+
currentPage,
|
|
86
|
+
totalPages,
|
|
87
|
+
limit,
|
|
88
|
+
};
|
|
89
|
+
}
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import { ApiClientError } from "@shopware/api-client";
|
|
2
|
+
import type { FetchResponse } from "ofetch";
|
|
3
|
+
import { describe, expect, it, vi } from "vitest";
|
|
4
|
+
import { useSetup } from "../_test";
|
|
5
|
+
import { useCustomerPassword } from "./useCustomerPassword";
|
|
6
|
+
|
|
7
|
+
describe("useCustomerPassword", () => {
|
|
8
|
+
it("reset password", async () => {
|
|
9
|
+
const { vm, injections } = await useSetup(useCustomerPassword, {
|
|
10
|
+
apiClient: {
|
|
11
|
+
invoke: vi.fn().mockResolvedValue({ data: {} }),
|
|
12
|
+
},
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
await vm.resetPassword({
|
|
16
|
+
email: "test@test.test",
|
|
17
|
+
storefrontUrl: "http://test.test",
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
expect(injections.apiClient.invoke).toHaveBeenCalledWith(
|
|
21
|
+
expect.stringContaining("sendRecoveryMail"),
|
|
22
|
+
expect.objectContaining({
|
|
23
|
+
body: {
|
|
24
|
+
email: "test@test.test",
|
|
25
|
+
storefrontUrl: "http://test.test",
|
|
26
|
+
},
|
|
27
|
+
}),
|
|
28
|
+
);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it("reset password - error", async () => {
|
|
32
|
+
const { vm } = await useSetup(useCustomerPassword, {
|
|
33
|
+
apiClient: {
|
|
34
|
+
invoke: vi.fn().mockImplementation(() => {
|
|
35
|
+
throw new ApiClientError({
|
|
36
|
+
_data: {
|
|
37
|
+
errors: [
|
|
38
|
+
{
|
|
39
|
+
code: "VIOLATION::INVALID_FORMAT_ERROR",
|
|
40
|
+
status: "400",
|
|
41
|
+
title: "Constraint violation error",
|
|
42
|
+
detail: "This value is not a valid email address.",
|
|
43
|
+
source: {
|
|
44
|
+
pointer: "/email",
|
|
45
|
+
},
|
|
46
|
+
meta: {
|
|
47
|
+
parameters: {
|
|
48
|
+
"{{ value }}": "\u0022werwerewr@ddddddd\u0022",
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
code: "VIOLATION::IS_BLANK_ERROR",
|
|
54
|
+
status: "400",
|
|
55
|
+
title: "Constraint violation error",
|
|
56
|
+
detail: "This value should not be blank.",
|
|
57
|
+
source: {
|
|
58
|
+
pointer: "/storefrontUrl",
|
|
59
|
+
},
|
|
60
|
+
meta: {
|
|
61
|
+
parameters: {
|
|
62
|
+
"{{ value }}": "\u0022\u0022",
|
|
63
|
+
},
|
|
64
|
+
},
|
|
65
|
+
},
|
|
66
|
+
{
|
|
67
|
+
code: "VIOLATION::NO_SUCH_CHOICE_ERROR",
|
|
68
|
+
status: "400",
|
|
69
|
+
title: "Constraint violation error",
|
|
70
|
+
detail: "The value you selected is not a valid choice.",
|
|
71
|
+
source: {
|
|
72
|
+
pointer: "/storefrontUrl",
|
|
73
|
+
},
|
|
74
|
+
meta: {
|
|
75
|
+
parameters: {
|
|
76
|
+
"{{ value }}": "\u0022\u0022",
|
|
77
|
+
"{{ choices }}":
|
|
78
|
+
"\u0022https://demo-frontends.shopware.store/de-De\u0022, \u0022https://demo-frontends.shopware.store/de\u0022, \u0022https://demo-frontends.shopware.store\u0022, \u0022https://demo-frontends.shopware.store/testde\u0022, \u0022https://demo-frontends.shopware.store/pl-PL\u0022, \u0022https://frontends-demo.vercel.app\u0022",
|
|
79
|
+
},
|
|
80
|
+
},
|
|
81
|
+
},
|
|
82
|
+
],
|
|
83
|
+
},
|
|
84
|
+
} as unknown as FetchResponse<{ errors: Array<{ title: string }> }>);
|
|
85
|
+
}),
|
|
86
|
+
},
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
await expect(
|
|
90
|
+
vm.resetPassword({
|
|
91
|
+
email: "test@test.test",
|
|
92
|
+
storefrontUrl: "http://test.test",
|
|
93
|
+
}),
|
|
94
|
+
).rejects.toThrowErrorMatchingInlineSnapshot(`
|
|
95
|
+
[ApiClientError: Failed request
|
|
96
|
+
- [Constraint violation error][/email] This value is not a valid email address.
|
|
97
|
+
- [Constraint violation error][/storefrontUrl] This value should not be blank.
|
|
98
|
+
- [Constraint violation error][/storefrontUrl] The value you selected is not a valid choice.]
|
|
99
|
+
`);
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it("update password", async () => {
|
|
103
|
+
const { vm, injections } = await useSetup(useCustomerPassword, {
|
|
104
|
+
apiClient: {
|
|
105
|
+
invoke: vi.fn().mockResolvedValue({ data: {} }),
|
|
106
|
+
},
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
await vm.updatePassword({
|
|
110
|
+
newPassword: "test",
|
|
111
|
+
newPasswordConfirm: "test",
|
|
112
|
+
password: "test",
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
expect(injections.apiClient.invoke).toHaveBeenCalledWith(
|
|
116
|
+
expect.stringContaining("changePassword"),
|
|
117
|
+
expect.objectContaining({
|
|
118
|
+
body: {
|
|
119
|
+
newPassword: "test",
|
|
120
|
+
newPasswordConfirm: "test",
|
|
121
|
+
password: "test",
|
|
122
|
+
},
|
|
123
|
+
}),
|
|
124
|
+
);
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it("update password - error", async () => {
|
|
128
|
+
const { vm } = await useSetup(useCustomerPassword, {
|
|
129
|
+
apiClient: {
|
|
130
|
+
invoke: vi.fn().mockImplementation(() => {
|
|
131
|
+
throw new ApiClientError({
|
|
132
|
+
_data: {
|
|
133
|
+
errors: [
|
|
134
|
+
{
|
|
135
|
+
code: "VIOLATION::CUSTOMER_PASSWORD_NOT_CORRECT",
|
|
136
|
+
status: "400",
|
|
137
|
+
title: "Constraint violation error",
|
|
138
|
+
detail: "Your password is wrong",
|
|
139
|
+
source: {
|
|
140
|
+
pointer: "/password",
|
|
141
|
+
},
|
|
142
|
+
meta: {
|
|
143
|
+
parameters: [],
|
|
144
|
+
},
|
|
145
|
+
},
|
|
146
|
+
],
|
|
147
|
+
},
|
|
148
|
+
} as unknown as FetchResponse<{ errors: Array<{ title: string }> }>);
|
|
149
|
+
}),
|
|
150
|
+
},
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
await expect(
|
|
154
|
+
vm.updatePassword({
|
|
155
|
+
newPassword: "test",
|
|
156
|
+
newPasswordConfirm: "test",
|
|
157
|
+
password: "test",
|
|
158
|
+
}),
|
|
159
|
+
).rejects.toThrowErrorMatchingInlineSnapshot(`
|
|
160
|
+
[ApiClientError: Failed request
|
|
161
|
+
- [Constraint violation error][/password] Your password is wrong]
|
|
162
|
+
`);
|
|
163
|
+
});
|
|
164
|
+
});
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { useShopwareContext } from "#imports";
|
|
2
|
+
import type { operations } from "#shopware";
|
|
3
|
+
|
|
4
|
+
export type UseCustomerPasswordReturn = {
|
|
5
|
+
/**
|
|
6
|
+
* Change customer's current password
|
|
7
|
+
*/
|
|
8
|
+
updatePassword(
|
|
9
|
+
updatePasswordData: operations["changePassword post /account/change-password"]["body"],
|
|
10
|
+
): Promise<
|
|
11
|
+
operations["changePassword post /account/change-password"]["response"]
|
|
12
|
+
>;
|
|
13
|
+
/**
|
|
14
|
+
* Reset customer's password
|
|
15
|
+
*/
|
|
16
|
+
resetPassword(
|
|
17
|
+
resetPasswordData: operations["sendRecoveryMail post /account/recovery-password"]["body"],
|
|
18
|
+
): Promise<
|
|
19
|
+
operations["sendRecoveryMail post /account/recovery-password"]["response"]
|
|
20
|
+
>;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Composable for customer password management.
|
|
25
|
+
* @public
|
|
26
|
+
* @category Customer & Account
|
|
27
|
+
*/
|
|
28
|
+
export function useCustomerPassword(): UseCustomerPasswordReturn {
|
|
29
|
+
const { apiClient } = useShopwareContext();
|
|
30
|
+
|
|
31
|
+
async function updatePassword(
|
|
32
|
+
updatePasswordData: operations["changePassword post /account/change-password"]["body"],
|
|
33
|
+
) {
|
|
34
|
+
const response = await apiClient.invoke(
|
|
35
|
+
"changePassword post /account/change-password",
|
|
36
|
+
{
|
|
37
|
+
body: updatePasswordData,
|
|
38
|
+
},
|
|
39
|
+
);
|
|
40
|
+
return response.data;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
async function resetPassword(
|
|
44
|
+
resetPasswordData: operations["sendRecoveryMail post /account/recovery-password"]["body"],
|
|
45
|
+
) {
|
|
46
|
+
const response = await apiClient.invoke(
|
|
47
|
+
"sendRecoveryMail post /account/recovery-password",
|
|
48
|
+
{ body: resetPasswordData },
|
|
49
|
+
);
|
|
50
|
+
return response.data;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return {
|
|
54
|
+
updatePassword,
|
|
55
|
+
resetPassword,
|
|
56
|
+
};
|
|
57
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { useSetup } from "../_test";
|
|
3
|
+
import { useDefaultOrderAssociations } from "./useDefaultOrderAssociations";
|
|
4
|
+
|
|
5
|
+
describe("useDefaultOrderAssociations", () => {
|
|
6
|
+
it("returns default associations object", async () => {
|
|
7
|
+
const { vm } = useSetup(() => useDefaultOrderAssociations());
|
|
8
|
+
|
|
9
|
+
expect(vm).toMatchInlineSnapshot(`
|
|
10
|
+
{
|
|
11
|
+
"associations": {
|
|
12
|
+
"addresses": {},
|
|
13
|
+
"deliveries": {
|
|
14
|
+
"associations": {
|
|
15
|
+
"shippingMethod": {},
|
|
16
|
+
"shippingOrderAddress": {},
|
|
17
|
+
"stateMachineState": {},
|
|
18
|
+
},
|
|
19
|
+
},
|
|
20
|
+
"lineItems": {
|
|
21
|
+
"associations": {
|
|
22
|
+
"cover": {},
|
|
23
|
+
"downloads": {
|
|
24
|
+
"associations": {
|
|
25
|
+
"media": {},
|
|
26
|
+
},
|
|
27
|
+
},
|
|
28
|
+
},
|
|
29
|
+
},
|
|
30
|
+
"stateMachineState": {},
|
|
31
|
+
"transactions": {
|
|
32
|
+
"associations": {
|
|
33
|
+
"paymentMethod": {},
|
|
34
|
+
"stateMachineState": {},
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
},
|
|
38
|
+
}
|
|
39
|
+
`);
|
|
40
|
+
});
|
|
41
|
+
});
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import type { Schemas } from "#shopware";
|
|
2
|
+
|
|
3
|
+
export type UseDefaultOrderAssociationsReturn = Schemas["Criteria"] & {
|
|
4
|
+
checkPromotion?: boolean;
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Returns default order associations. You can override this composable in your project.
|
|
9
|
+
* @public
|
|
10
|
+
* @category Order
|
|
11
|
+
*/
|
|
12
|
+
export function useDefaultOrderAssociations(): UseDefaultOrderAssociationsReturn {
|
|
13
|
+
const orderAssociations: Schemas["Criteria"] & { checkPromotion?: boolean } =
|
|
14
|
+
{
|
|
15
|
+
associations: {
|
|
16
|
+
stateMachineState: {},
|
|
17
|
+
lineItems: {
|
|
18
|
+
associations: {
|
|
19
|
+
cover: {},
|
|
20
|
+
downloads: {
|
|
21
|
+
associations: {
|
|
22
|
+
media: {},
|
|
23
|
+
},
|
|
24
|
+
},
|
|
25
|
+
},
|
|
26
|
+
},
|
|
27
|
+
addresses: {},
|
|
28
|
+
deliveries: {
|
|
29
|
+
associations: {
|
|
30
|
+
shippingMethod: {},
|
|
31
|
+
shippingOrderAddress: {},
|
|
32
|
+
stateMachineState: {},
|
|
33
|
+
},
|
|
34
|
+
},
|
|
35
|
+
transactions: {
|
|
36
|
+
associations: {
|
|
37
|
+
paymentMethod: {},
|
|
38
|
+
stateMachineState: {},
|
|
39
|
+
},
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
return orderAssociations;
|
|
45
|
+
}
|