����JFIF��`�`�����Viewing File: /home/u820193700/domains/throtllr.com/public_html/js/api.js
// Configuration for API
let currentPath = window.location.pathname;
// Remove any trailing filename (like index.html)
if (currentPath.includes('.') || !currentPath.endsWith('/')) {
    currentPath = currentPath.substring(0, currentPath.lastIndexOf('/') + 1);
}
// If we are in a 'frontend' subdirectory, go up one level to reach the shared project root
if (currentPath.includes('/frontend/')) {
    currentPath = currentPath.replace('/frontend/', '/');
}
const API_BASE = window.location.origin + currentPath.replace(/\/$/, '') + '/backend/api';
window.API_BASE_URL = API_BASE;

const fetchAPI = async (endpoint, options = {}) => {
    const defaultHeaders = {};
    if (!(options.body instanceof FormData)) {
        defaultHeaders['Content-Type'] = 'application/json';
    }

    const userStr = sessionStorage.getItem('throtllr_user');
    if (userStr) {
        const user = JSON.parse(userStr);
        if (user.session_token) {
            defaultHeaders['Authorization'] = `Bearer ${user.session_token}`;
        }
    }

    const config = {
        ...options,
        headers: {
            ...defaultHeaders,
            ...options.headers,
        },
    };

    try {
        const response = await fetch(`${API_BASE}${endpoint}`, config);

        let data;
        const contentType = response.headers.get("content-type");
        if (contentType && contentType.indexOf("application/json") !== -1) {
            data = await response.json();
        } else {
            const text = await response.text();
            throw new Error(text || 'API request failed with non-JSON response');
        }

        if (!response.ok) {
            throw new Error(data.message || 'API request failed');
        }

        return data;
    } catch (error) {
        throw error;
    }
};

window.fetchAPI = fetchAPI;
Back to Directory �������}�!1AQa"q2���#B��R��$3br� %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz�������������������������������������������������������������������������������� ������w�!1AQaq"2�B���� #3R�br� $4�%�&'()*56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz������������������������������������������������������������������������ ��?��_��+��?��(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(�����