@commercejs/nuxt 0.2.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 +99 -0
- package/dist/module.cjs +5 -0
- package/dist/module.d.mts +23 -0
- package/dist/module.d.ts +23 -0
- package/dist/module.json +12 -0
- package/dist/module.mjs +101 -0
- package/dist/runtime/composables/index.d.ts +0 -0
- package/dist/runtime/composables/index.js +15 -0
- package/dist/runtime/composables/useAdapter.d.ts +0 -0
- package/dist/runtime/composables/useAdapter.js +11 -0
- package/dist/runtime/composables/useBrands.d.ts +0 -0
- package/dist/runtime/composables/useBrands.js +32 -0
- package/dist/runtime/composables/useCart.d.ts +0 -0
- package/dist/runtime/composables/useCart.js +144 -0
- package/dist/runtime/composables/useCategories.d.ts +0 -0
- package/dist/runtime/composables/useCategories.js +9 -0
- package/dist/runtime/composables/useCheckout.d.ts +0 -0
- package/dist/runtime/composables/useCheckout.js +142 -0
- package/dist/runtime/composables/useCountries.d.ts +0 -0
- package/dist/runtime/composables/useCountries.js +32 -0
- package/dist/runtime/composables/useCustomer.d.ts +0 -0
- package/dist/runtime/composables/useCustomer.js +179 -0
- package/dist/runtime/composables/useLocations.d.ts +0 -0
- package/dist/runtime/composables/useLocations.js +32 -0
- package/dist/runtime/composables/useProduct.d.ts +0 -0
- package/dist/runtime/composables/useProduct.js +16 -0
- package/dist/runtime/composables/useProducts.d.ts +0 -0
- package/dist/runtime/composables/useProducts.js +26 -0
- package/dist/runtime/composables/usePromotions.d.ts +0 -0
- package/dist/runtime/composables/usePromotions.js +48 -0
- package/dist/runtime/composables/useReturns.d.ts +0 -0
- package/dist/runtime/composables/useReturns.js +89 -0
- package/dist/runtime/composables/useReviews.d.ts +0 -0
- package/dist/runtime/composables/useReviews.js +77 -0
- package/dist/runtime/composables/useStoreInfo.d.ts +0 -0
- package/dist/runtime/composables/useStoreInfo.js +15 -0
- package/dist/runtime/composables/useWishlist.d.ts +0 -0
- package/dist/runtime/composables/useWishlist.js +82 -0
- package/dist/runtime/plugin.d.ts +0 -0
- package/dist/runtime/plugin.js +24 -0
- package/dist/runtime/server/api/auth.forgot-password.post.d.ts +0 -0
- package/dist/runtime/server/api/auth.forgot-password.post.js +8 -0
- package/dist/runtime/server/api/auth.login.post.d.ts +0 -0
- package/dist/runtime/server/api/auth.login.post.js +7 -0
- package/dist/runtime/server/api/auth.logout.post.d.ts +0 -0
- package/dist/runtime/server/api/auth.logout.post.js +7 -0
- package/dist/runtime/server/api/auth.register.post.d.ts +0 -0
- package/dist/runtime/server/api/auth.register.post.js +7 -0
- package/dist/runtime/server/api/auth.reset-password.post.d.ts +0 -0
- package/dist/runtime/server/api/auth.reset-password.post.js +8 -0
- package/dist/runtime/server/api/brands.get.d.ts +0 -0
- package/dist/runtime/server/api/brands.get.js +6 -0
- package/dist/runtime/server/api/cart.[id].get.d.ts +0 -0
- package/dist/runtime/server/api/cart.[id].get.js +7 -0
- package/dist/runtime/server/api/cart.[id].items.[itemId].delete.d.ts +0 -0
- package/dist/runtime/server/api/cart.[id].items.[itemId].delete.js +8 -0
- package/dist/runtime/server/api/cart.[id].items.[itemId].put.d.ts +0 -0
- package/dist/runtime/server/api/cart.[id].items.[itemId].put.js +9 -0
- package/dist/runtime/server/api/cart.[id].items.post.d.ts +0 -0
- package/dist/runtime/server/api/cart.[id].items.post.js +8 -0
- package/dist/runtime/server/api/cart.post.d.ts +0 -0
- package/dist/runtime/server/api/cart.post.js +6 -0
- package/dist/runtime/server/api/categories.get.d.ts +0 -0
- package/dist/runtime/server/api/categories.get.js +10 -0
- package/dist/runtime/server/api/checkout.payment-methods.[cartId].get.d.ts +0 -0
- package/dist/runtime/server/api/checkout.payment-methods.[cartId].get.js +7 -0
- package/dist/runtime/server/api/checkout.place-order.post.d.ts +0 -0
- package/dist/runtime/server/api/checkout.place-order.post.js +7 -0
- package/dist/runtime/server/api/checkout.shipping-methods.[cartId].get.d.ts +0 -0
- package/dist/runtime/server/api/checkout.shipping-methods.[cartId].get.js +7 -0
- package/dist/runtime/server/api/countries.get.d.ts +0 -0
- package/dist/runtime/server/api/countries.get.js +6 -0
- package/dist/runtime/server/api/customer.addresses.[addressId].delete.d.ts +0 -0
- package/dist/runtime/server/api/customer.addresses.[addressId].delete.js +8 -0
- package/dist/runtime/server/api/customer.addresses.[addressId].put.d.ts +0 -0
- package/dist/runtime/server/api/customer.addresses.[addressId].put.js +8 -0
- package/dist/runtime/server/api/customer.addresses.get.d.ts +0 -0
- package/dist/runtime/server/api/customer.addresses.get.js +6 -0
- package/dist/runtime/server/api/customer.addresses.post.d.ts +0 -0
- package/dist/runtime/server/api/customer.addresses.post.js +7 -0
- package/dist/runtime/server/api/customer.get.d.ts +0 -0
- package/dist/runtime/server/api/customer.get.js +6 -0
- package/dist/runtime/server/api/customer.orders.get.d.ts +0 -0
- package/dist/runtime/server/api/customer.orders.get.js +10 -0
- package/dist/runtime/server/api/locations.get.d.ts +0 -0
- package/dist/runtime/server/api/locations.get.js +6 -0
- package/dist/runtime/server/api/products.[id].get.d.ts +0 -0
- package/dist/runtime/server/api/products.[id].get.js +7 -0
- package/dist/runtime/server/api/products.get.d.ts +0 -0
- package/dist/runtime/server/api/products.get.js +13 -0
- package/dist/runtime/server/api/promotions.get.d.ts +0 -0
- package/dist/runtime/server/api/promotions.get.js +6 -0
- package/dist/runtime/server/api/promotions.validate.post.d.ts +0 -0
- package/dist/runtime/server/api/promotions.validate.post.js +7 -0
- package/dist/runtime/server/api/returns.[returnId].cancel.post.d.ts +0 -0
- package/dist/runtime/server/api/returns.[returnId].cancel.post.js +7 -0
- package/dist/runtime/server/api/returns.[returnId].get.d.ts +0 -0
- package/dist/runtime/server/api/returns.[returnId].get.js +7 -0
- package/dist/runtime/server/api/returns.get.d.ts +0 -0
- package/dist/runtime/server/api/returns.get.js +10 -0
- package/dist/runtime/server/api/returns.post.d.ts +0 -0
- package/dist/runtime/server/api/returns.post.js +7 -0
- package/dist/runtime/server/api/reviews.[productId].get.d.ts +0 -0
- package/dist/runtime/server/api/reviews.[productId].get.js +11 -0
- package/dist/runtime/server/api/reviews.[productId].summary.get.d.ts +0 -0
- package/dist/runtime/server/api/reviews.[productId].summary.get.js +7 -0
- package/dist/runtime/server/api/reviews.post.d.ts +0 -0
- package/dist/runtime/server/api/reviews.post.js +7 -0
- package/dist/runtime/server/api/store.get.d.ts +0 -0
- package/dist/runtime/server/api/store.get.js +6 -0
- package/dist/runtime/server/api/wishlist.get.d.ts +0 -0
- package/dist/runtime/server/api/wishlist.get.js +6 -0
- package/dist/runtime/server/api/wishlist.items.[itemId].delete.d.ts +0 -0
- package/dist/runtime/server/api/wishlist.items.[itemId].delete.js +7 -0
- package/dist/runtime/server/api/wishlist.items.post.d.ts +0 -0
- package/dist/runtime/server/api/wishlist.items.post.js +7 -0
- package/dist/runtime/server/plugins/commerce-adapter.d.ts +0 -0
- package/dist/runtime/server/plugins/commerce-adapter.js +25 -0
- package/dist/runtime/server/utils/adapter.d.ts +0 -0
- package/dist/runtime/server/utils/adapter.js +11 -0
- package/dist/types.d.mts +7 -0
- package/dist/types.d.ts +7 -0
- package/package.json +43 -0
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import { useState, computed, readonly } from "#imports";
|
|
2
|
+
import { CommerceError, isCommerceError } from "@commercejs/types";
|
|
3
|
+
import { createEventHook } from "@vueuse/core";
|
|
4
|
+
import { useAdapter } from "./useAdapter.js";
|
|
5
|
+
export function useCustomer() {
|
|
6
|
+
const adapter = useAdapter();
|
|
7
|
+
const customer = useState("commerce_customer", () => null);
|
|
8
|
+
const loading = useState("commerce_customer_loading", () => false);
|
|
9
|
+
const error = useState("commerce_customer_error", () => null);
|
|
10
|
+
const loginHook = createEventHook();
|
|
11
|
+
const logoutHook = createEventHook();
|
|
12
|
+
const errorHook = createEventHook();
|
|
13
|
+
function handleError(err) {
|
|
14
|
+
const e = isCommerceError(err) ? err : new CommerceError(
|
|
15
|
+
err instanceof Error ? err.message : String(err),
|
|
16
|
+
"UNKNOWN",
|
|
17
|
+
void 0,
|
|
18
|
+
err
|
|
19
|
+
);
|
|
20
|
+
error.value = e;
|
|
21
|
+
errorHook.trigger(e);
|
|
22
|
+
return e;
|
|
23
|
+
}
|
|
24
|
+
async function login(email, password) {
|
|
25
|
+
loading.value = true;
|
|
26
|
+
error.value = null;
|
|
27
|
+
try {
|
|
28
|
+
customer.value = await adapter.login(email, password);
|
|
29
|
+
loginHook.trigger(customer.value);
|
|
30
|
+
} catch (err) {
|
|
31
|
+
throw handleError(err);
|
|
32
|
+
} finally {
|
|
33
|
+
loading.value = false;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
async function register(input) {
|
|
37
|
+
loading.value = true;
|
|
38
|
+
error.value = null;
|
|
39
|
+
try {
|
|
40
|
+
customer.value = await adapter.register(input);
|
|
41
|
+
loginHook.trigger(customer.value);
|
|
42
|
+
} catch (err) {
|
|
43
|
+
throw handleError(err);
|
|
44
|
+
} finally {
|
|
45
|
+
loading.value = false;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
async function logout() {
|
|
49
|
+
loading.value = true;
|
|
50
|
+
error.value = null;
|
|
51
|
+
try {
|
|
52
|
+
await adapter.logout();
|
|
53
|
+
customer.value = null;
|
|
54
|
+
logoutHook.trigger();
|
|
55
|
+
} catch (err) {
|
|
56
|
+
throw handleError(err);
|
|
57
|
+
} finally {
|
|
58
|
+
loading.value = false;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
async function refresh() {
|
|
62
|
+
loading.value = true;
|
|
63
|
+
error.value = null;
|
|
64
|
+
try {
|
|
65
|
+
customer.value = await adapter.getCustomer();
|
|
66
|
+
} catch (err) {
|
|
67
|
+
handleError(err);
|
|
68
|
+
customer.value = null;
|
|
69
|
+
} finally {
|
|
70
|
+
loading.value = false;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
async function update(input) {
|
|
74
|
+
loading.value = true;
|
|
75
|
+
error.value = null;
|
|
76
|
+
try {
|
|
77
|
+
customer.value = await adapter.updateCustomer(input);
|
|
78
|
+
} catch (err) {
|
|
79
|
+
throw handleError(err);
|
|
80
|
+
} finally {
|
|
81
|
+
loading.value = false;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
async function forgotPassword(email) {
|
|
85
|
+
loading.value = true;
|
|
86
|
+
error.value = null;
|
|
87
|
+
try {
|
|
88
|
+
await adapter.forgotPassword(email);
|
|
89
|
+
} catch (err) {
|
|
90
|
+
throw handleError(err);
|
|
91
|
+
} finally {
|
|
92
|
+
loading.value = false;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
async function resetPassword(token, newPassword) {
|
|
96
|
+
loading.value = true;
|
|
97
|
+
error.value = null;
|
|
98
|
+
try {
|
|
99
|
+
await adapter.resetPassword(token, newPassword);
|
|
100
|
+
} catch (err) {
|
|
101
|
+
throw handleError(err);
|
|
102
|
+
} finally {
|
|
103
|
+
loading.value = false;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
async function getAddresses() {
|
|
107
|
+
loading.value = true;
|
|
108
|
+
error.value = null;
|
|
109
|
+
try {
|
|
110
|
+
return await adapter.getAddresses();
|
|
111
|
+
} catch (err) {
|
|
112
|
+
throw handleError(err);
|
|
113
|
+
} finally {
|
|
114
|
+
loading.value = false;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
async function addAddress(address) {
|
|
118
|
+
loading.value = true;
|
|
119
|
+
error.value = null;
|
|
120
|
+
try {
|
|
121
|
+
const saved = await adapter.addAddress(address);
|
|
122
|
+
await refresh();
|
|
123
|
+
return saved;
|
|
124
|
+
} catch (err) {
|
|
125
|
+
throw handleError(err);
|
|
126
|
+
} finally {
|
|
127
|
+
loading.value = false;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
async function updateAddress(addressId, address) {
|
|
131
|
+
loading.value = true;
|
|
132
|
+
error.value = null;
|
|
133
|
+
try {
|
|
134
|
+
const updated = await adapter.updateAddress(addressId, address);
|
|
135
|
+
await refresh();
|
|
136
|
+
return updated;
|
|
137
|
+
} catch (err) {
|
|
138
|
+
throw handleError(err);
|
|
139
|
+
} finally {
|
|
140
|
+
loading.value = false;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
async function deleteAddress(addressId) {
|
|
144
|
+
loading.value = true;
|
|
145
|
+
error.value = null;
|
|
146
|
+
try {
|
|
147
|
+
await adapter.deleteAddress(addressId);
|
|
148
|
+
await refresh();
|
|
149
|
+
} catch (err) {
|
|
150
|
+
throw handleError(err);
|
|
151
|
+
} finally {
|
|
152
|
+
loading.value = false;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
return {
|
|
156
|
+
customer: readonly(customer),
|
|
157
|
+
isAuthenticated: computed(() => customer.value !== null),
|
|
158
|
+
loading: readonly(loading),
|
|
159
|
+
error: readonly(error),
|
|
160
|
+
// Event hooks
|
|
161
|
+
onLogin: loginHook.on,
|
|
162
|
+
onLogout: logoutHook.on,
|
|
163
|
+
onError: errorHook.on,
|
|
164
|
+
// Methods
|
|
165
|
+
login,
|
|
166
|
+
register,
|
|
167
|
+
logout,
|
|
168
|
+
refresh,
|
|
169
|
+
update,
|
|
170
|
+
// Password reset
|
|
171
|
+
forgotPassword,
|
|
172
|
+
resetPassword,
|
|
173
|
+
// Address book
|
|
174
|
+
getAddresses,
|
|
175
|
+
addAddress,
|
|
176
|
+
updateAddress,
|
|
177
|
+
deleteAddress
|
|
178
|
+
};
|
|
179
|
+
}
|
|
File without changes
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { useState, readonly } from "#imports";
|
|
2
|
+
import { CommerceError, isCommerceError } from "@commercejs/types";
|
|
3
|
+
import { useAdapter } from "./useAdapter.js";
|
|
4
|
+
export function useLocations() {
|
|
5
|
+
const adapter = useAdapter();
|
|
6
|
+
const locations = useState("commerce:locations", () => []);
|
|
7
|
+
const loading = useState("commerce:locations:loading", () => false);
|
|
8
|
+
const error = useState("commerce:locations:error", () => null);
|
|
9
|
+
async function refresh() {
|
|
10
|
+
loading.value = true;
|
|
11
|
+
error.value = null;
|
|
12
|
+
try {
|
|
13
|
+
locations.value = await adapter.getStoreLocations();
|
|
14
|
+
} catch (err) {
|
|
15
|
+
const e = isCommerceError(err) ? err : new CommerceError(
|
|
16
|
+
err instanceof Error ? err.message : String(err),
|
|
17
|
+
"UNKNOWN",
|
|
18
|
+
void 0,
|
|
19
|
+
err
|
|
20
|
+
);
|
|
21
|
+
error.value = e;
|
|
22
|
+
} finally {
|
|
23
|
+
loading.value = false;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return {
|
|
27
|
+
locations: readonly(locations),
|
|
28
|
+
loading: readonly(loading),
|
|
29
|
+
error: readonly(error),
|
|
30
|
+
refresh
|
|
31
|
+
};
|
|
32
|
+
}
|
|
File without changes
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { useFetch, useRuntimeConfig, toValue } from "#imports";
|
|
2
|
+
export function useProduct(params) {
|
|
3
|
+
const config = useRuntimeConfig();
|
|
4
|
+
const apiBase = config.public.commerce?.apiBase || "/api/_commerce";
|
|
5
|
+
const productId = computed(() => {
|
|
6
|
+
const p = toValue(params);
|
|
7
|
+
return p.id || p.slug || "";
|
|
8
|
+
});
|
|
9
|
+
return useFetch(
|
|
10
|
+
() => `${apiBase}/products/${productId.value}`,
|
|
11
|
+
{
|
|
12
|
+
key: computed(() => `product-${productId.value}`).value,
|
|
13
|
+
watch: [productId]
|
|
14
|
+
}
|
|
15
|
+
);
|
|
16
|
+
}
|
|
File without changes
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { useFetch, useRuntimeConfig, toValue } from "#imports";
|
|
2
|
+
export function useProducts(params = {}) {
|
|
3
|
+
const config = useRuntimeConfig();
|
|
4
|
+
const apiBase = config.public.commerce?.apiBase || "/api/_commerce";
|
|
5
|
+
const queryParams = computed(() => {
|
|
6
|
+
const p = toValue(params);
|
|
7
|
+
const q = {};
|
|
8
|
+
if (p.query) q.query = p.query;
|
|
9
|
+
if (p.categoryId) q.categoryId = p.categoryId;
|
|
10
|
+
if (p.page) q.page = p.page;
|
|
11
|
+
if (p.perPage) q.perPage = p.perPage;
|
|
12
|
+
if (p.sort) {
|
|
13
|
+
q.sortField = p.sort.field;
|
|
14
|
+
q.sortDirection = p.sort.direction;
|
|
15
|
+
}
|
|
16
|
+
return q;
|
|
17
|
+
});
|
|
18
|
+
return useFetch(
|
|
19
|
+
() => `${apiBase}/products`,
|
|
20
|
+
{
|
|
21
|
+
key: computed(() => `products-${JSON.stringify(toValue(params))}`).value,
|
|
22
|
+
query: queryParams,
|
|
23
|
+
watch: [queryParams]
|
|
24
|
+
}
|
|
25
|
+
);
|
|
26
|
+
}
|
|
File without changes
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { useState, readonly } from "#imports";
|
|
2
|
+
import { CommerceError, isCommerceError } from "@commercejs/types";
|
|
3
|
+
import { useAdapter } from "./useAdapter.js";
|
|
4
|
+
export function usePromotions() {
|
|
5
|
+
const adapter = useAdapter();
|
|
6
|
+
const promotions = useState("commerce:promotions", () => []);
|
|
7
|
+
const loading = useState("commerce:promotions:loading", () => false);
|
|
8
|
+
const error = useState("commerce:promotions:error", () => null);
|
|
9
|
+
function handleError(err) {
|
|
10
|
+
const e = isCommerceError(err) ? err : new CommerceError(
|
|
11
|
+
err instanceof Error ? err.message : String(err),
|
|
12
|
+
"UNKNOWN",
|
|
13
|
+
void 0,
|
|
14
|
+
err
|
|
15
|
+
);
|
|
16
|
+
error.value = e;
|
|
17
|
+
return e;
|
|
18
|
+
}
|
|
19
|
+
async function fetchPromotions() {
|
|
20
|
+
loading.value = true;
|
|
21
|
+
error.value = null;
|
|
22
|
+
try {
|
|
23
|
+
promotions.value = await adapter.getActivePromotions();
|
|
24
|
+
} catch (err) {
|
|
25
|
+
handleError(err);
|
|
26
|
+
} finally {
|
|
27
|
+
loading.value = false;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
async function validateCoupon(code) {
|
|
31
|
+
loading.value = true;
|
|
32
|
+
error.value = null;
|
|
33
|
+
try {
|
|
34
|
+
return await adapter.validateCoupon(code);
|
|
35
|
+
} catch (err) {
|
|
36
|
+
throw handleError(err);
|
|
37
|
+
} finally {
|
|
38
|
+
loading.value = false;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return {
|
|
42
|
+
promotions: readonly(promotions),
|
|
43
|
+
loading: readonly(loading),
|
|
44
|
+
error: readonly(error),
|
|
45
|
+
fetchPromotions,
|
|
46
|
+
validateCoupon
|
|
47
|
+
};
|
|
48
|
+
}
|
|
File without changes
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { useState, readonly } from "#imports";
|
|
2
|
+
import { CommerceError, isCommerceError } from "@commercejs/types";
|
|
3
|
+
import { createEventHook } from "@vueuse/core";
|
|
4
|
+
import { useAdapter } from "./useAdapter.js";
|
|
5
|
+
export function useReturns() {
|
|
6
|
+
const adapter = useAdapter();
|
|
7
|
+
const returns = useState("commerce:returns", () => null);
|
|
8
|
+
const loading = useState("commerce:returns:loading", () => false);
|
|
9
|
+
const error = useState("commerce:returns:error", () => null);
|
|
10
|
+
const returnCreatedHook = createEventHook();
|
|
11
|
+
const returnCancelledHook = createEventHook();
|
|
12
|
+
const errorHook = createEventHook();
|
|
13
|
+
function handleError(err) {
|
|
14
|
+
const e = isCommerceError(err) ? err : new CommerceError(
|
|
15
|
+
err instanceof Error ? err.message : String(err),
|
|
16
|
+
"UNKNOWN",
|
|
17
|
+
void 0,
|
|
18
|
+
err
|
|
19
|
+
);
|
|
20
|
+
error.value = e;
|
|
21
|
+
errorHook.trigger(e);
|
|
22
|
+
return e;
|
|
23
|
+
}
|
|
24
|
+
async function fetchReturns(params) {
|
|
25
|
+
loading.value = true;
|
|
26
|
+
error.value = null;
|
|
27
|
+
try {
|
|
28
|
+
returns.value = await adapter.getReturns(params);
|
|
29
|
+
} catch (err) {
|
|
30
|
+
handleError(err);
|
|
31
|
+
} finally {
|
|
32
|
+
loading.value = false;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
async function getReturn(returnId) {
|
|
36
|
+
loading.value = true;
|
|
37
|
+
error.value = null;
|
|
38
|
+
try {
|
|
39
|
+
return await adapter.getReturn(returnId);
|
|
40
|
+
} catch (err) {
|
|
41
|
+
throw handleError(err);
|
|
42
|
+
} finally {
|
|
43
|
+
loading.value = false;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
async function createReturn(input) {
|
|
47
|
+
loading.value = true;
|
|
48
|
+
error.value = null;
|
|
49
|
+
try {
|
|
50
|
+
const returnReq = await adapter.createReturn(input);
|
|
51
|
+
returnCreatedHook.trigger(returnReq);
|
|
52
|
+
await fetchReturns();
|
|
53
|
+
return returnReq;
|
|
54
|
+
} catch (err) {
|
|
55
|
+
throw handleError(err);
|
|
56
|
+
} finally {
|
|
57
|
+
loading.value = false;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
async function cancelReturn(returnId) {
|
|
61
|
+
loading.value = true;
|
|
62
|
+
error.value = null;
|
|
63
|
+
try {
|
|
64
|
+
const returnReq = await adapter.cancelReturn(returnId);
|
|
65
|
+
returnCancelledHook.trigger(returnReq);
|
|
66
|
+
await fetchReturns();
|
|
67
|
+
return returnReq;
|
|
68
|
+
} catch (err) {
|
|
69
|
+
throw handleError(err);
|
|
70
|
+
} finally {
|
|
71
|
+
loading.value = false;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return {
|
|
75
|
+
// State (readonly)
|
|
76
|
+
returns: readonly(returns),
|
|
77
|
+
loading: readonly(loading),
|
|
78
|
+
error: readonly(error),
|
|
79
|
+
// Methods
|
|
80
|
+
fetchReturns,
|
|
81
|
+
getReturn,
|
|
82
|
+
createReturn,
|
|
83
|
+
cancelReturn,
|
|
84
|
+
// Lifecycle hooks
|
|
85
|
+
onReturnCreated: returnCreatedHook.on,
|
|
86
|
+
onReturnCancelled: returnCancelledHook.on,
|
|
87
|
+
onError: errorHook.on
|
|
88
|
+
};
|
|
89
|
+
}
|
|
File without changes
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { useState, readonly, computed } from "#imports";
|
|
2
|
+
import { CommerceError, isCommerceError } from "@commercejs/types";
|
|
3
|
+
import { createEventHook } from "@vueuse/core";
|
|
4
|
+
import { useAdapter } from "./useAdapter.js";
|
|
5
|
+
export function useReviews(productId) {
|
|
6
|
+
const adapter = useAdapter();
|
|
7
|
+
const reviews = useState(`commerce:reviews:${productId}`, () => null);
|
|
8
|
+
const summary = useState(`commerce:reviews:summary:${productId}`, () => null);
|
|
9
|
+
const loading = useState(`commerce:reviews:loading:${productId}`, () => false);
|
|
10
|
+
const error = useState(`commerce:reviews:error:${productId}`, () => null);
|
|
11
|
+
const reviewSubmittedHook = createEventHook();
|
|
12
|
+
const errorHook = createEventHook();
|
|
13
|
+
function handleError(err) {
|
|
14
|
+
const e = isCommerceError(err) ? err : new CommerceError(
|
|
15
|
+
err instanceof Error ? err.message : String(err),
|
|
16
|
+
"UNKNOWN",
|
|
17
|
+
void 0,
|
|
18
|
+
err
|
|
19
|
+
);
|
|
20
|
+
error.value = e;
|
|
21
|
+
errorHook.trigger(e);
|
|
22
|
+
return e;
|
|
23
|
+
}
|
|
24
|
+
async function fetchReviews(params) {
|
|
25
|
+
loading.value = true;
|
|
26
|
+
error.value = null;
|
|
27
|
+
try {
|
|
28
|
+
reviews.value = await adapter.getProductReviews(productId, params);
|
|
29
|
+
} catch (err) {
|
|
30
|
+
handleError(err);
|
|
31
|
+
} finally {
|
|
32
|
+
loading.value = false;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
async function fetchSummary() {
|
|
36
|
+
loading.value = true;
|
|
37
|
+
error.value = null;
|
|
38
|
+
try {
|
|
39
|
+
summary.value = await adapter.getReviewSummary(productId);
|
|
40
|
+
} catch (err) {
|
|
41
|
+
handleError(err);
|
|
42
|
+
} finally {
|
|
43
|
+
loading.value = false;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
async function submit(input) {
|
|
47
|
+
loading.value = true;
|
|
48
|
+
error.value = null;
|
|
49
|
+
try {
|
|
50
|
+
const review = await adapter.submitReview({ ...input, productId });
|
|
51
|
+
reviewSubmittedHook.trigger(review);
|
|
52
|
+
await Promise.all([fetchReviews(), fetchSummary()]);
|
|
53
|
+
return review;
|
|
54
|
+
} catch (err) {
|
|
55
|
+
throw handleError(err);
|
|
56
|
+
} finally {
|
|
57
|
+
loading.value = false;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return {
|
|
61
|
+
// State (readonly)
|
|
62
|
+
reviews: readonly(reviews),
|
|
63
|
+
summary: readonly(summary),
|
|
64
|
+
loading: readonly(loading),
|
|
65
|
+
error: readonly(error),
|
|
66
|
+
// Derived
|
|
67
|
+
averageRating: computed(() => summary.value?.averageRating ?? 0),
|
|
68
|
+
totalReviews: computed(() => summary.value?.totalCount ?? 0),
|
|
69
|
+
// Methods
|
|
70
|
+
fetchReviews,
|
|
71
|
+
fetchSummary,
|
|
72
|
+
submit,
|
|
73
|
+
// Lifecycle hooks
|
|
74
|
+
onReviewSubmitted: reviewSubmittedHook.on,
|
|
75
|
+
onError: errorHook.on
|
|
76
|
+
};
|
|
77
|
+
}
|
|
File without changes
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { useFetch, useRuntimeConfig } from "#imports";
|
|
2
|
+
export function useStoreInfo() {
|
|
3
|
+
const config = useRuntimeConfig();
|
|
4
|
+
const apiBase = config.public.commerce?.apiBase || "/api/_commerce";
|
|
5
|
+
const { data: store, status, refresh, error } = useFetch(
|
|
6
|
+
`${apiBase}/store`,
|
|
7
|
+
{ key: "commerce:store" }
|
|
8
|
+
);
|
|
9
|
+
return {
|
|
10
|
+
store,
|
|
11
|
+
loading: computed(() => status.value === "pending"),
|
|
12
|
+
error,
|
|
13
|
+
refresh
|
|
14
|
+
};
|
|
15
|
+
}
|
|
File without changes
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { useState, readonly } from "#imports";
|
|
2
|
+
import { CommerceError, isCommerceError } from "@commercejs/types";
|
|
3
|
+
import { createEventHook } from "@vueuse/core";
|
|
4
|
+
import { useAdapter } from "./useAdapter.js";
|
|
5
|
+
export function useWishlist() {
|
|
6
|
+
const adapter = useAdapter();
|
|
7
|
+
const wishlist = useState("commerce:wishlist", () => null);
|
|
8
|
+
const loading = useState("commerce:wishlist:loading", () => false);
|
|
9
|
+
const error = useState("commerce:wishlist:error", () => null);
|
|
10
|
+
const itemAddedHook = createEventHook();
|
|
11
|
+
const itemRemovedHook = createEventHook();
|
|
12
|
+
const errorHook = createEventHook();
|
|
13
|
+
function handleError(err) {
|
|
14
|
+
const e = isCommerceError(err) ? err : new CommerceError(
|
|
15
|
+
err instanceof Error ? err.message : String(err),
|
|
16
|
+
"UNKNOWN",
|
|
17
|
+
void 0,
|
|
18
|
+
err
|
|
19
|
+
);
|
|
20
|
+
error.value = e;
|
|
21
|
+
errorHook.trigger(e);
|
|
22
|
+
return e;
|
|
23
|
+
}
|
|
24
|
+
async function refresh() {
|
|
25
|
+
loading.value = true;
|
|
26
|
+
error.value = null;
|
|
27
|
+
try {
|
|
28
|
+
wishlist.value = await adapter.getWishlist();
|
|
29
|
+
} catch (err) {
|
|
30
|
+
handleError(err);
|
|
31
|
+
} finally {
|
|
32
|
+
loading.value = false;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
async function addItem(productId, variantId) {
|
|
36
|
+
loading.value = true;
|
|
37
|
+
error.value = null;
|
|
38
|
+
try {
|
|
39
|
+
wishlist.value = await adapter.addToWishlist(productId, variantId);
|
|
40
|
+
itemAddedHook.trigger(wishlist.value);
|
|
41
|
+
} catch (err) {
|
|
42
|
+
handleError(err);
|
|
43
|
+
} finally {
|
|
44
|
+
loading.value = false;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
async function removeItem(itemId) {
|
|
48
|
+
loading.value = true;
|
|
49
|
+
error.value = null;
|
|
50
|
+
try {
|
|
51
|
+
wishlist.value = await adapter.removeFromWishlist(itemId);
|
|
52
|
+
itemRemovedHook.trigger(wishlist.value);
|
|
53
|
+
} catch (err) {
|
|
54
|
+
handleError(err);
|
|
55
|
+
} finally {
|
|
56
|
+
loading.value = false;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
function isInWishlist(productId) {
|
|
60
|
+
return wishlist.value?.items.some((item) => item.product.id === productId) ?? false;
|
|
61
|
+
}
|
|
62
|
+
return {
|
|
63
|
+
// State (readonly)
|
|
64
|
+
wishlist: readonly(wishlist),
|
|
65
|
+
loading: readonly(loading),
|
|
66
|
+
error: readonly(error),
|
|
67
|
+
// Item count
|
|
68
|
+
itemCount: readonly(useState(
|
|
69
|
+
"commerce:wishlist:count",
|
|
70
|
+
() => wishlist.value?.itemCount ?? 0
|
|
71
|
+
)),
|
|
72
|
+
// Methods
|
|
73
|
+
refresh,
|
|
74
|
+
addItem,
|
|
75
|
+
removeItem,
|
|
76
|
+
isInWishlist,
|
|
77
|
+
// Lifecycle hooks
|
|
78
|
+
onItemAdded: itemAddedHook.on,
|
|
79
|
+
onItemRemoved: itemRemovedHook.on,
|
|
80
|
+
onError: errorHook.on
|
|
81
|
+
};
|
|
82
|
+
}
|
|
File without changes
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { defineNuxtPlugin } from "#imports";
|
|
2
|
+
import { consola } from "consola";
|
|
3
|
+
const logger = consola.withTag("@commercejs/nuxt");
|
|
4
|
+
export default defineNuxtPlugin({
|
|
5
|
+
name: "commercejs",
|
|
6
|
+
enforce: "pre",
|
|
7
|
+
async setup(nuxtApp) {
|
|
8
|
+
if (import.meta.server) {
|
|
9
|
+
const event = nuxtApp.ssrContext?.event;
|
|
10
|
+
if (event) {
|
|
11
|
+
const adapter = event.context?._commerceAdapter;
|
|
12
|
+
if (adapter) {
|
|
13
|
+
nuxtApp.provide("commerce", adapter);
|
|
14
|
+
} else {
|
|
15
|
+
logger.warn(
|
|
16
|
+
"No commerce adapter found in server context. Ensure the Nitro server plugin is registered."
|
|
17
|
+
);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
} else {
|
|
21
|
+
logger.debug("CommerceJS running in client mode \u2014 using SSR-hydrated data.");
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
});
|
|
File without changes
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { defineEventHandler, readBody } from "h3";
|
|
2
|
+
import { useServerAdapter } from "../utils/adapter.js";
|
|
3
|
+
export default defineEventHandler(async (event) => {
|
|
4
|
+
const adapter = useServerAdapter(event);
|
|
5
|
+
const { email } = await readBody(event);
|
|
6
|
+
await adapter.forgotPassword(email);
|
|
7
|
+
return { success: true };
|
|
8
|
+
});
|
|
File without changes
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { defineEventHandler, readBody } from "h3";
|
|
2
|
+
import { useServerAdapter } from "../utils/adapter.js";
|
|
3
|
+
export default defineEventHandler(async (event) => {
|
|
4
|
+
const adapter = useServerAdapter(event);
|
|
5
|
+
const body = await readBody(event);
|
|
6
|
+
return adapter.login(body.email, body.password);
|
|
7
|
+
});
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { defineEventHandler, readBody } from "h3";
|
|
2
|
+
import { useServerAdapter } from "../utils/adapter.js";
|
|
3
|
+
export default defineEventHandler(async (event) => {
|
|
4
|
+
const adapter = useServerAdapter(event);
|
|
5
|
+
const body = await readBody(event);
|
|
6
|
+
return adapter.register(body);
|
|
7
|
+
});
|
|
File without changes
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { defineEventHandler, readBody } from "h3";
|
|
2
|
+
import { useServerAdapter } from "../utils/adapter.js";
|
|
3
|
+
export default defineEventHandler(async (event) => {
|
|
4
|
+
const adapter = useServerAdapter(event);
|
|
5
|
+
const { token, newPassword } = await readBody(event);
|
|
6
|
+
await adapter.resetPassword(token, newPassword);
|
|
7
|
+
return { success: true };
|
|
8
|
+
});
|
|
File without changes
|
|
File without changes
|