Merge branch 'fix_fetch'

This commit is contained in:
2025-06-24 14:18:12 +02:00
17 changed files with 3451 additions and 13523 deletions

View File

@ -1,24 +1,31 @@
import { NuxtErrorBoundary } from "#components";
import { useMyFetch } from "#imports";
import type { GenericResponse, GenericResponseItems, UserCart } from "~/types";
import type { Product } from "~/types/product";
export const useProductStore = defineStore("productStore", () => {
const productList = ref();
const productList = ref<Product>();
const modules = ref();
async function getList(count: number, categoryId = 1) {
try {
const res = await fetch(
`http://127.0.0.1:4000/api/public/products/category/${categoryId}?p=1&elems=${count}`,
const { data } = await useMyFetch<GenericResponseItems<Product>>(
`/api/public/products/category/${categoryId}?p=1&elems=${count}`,
{
headers: {
"Content-Type": "application/json",
},
onErrorOccured: async (_, status) => {
// await navigateTo("/error", { replace: true });
throw createError({
statusCode: status,
statusMessage: `HTTP error: ${status}`,
});
},
}
);
if (!res.ok) {
throw new Error(`HTTP error: ${res.status}`);
}
const data = await res.json();
productList.value = data.data.items;
productList.value = data.items;
} catch (error) {
console.error("getList error:", error);
}
@ -49,7 +56,7 @@ export const useProductStore = defineStore("productStore", () => {
}
}
async function addToCart(product) {
async function addToCart(product: Product) {
try {
const res = await fetch(
`http://127.0.0.1:4000/api/public/user/cart/item/add/${product.id}/1`,
@ -72,27 +79,30 @@ export const useProductStore = defineStore("productStore", () => {
}
}
const cart = ref();
// async function getCart() {
// try {
// const res = await fetch(`http://127.0.0.1:4000/api/public/user/cart`, {
// headers: {
// "Content-Type": "application/json",
// },
// });
const cart = ref({} as UserCart);
async function getCart() {
try {
const { data } = await useMyFetch<GenericResponse<UserCart>>(
`/api/public/user/cart`,
{
headers: {
"Content-Type": "application/json",
},
onErrorOccured: (_, status) => {
throw new Error(`HTTP error: ${status}`);
},
}
);
// if (!res.ok) {
// throw new Error(`HTTP error: ${res.status}`);
// }
// if (!res.ok) {
// throw new Error(`HTTP error: ${res.status}`);
// }
// const data = await res.json();
// console.log(data);
// cart.value = data;
// } catch (error) {
// console.error("getList error:", error);
// }
// }
cart.value = data;
} catch (error) {
console.error("getList error:", error);
}
}
return {
productList,