@commercejs/nuxt 0.2.0 → 0.2.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 (153) hide show
  1. package/LICENSE +21 -0
  2. package/dist/module.json +1 -1
  3. package/dist/module.mjs +3 -50
  4. package/dist/runtime/composables/index.js +0 -15
  5. package/dist/runtime/composables/useCart.js +4 -4
  6. package/dist/runtime/composables/useCheckout.js +25 -10
  7. package/dist/runtime/composables/useCountries.js +5 -4
  8. package/dist/runtime/composables/useCustomer.js +37 -14
  9. package/dist/runtime/composables/useOrders.js +110 -0
  10. package/dist/runtime/composables/usePrice.js +42 -0
  11. package/dist/runtime/server/api/_commerce/auth/forgot-password.post.js +5 -0
  12. package/dist/runtime/server/api/_commerce/auth/login.post.js +5 -0
  13. package/dist/runtime/server/api/_commerce/auth/logout.post.js +3 -0
  14. package/dist/runtime/server/api/_commerce/auth/register.post.js +5 -0
  15. package/dist/runtime/server/api/_commerce/auth/reset-password.post.js +5 -0
  16. package/dist/runtime/server/api/_commerce/brands.get.js +3 -0
  17. package/dist/runtime/server/api/_commerce/cart/[id]/index.get.js +5 -0
  18. package/dist/runtime/server/api/_commerce/cart/[id]/items/[itemId].delete.js +6 -0
  19. package/dist/runtime/server/api/_commerce/cart/[id]/items/[itemId].put.js +7 -0
  20. package/dist/runtime/server/api/_commerce/cart/[id]/items/index.post.js +6 -0
  21. package/dist/runtime/server/api/_commerce/cart/index.post.js +3 -0
  22. package/dist/runtime/server/api/_commerce/categories.get.js +8 -0
  23. package/dist/runtime/server/api/_commerce/checkout/payment-methods/[cartId].get.js +5 -0
  24. package/dist/runtime/server/api/_commerce/checkout/place-order.post.js +5 -0
  25. package/dist/runtime/server/api/_commerce/checkout/set-billing-address.post.js +5 -0
  26. package/dist/runtime/server/api/_commerce/checkout/set-payment-method.post.js +5 -0
  27. package/dist/runtime/server/api/_commerce/checkout/set-shipping-address.post.js +5 -0
  28. package/dist/runtime/server/api/_commerce/checkout/set-shipping-method.post.js +5 -0
  29. package/dist/runtime/server/api/_commerce/checkout/shipping-methods/[cartId].get.js +5 -0
  30. package/dist/runtime/server/api/_commerce/cities.get.js +10 -0
  31. package/dist/runtime/server/api/_commerce/countries.get.js +12 -0
  32. package/dist/runtime/server/api/_commerce/customer/addresses/[addressId].delete.js +5 -0
  33. package/dist/runtime/server/api/_commerce/customer/addresses/[addressId].put.js +6 -0
  34. package/dist/runtime/server/api/_commerce/customer/addresses/index.get.js +3 -0
  35. package/dist/runtime/server/api/_commerce/customer/addresses/index.post.js +5 -0
  36. package/dist/runtime/server/api/_commerce/customer/index.get.js +3 -0
  37. package/dist/runtime/server/api/_commerce/customer/index.put.js +5 -0
  38. package/dist/runtime/server/api/_commerce/customer/orders/[orderId]/cancel.post.js +5 -0
  39. package/dist/runtime/server/api/_commerce/customer/orders/[orderId]/index.get.js +5 -0
  40. package/dist/runtime/server/api/_commerce/customer/orders/index.get.js +8 -0
  41. package/dist/runtime/server/api/_commerce/locations.get.js +3 -0
  42. package/dist/runtime/server/api/_commerce/products/[id].get.js +6 -0
  43. package/dist/runtime/server/api/{products.get.js → _commerce/products/index.get.js} +2 -4
  44. package/dist/runtime/server/api/_commerce/promotions/index.get.js +3 -0
  45. package/dist/runtime/server/api/_commerce/promotions/validate.post.js +5 -0
  46. package/dist/runtime/server/api/_commerce/returns/[returnId]/cancel.post.js +5 -0
  47. package/dist/runtime/server/api/_commerce/returns/[returnId]/index.get.d.ts +0 -0
  48. package/dist/runtime/server/api/_commerce/returns/[returnId]/index.get.js +5 -0
  49. package/dist/runtime/server/api/_commerce/returns/index.get.d.ts +0 -0
  50. package/dist/runtime/server/api/_commerce/returns/index.get.js +8 -0
  51. package/dist/runtime/server/api/_commerce/returns/index.post.d.ts +0 -0
  52. package/dist/runtime/server/api/_commerce/returns/index.post.js +5 -0
  53. package/dist/runtime/server/api/_commerce/reviews/[productId]/index.get.d.ts +0 -0
  54. package/dist/runtime/server/api/_commerce/reviews/[productId]/index.get.js +9 -0
  55. package/dist/runtime/server/api/_commerce/reviews/[productId]/summary.get.d.ts +0 -0
  56. package/dist/runtime/server/api/_commerce/reviews/[productId]/summary.get.js +5 -0
  57. package/dist/runtime/server/api/_commerce/reviews/index.post.d.ts +0 -0
  58. package/dist/runtime/server/api/_commerce/reviews/index.post.js +5 -0
  59. package/dist/runtime/server/api/_commerce/store.get.d.ts +0 -0
  60. package/dist/runtime/server/api/_commerce/store.get.js +3 -0
  61. package/dist/runtime/server/api/_commerce/wishlist/index.get.d.ts +0 -0
  62. package/dist/runtime/server/api/_commerce/wishlist/index.get.js +3 -0
  63. package/dist/runtime/server/api/_commerce/wishlist/items/[itemId].delete.d.ts +0 -0
  64. package/dist/runtime/server/api/_commerce/wishlist/items/[itemId].delete.js +5 -0
  65. package/dist/runtime/server/api/_commerce/wishlist/items/index.post.d.ts +0 -0
  66. package/dist/runtime/server/api/_commerce/wishlist/items/index.post.js +5 -0
  67. package/dist/runtime/server/data/cities.d.ts +0 -0
  68. package/dist/runtime/server/data/cities.js +100 -0
  69. package/dist/runtime/server/data/country-meta.d.ts +0 -0
  70. package/dist/runtime/server/data/country-meta.js +8 -0
  71. package/dist/runtime/server/plugins/commerce-adapter.js +37 -16
  72. package/dist/runtime/server/utils/context.d.ts +0 -0
  73. package/dist/runtime/server/utils/context.js +26 -0
  74. package/dist/runtime/server/utils/handler.d.ts +0 -0
  75. package/dist/runtime/server/utils/handler.js +42 -0
  76. package/dist/runtime/server/utils/schemas.d.ts +0 -0
  77. package/dist/runtime/server/utils/schemas.js +89 -0
  78. package/package.json +19 -12
  79. package/dist/runtime/server/api/auth.forgot-password.post.js +0 -8
  80. package/dist/runtime/server/api/auth.login.post.js +0 -7
  81. package/dist/runtime/server/api/auth.logout.post.js +0 -7
  82. package/dist/runtime/server/api/auth.register.post.js +0 -7
  83. package/dist/runtime/server/api/auth.reset-password.post.js +0 -8
  84. package/dist/runtime/server/api/brands.get.js +0 -6
  85. package/dist/runtime/server/api/cart.[id].get.js +0 -7
  86. package/dist/runtime/server/api/cart.[id].items.[itemId].delete.js +0 -8
  87. package/dist/runtime/server/api/cart.[id].items.[itemId].put.js +0 -9
  88. package/dist/runtime/server/api/cart.[id].items.post.js +0 -8
  89. package/dist/runtime/server/api/cart.post.js +0 -6
  90. package/dist/runtime/server/api/categories.get.js +0 -10
  91. package/dist/runtime/server/api/checkout.payment-methods.[cartId].get.js +0 -7
  92. package/dist/runtime/server/api/checkout.place-order.post.js +0 -7
  93. package/dist/runtime/server/api/checkout.shipping-methods.[cartId].get.js +0 -7
  94. package/dist/runtime/server/api/countries.get.js +0 -6
  95. package/dist/runtime/server/api/customer.addresses.[addressId].delete.js +0 -8
  96. package/dist/runtime/server/api/customer.addresses.[addressId].put.js +0 -8
  97. package/dist/runtime/server/api/customer.addresses.get.js +0 -6
  98. package/dist/runtime/server/api/customer.addresses.post.js +0 -7
  99. package/dist/runtime/server/api/customer.get.js +0 -6
  100. package/dist/runtime/server/api/customer.orders.get.js +0 -10
  101. package/dist/runtime/server/api/locations.get.js +0 -6
  102. package/dist/runtime/server/api/products.[id].get.js +0 -7
  103. package/dist/runtime/server/api/promotions.get.js +0 -6
  104. package/dist/runtime/server/api/promotions.validate.post.js +0 -7
  105. package/dist/runtime/server/api/returns.[returnId].cancel.post.js +0 -7
  106. package/dist/runtime/server/api/returns.[returnId].get.js +0 -7
  107. package/dist/runtime/server/api/returns.get.js +0 -10
  108. package/dist/runtime/server/api/returns.post.js +0 -7
  109. package/dist/runtime/server/api/reviews.[productId].get.js +0 -11
  110. package/dist/runtime/server/api/reviews.[productId].summary.get.js +0 -7
  111. package/dist/runtime/server/api/reviews.post.js +0 -7
  112. package/dist/runtime/server/api/store.get.js +0 -6
  113. package/dist/runtime/server/api/wishlist.get.js +0 -6
  114. package/dist/runtime/server/api/wishlist.items.[itemId].delete.js +0 -7
  115. package/dist/runtime/server/api/wishlist.items.post.js +0 -7
  116. /package/dist/runtime/{server/api/auth.forgot-password.post.d.ts → composables/useOrders.d.ts} +0 -0
  117. /package/dist/runtime/{server/api/auth.login.post.d.ts → composables/usePrice.d.ts} +0 -0
  118. /package/dist/runtime/server/api/{auth.logout.post.d.ts → _commerce/auth/forgot-password.post.d.ts} +0 -0
  119. /package/dist/runtime/server/api/{auth.register.post.d.ts → _commerce/auth/login.post.d.ts} +0 -0
  120. /package/dist/runtime/server/api/{auth.reset-password.post.d.ts → _commerce/auth/logout.post.d.ts} +0 -0
  121. /package/dist/runtime/server/api/{brands.get.d.ts → _commerce/auth/register.post.d.ts} +0 -0
  122. /package/dist/runtime/server/api/{cart.[id].get.d.ts → _commerce/auth/reset-password.post.d.ts} +0 -0
  123. /package/dist/runtime/server/api/{cart.[id].items.[itemId].delete.d.ts → _commerce/brands.get.d.ts} +0 -0
  124. /package/dist/runtime/server/api/{cart.[id].items.[itemId].put.d.ts → _commerce/cart/[id]/index.get.d.ts} +0 -0
  125. /package/dist/runtime/server/api/{cart.[id].items.post.d.ts → _commerce/cart/[id]/items/[itemId].delete.d.ts} +0 -0
  126. /package/dist/runtime/server/api/{cart.post.d.ts → _commerce/cart/[id]/items/[itemId].put.d.ts} +0 -0
  127. /package/dist/runtime/server/api/{categories.get.d.ts → _commerce/cart/[id]/items/index.post.d.ts} +0 -0
  128. /package/dist/runtime/server/api/{checkout.payment-methods.[cartId].get.d.ts → _commerce/cart/index.post.d.ts} +0 -0
  129. /package/dist/runtime/server/api/{checkout.place-order.post.d.ts → _commerce/categories.get.d.ts} +0 -0
  130. /package/dist/runtime/server/api/{checkout.shipping-methods.[cartId].get.d.ts → _commerce/checkout/payment-methods/[cartId].get.d.ts} +0 -0
  131. /package/dist/runtime/server/api/{countries.get.d.ts → _commerce/checkout/place-order.post.d.ts} +0 -0
  132. /package/dist/runtime/server/api/{customer.addresses.[addressId].delete.d.ts → _commerce/checkout/set-billing-address.post.d.ts} +0 -0
  133. /package/dist/runtime/server/api/{customer.addresses.[addressId].put.d.ts → _commerce/checkout/set-payment-method.post.d.ts} +0 -0
  134. /package/dist/runtime/server/api/{customer.addresses.get.d.ts → _commerce/checkout/set-shipping-address.post.d.ts} +0 -0
  135. /package/dist/runtime/server/api/{customer.addresses.post.d.ts → _commerce/checkout/set-shipping-method.post.d.ts} +0 -0
  136. /package/dist/runtime/server/api/{customer.get.d.ts → _commerce/checkout/shipping-methods/[cartId].get.d.ts} +0 -0
  137. /package/dist/runtime/server/api/{customer.orders.get.d.ts → _commerce/cities.get.d.ts} +0 -0
  138. /package/dist/runtime/server/api/{locations.get.d.ts → _commerce/countries.get.d.ts} +0 -0
  139. /package/dist/runtime/server/api/{products.[id].get.d.ts → _commerce/customer/addresses/[addressId].delete.d.ts} +0 -0
  140. /package/dist/runtime/server/api/{products.get.d.ts → _commerce/customer/addresses/[addressId].put.d.ts} +0 -0
  141. /package/dist/runtime/server/api/{promotions.get.d.ts → _commerce/customer/addresses/index.get.d.ts} +0 -0
  142. /package/dist/runtime/server/api/{promotions.validate.post.d.ts → _commerce/customer/addresses/index.post.d.ts} +0 -0
  143. /package/dist/runtime/server/api/{returns.[returnId].cancel.post.d.ts → _commerce/customer/index.get.d.ts} +0 -0
  144. /package/dist/runtime/server/api/{returns.[returnId].get.d.ts → _commerce/customer/index.put.d.ts} +0 -0
  145. /package/dist/runtime/server/api/{returns.get.d.ts → _commerce/customer/orders/[orderId]/cancel.post.d.ts} +0 -0
  146. /package/dist/runtime/server/api/{returns.post.d.ts → _commerce/customer/orders/[orderId]/index.get.d.ts} +0 -0
  147. /package/dist/runtime/server/api/{reviews.[productId].get.d.ts → _commerce/customer/orders/index.get.d.ts} +0 -0
  148. /package/dist/runtime/server/api/{reviews.[productId].summary.get.d.ts → _commerce/locations.get.d.ts} +0 -0
  149. /package/dist/runtime/server/api/{reviews.post.d.ts → _commerce/products/[id].get.d.ts} +0 -0
  150. /package/dist/runtime/server/api/{store.get.d.ts → _commerce/products/index.get.d.ts} +0 -0
  151. /package/dist/runtime/server/api/{wishlist.get.d.ts → _commerce/promotions/index.get.d.ts} +0 -0
  152. /package/dist/runtime/server/api/{wishlist.items.[itemId].delete.d.ts → _commerce/promotions/validate.post.d.ts} +0 -0
  153. /package/dist/runtime/server/api/{wishlist.items.post.d.ts → _commerce/returns/[returnId]/cancel.post.d.ts} +0 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 CommerceJS Contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/dist/module.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "compatibility": {
5
5
  "nuxt": ">=3.0.0"
6
6
  },
