-
Notifications
You must be signed in to change notification settings - Fork 1
/
fetchCategories.js
54 lines (48 loc) · 1.25 KB
/
fetchCategories.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
const CATEGORIES_QUERY = `
query ProductCategories($categoryLevel: String!) {
categories(filters: { parent_id: { eq: $categoryLevel } }, currentPage: 1) {
total_count
items {
uid
level
name
path
image
children_count
}
page_info {
current_page
page_size
total_pages
}
}
}
`;
// Fetch product categories from venia.magento.com
const fetchCategories = async (categoryLevel = '2') => {
const { hostname } = window.location;
const GRAPHQL_ENDPOINT = import.meta.env.VITE_GRAPHQL_ENDPOINT;
try {
const response = await fetch(GRAPHQL_ENDPOINT, {
method: "POST",
headers: {
"X-Custom-Hostname": hostname,
"Content-Type": "application/json",
Accept: "application/json",
},
body: JSON.stringify({
query: CATEGORIES_QUERY,
variables: { categoryLevel },
}),
});
if (!response.ok) {
throw new Error(`HTTP error ${response.status}: ${response.statusText}`);
}
const data = await response.json();
return data.data?.categories?.items;
} catch (error) {
console.error("Error fetching categories:", error);
throw error;
}
};
export default fetchCategories;