30 lines
810 B
TypeScript
30 lines
810 B
TypeScript
// Define API URL from environment
|
|
export const API_URL = import.meta.env.MODE === 'development'
|
|
? `${import.meta.env.VITE_API_URL_DEV}`
|
|
: `${import.meta.env.VITE_API_URL_PROD}`
|
|
|
|
|
|
/**
|
|
* Fetch data from Directus API
|
|
*/
|
|
export const fetchAPI = async (query: string) => {
|
|
try {
|
|
const res = await fetch(`${API_URL}${import.meta.env.VITE_API_GRAPHQL_PATH}`, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
Authorization: `Bearer ${import.meta.env.VITE_API_TOKEN}`,
|
|
},
|
|
body: JSON.stringify({
|
|
query
|
|
})
|
|
})
|
|
|
|
if (res.ok) {
|
|
const data = await res.json()
|
|
return data
|
|
}
|
|
} catch (error) {
|
|
console.error(error)
|
|
}
|
|
} |