7
- "version": "0.2.0",
7
+ "version": "0.2.1",
8
8
  "builder": {
9
9
  "@nuxt/module-builder": "0.8.4",
10
10
  "unbuild": "unknown"
package/dist/module.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { defineNuxtModule, createResolver, addTypeTemplate, addPlugin, addServerPlugin, addImportsDir, addServerHandler } from '@nuxt/kit';
1
+ import { defineNuxtModule, createResolver, addTypeTemplate, addPlugin, addServerPlugin, addImportsDir, addServerScanDir } from '@nuxt/kit';
2
2
  import { consola } from 'consola';
3
3
 
4
4
  const logger = consola.withTag("@commercejs/nuxt");
@@ -45,55 +45,8 @@ const commerceModule = defineNuxtModule({
45
45
  addServerPlugin(resolve("./runtime/server/plugins/commerce-adapter"));
46
46
  addImportsDir(resolve("./runtime/composables"));
47
47
  if (options.apiRoutes) {
48
- const apiBase = options.apiBase || "/api/_commerce";
49
- let routeCount = 0;
50
- const route = (path, handler, method) => {
51
- addServerHandler({
52
- route: `${apiBase}${path}`,
53
- handler: resolve(`./runtime/server/api/${handler}`),
54
- ...method ? { method } : {}
55
- });
56
- routeCount++;
57
- };
58
- route("/store", "store.get");
59
- route("/products", "products.get");
60
- route("/products/:id", "products.[id].get");
61
- route("/categories", "categories.get");
62
- route("/brands", "brands.get");
63
- route("/cart", "cart.post", "post");
64
- route("/cart/:id", "cart.[id].get");
65
- route("/cart/:id/items", "cart.[id].items.post", "post");
66
- route("/cart/:id/items/:itemId", "cart.[id].items.[itemId].put", "put");
67
- route("/cart/:id/items/:itemId", "cart.[id].items.[itemId].delete", "delete");
68
- route("/checkout/shipping-methods/:cartId", "checkout.shipping-methods.[cartId].get");
69
- route("/checkout/payment-methods/:cartId", "checkout.payment-methods.[cartId].get");
70
- route("/checkout/place-order", "checkout.place-order.post", "post");
71
- route("/auth/login", "auth.login.post", "post");
72
- route("/auth/register", "auth.register.post", "post");
73
- route("/auth/logout", "auth.logout.post", "post");
74
- route("/auth/forgot-password", "auth.forgot-password.post", "post");
75
- route("/auth/reset-password", "auth.reset-password.post", "post");
76
- route("/customer", "customer.get");
77
- route("/customer/orders", "customer.orders.get");
78
- route("/customer/addresses", "customer.addresses.get");
79
- route("/customer/addresses", "customer.addresses.post", "post");
80
- route("/customer/addresses/:addressId", "customer.addresses.[addressId].put", "put");
81
- route("/customer/addresses/:addressId", "customer.addresses.[addressId].delete", "delete");
82
- route("/reviews/:productId", "reviews.[productId].get");
83
- route("/reviews/:productId/summary", "reviews.[productId].summary.get");
84
- route("/reviews", "reviews.post", "post");
85
- route("/wishlist", "wishlist.get");
86
- route("/wishlist/items", "wishlist.items.post", "post");
87
- route("/wishlist/items/:itemId", "wishlist.items.[itemId].delete", "delete");
88
- route("/promotions", "promotions.get");
89
- route("/promotions/validate", "promotions.validate.post", "post");
90
- route("/returns", "returns.get");
91
- route("/returns", "returns.post", "post");
92
- route("/returns/:returnId", "returns.[returnId].get");
93
- route("/returns/:returnId/cancel", "returns.[returnId].cancel.post", "post");
94
- route("/countries", "countries.get");
95
- route("/locations", "locations.get");
96
- logger.info(`Registered ${routeCount} REST API routes under ${apiBase}`);
48
+ addServerScanDir(resolve("./runtime/server"));
49
+ logger.info(`Server routes auto-discovered under ${options.apiBase}`);
97
50
  }
98
51
  }
99
52
  });
