const apiURL: string = process.env.NODE_ENV === '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(`${apiURL}${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)
    }
}