fix fetch

This commit is contained in:
2025-06-24 12:05:43 +02:00
parent 26e7467f7f
commit a000f966eb
14 changed files with 3326 additions and 13451 deletions

View File

@ -1,24 +1,30 @@
import { NuxtErrorBoundary } from "#components";
import { useMyFetch } from "#imports";
import type { GenericResponse, UserCart } from "~/types";
export const useProductStore = defineStore("productStore", () => {
const productList = ref();
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<GenericResponse<object>>(
`/api/public/products/category/${categoryId}?p=1&elems=${count}`,
{
headers: {
"Content-Type": "application/json",
},
onErrorOccured: (_, status) => { throw new Error(`HTTP error: ${status}`) },
}
);
console.log(data);
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();
productList.value = data.data.items;
// const data = await res.json();
// productList.value = data.data.items;
} catch (error) {
console.error("getList error:", error);
}
@ -72,21 +78,22 @@ export const useProductStore = defineStore("productStore", () => {
}
}
const cart = ref();
const cart = ref({} as UserCart);
async function getCart() {
try {
const res = await fetch(`http://127.0.0.1:4000/api/public/user/cart`, {
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);
// const data = await res.json();
// console.log(data);
cart.value = data;
} catch (error) {