@@ -1,15 +0,0 @@
1
- export { useAdapter } from "./useAdapter.js";
2
- export { useProduct } from "./useProduct.js";
3
- export { useProducts } from "./useProducts.js";
4
- export { useCategories } from "./useCategories.js";
5
- export { useCart } from "./useCart.js";
6
- export { useCustomer } from "./useCustomer.js";
7
- export { useCheckout } from "./useCheckout.js";
8
- export { useWishlist } from "./useWishlist.js";
9
- export { useReviews } from "./useReviews.js";
10
- export { useStoreInfo } from "./useStoreInfo.js";
11
- export { usePromotions } from "./usePromotions.js";
12
- export { useReturns } from "./useReturns.js";
13
- export { useBrands } from "./useBrands.js";
14
- export { useCountries } from "./useCountries.js";
15
- export { useLocations } from "./useLocations.js";
@@ -1,6 +1,10 @@
1
1
  import { useState, useCookie, computed, readonly, useRuntimeConfig } from "#imports";
2
2
  import { CommerceError, isCommerceError } from "@commercejs/types";
3
3
  import { createEventHook } from "@vueuse/core";
4
+ const itemAddedHook = createEventHook();
5
+ const itemUpdatedHook = createEventHook();
6
+ const itemRemovedHook = createEventHook();
7
+ const errorHook = createEventHook();
4
8
  export function useCart() {
5
9
  const config = useRuntimeConfig();
6
10
  const apiBase = config.public.commerce?.apiBase || "/api/_commerce";
@@ -8,10 +12,6 @@ export function useCart() {
8
12
  const cart = useState("commerce_cart", () => null);
9
13
  const loading = useState("commerce_cart_loading", () => false);
10
14
  const error = useState("commerce_cart_error", () => null);
11
- const itemAddedHook = createEventHook();
12
- const itemUpdatedHook = createEventHook();
13
- const itemRemovedHook = createEventHook();
14
- const errorHook = createEventHook();
15
15
  function handleError(err) {
16
16
  const e = isCommerceError(err) ? err : new CommerceError(
17
17
  err instanceof Error ? err.message : String(err),
@@ -1,10 +1,10 @@
1
- import { useState, readonly } from "#imports";
1
+ import { useState, readonly, useRuntimeConfig } from "#imports";
2
2
  import { CommerceError, isCommerceError } from "@commercejs/types";
3
3
  import { createEventHook } from "@vueuse/core";
4
- import { useAdapter } from "./useAdapter.js";
5
4
  import { useCart } from "./useCart.js";
6
5
  export function useCheckout() {
7
- const adapter = useAdapter();
6
+ const config = useRuntimeConfig();
7
+ const apiBase = config.public.commerce?.apiBase || "/api/_commerce";
8
8
  const { cart, cartId } = useCart();
9
9
  const shippingMethods = useState("commerce_shipping_methods", () => []);
10
10
  const paymentMethods = useState("commerce_payment_methods", () => []);
@@ -34,7 +34,7 @@ export function useCheckout() {
34
34
  loading.value = true;
35
35
  error.value = null;
36
36
  try {
37
- shippingMethods.value = await adapter.getShippingMethods(id);
37
+ shippingMethods.value = await $fetch(`${apiBase}/checkout/shipping-methods/${id}`);
38
38
  } catch (err) {
39
39
  throw handleError(err);
40
40
  } finally {
@@ -46,7 +46,7 @@ export function useCheckout() {
46
46
  loading.value = true;
47
47
  error.value = null;
48
48
  try {
49
- paymentMethods.value = await adapter.getPaymentMethods(id);
49
+ paymentMethods.value = await $fetch(`${apiBase}/checkout/payment-methods/${id}`);
50
50
  } catch (err) {
51
51
  throw handleError(err);
52
52
  } finally {
@@ -58,7 +58,10 @@ export function useCheckout() {
58
58
  loading.value = true;
59
59
  error.value = null;
60
60
  try {
61
- const updatedCart = await adapter.setShippingAddress(id, address);
61
+ const updatedCart = await $fetch(`${apiBase}/checkout/set-shipping-address`, {
62
+ method: "POST",
63
+ body: { cartId: id, address }
64
+ });
62
65
  useState("commerce_cart").value = updatedCart;
63
66
  } catch (err) {
64
67
  throw handleError(err);
@@ -71,7 +74,10 @@ export function useCheckout() {
71
74
  loading.value = true;
72
75
  error.value = null;
73
76
  try {
74
- const updatedCart = await adapter.setBillingAddress(id, address);
77
+ const updatedCart = await $fetch(`${apiBase}/checkout/set-billing-address`, {
78
+ method: "POST",
79
+ body: { cartId: id, address }
80
+ });
75
81
  useState("commerce_cart").value = updatedCart;
76
82
  } catch (err) {
77
83
  throw handleError(err);
@@ -84,7 +90,10 @@ export function useCheckout() {
84
90
  loading.value = true;
85
91
  error.value = null;
86
92
  try {
87
- const updatedCart = await adapter.setShippingMethod(id, methodId);
93
+ const updatedCart = await $fetch(`${apiBase}/checkout/set-shipping-method`, {
94
+ method: "POST",
95
+ body: { cartId: id, methodId }
96
+ });
88
97
  useState("commerce_cart").value = updatedCart;
89
98
  } catch (err) {
90
99
  throw handleError(err);
@@ -97,7 +106,10 @@ export function useCheckout() {
97
106
  loading.value = true;
98
107
  error.value = null;
99
108
  try {
100
- const updatedCart = await adapter.setPaymentMethod(id, methodId);
109
+ const updatedCart = await $fetch(`${apiBase}/checkout/set-payment-method`, {
110
+ method: "POST",
111
+ body: { cartId: id, methodId }
112
+ });
101
113
  useState("commerce_cart").value = updatedCart;
102
114
  } catch (err) {
103
115
  throw handleError(err);
@@ -110,7 +122,10 @@ export function useCheckout() {
110
122
  loading.value = true;
111
123
  error.value = null;
112
124
  try {
113
- const order = await adapter.placeOrder(id);
125
+ const order = await $fetch(`${apiBase}/checkout/place-order`, {
126
+ method: "POST",
127
+ body: { cartId: id }
128
+ });
114
129
  useState("commerce_cart").value = null;
115
130
  cartId.value = "";
116
131
  orderPlacedHook.trigger(order);
@@ -1,8 +1,8 @@
1
- import { useState, readonly } from "#imports";
1
+ import { useState, readonly, useRuntimeConfig } from "#imports";
2
2
  import { CommerceError, isCommerceError } from "@commercejs/types";
3
- import { useAdapter } from "./useAdapter.js";
4
3
  export function useCountries() {
5
- const adapter = useAdapter();
4
+ const config = useRuntimeConfig();
5
+ const apiBase = config.public.commerceApiBase || "/api/_commerce";
6
6
  const countries = useState("commerce:countries", () => []);
7
7
  const loading = useState("commerce:countries:loading", () => false);
8
8
  const error = useState("commerce:countries:error", () => null);
@@ -10,8 +10,9 @@ export function useCountries() {
10
10
  loading.value = true;
11
11
  error.value = null;
12
12
  try {
13
- countries.value = await adapter.getCountries();
13
+ countries.value = await $fetch(`${apiBase}/countries`);
14
14
  } catch (err) {
15
+ console.error("[useCountries] Failed to fetch countries:", err);
15
16
  const e = isCommerceError(err) ? err : new CommerceError(
16
17
  err instanceof Error ? err.message : String(err),
17
18
  "UNKNOWN",
@@ -1,9 +1,9 @@
1
- import { useState, computed, readonly } from "#imports";
1
+ import { useState, computed, readonly, useRuntimeConfig } from "#imports";
2
2
  import { CommerceError, isCommerceError } from "@commercejs/types";
3
3
  import { createEventHook } from "@vueuse/core";
4
- import { useAdapter } from "./useAdapter.js";
5
4
  export function useCustomer() {
6
- const adapter = useAdapter();
5
+ const config = useRuntimeConfig();
6
+ const apiBase = config.public.commerce?.apiBase || "/api/_commerce";
7
7
  const customer = useState("commerce_customer", () => null);
8
8
  const loading = useState("commerce_customer_loading", () => false);
9
9
  const error = useState("commerce_customer_error", () => null);
@@ -25,7 +25,10 @@ export function useCustomer() {
25
25
  loading.value = true;
26
26
  error.value = null;
27
27
  try {
28
- customer.value = await adapter.login(email, password);
28
+ customer.value = await $fetch(`${apiBase}/auth/login`, {
29
+ method: "POST",
30
+ body: { email, password }
31
+ });
29
32
  loginHook.trigger(customer.value);
30
33
  } catch (err) {
31
34
  throw handleError(err);
@@ -37,7 +40,10 @@ export function useCustomer() {
37
40
  loading.value = true;
38
41
  error.value = null;
39
42
  try {
40
- customer.value = await adapter.register(input);
43
+ customer.value = await $fetch(`${apiBase}/auth/register`, {
44
+ method: "POST",
45
+ body: input
46
+ });
41
47
  loginHook.trigger(customer.value);
42
48
  } catch (err) {
43
49
  throw handleError(err);
@@ -49,7 +55,7 @@ export function useCustomer() {
49
55
  loading.value = true;
50
56
  error.value = null;
51
57
  try {
52
- await adapter.logout();
58
+ await $fetch(`${apiBase}/auth/logout`, { method: "POST" });
53
59
  customer.value = null;
54
60
  logoutHook.trigger();
55
61
  } catch (err) {
@@ -62,7 +68,7 @@ export function useCustomer() {
62
68
  loading.value = true;
63
69
  error.value = null;
64
70
  try {
65
- customer.value = await adapter.getCustomer();
71
+ customer.value = await $fetch(`${apiBase}/customer`);
66
72
  } catch (err) {
67
73
  handleError(err);
68
74
  customer.value = null;
@@ -74,7 +80,10 @@ export function useCustomer() {
74
80
  loading.value = true;
75
81
  error.value = null;
76
82
  try {
77
- customer.value = await adapter.updateCustomer(input);
83
+ customer.value = await $fetch(`${apiBase}/customer`, {
84
+ method: "PUT",
85
+ body: input
86
+ });
78
87
  } catch (err) {
79
88
  throw handleError(err);
80
89
  } finally {
@@ -85,7 +94,10 @@ export function useCustomer() {
85
94
  loading.value = true;
86
95
  error.value = null;
87
96
  try {
88
- await adapter.forgotPassword(email);
97
+ await $fetch(`${apiBase}/auth/forgot-password`, {
98
+ method: "POST",
99
+ body: { email }
100
+ });
89
101
  } catch (err) {
90
102
  throw handleError(err);
91
103
  } finally {
@@ -96,7 +108,10 @@ export function useCustomer() {
96
108
  loading.value = true;
97
109
  error.value = null;
98
110
  try {
99
- await adapter.resetPassword(token, newPassword);
111
+ await $fetch(`${apiBase}/auth/reset-password`, {
112
+ method: "POST",
113
+ body: { token, password: newPassword }
114
+ });
100
115
  } catch (err) {
101
116
  throw handleError(err);
102
117
  } finally {
@@ -107,7 +122,7 @@ export function useCustomer() {
107
122
  loading.value = true;
108
123
  error.value = null;
109
124
  try {
110
- return await adapter.getAddresses();
125
+ return await $fetch(`${apiBase}/customer/addresses`);
111
126
  } catch (err) {
112
127
  throw handleError(err);
113
128
  } finally {
@@ -118,7 +133,10 @@ export function useCustomer() {
118
133
  loading.value = true;
119
134
  error.value = null;
120
135
  try {
121
- const saved = await adapter.addAddress(address);
136
+ const saved = await $fetch(`${apiBase}/customer/addresses`, {
137
+ method: "POST",
138
+ body: address
139
+ });
122
140
  await refresh();
123
141
  return saved;
124
142
  } catch (err) {
@@ -131,7 +149,10 @@ export function useCustomer() {
131
149
  loading.value = true;
132
150
  error.value = null;
133
151
  try {
134
- const updated = await adapter.updateAddress(addressId, address);
152
+ const updated = await $fetch(`${apiBase}/customer/addresses/${addressId}`, {
153
+ method: "PUT",
154
+ body: address
155
+ });
135
156
  await refresh();
136
157
  return updated;
137
158
  } catch (err) {
@@ -144,7 +165,9 @@ export function useCustomer() {
144
165
  loading.value = true;
145
166
  error.value = null;
146
167
  try {
147
- await adapter.deleteAddress(addressId);
168
+ await $fetch(`${apiBase}/customer/addresses/${addressId}`, {
169
+ method: "DELETE"
170
+ });
148
171
  await refresh();
149
172
  } catch (err) {
150
173
  throw handleError(err);
@@ -0,0 +1,110 @@
1
+ import { useState, readonly, useRuntimeConfig } from "#imports";
2
+ import { CommerceError, isCommerceError } from "@commercejs/types";
3
+ import { createEventHook } from "@vueuse/core";
4
+ const cancelledHook = createEventHook();
5
+ const errorHook = createEventHook();
6
+ export function useOrders() {
7
+ const config = useRuntimeConfig();
8
+ const apiBase = config.public.commerce?.apiBase || "/api/_commerce";
9
+ const orders = useState("commerce_orders", () => []);
10
+ const currentOrder = useState("commerce_current_order", () => null);
11
+ const totalPages = useState("commerce_orders_total_pages", () => 1);
12
+ const currentPage = useState("commerce_orders_page", () => 1);
13
+ const loading = useState("commerce_orders_loading", () => false);
14
+ const error = useState("commerce_orders_error", () => null);
15
+ function handleError(err) {
16
+ const e = isCommerceError(err) ? err : new CommerceError(
17
+ err instanceof Error ? err.message : String(err),
18
+ "UNKNOWN",
19
+ void 0,
20
+ err
21
+ );
22
+ error.value = e;
23
+ errorHook.trigger(e);
24
+ return e;
25
+ }
26
+ async function loadOrders(params) {
27
+ loading.value = true;
28
+ error.value = null;
29
+ try {
30
+ const query = {};
31
+ if (params?.page) query.page = String(params.page);
32
+ if (params?.perPage) query.perPage = String(params.perPage);
33
+ const result = await $fetch(`${apiBase}/customer/orders`, {
34
+ query
35
+ });
36
+ orders.value = result.items;
37
+ totalPages.value = result.perPage > 0 ? Math.ceil(result.total / result.perPage) : 1;
38
+ currentPage.value = params?.page ?? 1;
39
+ } catch (err) {
40
+ throw handleError(err);
41
+ } finally {
42
+ loading.value = false;
43
+ }
44
+ }
45
+ async function getOrder(orderId) {
46
+ loading.value = true;
47
+ error.value = null;
48
+ try {
49
+ const order = await $fetch(`${apiBase}/customer/orders/${orderId}`);
50
+ currentOrder.value = order;
51
+ return order;
52
+ } catch (err) {
53
+ throw handleError(err);
54
+ } finally {
55
+ loading.value = false;
56
+ }
57
+ }
58
+ async function cancelOrder(orderId) {
59
+ loading.value = true;
60
+ error.value = null;
61
+ try {
62
+ const order = await $fetch(`${apiBase}/customer/orders/${orderId}/cancel`, {
63
+ method: "POST"
64
+ });
65
+ const idx = orders.value.findIndex((o) => o.id === orderId);
66
+ if (idx !== -1) orders.value[idx] = order;
67
+ if (currentOrder.value?.id === orderId) currentOrder.value = order;
68
+ cancelledHook.trigger(order);
69
+ return order;
70
+ } catch (err) {
71
+ throw handleError(err);
72
+ } finally {
73
+ loading.value = false;
74
+ }
75
+ }
76
+ async function reorder(orderId) {
77
+ const order = currentOrder.value?.id === orderId ? currentOrder.value : await getOrder(orderId);
78
+ if (!order?.items?.length) {
79
+ throw new CommerceError("Order has no items to reorder", "VALIDATION");
80
+ }
81
+ const cart = await $fetch(`${apiBase}/cart`, { method: "POST" });
82
+ for (const item of order.items) {
83
+ await $fetch(`${apiBase}/cart/${cart.id}/items`, {
84
+ method: "POST",
85
+ body: {
86
+ productId: item.productId,
87
+ variantId: item.variantId,
88
+ quantity: item.quantity
89
+ }
90
+ });
91
+ }
92
+ return cart;
93
+ }
94
+ return {
95
+ orders: readonly(orders),
96
+ currentOrder: readonly(currentOrder),
97
+ totalPages: readonly(totalPages),
98
+ currentPage: readonly(currentPage),
99
+ loading: readonly(loading),
100
+ error: readonly(error),
101
+ // Event hooks
102
+ onOrderCancelled: cancelledHook.on,
103
+ onError: errorHook.on,
104
+ // Methods
105
+ loadOrders,
106
+ getOrder,
107
+ cancelOrder,
108
+ reorder
109
+ };
110
+ }
@@ -0,0 +1,42 @@
1
+ import { computed, toValue } from "#imports";
2
+ export function usePrice(locale) {
3
+ const resolvedLocale = computed(() => toValue(locale) || "en-SA");
4
+ function formatPrice(price, overrideLocale) {
5
+ if (!price) return "";
6
+ try {
7
+ return new Intl.NumberFormat(overrideLocale || resolvedLocale.value, {
8
+ style: "currency",
9
+ currency: price.currency,
10
+ minimumFractionDigits: 2,
11
+ maximumFractionDigits: 2
12
+ }).format(price.amount);
13
+ } catch {
14
+ return price.formatted || `${price.currency} ${price.amount}`;
15
+ }
16
+ }
17
+ function formatDiscount(price, overrideLocale) {
18
+ if (!price) return { current: "", original: "", percent: "" };
19
+ const current = formatPrice(price, overrideLocale);
20
+ const original = price.originalAmount != null ? formatPrice({ amount: price.originalAmount, currency: price.currency, formatted: "" }, overrideLocale) : "";
21
+ const percent = price.discountPercent != null ? `${Math.round(price.discountPercent)}%` : "";
22
+ return { current, original, percent };
23
+ }
24
+ function formatAmount(amount, currency, overrideLocale) {
25
+ try {
26
+ return new Intl.NumberFormat(overrideLocale || resolvedLocale.value, {
27
+ style: "currency",
28
+ currency,
29
+ minimumFractionDigits: 2,
30
+ maximumFractionDigits: 2
31
+ }).format(amount);
32
+ } catch {
33
+ return `${currency} ${amount.toFixed(2)}`;
34
+ }
35
+ }
36
+ return {
37
+ formatPrice,
38
+ formatDiscount,
39
+ formatAmount,
40
+ locale: resolvedLocale
41
+ };
42
+ }
@@ -0,0 +1,5 @@
1
+ import { readBody } from "h3";
2
+ export default defineCommerceHandler(async (event, adapter) => {
3
+ const { email } = forgotPasswordSchema.parse(await readBody(event));
4
+ return adapter.forgotPassword(email);
5
+ });
@@ -0,0 +1,5 @@
1
+ import { readBody } from "h3";
2
+ export default defineCommerceHandler(async (event, adapter) => {
3
+ const { email, password } = loginSchema.parse(await readBody(event));
4
+ return adapter.login(email, password);
5
+ });
@@ -0,0 +1,3 @@
1
+ export default defineCommerceHandler(async (_event, adapter) => {
2
+ return adapter.logout();
3
+ });
@@ -0,0 +1,5 @@
1
+ import { readBody } from "h3";
2
+ export default defineCommerceHandler(async (event, adapter) => {
3
+ const body = registerSchema.parse(await readBody(event));
4
+ return adapter.register(body);
5
+ });
@@ -0,0 +1,5 @@
1
+ import { readBody } from "h3";
2
+ export default defineCommerceHandler(async (event, adapter) => {
3
+ const { token, password } = resetPasswordSchema.parse(await readBody(event));
4
+ return adapter.resetPassword(token, password);
5
+ });
@@ -0,0 +1,3 @@
1
+ export default defineCommerceHandler(async (_event, adapter) => {
2
+ return adapter.getBrands();
3
+ });
@@ -0,0 +1,5 @@
1
+ import { getRouterParam } from "h3";
2
+ export default defineCommerceHandler(async (event, adapter) => {
3
+ const id = getRouterParam(event, "id");
4
+ return adapter.getCart(id);
5
+ });
@@ -0,0 +1,6 @@
1
+ import { getRouterParam } from "h3";
2
+ export default defineCommerceHandler(async (event, adapter) => {
3
+ const id = getRouterParam(event, "id");
4
+ const itemId = getRouterParam(event, "itemId");
5
+ return adapter.removeFromCart(id, itemId);
6
+ });
@@ -0,0 +1,7 @@
1
+ import { getRouterParam, readBody } from "h3";
2
+ export default defineCommerceHandler(async (event, adapter) => {
3
+ const id = getRouterParam(event, "id");
4
+ const itemId = getRouterParam(event, "itemId");
5
+ const { quantity } = updateCartItemSchema.parse(await readBody(event));
6
+ return adapter.updateCartItem(id, itemId, quantity);
7
+ });
@@ -0,0 +1,6 @@
1
+ import { getRouterParam, readBody } from "h3";
2
+ export default defineCommerceHandler(async (event, adapter) => {
3
+ const id = getRouterParam(event, "id");
4
+ const body = addToCartSchema.parse(await readBody(event));
5
+ return adapter.addToCart(id, body);
6
+ });
@@ -0,0 +1,3 @@
1
+ export default defineCommerceHandler(async (_event, adapter) => {
2
+ return adapter.createCart();
3
+ });
@@ -0,0 +1,8 @@
1
+ import { getQuery } from "h3";
2
+ export default defineCommerceHandler(async (event, adapter) => {
3
+ const query = getQuery(event);
4
+ return adapter.getCategories({
5
+ parentId: query.parentId,
6
+ depth: query.depth ? Number(query.depth) : void 0
7
+ });
8
+ });
@@ -0,0 +1,5 @@
1
+ import { getRouterParam } from "h3";
2
+ export default defineCommerceHandler(async (event, adapter) => {
3
+ const cartId = getRouterParam(event, "cartId");
4
+ return adapter.getPaymentMethods(cartId);
5
+ });
@@ -0,0 +1,5 @@
1
+ import { readBody } from "h3";
2
+ export default defineCommerceHandler(async (event, adapter) => {
3
+ const { cartId } = placeOrderSchema.parse(await readBody(event));
4
+ return adapter.placeOrder(cartId);
5
+ });
@@ -0,0 +1,5 @@
1
+ import { readBody } from "h3";
2
+ export default defineCommerceHandler(async (event, adapter) => {
3
+ const { cartId, address } = setBillingAddressSchema.parse(await readBody(event));
4
+ return adapter.setBillingAddress(cartId, address);
5
+ });
@@ -0,0 +1,5 @@
1
+ import { readBody } from "h3";
2
+ export default defineCommerceHandler(async (event, adapter) => {
3
+ const { cartId, methodId } = setPaymentMethodSchema.parse(await readBody(event));
4
+ return adapter.setPaymentMethod(cartId, methodId);
5
+ });
@@ -0,0 +1,5 @@
1
+ import { readBody } from "h3";
2
+ export default defineCommerceHandler(async (event, adapter) => {
3
+ const { cartId, address } = setShippingAddressSchema.parse(await readBody(event));
4
+ return adapter.setShippingAddress(cartId, address);
5
+ });